diff --git a/src/Connection/Internal/Http1Parser.php b/src/Connection/Internal/Http1Parser.php index 84719917..e775deea 100644 --- a/src/Connection/Internal/Http1Parser.php +++ b/src/Connection/Internal/Http1Parser.php @@ -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) + (?[ \t]*) + (?[!#$%&'*+\-.^_`|~0-9A-Za-z]+) + # quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE + (?"(?:[\t !\x23-\x5B\x5D-\x7E\x80-\xFF]|\\[\t \x21-\x7E\x80-\xFF])*") + ) + + ^ + # chunk-size = 1*HEXDIG + (?[0-9A-Fa-f]+) + (?: + # chunk-ext = *( BWS ";" BWS chunk-ext-name [ BWS "=" BWS chunk-ext-val ] ) + (?: + (?&bws);(?&bws)(?&token) + (?:(?&bws)=(?&bws)(?:(?&token)|(?"ed_string)))? + )+ + | + # Trailing whitespace without chunk extensions. + (?&bws) + ) + \z + )x + REGEX; + private bool $chunkedEncoding = false; private ?int $chunkLengthRemaining = null; @@ -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)) { diff --git a/test/ParserTest.php b/test/ParserTest.php index 25239e00..8986e819 100644 --- a/test/ParserTest.php +++ b/test/ParserTest.php @@ -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]; + } }