Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update HeaderValue.php #463

Merged
merged 6 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
72 changes: 52 additions & 20 deletions library/Zend/Mail/Header/HeaderValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,37 +84,69 @@ public static function filter($value)
/**
* Determine if the header value contains any invalid characters.
*
* @see http://www.rfc-base.org/txt/rfc-2822.txt (section 2.2)
* @see rfc-6532
* @param string $value
* @return bool
*/
public static function isValid($value)
{
$tot = strlen($value);
for ($i = 0; $i < $tot; $i += 1) {
$i = 0;

while ($i < $tot) {
$ord = ord($value[$i]);
if (($ord < 32 || $ord > 126)
&& $ord !== 13
) {
return false;
}

if ($ord === 13) {
if ($i + 2 >= $tot) {

// Check for control characters (CR and LF) as per RFC 6532
if ($ord === 13) { // Carriage Return (CR)
if ($i + 1 >= $tot || ord($value[$i + 1]) !== 10) { // Must be followed by Line Feed (LF)
return false;
}

$lf = ord($value[$i + 1]);
$sp = ord($value[$i + 2]);

if ($lf !== 10 || $sp !== 32) {
return false;
}

$i += 2;
$i += 2; // Skip the CRLF sequence
continue;
} elseif ($ord === 10) { // Standalone Line Feed (LF) is not allowed
return false;
}

// Validate that the character is a valid UTF-8 character
if (!self::isUtf8Character($value, $i, $tot)) {
return false;
}
}


return true;
}

private static function isUtf8Character($value, &$i, $tot)
{
$byte = ord($value[$i]);

// ASCII character (1 byte): 0x00 - 0x7F
if ($byte >= 0x00 && $byte <= 0x7F) {
$i++;
return true;
}

// Determine the number of bytes in the UTF-8 character
$numBytes = 0;

if ($byte >= 0xC2 && $byte <= 0xDF) { // 2-byte sequence
$numBytes = 2;
} elseif ($byte >= 0xE0 && $byte <= 0xEF) { // 3-byte sequence
$numBytes = 3;
} elseif ($byte >= 0xF0 && $byte <= 0xF4) { // 4-byte sequence
$numBytes = 4;
} else {
return false; // Invalid byte for UTF-8
}

// Validate the next numBytes - 1 bytes (must be of the format 10xxxxxx)
for ($j = 1; $j < $numBytes; $j++) {
if ($i + $j >= $tot || (ord($value[$i + $j]) & 0xC0) !== 0x80) {
return false; // Invalid UTF-8 sequence
}
}

$i += $numBytes; // Move the pointer forward by the number of bytes
return true;
}

Expand Down
9 changes: 6 additions & 3 deletions tests/Zend/Mail/Header/HeaderValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,16 @@
["This is a\r test", 'assertFalse'],
["This is a\n\r test", 'assertFalse'],
["This is a\r\n test", 'assertTrue'],
["This is a \r\ntest", 'assertFalse'],
["This is a \r\ntest", 'assertTrue'],
["This is a \r\n\n test", 'assertFalse'],
["This is a\n\n test", 'assertFalse'],
["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\r\ntest", 'assertTrue'],
["This is a \r\n\n\r\n test", 'assertFalse'],
["This is à tèst àèìòù", 'assertTrue'],
["это тест по русскому языку", 'assertTrue'],
["هذااختار في اللغة العربيةу", 'assertTru
];
}

Expand Down Expand Up @@ -107,7 +110,7 @@
*/
public function testAssertValidRaisesExceptionForInvalidValues($value)
{
$this->expectException('Zend_Mail_Exception');

Check failure on line 113 in tests/Zend/Mail/Header/HeaderValueTest.php

View workflow job for this annotation

GitHub Actions / Tests on PHP 7.1

Parse error: syntax error, unexpected 'Zend_Mail_Exception' (T_STRING), expecting ']' in ./tests/Zend/Mail/Header/HeaderValueTest.php on line 113

Check failure on line 113 in tests/Zend/Mail/Header/HeaderValueTest.php

View workflow job for this annotation

GitHub Actions / Tests on PHP 7.2

Parse error: syntax error, unexpected 'Zend_Mail_Exception' (T_STRING), expecting ']' in ./tests/Zend/Mail/Header/HeaderValueTest.php on line 113

Check failure on line 113 in tests/Zend/Mail/Header/HeaderValueTest.php

View workflow job for this annotation

GitHub Actions / Tests on PHP 7.3

Parse error: syntax error, unexpected 'Zend_Mail_Exception' (T_STRING), expecting ']' in ./tests/Zend/Mail/Header/HeaderValueTest.php on line 113

Check failure on line 113 in tests/Zend/Mail/Header/HeaderValueTest.php

View workflow job for this annotation

GitHub Actions / Tests on PHP 7.4

Parse error: syntax error, unexpected 'Zend_Mail_Exception' (T_STRING), expecting ']' in ./tests/Zend/Mail/Header/HeaderValueTest.php on line 113

Check failure on line 113 in tests/Zend/Mail/Header/HeaderValueTest.php

View workflow job for this annotation

GitHub Actions / Tests on PHP 8.0

Parse error: syntax error, unexpected identifier "Zend_Mail_Exception", expecting "]" in ./tests/Zend/Mail/Header/HeaderValueTest.php on line 113

Check failure on line 113 in tests/Zend/Mail/Header/HeaderValueTest.php

View workflow job for this annotation

GitHub Actions / Tests on PHP 8.1

Parse error: syntax error, unexpected identifier "Zend_Mail_Exception", expecting "]" in ./tests/Zend/Mail/Header/HeaderValueTest.php on line 113

Check failure on line 113 in tests/Zend/Mail/Header/HeaderValueTest.php

View workflow job for this annotation

GitHub Actions / Tests on PHP 8.2

Parse error: syntax error, unexpected identifier "Zend_Mail_Exception", expecting "]" in ./tests/Zend/Mail/Header/HeaderValueTest.php on line 113

Check failure on line 113 in tests/Zend/Mail/Header/HeaderValueTest.php

View workflow job for this annotation

GitHub Actions / Tests on PHP 8.3

Parse error: syntax error, unexpected identifier "Zend_Mail_Exception", expecting "]" in ./tests/Zend/Mail/Header/HeaderValueTest.php on line 113
$this->expectExceptionMessage('Invalid');
Zend_Mail_Header_HeaderValue::assertValid($value);
}
Expand Down
Loading