Skip to content

Commit 67053b3

Browse files
committed
Deprecate parsing folded header lines as per RFC 7230
1 parent b257338 commit 67053b3

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Fix `AppendStream::detach` to not close streams
55
* Clarify exception message when stream is detached
66
* Added a test for #129 behavior
7+
* Deprecated parsing folded header lines as per RFC 7230
78

89
## 1.4.2 - 2017-03-20
910

src/Rfc7230.php

+1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ final class Rfc7230
1212
* @license https://github.com/amphp/http/blob/16e465fa82555104d1cff98cb8e412295a380214/LICENSE
1313
*/
1414
const HEADER_REGEX = "(^([^()<>@,;:\\\"/[\]?={}\x01-\x20\x7F]++):[ \t]*+((?:[ \t]*+[\x21-\x7E\x80-\xFF]++)*+)[ \t]*+\r\n)m";
15+
const HEADER_FOLD_REGEX = "(\r\n[ \t]++)";
1516
}

src/functions.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,17 @@ function _parse_message($message)
771771
$rawHeaders = substr($rawHeaders, $startLineEndPosition + 2);
772772

773773
/** @var array[] $headerLines */
774-
preg_match_all(Rfc7230::HEADER_REGEX, $rawHeaders, $headerLines, PREG_SET_ORDER);
774+
$count = preg_match_all(Rfc7230::HEADER_REGEX, $rawHeaders, $headerLines, PREG_SET_ORDER);
775+
776+
// If these aren't the same, then one line didn't match and there's an invalid header.
777+
if ($count !== substr_count($rawHeaders, "\n")) {
778+
// Folding is deprecated, see https://tools.ietf.org/html/rfc7230#section-3.2.4
779+
if (preg_match(Rfc7230::HEADER_FOLD_REGEX, $rawHeaders)) {
780+
throw new \InvalidArgumentException('Invalid header syntax: Obsolete line folding');
781+
}
782+
783+
throw new \InvalidArgumentException('Invalid header syntax');
784+
}
775785

776786
foreach ($headerLines as $headerLine) {
777787
$result['headers'][strtolower($headerLine[1])][] = $headerLine[2];

tests/FunctionsTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,15 @@ public function testParsesResponseWithoutReason()
353353
$this->assertSame('Test', (string) $response->getBody());
354354
}
355355

356+
/**
357+
* @expectedException \InvalidArgumentException
358+
* @expectedExceptionMessage Invalid header syntax: Obsolete line folding
359+
*/
360+
public function testParsingFailsWithFoldedHeaders()
361+
{
362+
Psr7\parse_response("HTTP/1.0 200\r\nFoo: Bar\r\n Baz: Bam\r\nBaz: Qux\r\n\r\nTest");
363+
}
364+
356365
/**
357366
* @expectedException \InvalidArgumentException
358367
*/

0 commit comments

Comments
 (0)