Skip to content
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

Option to update CSP during runtime #403

Closed
link-point opened this issue Mar 10, 2023 · 1 comment
Closed

Option to update CSP during runtime #403

link-point opened this issue Mar 10, 2023 · 1 comment

Comments

@link-point
Copy link

Our web app is being embedded into other people's websites through iframes. For this reason we need to change the code (add frame ancestor exception to CSP) and deploy it every time we have a new client.

Is there a possibility to update CSP information in helmet during runtime? In this case we could have the additional list of CSP in the database and refresh it in the running application automatically.

@EvanHahn
Copy link
Member

Yes, this is possible. I think you have three options.

  1. Conditionally use the middleware with different options. This is documented here.

  2. Use functions for contentSecurityPolicy directive values, if that works for you.

    The contentSecurityPolicy middleware supports functions, not just strings, as directive values. For example:

    app.use(helmet({
      contentSecurityPolicy: {
        directives: {
          // ...
          frameAncestors: [(req, res) => {
            return Math.random() > 0.5 ? "example.com" : "example.net";
          }],
        },
      },
    }));

    This only supports one directive value, though, so it might not work for you.

    This is poorly-documented and I filed CSP: document functions as directive values #404 to clean this up.

  3. Don't use Helmet at all, and roll your own Content-Security-Policy middleware.

    app.use(helmet({
      contentSecurityPolicy: false,
    }));
    
    app.use((req, res, next) => {
      res.setHeader(
        "Content-Security-Policy",
        `default-src 'self'; frame-ancestors ${sanitizedFrameAncestorsList.join(" ")}`
      );
      next();
    });

I'm going to close this issue because I think I've answered your question, but let me know if that's wrong and I'll reopen.

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

No branches or pull requests

2 participants