-
Notifications
You must be signed in to change notification settings - Fork 816
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
Trusted types support in workbox-window's register() #2855
Comments
Hello @budarin—thanks for reaching out about this. Is that code you shared from I'm not 100% clear on the implications of using trusted types and how Workbox could help with the |
...actually, does doing something like what's outlined in Example 2 of https://w3c.github.io/webappsec-trusted-types/dist/spec/#policies-hdr help, where you pass in an object with a |
Hi!
It's transpiled workbox code
I 've got what you're talking about. Well, okay - this option solves the problem! |
I don't know if I should close this discussion - will you still think about the implementation? |
I implemented a simple policy for verifying that a URL belongs to the same domain as the document is, which can also be implemented in a package if (window.trustedTypes && window.trustedTypes.createPolicy) {
window.trustedTypes.createPolicy('default', {
createScriptURL: (urlStr: string) => {
if (typeof urlStr !== 'string') {
// eslint-disable-next-line fp/no-throw
throw new TypeError('invalid URL');
}
const url = new URL(urlStr, window.location.origin);
if (url.origin !== window.location.origin) {
// eslint-disable-next-line fp/no-throw
throw new TypeError('invalid URL');
}
return urlStr;
},
});
} but its not a protection - its a simple stub to mute security errors |
Okay, let's leave this open to track the feature request of having But, for what it's worth, |
This comment has been minimized.
This comment has been minimized.
Without changes in WorkBox - I can not apply trusted type security on my site. let wb;
if (window.trustedTypes && window.trustedTypes.createPolicy) {
const customPolicy = window.trustedTypes.createPolicy('myPolicy', {
createScriptURL: (urlStr: string) => {
if (typeof urlStr !== 'string') {
// eslint-disable-next-line fp/no-throw
throw new TypeError('invalid URL');
}
const url = new URL(urlStr, window.location.origin);
if (url.origin !== window.location.origin) {
// eslint-disable-next-line fp/no-throw
throw new TypeError('invalid URL');
}
return urlStr;
},
});
// pass in the policy
wb = new WorkBox(customPolicy, '/sw.js'); // ...navigator.serviceWorker.register(customPolicy.createScriptURL('/sw.js'))
} else {
wb = new WorkBox('/sw.js');
} https://webappsec.dev/assets/pub/Google_IO-Securing_Web_Apps_with_Modern_Platform_Features.pdf |
I took a closer look at this, and I can see how we can improve things from a TypeScript perspective, but I'm not sure that it is necessary to overload the constructor to explicitly support What I can do is modify the constructor so that it's clear that the parameter can either be a Here's a quick standalone page that works for me (except for the TypeScript types), allowing you to just pass the
|
Okay, I actually had to add in a few explicit This seems to work, for anyone looking for an example:
|
The fix is now deployed in a pre-release of Workbox v6.2.0: https://github.com/GoogleChrome/workbox/releases/tag/v6.2.0-alpha.0 |
Library Affected:
workbox--window
Browser & Platform:
Any browser with support of CSP 2.0
Issue or Feature Request Description:
When installing the CSP policy "require-trusted-types-for 'script'" - the security error occurs in the browser
Usefull link : Prevent DOM-based cross-site scripting vulnerabilities with Trusted Types
The text was updated successfully, but these errors were encountered: