Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Security Nonce for sapper.scripts #343

Open

Description

@AlexRiedler

Issue: when we use sapper.scripts for injection (changed from sapper.main) we now no longer have access to inject nonce for scripts; maybe there is a better way to do this? (CSP policy)

Any ideas would be greatly appreciated :)

Activity

  1. Rich-Harris commented on Aug 7, 2018

    @Rich-Harris
    Member

    This sounds like a good and sensible idea, though I have to confess I've never used nonce before 😬 Could you walk me through the process of generating/applying it? Thanks!

  2. AlexRiedler commented on Aug 7, 2018

    @AlexRiedler
    Author

    Basically on the request, a UUIDv4 is normally generated and put into the CSP header like so:

    HTTP Header:
    Content-Security-Policy: default-src 'self'; script-src 'nonce-4AEemGb0xJptoIGFP3Nd'

    and then when the script tag is generated it uses the nonce:

    <script type="text/javascript" nonce="4AEemGb0xJptoIGFP3Nd"> ... </script>

    In javascript I have seen people use helmetjs/csp library, and do something like:

    For generating UUID for nonce

    var uuidv4 = require('uuid/v4')
    
    app.use(function (req, res, next) {
      res.locals.nonce = uuidv4()
      next()
    })

    and then upstream when generating the actual payload:

    return '<script nonce="' + res.locals.nonce + '">alert('hello world');</script>'

    for the lengthy read troy hunt gives a good read for it: https://www.troyhunt.com/locking-down-your-website-scripts-with-csp-hashes-nonces-and-report-uri/

    It basically blocks scripts from running on the page that do not have the nonce that matches the one the the HTTP Header.

    I probably have time to investigate on how to implement this; would it make more sense to somehow add parameters to %sapper.scripts% ; or would it make more sense to support this feature by configuration?

  3. Seb35 commented on Mar 25, 2019

    @Seb35
    Contributor

    In the meantime this was implemented by @nolanlawson on 7 September 2018 (e377515) and documented (sveltejs/sapper-legacy.svelte.dev@6cde0fa).

    I’ve just tested with Helmet and Polka on my app (here) and it works: a nonce is added in the CSP by Helmet and the nonce is used in %sapper.scripts%.

    Is there anything else to do in this issue? -- btw thanks for this issue and the implementation, that’s an interesting feature.

  4. ShimShamSam commented on Apr 23, 2019

    @ShimShamSam

    @Seb35 I'm having an issue with Rollup's use of Shimport. It's using eval() which fails the nonce check.

  5. evanleck commented on May 7, 2019

    @evanleck

    @Seb35 @ShimShamSam @nolanlawson I'm getting errors using the recommended setup for CSP in Firefox:

    Content Security Policy: The page’s settings blocked the loading of a resource at eval (“script-src”).
    Loading failed for the <script> with source “blob:http://localhost:3000/de7746fc-d74d-4648-8c1c-2084c3af7b23”.
    Content Security Policy: The page’s settings blocked the loading of a resource at blob:http://localhost:3000/de7746fc-d74d-4648-8c1c-2084c3af7b23 (“script-src”).
    

    It looks like something's trying to eval or load a script from the blob protocol, right? I wonder if that's Shimport...

  6. Conduitry commented on May 8, 2019

    @Conduitry
    Member

    That's almost certainly Shimport, yes. The good news is that caniuse is telling me that Firefox 67 is going to natively support dynamic imports, So whenever that comes out, Firefox shouldn't even need to use Shimport.

  7. evanleck commented on May 8, 2019

    @evanleck

    It sounds like CSP needs to include 'unsafe-eval' to work in browsers that don't support dynamic imports natively, right? Shimport appears to use eval and source from the blob: protocol, which would mean our script-src would be 'self' 'unsafe-eval' blob:. I tested that out locally and it works, though Sapper appears to try and connect to localhost:10000 as well.

    According to caniuse, that's for current and previous versions of Firefox and Edge.

  8. evdama commented on Jul 9, 2019

    @evdama

    having implemented CSP just yesterday using this code in server.js

    app.use( ( request, response, next ) => {
    	response.locals.nonce = uuidv4();
    	next();
    } );
    
    app.use(
    	helmet(
    		{
    			contentSecurityPolicy: {
    				directives: {
    					scriptSrc: [
    						"'self'",
    						( request, response ) => `'nonce-${response.locals.nonce}'`
    					]
    				},
    				browserSniff: false
    			}
    		}
    	),
    	sapper.middleware(),
    	compression({ threshold: 0 })
    );
    

    I also see

    [email protected]:1 Refused to load the script 'blob:http://localhost:3000/c4f0757d-0cb2-4663-b33a-7d8495546172' because it violates the following Content Security Policy directive: "script-src 'self' 'nonce-12c733c1-28d6-44d8-8c91-b71af9f59e3b'". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.
    

    Can we get rid of shimport now that it seems both Firefox and Edge already have dynamic import support?
    https://caniuse.com/#feat=es6-module-dynamic-import

  9. lmaccherone commented on Aug 21, 2019

    @lmaccherone

    +1 on getting rid of shimport. I'm being told that I won't be allowed to go into production with a new product that has unsafe-eval in my CSP.

    Until that happens, is there a way to turn it off today?

  10. antony commented on Aug 21, 2019

    @antony
    Member

    @evdama is there a reliable way to polyfill for es6 dynamic module import? It is important we maintain support for Edge < 76 and sadly IE11.

  11. Conduitry commented on Aug 21, 2019

    @Conduitry
    Member

    You should be able to disable it by forking Sapper and editing the code that loads it in get_page_handler.ts. I think using the webpack logic even when using Rollup would work.

    edit: You'll also need to change the script tag to type='module'.

  12. martinburger commented on Jun 26, 2020

    @martinburger

    @Seb35 @ShimShamSam @nolanlawson I'm getting errors using the recommended setup for CSP in Firefox:

    Content Security Policy: The page’s settings blocked the loading of a resource at eval (“script-src”).
    Loading failed for the <script> with source “blob:http://localhost:3000/de7746fc-d74d-4648-8c1c-2084c3af7b23”.
    Content Security Policy: The page’s settings blocked the loading of a resource at blob:http://localhost:3000/de7746fc-d74d-4648-8c1c-2084c3af7b23 (“script-src”).
    

    It looks like something's trying to eval or load a script from the blob protocol, right? I wonder if that's Shimport...

    For me, switching from rollup to webpack solved above CSP violation.

  13. bryantbiggs commented on Dec 27, 2020

    @bryantbiggs

    any update on the removal of shimport or is this a rollup issue?

  14. benmccann commented on Dec 28, 2020

    @benmccann
    Member

    I'm surprised that shimport is used in the non-legacy build. I would expect it could probably be removed.

    script += `var s=document.createElement("script");try{new Function("if(0)import('')")();s.src="${main}";s.type="module";s.crossOrigin="use-credentials";}catch(e){s.src="${req.baseUrl}/client/shimport@${build_info.shimport}.js";s.setAttribute("data-main","${main}")}document.head.appendChild(s)`;

    It looks like SvelteKit doesn't contain Shimport at all - though it doesn't have legacy asset support yet. Perhaps we should keep an eye out when we add legacy support to SvelteKit and see if we can add Shimport only in the legacy case there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions