JavaScript APIs not being secure by default. • User controlled strings get converted into code • Many dangerous and error-prone DOM sinks like innerHTML Example: https://example.com/#<img src=x onerror=alert('xss')> How does XSS happen? var foo = location.hash.slice(1); document.querySelector('#foo').innerHTML = foo;
that we can set a strict hash-based CSP s.refactorSourcedScriptsForHashBasedCsp(); // Hash inline scripts from this html file, if there are any const scriptHashes = s.hashAllInlineScripts(); // Generate a strict CSP as a string const strictCsp = StrictCsp.getStrictCsp(scriptHashes, { enableBrowserFallbacks: true, }); // Set this CSP via a meta tag s.addMetaTag(strictCsp); const htmlStringWithCsp = s.serializeDom(); Source: github.com/google/strict-csp/blob/main/strict-csp/README.md
configureServer(server) { server.middlewares.use((req, res, next) => { let data = ''; res.on('data', (chunk) => { data += chunk; // Accumulate the response data }); res.on('end', () => { const s = new StrictCsp(data); // Refactor sourced scripts so that we can set a strict hash-based CSP s.refactorSourcedScriptsForHashBasedCsp(); // Hash inline scripts from this html file, if there are any const scriptHashes = s.hashAllInlineScripts(); // Generate a strict CSP as a string const strictCsp = StrictCsp.getStrictCsp(scriptHashes, { enableBrowserFallbacks: true, }); // Send the CSP as response headers res.setHeader('Content-Security-Policy', "require-trusted-types-for 'script'"); res.setHeader('Content-Security-Policy', strictCsp); // Send the modified data at once const htmlStringWithCsp = s.serializeDom(); res.end(htmlStringWithCsp); next(); }); }) }, }) Note: Specifically for Vite, this works better with https://vitejs.dev/guide/api-plugin#transformindexhtml but this example shows how the previous two slides can be combined in a framework-agnostic way
from the web developer Opinionated Wrapper APIs Guide developers towards safer coding patterns github.com/google/sa fevalues Static Analysis Check for potentially dangerous patterns and warn developers as early in the feedback loop as possible github.com/google/sa fety-web Runtime Enforcement Guarantee that insecure code will not run for the client web.dev/articles/strict -csp web.dev/articles/trust ed-types A Pipeline of Protections (devs) (users)
from the web developer Opinionated Wrapper APIs Guide developers towards safer coding patterns github.com/google/sa fevalues Static Analysis Check for potentially dangerous patterns and warn developers as early in the feedback loop as possible Runtime Enforcement Guarantee that no insecure DOM APIs will run on the client A Pipeline of Protections (devs) (users)
from the web developer New Web APIs? Is it possible to provide safe-by-default APIs as a part of the web platform? Static Analysis Check for potentially dangerous patterns and warn developers as early in the feedback loop as possible Runtime Enforcement Guarantee that no insecure DOM APIs will run on the client Trusted Types v2 / Perfect Types (devs) (users)