Skip to content

Commit

Permalink
fix: use Trusted Types to assign innerHTML in WebviewImpl
Browse files Browse the repository at this point in the history
This makes the webview compliant with clients that enforce Trusted Types.
See https://web.dev/trusted-types. Fixes electron#27211.
  • Loading branch information
koto committed Jan 8, 2021
1 parent 9aef11a commit 02d0fd0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/renderer/web-view/web-view-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ export class WebViewImpl {
// Create internal iframe element.
this.internalElement = this.createInternalElement();
const shadowRoot = this.webviewNode.attachShadow({ mode: 'open' });
shadowRoot.innerHTML = '<!DOCTYPE html><style type="text/css">:host { display: flex; }</style>';
// Assign to innerHTML using Trusted Types if available,
// for hosts that enforce Trusted Types.
const view = this.webviewNode.ownerDocument.defaultView;
const webviewHTML = '<!DOCTYPE html><style type="text/css">:host { display: flex; }</style>';
const ttPolicy = view?.trustedTypes?.createPolicy('electron', { createHTML: (s) => s });

This comment has been minimized.

Copy link
@Siegrift

Siegrift Jan 8, 2021

@koto Are you sure this is executed only once?

const html = ttPolicy?.createHTML(webviewHTML) ?? webviewHTML;
shadowRoot.innerHTML = html as string;

this.setupWebViewAttributes();
this.viewInstanceId = getNextId();
shadowRoot.appendChild(this.internalElement);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@types/send": "^0.14.5",
"@types/split": "^1.0.0",
"@types/stream-json": "^1.5.1",
"@types/trusted-types": "^2.0.0",
"@types/uuid": "^3.4.6",
"@types/webpack": "^4.41.21",
"@types/webpack-env": "^1.15.2",
Expand Down

0 comments on commit 02d0fd0

Please sign in to comment.