Skip to content

fix: reject nested DSN query parameters - #59

Open
trearcul wants to merge 1 commit into
cdn77:masterfrom
trearcul:connection-dsn-parameters-array-guard
Open

fix: reject nested DSN query parameters#59
trearcul wants to merge 1 commit into
cdn77:masterfrom
trearcul:connection-dsn-parameters-array-guard

Conversation

@trearcul

Copy link
Copy Markdown
Collaborator

parse_str() turns a query such as "?heartbeat[]=120" into an array value, and casting that to int in Connection::fromDsn() silently evaluates to 1 on php 8 with no warning or notice at all. Such a DSN therefore configured a 1 second heartbeat instead of 120, with nothing to point at the cause.

Reject nested parameters in the Dsn constructor, next to the existing malformed/incomplete/invalid scheme validation, naming the offending keys so a config typo is obvious.

This also makes the string[] annotation on Dsn::$parameters true again, so the previously widened array<array|string> is reverted and consumers keep reading plain strings.

parse_str() turns a query such as "?heartbeat[]=120" into an array value,
and casting that to int in Connection::fromDsn() silently evaluates to 1 on
php 8 with no warning or notice at all. Such a DSN therefore configured a
1 second heartbeat instead of 120, with nothing to point at the cause.

Reject nested parameters in the Dsn constructor, next to the existing
malformed/incomplete/invalid scheme validation, naming the offending keys so
a config typo is obvious.

This also makes the string[] annotation on Dsn::$parameters true again, so
the previously widened array<array<mixed>|string> is reverted and consumers
keep reading plain strings.
@trearcul
trearcul requested review from judzi and a lite review from Copilot August 24, 2026 13:26
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 52.63%. Comparing base (ed515ba) to head (edd90a2).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #59      +/-   ##
==========================================
+ Coverage   51.98%   52.63%   +0.65%     
==========================================
  Files          29       29              
  Lines         654      663       +9     
==========================================
+ Hits          340      349       +9     
  Misses        314      314              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens DSN parsing by rejecting nested query parameters (e.g., heartbeat[]=120) that parse_str() would otherwise decode into arrays, preventing silent misconfiguration when those values are later cast to integers.

Changes:

  • Added validation in Dsn to reject any DSN query parameters whose decoded value is not a string, and surface the offending key(s) in an exception.
  • Introduced a dedicated InvalidDsn::nestedParameters() exception factory for clearer configuration error reporting.
  • Added a PHPUnit test covering nested DSN query parameters.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
tests/Configuration/DsnTest.php Adds coverage for rejecting nested query parameters in DSNs.
src/Exception/InvalidDsn.php Adds a new exception factory to report nested DSN query keys.
src/Configuration/Dsn.php Validates decoded DSN query parameters are single (string) values and rejects nested structures.
Suppressed comments (1)

src/Configuration/Dsn.php:122

  • getParameters() returns an associative map of query param name => value, so @return string[] is incorrect (it implies a list).
    /** @return string[] */
    public function getParameters(): array
    {

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/Configuration/Dsn.php
Comment on lines +40 to 41
/** @var string[] */
private $parameters = [];
Comment on lines +29 to +31
/** @param array<int|string> $keys */
public static function nestedParameters(array $keys): self
{
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants