Skip to content

feat: Add granular nonces#10053

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

feat: Add granular nonces#10053
patel-vansh wants to merge 9 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.

@patel-vansh
Copy link
Copy Markdown
Contributor Author

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.

Done

@michalsn michalsn added the enhancement PRs that improve existing functionalities label Apr 2, 2026
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.

Thank you. LGTM!

*
* @var bool
*/
protected $enableStyleNonce = true;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please add the native types as these are newly added.

@michalsn
Copy link
Copy Markdown
Member

michalsn commented Apr 4, 2026

Please run: git rebase upstream/4.8 to fix PHPStan errors.

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. enhancement PRs that improve existing functionalities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants