Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/Connection/Internal/Http1Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,37 @@ final class Http1Parser

private int $bodyBytesConsumed = 0;

/**
* chunk-size [ chunk-ext ], where chunk-size = 1*HEXDIG (captured) and
* chunk-ext = *( BWS ";" BWS chunk-ext-name [ BWS "=" BWS chunk-ext-val ] ), the name being a token
* and the value a token or a quoted-string (RFC 9112, section 7.1.1).
*/
private const CHUNK_SIZE_LINE_REGEX = /* @lang RegExp */ <<<'REGEX'
(
(?(DEFINE)
(?<bws>[ \t]*)
(?<token>[!#$%&'*+\-.^_`|~0-9A-Za-z]+)
# quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE
(?<quoted_string>"(?:[\t !\x23-\x5B\x5D-\x7E\x80-\xFF]|\\[\t \x21-\x7E\x80-\xFF])*")
)

^
# chunk-size = 1*HEXDIG
(?<size>[0-9A-Fa-f]+)
(?:
# chunk-ext = *( BWS ";" BWS chunk-ext-name [ BWS "=" BWS chunk-ext-val ] )
(?:
(?&bws);(?&bws)(?&token)
(?:(?&bws)=(?&bws)(?:(?&token)|(?&quoted_string)))?
)+
|
# Trailing whitespace without chunk extensions.
(?&bws)
)
\z
)x
REGEX;

private bool $chunkedEncoding = false;

private ?int $chunkLengthRemaining = null;
Expand Down Expand Up @@ -414,7 +445,14 @@ private function parseChunkedBody(): bool
}

$line = \substr($this->buffer, 0, $lineEndPos);
$hex = \strtolower(\trim(\ltrim($line, '0'))) ?: '0';

// chunk = chunk-size [ chunk-ext ] CRLF. Chunk extensions are not used, so they are
// ignored once checked against the grammar (RFC 9112, section 7.1.1).
if (!\preg_match(self::CHUNK_SIZE_LINE_REGEX, $line, $matches)) {
throw new ParseException('Invalid chunk size line', HttpStatus::BAD_REQUEST);
}

$hex = \strtolower(\ltrim($matches['size'], '0')) ?: '0';
$dec = \hexdec($hex);

if ($hex !== \dechex($dec)) {
Expand Down
77 changes: 77 additions & 0 deletions test/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,81 @@ public function testResponseWithTrailers(): void
$this->assertSame(200, $response->getStatus());
$this->assertSame('Expires', $response->getHeader('trailers'));
}

public function testChunkExtensionsAreIgnored(): void
{
$data = "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"
. "7;name=value\r\nHello, \r\n"
. "6 ; flag ; q=\"a;\\\"b\"\r\nWorld!\r\n"
. "0;last\r\n\r\n";

[$response, $body] = $this->parseChunked($data);

$this->assertSame(200, $response->getStatus());
$this->assertSame('Hello, World!', $body);
}

/**
* @dataProvider provideInvalidChunkSizeLines
*/
public function testInvalidChunkSizeLineIsRejected(string $sizeLine): void
{
$this->expectException(ParseException::class);
$this->expectExceptionMessage('Invalid chunk size line');

$this->parseChunked("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n{$sizeLine}hello\r\n0\r\n\r\n");
}

public function provideInvalidChunkSizeLines(): array
{
return [
'empty extension' => ["5;\r\n"],
'missing extension name' => ["5;=v\r\n"],
'unterminated quoted-string' => ["5;n=\"v\r\n"],
'space in token value' => ["5;n=a b\r\n"],
'control character in extension' => ["5;n\x01\r\n"],
'bare LF in extension' => ["5;n\n\r\n"],
'whitespace only' => [" \r\n"],
'extension without size' => [";x\r\n"],
'whitespace and extension without size' => [" ;x\r\n"],
'leading whitespace' => [" 5\r\n"],
'bare LF before extension' => ["5\n;x\r\n"],
'vertical tab after size' => ["5\x0b\r\n"],
'non-hex size' => ["g\r\n"],
];
}

/**
* @return array{Response, string}
*/
private function parseChunked(string $data): array
{
$request = new Request('/', 'GET');

events()->requestStart($request);
events()->connectionAcquired($request, $this->createMock(Connection::class), 1);
events()->requestHeaderStart($request, $this->createMock(Stream::class));
events()->requestHeaderEnd($request, $this->createMock(Stream::class));
events()->requestBodyStart($request, $this->createMock(Stream::class));
events()->requestBodyEnd($request, $this->createMock(Stream::class));

$body = '';
$parser = new Http1Parser(
$request,
$this->createMock(Stream::class),
function (string $data) use (&$body): Future {
$body .= $data;
return Future::complete();
},
new NullCancellation(),
fn () => null,
);

$response = $parser->parse($data);
while (!$parser->isComplete()) {
$parser->parse();
}

return [$response, $body];
}
}
Loading