-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Specify iframe sandbox and allow rules (#3650)
- Loading branch information
1 parent
0613d51
commit ad0b5de
Showing
5 changed files
with
74 additions
and
2 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
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,14 @@ | ||
/** | ||
* Retrieves the allow rules for iframes and modifies them according to w3c standard conditions. | ||
* @param {string[]} allowRules - An array of allow rules. | ||
* @returns {string|undefined} The modified allow rules joined as a single string, or undefined if allowRules is falsy. | ||
*/ | ||
export const getAllowRules = (allowRules: string[]) => { | ||
if (!allowRules) return undefined; | ||
const rules = allowRules; | ||
rules.forEach((rule, index) => { | ||
rules[index] = rule + (rule.indexOf(';') != -1 ? '' : ';'); | ||
rules[index] = (allowRules[index] as any).replaceAll('"', "'"); | ||
}); | ||
return rules.join(' '); | ||
}; |
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,23 @@ | ||
import { getAllowRules } from '../../src/services/iframe-helpers'; | ||
|
||
describe('getAllowRules function', () => { | ||
it('returns undefined if allowRules is undefined', () => { | ||
expect(getAllowRules(undefined)).toBeUndefined(); | ||
}); | ||
|
||
it('returns an empty string if allowRules is an empty array', () => { | ||
expect(getAllowRules([])).toBe(''); | ||
}); | ||
|
||
it('if semicolon already present, keep it.', () => { | ||
const allowRules = ['rule1', 'rule2;', 'rule3;']; | ||
const expectedRules = 'rule1; rule2; rule3;'; | ||
expect(getAllowRules(allowRules)).toBe(expectedRules); | ||
}); | ||
|
||
it('replaces double quotes with single quotes in each rule', () => { | ||
const allowRules = ["fullscreen", "microphone", "camera \"none\"", "geolocation \"self\" https://a.example.com https://b.example.com"]; | ||
const expectedRules = "fullscreen; microphone; camera 'none'; geolocation 'self' https://a.example.com https://b.example.com;"; | ||
expect(getAllowRules(allowRules)).toBe(expectedRules); | ||
}); | ||
}); |
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