Skip to content

feat: Add granular nonces#10053

Open
patel-vansh wants to merge 7 commits intocodeigniter4:4.8from
patel-vansh:feat/granular-nonces
Open

feat: Add granular nonces#10053
patel-vansh wants to merge 7 commits intocodeigniter4:4.8from
patel-vansh:feat/granular-nonces

Conversation

@patel-vansh
Copy link
Copy Markdown
Contributor

Description
This PR adds ability to have more control over whether to add nonces on style-src, style-src-elem, script-src and script-src-elem individually by introducing two new boolean variables in CSP config.

I am not sure this is the optimal solution for this feature, that's why I've made this draft PR for others to look at the implementation and provide necessary changes. That's why I haven't still updated the user guide or changelog.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

Copy link
Copy Markdown
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like disabling enableScriptNonce / enableStyleNonce still adds nonce="..." to the HTML tag, but no longer adds the matching 'nonce-...' value to the CSP header. As a result, those inline <script> / <style> blocks will be blocked by the browser.

@github-actions github-actions bot added the 4.8 PRs that target the `4.8` branch. label Apr 1, 2026
@patel-vansh patel-vansh force-pushed the feat/granular-nonces branch from 59c20bb to d4275af Compare April 1, 2026 10:18
@patel-vansh patel-vansh marked this pull request as ready for review April 1, 2026 10:26
@patel-vansh
Copy link
Copy Markdown
Contributor Author

Seems like disabling enableScriptNonce / enableStyleNonce still adds nonce="..." to the HTML tag, but no longer adds the matching 'nonce-...' value to the CSP header. As a result, those inline <script> / <style> blocks will be blocked by the browser.

Done. Now both the header as well as the nonce="..." are removed from the html.

@michalsn
Copy link
Copy Markdown
Member

michalsn commented Apr 1, 2026

Thank you. There is one place left: https://github.com/codeigniter4/CodeIgniter4/blob/develop/system/Autoloader/Autoloader.php#L541 - it still fetches nonces based only on enabled(). That feels like the last inconsistent edge case.

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

Labels

4.8 PRs that target the `4.8` branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants