Skip to content
This repository has been archived by the owner on Mar 12, 2020. It is now read-only.

Commit

Permalink
#18: Modified Parser to strip out stray NUL, CR, and LF sequences bef…
Browse files Browse the repository at this point in the history
…ore parsing
  • Loading branch information
elazar committed Mar 30, 2015
1 parent bb84d9a commit 7d524d7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
10 changes: 6 additions & 4 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,14 @@ class Parser implements ParserInterface
public function __construct()
{
$crlf = "\r\n";
$not_crlf = "(?:(?:(?<!\r)\n)|(?:\r[^\n]))";
$letter = 'a-zA-Z';
$number = '0-9';
$special = preg_quote('[]\`_^{|}');
$null = '\\x00';
$command = "(?P<command>[$letter]+|[$number]{3})";
$middle = "(?: (?:[^ $null:]|$not_crlf)(?:[^ $null]|$not_crlf)*)";
$middle = "(?: [^ $null$crlf:][^ $null$crlf]*)";
// ? provides for relaxed parsing of messages without trailing parameters properly demarcated
$trailing = "(?: :?(?:[^$null]|$not_crlf)*)";
$trailing = "(?: :?[^$null$crlf]*)";
$params = "(?P<params>$trailing?|(?:$middle{0,14}$trailing))";
$name = "[$letter](?:[$letter$number\\-]*[$letter$number])?";
$host = "$name(?:\\.(?:$name)*)+";
Expand All @@ -215,7 +214,7 @@ public function __construct()
$message = "(?P<prefix>:$prefix )?$command$params$crlf";
$this->message = "/^$message/SU";

$chstring = "(?:[^ \a$null,]|$not_crlf)+";
$chstring = "[^ \a$null,$crlf]+";
$channel = $this->channel = "(?:[#&]$chstring)";
$mask = "(?:[#$]$chstring)";
$to = "(?:$channel|(?:$user@$host)|$nick|$mask)";
Expand Down Expand Up @@ -320,6 +319,9 @@ protected function removeIntegerKeys(array $array)
*/
public function parse($message)
{
// Strip out invalid characters
$message = preg_replace("/\\0|(?:(?<!\r)\n)|(?:\r(?!\n))/", '', $message);

// Extract the first full message or bail if there is none
if (!preg_match($this->message, $message, $parsed)) {
return null;
Expand Down
30 changes: 24 additions & 6 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2519,34 +2519,52 @@ public function dataProviderTestParse()
),
),

// Commands containing individual CR or LF characters
// Individual NUL, CR, or LF characters are stripped out
array(
":server.name 372 BotNick :Who left a null byte \0 in here?\r\n",
array(
'prefix' => ':server.name',
'servername' => 'server.name',
'command' => '372',
'params' => array(
1 => "Who left a null byte in here?",
'all' => "Who left a null byte in here?",
),
'code' => 'RPL_MOTD',
'target' => 'BotNick',
'message' => ":server.name 372 BotNick :Who left a null byte in here?\r\n",
),
),

array(
":server.name 372 BotNick :Who left a carriage return \r in here?\r\n",
array(
'prefix' => ':server.name',
'servername' => 'server.name',
'command' => '372',
'params' => array(
1 => "Who left a carriage return \r in here?",
'all' => "Who left a carriage return \r in here?",
1 => "Who left a carriage return in here?",
'all' => "Who left a carriage return in here?",
),
'code' => 'RPL_MOTD',
'target' => 'BotNick',
'message' => ":server.name 372 BotNick :Who left a carriage return in here?\r\n",
),
),

array(
":server.name 372 BotNick :Who left a carriage return \n in here?\r\n",
":server.name 372 BotNick :Who left a line feed \n in here?\r\n",
array(
'prefix' => ':server.name',
'servername' => 'server.name',
'command' => '372',
'params' => array(
1 => "Who left a carriage return \n in here?",
'all' => "Who left a carriage return \n in here?",
1 => "Who left a line feed in here?",
'all' => "Who left a line feed in here?",
),
'code' => 'RPL_MOTD',
'target' => 'BotNick',
'message' => ":server.name 372 BotNick :Who left a line feed in here?\r\n",
),
),

Expand Down

0 comments on commit 7d524d7

Please sign in to comment.