This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AA-9932 Handle legacy.livechat.com and legacy.lc:3001 domains as allo…
…wed origins (#22) * [Chore] Add legacy Agent App domains handling together with testing. Add jest configuration. * [Fix] Revert unwanted changes to package-lock.json. * [Chore] Bump package version.
- Loading branch information
Szymon Graczyk
authored
Aug 21, 2020
1 parent
b4f5f72
commit a65f721
Showing
7 changed files
with
8,355 additions
and
1,704 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Dedicated only to `jest` as it cannot read a `rollup` babel configuration dedicated for build process. | ||
* @see https://jestjs.io/docs/en/getting-started#using-typescript | ||
* @see https://stackoverflow.com/questions/45327218/how-do-i-get-jest-to-run-tests-against-a-rollupbabel-build | ||
*/ | ||
module.exports = { | ||
presets: [ | ||
['@babel/preset-env', { targets: { node: 'current' } }], | ||
'@babel/preset-typescript' | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* For a detailed explanation regarding each configuration property, visit: | ||
* @see https://jestjs.io/docs/en/configuration.html | ||
*/ | ||
module.exports = { | ||
roots: ['<rootDir>/src'], | ||
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$', | ||
moduleFileExtensions: ['ts', 'js'] | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { getIsEventOriginAllowed } from './helpers'; | ||
|
||
describe('getIsEventOriginAllowed', () => { | ||
it('returns true for valid domains', () => { | ||
expect(getIsEventOriginAllowed('https://my.lc:3000/chats')).toBe(true); | ||
expect(getIsEventOriginAllowed('https://legacy.lc:3001/archives')).toBe( | ||
true | ||
); | ||
expect( | ||
getIsEventOriginAllowed('https://my.labs.livechatinc.com/agents') | ||
).toBe(true); | ||
expect( | ||
getIsEventOriginAllowed('https://my.staging.livechatinc.com/customers') | ||
).toBe(true); | ||
expect( | ||
getIsEventOriginAllowed('https://my.staging.livechatinc.com/customers') | ||
).toBe(true); | ||
expect(getIsEventOriginAllowed('https://my.livechat.com/')).toBe(true); | ||
}); | ||
|
||
it('returns false for invalid domains', () => { | ||
expect(getIsEventOriginAllowed('https://localhost:3000/chats')).toBe(false); | ||
expect(getIsEventOriginAllowed('https://my.lp:3000/archives')).toBe(false); | ||
expect(getIsEventOriginAllowed('https://fakelivechat.com/customers')).toBe( | ||
false | ||
); | ||
expect(getIsEventOriginAllowed('https://google.com')).toBe(false); | ||
}); | ||
|
||
it('returns false for invalid URL', () => { | ||
expect(getIsEventOriginAllowed('')).toBe(false); | ||
expect(getIsEventOriginAllowed(null)).toBe(false); | ||
expect(getIsEventOriginAllowed('Its not a valid URL')).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Domain allowed to communicate via iframe with Agent App SDK. | ||
*/ | ||
const ALLOWED_DOMAINS = [ | ||
'livechatinc.com', | ||
'livechat.com', | ||
'my.lc:3000', | ||
'legacy.lc:3001' | ||
]; | ||
|
||
/** | ||
* Returns `true`, if origin is allowed to communicate with Agent App SDK. | ||
* @param {string} origin Origin of `message` event targetting Agent App SDK. | ||
* @ | ||
*/ | ||
export function getIsEventOriginAllowed(origin: string): boolean { | ||
if (!origin) { | ||
return false; | ||
} | ||
|
||
try { | ||
const originURL = new URL(origin); | ||
const originDomain = originURL.hostname; | ||
const splitted = originDomain.split('.'); | ||
const domainOnly = splitted.slice(splitted.length - 2).join('.'); | ||
|
||
// As .lc domain is registerable we restrict our development environment | ||
// based on port usage. | ||
const toBeVerified = originURL.port | ||
? `${domainOnly}:${originURL.port}` | ||
: domainOnly; | ||
|
||
return ALLOWED_DOMAINS.includes(toBeVerified); | ||
} catch { | ||
return false; | ||
} | ||
} |