Skip to content

Commit

Permalink
HttpRequest: drops non-UTF8 strings, but control characters only removes
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 20, 2015
1 parent 01ef085 commit 9ad00c8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 34 deletions.
15 changes: 8 additions & 7 deletions src/Http/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class RequestFactory extends Nette\Object
{
/** @internal */
const CHARS = '#^[\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}]*+\z#u';
const CHARS = '\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}';

/** @var array */
public $urlFilters = array(
Expand Down Expand Up @@ -137,6 +137,7 @@ public function createHttpRequest()
$gpc = (bool) get_magic_quotes_gpc();

// remove fucking quotes, control characters and check encoding
$reChars = '#^[' . self::CHARS . ']*+\z#u';
if ($gpc || !$this->binary) {
$list = array(& $query, & $post, & $cookies);
while (list($key, $val) = each($list)) {
Expand All @@ -147,7 +148,7 @@ public function createHttpRequest()
$k = stripslashes($k);
}

if (!$this->binary && is_string($k) && (!preg_match(self::CHARS, $k) || preg_last_error())) {
if (!$this->binary && is_string($k) && (!preg_match($reChars, $k) || preg_last_error())) {
// invalid key -> ignore

} elseif (is_array($v)) {
Expand All @@ -158,8 +159,8 @@ public function createHttpRequest()
if ($gpc && !$useFilter) {
$v = stripSlashes($v);
}
if (!$this->binary && (!preg_match(self::CHARS, $v) || preg_last_error())) {
$v = '';
if (!$this->binary) {
$v = (string) preg_replace('#[^' . self::CHARS . ']+#u', '', $v);
}
$list[$key][$k] = $v;
}
Expand All @@ -174,7 +175,7 @@ public function createHttpRequest()
$list = array();
if (!empty($_FILES)) {
foreach ($_FILES as $k => $v) {
if (!$this->binary && is_string($k) && (!preg_match(self::CHARS, $k) || preg_last_error())) {
if (!$this->binary && is_string($k) && (!preg_match($reChars, $k) || preg_last_error())) {
continue;
}
$v['@'] = & $files[$k];
Expand All @@ -190,7 +191,7 @@ public function createHttpRequest()
if ($gpc) {
$v['name'] = stripSlashes($v['name']);
}
if (!$this->binary && (!preg_match(self::CHARS, $v['name']) || preg_last_error())) {
if (!$this->binary && (!preg_match($reChars, $v['name']) || preg_last_error())) {
$v['name'] = '';
}
if ($v['error'] !== UPLOAD_ERR_NO_FILE) {
Expand All @@ -200,7 +201,7 @@ public function createHttpRequest()
}

foreach ($v['name'] as $k => $foo) {
if (!$this->binary && is_string($k) && (!preg_match(self::CHARS, $k) || preg_last_error())) {
if (!$this->binary && is_string($k) && (!preg_match($reChars, $k) || preg_last_error())) {
continue;
}
$list[] = array(
Expand Down
31 changes: 4 additions & 27 deletions tests/Http/Request.invalidEncoding.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require __DIR__ . '/../bootstrap.php';

// Setup environment
define('INVALID', "\xC4\x76\xC5\xBE");
define('CONTROL_CHARACTERS', "A\x00B\x80C");
define('CONTROL_CHARACTERS', "A\x01B\x02C");

$_GET = array(
'invalid' => INVALID,
Expand Down Expand Up @@ -61,27 +61,6 @@ $_FILES = array(
'error' => 0,
'size' => 209,
),
'file2' => array(
'name' => array(
2 => INVALID,
),

'type' => array(
2 => INVALID,
),

'tmp_name' => array(
2 => 'C:\\PHP\\temp\\php1D5C.tmp',
),

'error' => array(
2 => 0,
),

'size' => array(
2 => 3013,
),
),
);

test(function() { // unfiltered data
Expand Down Expand Up @@ -118,19 +97,19 @@ test(function() { // filtered data
$request = $factory->createHttpRequest();

Assert::same( '', $request->getQuery('invalid') );
Assert::same( '', $request->getQuery('control') );
Assert::same( 'ABC', $request->getQuery('control') );
Assert::null( $request->getQuery(INVALID) );
Assert::null( $request->getQuery(CONTROL_CHARACTERS) );
Assert::false( isset($request->query['array'][INVALID]) );

Assert::same( '', $request->getPost('invalid') );
Assert::same( '', $request->getPost('control') );
Assert::same( 'ABC', $request->getPost('control') );
Assert::null( $request->getPost(INVALID) );
Assert::null( $request->getPost(CONTROL_CHARACTERS) );
Assert::false( isset($request->post['array'][INVALID]) );

Assert::same( '', $request->getCookie('invalid') );
Assert::same( '', $request->getCookie('control') );
Assert::same( 'ABC', $request->getCookie('control') );
Assert::null( $request->getCookie(INVALID) );
Assert::null( $request->getCookie(CONTROL_CHARACTERS) );
Assert::false( isset($request->cookies['array'][INVALID]) );
Expand All @@ -139,6 +118,4 @@ test(function() { // filtered data
Assert::null( $request->getFile(CONTROL_CHARACTERS) );
Assert::type( 'Nette\Http\FileUpload', $request->files['file1'] );
Assert::same( '', $request->files['file1']->name );
Assert::type( 'Nette\Http\FileUpload', $request->files['file2'][2] );
Assert::same( '', $request->files['file2'][2]->name );
});

0 comments on commit 9ad00c8

Please sign in to comment.