Skip to content

Feature Request : Add Support for Strict Content Security Policy #21711

@naveedahmed1

Description

@naveedahmed1

🚀 Feature request

Command (mark with an x)

  • [x ] build

Description

Lighthouse now throws error if No CSP is found in enforcement mode.

Ensure CSP is effective against XSS attacks

No CSP found in enforcement mode with High Severity

Describe the solution you'd like

My knowledge about CSP is limited and I was going through these guides on CSP:

http://web.dev
https://web.dev/csp-xss/
https://web.dev/strict-csp/

Apparently Nonce-based CSP seems easy to implement for SSR, for example for scripts, after Angular returns the final html, we can use string replace function on server side, to replace "<script" with "<script nonce="RANDOM_NONCE"" add RANDOM_NONCE in CSP header and I believe it should work.

But this string replace work will have to be done for each request.

On the other hand, in case of Hash-based strict CSP if Angular CLI calculates hashes and include these hashes in script tags at build time, this could save us the string replace work that has to be done for each request in case of nonce. But there's a tradeoff, we will have to sync these hashes with our Server Side App so that appropriate headers could be added for generated response. Alternatively CSP could be generated by Angular CLI and added as Content-Security-Policy meta tag.

Ref: https://web.dev/strict-csp/#step-1:-decide-if-you-need-a-nonce-or-hash-based-csp

There are two types of strict CSPs, nonce- and hash-based. Here's how they work:

Nonce-based CSP: You generate a random number at runtime, include it in your CSP, and associate it with every script tag in your page. An attacker can't include and run a malicious script in your page, because they would need to guess the correct random number for that script. This only works if the number is not guessable and newly generated at runtime for every response.

Hash-based CSP: The hash of every inline script tag is added to the CSP. Note that each script has a different hash. An attacker can't include and run a malicious script in your page, because the hash of that script would need to be present in your CSP.

Criteria for choosing a strict CSP approach:

Nonce-based CSP
For HTML pages rendered on the server where you can create a new random token (nonce) for every response

Hash-based CSP
For HTML pages served statically or those that need to be cached. For example, single-page web applications built with frameworks such as Angular, React or others, that are statically served without server-side rendering

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions