stas                                     Wed, 07 Mar 2012 07:49:56 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=323986

Log:
MFH: Headers: forbid \r and \n also after \0, allow CRLF followed by HT or SP 
and forbid \0. See bug #60227.

Bug: https://bugs.php.net/60227 (Closed) header() cannot detect the multi-line 
header with CR(0x0D).
      
Changed paths:
    D   
php/php-src/branches/PHP_5_4/ext/standard/tests/general_functions/bug60227.phpt
    A   
php/php-src/branches/PHP_5_4/ext/standard/tests/general_functions/bug60227_1.phpt
    A   
php/php-src/branches/PHP_5_4/ext/standard/tests/general_functions/bug60227_2.phpt
    A   
php/php-src/branches/PHP_5_4/ext/standard/tests/general_functions/bug60227_3.phpt
    A   
php/php-src/branches/PHP_5_4/ext/standard/tests/general_functions/bug60227_4.phpt
    U   php/php-src/branches/PHP_5_4/main/SAPI.c

Deleted: 
php/php-src/branches/PHP_5_4/ext/standard/tests/general_functions/bug60227.phpt
===================================================================
--- 
php/php-src/branches/PHP_5_4/ext/standard/tests/general_functions/bug60227.phpt 
    2012-03-07 07:38:57 UTC (rev 323985)
+++ 
php/php-src/branches/PHP_5_4/ext/standard/tests/general_functions/bug60227.phpt 
    2012-03-07 07:49:56 UTC (rev 323986)
@@ -1,20 +0,0 @@
---TEST--
-Bug #60227 (header() cannot detect the multi-line header with CR)
---FILE--
-<?php
-header("X-Foo1: a");
-header("X-Foo2: b\n ");
-header("X-Foo3: c\r\n ");
-header("X-Foo4: d\r ");
-header("X-Foo5: e\rSet-Cookie: ID=123");
-echo 'foo';
-?>
---EXPECTF--
-Warning: Header may not contain more than a single header, new line detected. 
in %s on line %d
-foo
---EXPECTHEADERS--
-X-Foo1: a
-X-Foo2: b
-X-Foo3: c
-X-Foo4: d
-

Added: 
php/php-src/branches/PHP_5_4/ext/standard/tests/general_functions/bug60227_1.phpt
===================================================================
--- 
php/php-src/branches/PHP_5_4/ext/standard/tests/general_functions/bug60227_1.phpt
                           (rev 0)
+++ 
php/php-src/branches/PHP_5_4/ext/standard/tests/general_functions/bug60227_1.phpt
   2012-03-07 07:49:56 UTC (rev 323986)
@@ -0,0 +1,20 @@
+--TEST--
+Bug #60227 (header() cannot detect the multi-line header with CR)
+--FILE--
+<?php
+header("X-Foo1: a");
+header("X-Foo2: b\n ");
+header("X-Foo3: c\r\n ");
+header("X-Foo4: d\r ");
+header("X-Foo5: e\rSet-Cookie: ID=123");
+echo 'foo';
+?>
+--EXPECTF--
+Warning: Header may not contain more than a single header, new line detected 
in %s on line %d
+foo
+--EXPECTHEADERS--
+X-Foo1: a
+X-Foo2: b
+X-Foo3: c
+X-Foo4: d
+

Added: 
php/php-src/branches/PHP_5_4/ext/standard/tests/general_functions/bug60227_2.phpt
===================================================================
--- 
php/php-src/branches/PHP_5_4/ext/standard/tests/general_functions/bug60227_2.phpt
                           (rev 0)
+++ 
php/php-src/branches/PHP_5_4/ext/standard/tests/general_functions/bug60227_2.phpt
   2012-03-07 07:49:56 UTC (rev 323986)
@@ -0,0 +1,14 @@
+--TEST--
+Bug #60227 (header() cannot detect the multi-line header with CR), \r before \n
+--FILE--
+<?php
+header("X-foo: e\n foo");
+header("X-Foo6: e\rSet-Cookie: ID=123\n d");
+echo 'foo';
+?>
+--EXPECTF--
+Warning: Header may not contain more than a single header, new line detected 
in %s on line %d
+foo
+--EXPECTHEADERS--
+X-foo: e
+foo

Added: 
php/php-src/branches/PHP_5_4/ext/standard/tests/general_functions/bug60227_3.phpt
===================================================================
--- 
php/php-src/branches/PHP_5_4/ext/standard/tests/general_functions/bug60227_3.phpt
                           (rev 0)
+++ 
php/php-src/branches/PHP_5_4/ext/standard/tests/general_functions/bug60227_3.phpt
   2012-03-07 07:49:56 UTC (rev 323986)
@@ -0,0 +1,14 @@
+--TEST--
+Bug #60227 (header() cannot detect the multi-line header with CR), \0 before \n
+--FILE--
+<?php
+header("X-foo: e\n foo");
+header("X-Foo6: e\0Set-Cookie: ID=\n123\n d");
+echo 'foo';
+?>
+--EXPECTF--
+Warning: Header may not contain NUL bytes in %s on line %d
+foo
+--EXPECTHEADERS--
+X-foo: e
+foo

Added: 
php/php-src/branches/PHP_5_4/ext/standard/tests/general_functions/bug60227_4.phpt
===================================================================
--- 
php/php-src/branches/PHP_5_4/ext/standard/tests/general_functions/bug60227_4.phpt
                           (rev 0)
+++ 
php/php-src/branches/PHP_5_4/ext/standard/tests/general_functions/bug60227_4.phpt
   2012-03-07 07:49:56 UTC (rev 323986)
@@ -0,0 +1,14 @@
+--TEST--
+Bug #60227 (header() cannot detect the multi-line header with CR), CRLF
+--FILE--
+<?php
+header("X-foo: e\r\n foo");
+header("X-foo: e\r\nfoo");
+echo 'foo';
+?>
+--EXPECTF--
+Warning: Header may not contain more than a single header, new line detected 
in %s on line %d
+foo
+--EXPECTHEADERS--
+X-foo: e
+ foo

Modified: php/php-src/branches/PHP_5_4/main/SAPI.c
===================================================================
--- php/php-src/branches/PHP_5_4/main/SAPI.c    2012-03-07 07:38:57 UTC (rev 
323985)
+++ php/php-src/branches/PHP_5_4/main/SAPI.c    2012-03-07 07:49:56 UTC (rev 
323986)
@@ -707,16 +707,26 @@
                efree(header_line);
                return SUCCESS;
        } else {
-               /* new line safety check */
-               char *s = header_line, *e = header_line + header_line_len, *p;
-               while (s < e && ((p = memchr(s, '\n', (e - s))) || (p = 
memchr(s, '\r', (e - s))))) {
-                       if (*(p + 1) == ' ' || *(p + 1) == '\t') {
-                               s = p + 1;
-                               continue;
+               /* new line/NUL character safety check */
+               int i;
+               for (i = 0; i < header_line_len; i++) {
+                       /* RFC 2616 allows new lines if followed by SP or HT */
+                       int illegal_break =
+                                       (header_line[i+1] != ' ' && 
header_line[i+1] != '\t')
+                                       && (
+                                               header_line[i] == '\n'
+                                               || (header_line[i] == '\r' && 
header_line[i+1] != '\n'));
+                       if (illegal_break) {
+                               efree(header_line);
+                               sapi_module.sapi_error(E_WARNING, "Header may 
not contain "
+                                               "more than a single header, new 
line detected");
+                               return FAILURE;
                        }
-                       efree(header_line);
-                       sapi_module.sapi_error(E_WARNING, "Header may not 
contain more than a single header, new line detected.");
-                       return FAILURE;
+                       if (header_line[i] == '\0') {
+                               efree(header_line);
+                               sapi_module.sapi_error(E_WARNING, "Header may 
not contain NUL bytes");
+                               return FAILURE;
+                       }
                }
        }


-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to