Skip to content

Support CSP Restrictions Without Nonce Attribute (IE11) #1137

Closed

Description

@arahansen

I have a project that I would like to explore using a css-in-js library such as styled-components.

However, the Content-Security-Policy restrictions for this project are such that style tags are not allowed to be injected onto the page, and must be served in the initial static html file.

One solution around this would be to provide a nonce attribute. Styled components accomplishes this through an undocumented feature of webpack (See Issue).

However, IE11 does not support nonce attributes, and CSP Level 2 in general, which is a browser I need to support for this project.

More info on this security policy here.

I've created a minimal server example that should (theoretically) produce the same CSP restrictions for loading styles.

There may be another way around these CSP settings that are equally as secure, but allow for dynamic styles from a trusted source such as glamor. But I am not aware of any. (I'm also not an expert in CSP 😞 )

I suppose one way around this would be to somehow statically analyze the styles and generate a stylesheet at build time. However, I think we would lose out on one of the very powerful features of styled-components and related libraries: dynamically re-defining styles based on props, state, and theming.

Activity

  1. kitten commented on Sep 3, 2017

    @kitten
    Member

    I suppose one way around this would be to somehow statically analyze the styles and generate a stylesheet at build time. However, I think we would lose out on one of the very powerful features of styled-components and related libraries: dynamically re-defining styles based on props, state, and theming.

    This will generally not be possible, even with v3, for various reasons. I've compiled a document that sums up the problems since it has come up repeatedly: https://gist.github.com/philpl/7b8a021f19ead2ec534091976a1ff6dc#file-2-specificity-md

    I don't think there'll be an easy way around. However, can we at least fix the problem with SSR somehow for IE11?

  2. changed the title [-]Support CSP Restrictions Without Nonce Attribute[/-] [+]Support CSP Restrictions Without Nonce Attribute (IE11)[/+] on Sep 3, 2017
  3. arahansen commented on Sep 3, 2017

    @arahansen
    Author

    We currently don't have any SSR implemented on this project, and I don't believe it's a priority for the team at the moment. I'm not sure the team could be convinced that s-c or another css-in-js lib will provide enough of an improved development experience to offset the setup and maintenance of a SSR solution.

    However, I'm open to suggestions! If there is something we can do at build time, for example, I'm very open to those possibilities.

  4. kitten commented on Sep 3, 2017

    @kitten
    Member

    So I've just read up on IE 11 not supporting CSP except HTML 5 Sandbox. I don't think IE 11 would even block anything, since it doesn't support any of the directives anyway. So where does this concern stem from, when IE 11 clearly doesn't support it anyway?

    However, the Content-Security-Policy restrictions for this project are such that style tags are not allowed to be injected onto the page, and must be served in the initial static html file.

    I don't really see how we can support CSP without SSR, when CSP is literally not (barely, but for all intents and purposes not at all) supported in the first place?

    I'm not too familiar with all its concepts, but I don't really know what your goal is then unfortunately?

    I don't want to come across to harsh, sorry 😉 But if you don't have any server-side rendering and want to avoid any injection w/o the CSP in place, there's not really anything we can do, is there not?

  5. geelen commented on Sep 3, 2017

    @geelen
    Member

    Yeah from my understanding IE11 won't block the inline stylesheets at all, right? Its implementation of CSP doesn't do that... I think??

  6. arahansen commented on Sep 3, 2017

    @arahansen
    Author

    Wow, @geelen you're totally right. MDN even lists style-src as "no support" for IE. I guess I never looked there 😅. I think this answers my question. Thanks guys!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions