Skip to content

fix: tokens and keys with a trailing newline must fail serializing - #78

Open
iefken wants to merge 1 commit into
gapple:developfrom
iefken:fix/serializer-trailing-newline
Open

iefken wants to merge 1 commit into
gapple:developfrom
iefken:fix/serializer-trailing-newline

Conversation

@iefken

@iefken iefken commented Dec 1, 2026

Copy link
Copy Markdown

Fixes #77

The token and key checks in Serializer end in $ without the D modifier, so a value with one
trailing "\n" passed and was written into the header value unchanged ("abc\n, 1",
"k\n, next=1"). RFC 9651 requires serialization to fail here, and Parser already rejects the
output.

  • serializeToken(): /ix -> /ixD
  • serializeKey(): / -> /D (covers dictionary keys and parameter names)

Tests (PHP 8.3, locally in a container):

  • new tests SerializeItemTest::testTokenWithTrailingNewline,
    SerializeItemTest::testParameterKeyWithTrailingNewline,
    SerializeDictionaryTest::testKeyWithTrailingNewline - fail before the fix:
    Tests: 2975, Assertions: 3028, Failures: 3. (only these three)
  • after the fix: OK (2975 tests, 3031 assertions)
  • PHPStan (level max): [OK] No errors
  • PHPCS: 0 errors; 1 existing warning on line 192 (line length, not touched by this change)
  • PHPMD (phpmd analyze --ruleset phpmd.ruleset.yml): Found 0 violations and 0 errors

Not run locally: the PHP 8.1 / 8.2 / 8.4 / 8.5 / 8.6 matrix and coverage upload (the D modifier
exists in every supported PHP version); composer validate (composer.json unchanged).

Commit is GPG-signed and DCO signed-off.



Issue found and fixed by Claude, reproduced issue in an isolated container, then reviewed and confirmed by Ief (GitHub: iefken).

Full disclosure from my end: I am in my 'starter'-steps in learning how to find and report these kinds of bugs/issue's with the help from my (spare-) AI (-usage tokens), so any feedback is greatly appreciated aswell :)

The token and key checks in Serializer end in `$` without the `D`
modifier. In PCRE `$` also matches just before a final "\n", so a token
"abc\n" or a key "key\n" passed the check, and the original value was
written to the output with the newline inside the header value, e.g.
"abc\n, 1" or "key\n, next=1". RFC 9651 (4.1.7, 4.1.1.3) requires
serialization to fail for such values, and Parser rejects the output.

Add the `D` modifier to both patterns, so `$` only matches at the very
end of the value.

Tests: a token, a dictionary key and a parameter key with a trailing
newline must now throw SerializeException.

Signed-off-by: Iefken <iefken@bughunt.dapp-normal.com>
@codecov

codecov Bot commented Dec 5, 2026

Copy link
Copy Markdown

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
2972 1 2971 1
View the top 1 failed test(s) by shortest run time
gapple.Tests.StructuredFields.Httpwg.SerializationNumberTest::testSerializing with data set "negative decimal rounds to zero - serialize"
Stack Traces | 0.00341s run time
gapple\Tests\StructuredFields\Httpwg\SerializationNumberTest::testSerializing with data set "negative decimal rounds to zero - serialize"
"negative decimal rounds to zero - serialize" was not serialized to expected value
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'0.0'
+'-0.0'

.../structured-fields/tests/SerializingRulesetTrait.php:36

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

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.

Serializer accepts tokens and keys with a trailing newline

1 participant