Fix/rfc2047 qp chunk off by two - #35
Merged
Merged
Conversation
…r limit
encodeHeaderValue() cuts quoted-printable text with
"(.{0,$maxLength}[^\=][^\=])". The two trailing atoms each match one further
character, so every chunk could run two characters past its bound: a subject
of "Lorem ipsum ... magna aliqua" plus an ellipsis produced a 77-character
encoded-word on a 78-character line, against RFC 2047 limits of 75 and 76.
The [^\=][^\=] pair is what stops a cut landing inside a "=XX" escape, so the
repetition count is reduced by two rather than the atoms removed. The chunk
length floors added in 4216044 (pear#34) guarantee the count cannot go negative.
Only the encodeHeaderValue() fallback is affected, i.e. builds without
ext/mbstring; encodeMB() already checked its limit correctly and produces
byte-identical output for the same input after this change.
tests/headers_without_mbstring.phpt pinned the old chunking, so its
expectations are regenerated. Case [07] now emits the same two lines as
headers_with_mbstring.phpt, where the two paths previously disagreed.
The test skips whenever ext/mbstring is present, which is every job in the CI matrix, so the non-mbstring header encoding path had no coverage at all. That is how its expectations came to encode a chunking bug unnoticed. Add an --INI-- section disabling mb_substr()/mb_strlen(). Unlike the -i command line flag, --INI-- also applies to --SKIPIF--, so the existing skip condition now acts as a fallback: the test runs when the functions can be disabled, and still skips if they cannot.
alecpl
pushed a commit
that referenced
this pull request
Aug 5, 2026
…37) The "Q" character class escaped printable punctuation and every 8-bit byte but neither \x00-\x1F nor \x7F, so C0 controls and DEL were written into the header verbatim. RFC 2047 defines encoded-text as printable ASCII other than "?" and SPACE, so any of them makes the encoded-word invalid, and a raw CR or LF in a header is a header injection vector. A literal LF failed in a second way with ext/mbstring. encodeMB() uses "\n" as its own chunk separator and expands it into a fold when assembling the result, so an LF in the value was indistinguishable from a chunk boundary: the value was split into two encoded-words and the byte silently disappeared from the decoded header. Add the two missing ranges, which lets \x7B-\x7E, \x7F and \x80-\xFF collapse into \x7B-\xFF. The class was duplicated verbatim in encodeQP() and encodeMB(), the second copy carrying a "see encodeQP()" comment, so it moves into Mail_mimePart::QP_ESCAPE_REGEXP as 4216044 (#34) did for MAX_CHARSET_LENGTH. tests/headers_with_mbstring.phpt pinned the defect as expected output and is regenerated. Case [31] encodes a Japanese subject to ISO-2022-JP, whose charset-switching escapes are ESC, and one of the JIS X 0208 bytes involved is \x0D: the expectations held ten raw ESC bytes and a bare carriage return inside a Subject header, invisible unless viewed with cat -v. Unlike #33, #34 and #35 this reproduces with ext/mbstring present, so tests/rfc2047_control_chars.phpt needs no --INI-- section. It sweeps all 32 C0 control characters plus DEL across both encodings, checking that the encoded-text holds only printable ASCII and that the byte survives a round trip. It reports 34 failures against the previous code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hello again,
This PR addresses another finding of the LLM that was mentioned in #34: encoded-words that overshoot the length limits set by RFC 2047.
Mail_mimePart::encodeHeaderValue()cuts quoted-printable text two characters later than it should, emitting encoded-words of up to 77 characters on lines of up to 78. RFC 2047 allows 75 and 76 respectively.How to reproduce
poc.php
Before
After
This is byte-identical to what
encodeMB()already produced for the same input, so the new chunking is not an arbitrary choice: the two code paths now agree.Root cause
In
encodeHeaderValue():The repetition is bounded at
$maxLength, but the two trailing atoms each match one further character, so the whole match can run to$maxLength + 2.Fix
The
[^\=][^\=]pair is what stops a cut landing inside a=XXescape, so it has to stay. The repetition count is reduced by two instead. The chunk length floors added in 4216044 (#34) guarantee the count cannot go negative.Scope
Only the
encodeHeaderValue()fallback is affected, i.e. builds without ext/mbstring.encodeMB()already checked its limit correctly.tests/headers_without_mbstring.phptpinned the old chunking, so its expectations are regenerated. Case[07]now emits the same two lines asheaders_with_mbstring.phpt, where the two paths previously disagreed.Test
tests/rfc2047_qp_chunk_length.phptsweeps the cut point across the value, 122 truncations for each of the two encodings, asserting that every encoded-word stays within 75 characters and every line within 76. It reports 98 failures against the current code and none with the fix.As in #34, an
--INI--section disablesmb_substr()/mb_strlen()so the test actually exercises the fallback in CI.Test fix carried along
headers_without_mbstring.phptnever ran: it skips whenever ext/mbstring is present, which is every job in the CI matrix, so the non-mbstring path had no coverage at all, which is how its expectations came to encode this bug unnoticed. Adding an--INI--section disabling the two functions makes it run. Unlike the-icommand line flag,--INI--also applies to--SKIPIF--, so the existing skip condition still acts as a fallback if the functions cannot be disabled.The suite goes from 52 passed / 2 skipped to 54 passed / 1 skipped.
AI use disclosure
I used Anthropic's Claude LLM with Opus 5 to find this bug. It also suggested a fix, which I have reviewed and tested.