Skip to content

fix: make ContentSecurityPolicy::setDefaultSrc() add directives instead of overwriting - #10635

Open
gr8man wants to merge 1 commit into
codeigniter4:developfrom
gr8man:fix/csp-set-default-src
Open

gr8man wants to merge 1 commit into
codeigniter4:developfrom
gr8man:fix/csp-set-default-src

Conversation

@gr8man

@gr8man gr8man commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Description
Fixed ContentSecurityPolicy::setDefaultSrc() overwriting existing directives instead of adding to them, and failing when passed an array of URIs. Also added addDefaultSrc() for consistency with other CSP directive methods.

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

@carson-codeigniter4 carson-codeigniter4 Bot added the bug Verified issues on the current code behavior or pull requests that will fix them label Oct 13, 2026

@michalsn michalsn left a comment

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.

Could we keep setDefaultSrc() replacing the current source list and use addDefaultSrc() to append sources? The existing test explicitly verifies replacement, so changing the setter to append would break existing behavior.

@gr8man
gr8man force-pushed the fix/csp-set-default-src branch from 5f6b810 to 9f759b5 Compare October 18, 2026 22:04
@paulbalandan paulbalandan changed the title fix: make ContentSecurityPolicy::setDefaultSrc() add directives instead of overwriting fix: make ContentSecurityPolicy::setDefaultSrc() add directives instead of overwriting Oct 19, 2026

@michalsn michalsn left a comment

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.

Thank you.

@carson-codeigniter4 carson-codeigniter4 Bot added the stale Pull requests with conflicts label Oct 23, 2026
@carson-codeigniter4

Copy link
Copy Markdown

👋 Hi, @gr8man!

We detected conflicts in your PR against the base branch 🙊
You may want to sync 🔄 your branch with upstream!

Ref: Syncing Your Branch

@paulbalandan paulbalandan left a comment

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.

The PR title and body still describes the stale behavior. Please update both.

* @return $this
*/
public function setDefaultSrc($uri, ?bool $explicitReporting = null)
public function addDefaultSrc($uri, ?bool $explicitReporting = null)

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.

I'm not sure whether to allow adding a new method in develop, @michalsn ?

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.

Should we allow it, then this new method must be fully typed, i.e., with parameter types and return type.

@michalsn michalsn Oct 23, 2026 •

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.

I'm not sure whether to allow adding a new method in develop, @michalsn ?

In theory, yes - this should go to 4.8. But in this case, fixing the behavior of setDefaultSrc() could force some users to rewrite parts of their apps, when they could simply switch to addDefaultSrc() to preserve the existing behavior.

That was my reasoning behind allowing this change in develop. Some users might actually rely on the current (incorrect) behavior of setDefaultSrc(), so providing an alternative makes the transition easier.

is used instead. Values that cannot be converted to a string, such as objects
not implementing ``Stringable``, now throw a ``FormatException`` instead of a
fatal ``Error``.
- **HTTP:** Fixed a bug in ``ContentSecurityPolicy::setDefaultSrc()`` where passing an array of URIs resulted in an invalid ``'Array'`` directive. Added ``ContentSecurityPolicy::addDefaultSrc()`` to allow appending sources to the ``default-src`` directive.

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.

This mixes a fix and an add but should be separated. Depends on the resolution whether we'll allow the new method on develop.

This branch has not been deployed

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

Labels

bug Verified issues on the current code behavior or pull requests that will fix them stale Pull requests with conflicts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants