Add Support for Dynamic CSP Nonces to Enhance Security in Azure Static Web Apps

emfau 25 Reputation points
2024-11-08T14:56:15.7266667+00:00

https://github.com.mcas.ms/Azure/static-web-apps/issues/1575 seems to be not monitored anymore, thus I copied my request here, maybe someone can help me.

Is your feature request related to a problem? Please describe. Currently, Azure Static Web Apps do not support Content Security Policy (CSP) nonces, which are crucial for securing inline scripts and styles under strict CSP rules. Without nonce support, it is challenging to implement a CSP that prevents the execution of potentially injected or malicious inline scripts. This limitation weakens CSP effectiveness and forces us to choose less secure configurations, increasing the security risks for our applications.

Describe the solution you'd like I'd like Azure Static Web Apps to support dynamic CSP nonces. This could involve allowing us to configure nonce headers on a per-request basis or providing a mechanism within staticwebapp.config.json to dynamically generate and apply nonces to inline resources. Ideally, the feature would allow nonces to be added to responses without requiring complex workarounds, such as custom proxy functions or relying on static hashes, which do not offer the same security flexibility as nonces.

Describe alternatives you've considered

  • Azure Functions Proxy: Creating a proxy endpoint with Azure Functions to inject nonce-based CSP headers, but this approach adds complexity and latency.
  • Hash-Based CSP: Using hashes instead of nonces, which requires recalculating and updating CSP headers for every change in inline scripts or styles, making it challenging to maintain.
  • Static Nonce: Adding a static nonce in staticwebapp.config.json, which reduces security because it can’t offer per-request uniqueness, compromising the intent of CSP nonces.

Additional context Nonce support for CSP is essential for modern web security and is increasingly required for web applications handling sensitive user data. Implementing this feature would allow developers to strengthen their app’s security posture without relying on complex or less secure alternatives. This feature would also align Azure Static Web Apps with CSP best practices and bring it in line with other hosting solutions that provide better support for CSP nonces.

Azure Static Web Apps
Azure Static Web Apps
An Azure service that provides streamlined full-stack web app development.
1,178 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Shree Hima Bindu Maganti 4,925 Reputation points Microsoft External Staff Moderator
    2024-11-26T16:41:23.07+00:00

    Hi @emfau ,
    Thank you for Your Response.
    The request regarding support for Dynamic CSP Nonces in Azure Static Web Apps.Currently, Azure Static Web Apps does not natively support dynamic CSP nonces. We recognize that this limitation can make it difficult to implement a strict CSP, especially for applications that require robust protection against inline script injection attacks.While the feature is not available, here are some workarounds you might consider in the interim.

    1. Use Azure Functions to intercept and modify responses, dynamically adding nonce-based CSP headers.
    2. Though this adds complexity, it ensures per-request uniqueness for nonces.
    3. Use hashes to specify allowed inline script content in your CSP. While less flexible than nonces, this approach is still effective for many scenarios.
    4. Tools like csp-hash-generator can help automate hash generation for your inline scripts.
    5. You can define a static CSP nonce in the staticwebapp.config.json. While not ideal, it offers some level of security for less dynamic environments.
      References links: Content Security Policy (CSP) Nonces Configuration File Documentation
    0 comments No comments

  2. emfau 25 Reputation points
    2024-11-29T13:29:11.31+00:00

    Thanks @Shree Hima Bindu Maganti for your proposals. We currently use Azure App Service and switching to static web apps would have many advantages (price, complexity, deployment time, features etc).

    Azure Static Web Apps does not natively support dynamic CSP nonces

    I assume this is not on a public roadmap that this will be supported soon?

    Use Azure Functions to intercept and modify responses, dynamically adding nonce-based CSP headers.

    this sounds interesting. Is there an example project out in the wild or covered in Azure documentation? From a architectural point of view I assume this function could be reused by multiple applications.

    Thanks again and have a nice day!

    Michael


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.