Skip to content

feat: Add support to enable/disable CSP at runtime#10397

Open
patel-vansh wants to merge 2 commits into
codeigniter4:4.8from
patel-vansh:feat/disable-csp-runtime
Open

feat: Add support to enable/disable CSP at runtime#10397
patel-vansh wants to merge 2 commits into
codeigniter4:4.8from
patel-vansh:feat/disable-csp-runtime

Conversation

@patel-vansh

Copy link
Copy Markdown
Contributor

Description
This PR adds support to let users enable/disable CSP for current request without modifying config variables in runtime.

This lets them exclude specific routes (especially routes handling serving files) from Content-Security-Policy header.

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

@github-actions github-actions Bot added the 4.8 PRs that target the `4.8` branch. label Jul 9, 2026
Comment on lines +91 to +103
For example, to enable/disable CSP for a specific response:

.. literalinclude:: csp/017.php

The runtime value takes precedence over the ``CSPEnabled`` configuration value
for the current response.

These methods only affect the current response. After the response has been sent,
the CSP instance is recreated and the value of ``CSPEnabled`` in
**app/Config/App.php** is used as the default for subsequent requests.

This allows applications to keep CSP enabled globally while temporarily disabling
it for specific responses, or enable it only where required.

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.

CSP is request-scoped, not response-scoped, and the CSP instance is not recreated when the response is sent. I understand the intended meaning, but the current wording does not accurately describe the implementation.

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.

3 participants