-
Notifications
You must be signed in to change notification settings - Fork 54
Add Trusted Types support for srcdoc in serializeFrames #2091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
21c67fb
b5303bd
7889990
447f00f
382e3bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,28 @@ | ||
| import serializeDOM from './serialize-dom'; | ||
|
|
||
| let policy = null; | ||
|
|
||
| export function resetPolicy() { | ||
| policy = null; | ||
| } | ||
|
|
||
| function getPolicy() { | ||
| if (typeof window !== 'undefined' && window.trustedTypes && window.trustedTypes.createPolicy) { | ||
| if (policy && policy.createHTML) return policy; | ||
|
|
||
| try { | ||
| policy = window.trustedTypes.createPolicy('percy-dom', { | ||
| // codeql[js/dom-text-reinterpreted-as-html] | ||
| createHTML: html => html | ||
| }); | ||
| } catch (e) { | ||
| // ignore | ||
| } | ||
| /* istanbul ignore next */ | ||
| return policy || {}; | ||
| } | ||
| } | ||
|
|
||
| // Adds a `<base>` element to the serialized iframe's `<head>`. This is necessary when | ||
| // embedded documents are serialized and their contents become root-relative. | ||
| function setBaseURI(dom) { | ||
|
|
@@ -46,7 +69,11 @@ export function serializeFrames({ dom, clone, warnings, resources, enableJavaScr | |
| for (let r of serialized.resources) resources.add(r); | ||
|
|
||
| // assign serialized html to srcdoc and remove src | ||
| cloneEl.setAttribute('srcdoc', serialized.html); | ||
| let p = getPolicy() || {}; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also was checking if site has specific CSP even though you add this it will have no impact and will still break.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true, it fixes it but we see that few resources are missing in that case. It might happen that some iframe or asset do not load in that case. added a try catch to catch this. |
||
| try { | ||
| cloneEl.setAttribute('srcdoc', p.createHTML ? p.createHTML(serialized.html) : serialized.html); | ||
| } catch {} | ||
|
|
||
| cloneEl.removeAttribute('src'); | ||
|
|
||
| // delete inaccessible frames built with js when js is disabled because they | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.