This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
Fix HeaderValue throwing an exception on legal characters #28
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,20 +28,18 @@ private function __construct() | |
public static function filter($value) | ||
{ | ||
$result = ''; | ||
$tot = strlen($value); | ||
$total = strlen($value); | ||
|
||
// Filter for CR and LF characters, leaving CRLF + WSP sequences for | ||
// Long Header Fields (section 2.2.3 of RFC 2822) | ||
for ($i = 0; $i < $tot; $i += 1) { | ||
for ($i = 0; $i < $total; $i += 1) { | ||
$ord = ord($value[$i]); | ||
if (($ord < 32 || $ord > 126) | ||
&& $ord !== 13 | ||
) { | ||
if ($ord === 10 || $ord > 126) { | ||
continue; | ||
} | ||
|
||
if ($ord === 13) { | ||
if ($i + 2 >= $tot) { | ||
if ($i + 2 >= $total) { | ||
continue; | ||
} | ||
|
||
|
@@ -72,17 +70,17 @@ public static function filter($value) | |
*/ | ||
public static function isValid($value) | ||
{ | ||
$tot = strlen($value); | ||
for ($i = 0; $i < $tot; $i += 1) { | ||
$total = strlen($value); | ||
for ($i = 0; $i < $total; $i += 1) { | ||
$ord = ord($value[$i]); | ||
if (($ord < 32 || $ord > 126) | ||
&& $ord !== 13 | ||
) { | ||
|
||
// bare LF means we aren't valid | ||
if ($ord === 10 || $ord > 126) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be |
||
return false; | ||
} | ||
|
||
if ($ord === 13) { | ||
if ($i + 2 >= $tot) { | ||
if ($i + 2 >= $total) { | ||
return false; | ||
} | ||
|
||
|
@@ -93,6 +91,7 @@ public static function isValid($value) | |
return false; | ||
} | ||
|
||
// skip over the LF following this | ||
$i += 2; | ||
} | ||
} | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
namespace ZendTest\Mail\Header; | ||
|
||
use PHPUnit_Framework_TestCase as TestCase; | ||
use ReflectionClass; | ||
use Zend\Mail\Header\HeaderValue; | ||
|
||
class HeaderValueTest extends TestCase | ||
|
@@ -30,7 +31,8 @@ public function getFilterValues() | |
["This is a\r\r test", "This is a test"], | ||
["This is a \r\r\n test", "This is a \r\n test"], | ||
["This is a \r\n\r\ntest", "This is a test"], | ||
["This is a \r\n\n\r\n test", "This is a \r\n test"] | ||
["This is a \r\n\n\r\n test", "This is a \r\n test"], | ||
["This is a test\r\n", "This is a test"], | ||
]; | ||
} | ||
|
||
|
@@ -56,7 +58,11 @@ public function validateValues() | |
["This is a\r\r test", 'assertFalse'], | ||
["This is a \r\r\n test", 'assertFalse'], | ||
["This is a \r\n\r\ntest", 'assertFalse'], | ||
["This is a \r\n\n\r\n test", 'assertFalse'] | ||
["This is a \r\n\n\r\n test", 'assertFalse'], | ||
["This\tis\ta test", 'assertTrue'], | ||
["This is\ta \r\n test", 'assertTrue'], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing test with the sequence |
||
["This\tis\ta\ntest", 'assertFalse'], | ||
["This is a \r\t\n \r\n test", 'assertFalse'], | ||
]; | ||
} | ||
|
||
|
@@ -81,7 +87,7 @@ public function assertValues() | |
["This is a\r\r test"], | ||
["This is a \r\r\n test"], | ||
["This is a \r\n\r\ntest"], | ||
["This is a \r\n\n\r\n test"] | ||
["This is a \r\n\n\r\n test"], | ||
]; | ||
} | ||
|
||
|
@@ -94,4 +100,10 @@ public function testAssertValidRaisesExceptionForInvalidValues($value) | |
$this->setExpectedException('Zend\Mail\Header\Exception\RuntimeException', 'Invalid'); | ||
HeaderValue::assertValid($value); | ||
} | ||
|
||
public function testCannotBeConstructed() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this test. It's not relevant. |
||
{ | ||
$class = new ReflectionClass('Zend\Mail\Header\HeaderValue'); | ||
$this->assertFalse($class->isInstantiable()); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be
> 127