Skip to content

Commit 9ad00c8

Browse files
committed
HttpRequest: drops non-UTF8 strings, but control characters only removes
1 parent 01ef085 commit 9ad00c8

File tree

2 files changed

+12
-34
lines changed

2 files changed

+12
-34
lines changed

src/Http/RequestFactory.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class RequestFactory extends Nette\Object
2020
{
2121
/** @internal */
22-
const CHARS = '#^[\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}]*+\z#u';
22+
const CHARS = '\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}';
2323

2424
/** @var array */
2525
public $urlFilters = array(
@@ -137,6 +137,7 @@ public function createHttpRequest()
137137
$gpc = (bool) get_magic_quotes_gpc();
138138

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

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

153154
} elseif (is_array($v)) {
@@ -158,8 +159,8 @@ public function createHttpRequest()
158159
if ($gpc && !$useFilter) {
159160
$v = stripSlashes($v);
160161
}
161-
if (!$this->binary && (!preg_match(self::CHARS, $v) || preg_last_error())) {
162-
$v = '';
162+
if (!$this->binary) {
163+
$v = (string) preg_replace('#[^' . self::CHARS . ']+#u', '', $v);
163164
}
164165
$list[$key][$k] = $v;
165166
}
@@ -174,7 +175,7 @@ public function createHttpRequest()
174175
$list = array();
175176
if (!empty($_FILES)) {
176177
foreach ($_FILES as $k => $v) {
177-
if (!$this->binary && is_string($k) && (!preg_match(self::CHARS, $k) || preg_last_error())) {
178+
if (!$this->binary && is_string($k) && (!preg_match($reChars, $k) || preg_last_error())) {
178179
continue;
179180
}
180181
$v['@'] = & $files[$k];
@@ -190,7 +191,7 @@ public function createHttpRequest()
190191
if ($gpc) {
191192
$v['name'] = stripSlashes($v['name']);
192193
}
193-
if (!$this->binary && (!preg_match(self::CHARS, $v['name']) || preg_last_error())) {
194+
if (!$this->binary && (!preg_match($reChars, $v['name']) || preg_last_error())) {
194195
$v['name'] = '';
195196
}
196197
if ($v['error'] !== UPLOAD_ERR_NO_FILE) {
@@ -200,7 +201,7 @@ public function createHttpRequest()
200201
}
201202

202203
foreach ($v['name'] as $k => $foo) {
203-
if (!$this->binary && is_string($k) && (!preg_match(self::CHARS, $k) || preg_last_error())) {
204+
if (!$this->binary && is_string($k) && (!preg_match($reChars, $k) || preg_last_error())) {
204205
continue;
205206
}
206207
$list[] = array(

tests/Http/Request.invalidEncoding.phpt

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require __DIR__ . '/../bootstrap.php';
1313

1414
// Setup environment
1515
define('INVALID', "\xC4\x76\xC5\xBE");
16-
define('CONTROL_CHARACTERS', "A\x00B\x80C");
16+
define('CONTROL_CHARACTERS', "A\x01B\x02C");
1717

1818
$_GET = array(
1919
'invalid' => INVALID,
@@ -61,27 +61,6 @@ $_FILES = array(
6161
'error' => 0,
6262
'size' => 209,
6363
),
64-
'file2' => array(
65-
'name' => array(
66-
2 => INVALID,
67-
),
68-
69-
'type' => array(
70-
2 => INVALID,
71-
),
72-
73-
'tmp_name' => array(
74-
2 => 'C:\\PHP\\temp\\php1D5C.tmp',
75-
),
76-
77-
'error' => array(
78-
2 => 0,
79-
),
80-
81-
'size' => array(
82-
2 => 3013,
83-
),
84-
),
8564
);
8665

8766
test(function() { // unfiltered data
@@ -118,19 +97,19 @@ test(function() { // filtered data
11897
$request = $factory->createHttpRequest();
11998

12099
Assert::same( '', $request->getQuery('invalid') );
121-
Assert::same( '', $request->getQuery('control') );
100+
Assert::same( 'ABC', $request->getQuery('control') );
122101
Assert::null( $request->getQuery(INVALID) );
123102
Assert::null( $request->getQuery(CONTROL_CHARACTERS) );
124103
Assert::false( isset($request->query['array'][INVALID]) );
125104

126105
Assert::same( '', $request->getPost('invalid') );
127-
Assert::same( '', $request->getPost('control') );
106+
Assert::same( 'ABC', $request->getPost('control') );
128107
Assert::null( $request->getPost(INVALID) );
129108
Assert::null( $request->getPost(CONTROL_CHARACTERS) );
130109
Assert::false( isset($request->post['array'][INVALID]) );
131110

132111
Assert::same( '', $request->getCookie('invalid') );
133-
Assert::same( '', $request->getCookie('control') );
112+
Assert::same( 'ABC', $request->getCookie('control') );
134113
Assert::null( $request->getCookie(INVALID) );
135114
Assert::null( $request->getCookie(CONTROL_CHARACTERS) );
136115
Assert::false( isset($request->cookies['array'][INVALID]) );
@@ -139,6 +118,4 @@ test(function() { // filtered data
139118
Assert::null( $request->getFile(CONTROL_CHARACTERS) );
140119
Assert::type( 'Nette\Http\FileUpload', $request->files['file1'] );
141120
Assert::same( '', $request->files['file1']->name );
142-
Assert::type( 'Nette\Http\FileUpload', $request->files['file2'][2] );
143-
Assert::same( '', $request->files['file2'][2]->name );
144121
});

0 commit comments

Comments
 (0)