mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2025-12-21 01:41:31 +00:00
Allow remote config to set valid origins (#205)
This commit is contained in:
21
src/Utils/MessageValidation.test.ts
Normal file
21
src/Utils/MessageValidation.test.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { isInvalidParentFrameOrigin } from "./MessageValidation";
|
||||
|
||||
test.each`
|
||||
domain | expected
|
||||
${"https://cosmos.azure.com"} | ${false}
|
||||
${"https://cosmos.azure.us"} | ${false}
|
||||
${"https://cosmos.azure.cn"} | ${false}
|
||||
${"https://cosmos.microsoftazure.de"} | ${false}
|
||||
${"https://subdomain.portal.azure.com"} | ${false}
|
||||
${"https://subdomain.portal.azure.us"} | ${false}
|
||||
${"https://subdomain.portal.azure.cn"} | ${false}
|
||||
${"https://subdomain.microsoftazure.de"} | ${false}
|
||||
${"https://main.documentdb.ext.azure.com"} | ${false}
|
||||
${"https://main.documentdb.ext.azure.us"} | ${false}
|
||||
${"https://main.documentdb.ext.azure.cn"} | ${false}
|
||||
${"https://main.documentdb.ext.microsoftazure.de"} | ${false}
|
||||
${"https://random.domain"} | ${true}
|
||||
${"https://malicious.cloudapp.azure.com"} | ${true}
|
||||
`("returns $expected when called with $domain", ({ domain, expected }) => {
|
||||
expect(isInvalidParentFrameOrigin({ origin: domain } as MessageEvent)).toBe(expected);
|
||||
});
|
||||
@@ -4,13 +4,18 @@ export function isInvalidParentFrameOrigin(event: MessageEvent): boolean {
|
||||
return !isValidOrigin(configContext.allowedParentFrameOrigins, event);
|
||||
}
|
||||
|
||||
function isValidOrigin(allowedOrigins: RegExp, event: MessageEvent): boolean {
|
||||
function isValidOrigin(allowedOrigins: string[], event: MessageEvent): boolean {
|
||||
const eventOrigin = (event && event.origin) || "";
|
||||
const windowOrigin = (window && window.origin) || "";
|
||||
if (eventOrigin === windowOrigin) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const result = allowedOrigins && allowedOrigins.test(eventOrigin);
|
||||
return result;
|
||||
for (const origin of allowedOrigins) {
|
||||
const result = new RegExp(origin).test(eventOrigin);
|
||||
if (result) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user