Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 0a78b71

Browse files
committed
Merge pull request #47 from mtymek/improve_header_security_performance
improve header validation speed
2 parents 79e07df + 298aa9a commit 0a78b71

File tree

2 files changed

+25
-30
lines changed

2 files changed

+25
-30
lines changed

src/HeaderSecurity.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,15 @@ public static function isValid($value)
106106
return false;
107107
}
108108

109-
$length = strlen($value);
110-
for ($i = 0; $i < $length; $i += 1) {
111-
$ascii = ord($value[$i]);
112-
113-
// Non-visible, non-whitespace characters
114-
// 9 === horizontal tab
115-
// 10 === line feed
116-
// 13 === carriage return
117-
// 32-126, 128-254 === visible
118-
// 127 === DEL
119-
// 255 === null byte
120-
if (($ascii < 32 && ! in_array($ascii, [9, 10, 13], true))
121-
|| $ascii === 127
122-
|| $ascii > 254
123-
) {
124-
return false;
125-
}
109+
// Non-visible, non-whitespace characters
110+
// 9 === horizontal tab
111+
// 10 === line feed
112+
// 13 === carriage return
113+
// 32-126, 128-254 === visible
114+
// 127 === DEL (disallowed)
115+
// 255 === null byte (disallowed)
116+
if (preg_match('/[^\x09\x0a\x0d\x20-\x7E\x80-\xFE]/', $value)) {
117+
return false;
126118
}
127119

128120
return true;

test/HeaderSecurityTest.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,22 @@ public function testFiltersValuesPerRfc7230($value, $expected)
5454

5555
public function validateValues()
5656
{
57-
return [
58-
["This is a\n test", 'assertFalse'],
59-
["This is a\r test", 'assertFalse'],
60-
["This is a\n\r test", 'assertFalse'],
61-
["This is a\r\n test", 'assertTrue'],
62-
["This is a \r\ntest", 'assertFalse'],
63-
["This is a \r\n\n test", 'assertFalse'],
64-
["This is a\n\n test", 'assertFalse'],
65-
["This is a\r\r test", 'assertFalse'],
66-
["This is a \r\r\n test", 'assertFalse'],
67-
["This is a \r\n\r\ntest", 'assertFalse'],
68-
["This is a \r\n\n\r\n test", 'assertFalse']
69-
];
57+
return array(
58+
array("This is a\n test", 'assertFalse'),
59+
array("This is a\r test", 'assertFalse'),
60+
array("This is a\n\r test", 'assertFalse'),
61+
array("This is a\r\n test", 'assertTrue'),
62+
array("This is a \r\ntest", 'assertFalse'),
63+
array("This is a \r\n\n test", 'assertFalse'),
64+
array("This is a\n\n test", 'assertFalse'),
65+
array("This is a\r\r test", 'assertFalse'),
66+
array("This is a \r\r\n test", 'assertFalse'),
67+
array("This is a \r\n\r\ntest", 'assertFalse'),
68+
array("This is a \r\n\n\r\n test", 'assertFalse'),
69+
array("This is a \xFF test", 'assertFalse'),
70+
array("This is a \x7F test", 'assertFalse'),
71+
array("This is a \x7E test", 'assertTrue'),
72+
);
7073
}
7174

7275
/**

0 commit comments

Comments
 (0)