Commit:    7ac36ec8e5bfbbe06019d6123a7ae5eb908fc5e3
Author:    Johannes Schl├╝ter <johan...@php.net>         Thu, 12 Apr 2012 
11:08:00 +0200
Parents:   8ccf0655776c512f1e1c341e115e07d0e8ba6091
Branches:  PHP-5.3.11

Link:       
http://git.php.net/?p=php-src.git;a=commitdiff;h=7ac36ec8e5bfbbe06019d6123a7ae5eb908fc5e3

Log:
Cherry-pick 4cc74767

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

Conflicts:

        ext/standard/tests/general_functions/bug60227.phpt
        ext/standard/tests/general_functions/bug60227_1.phpt
        ext/standard/tests/general_functions/bug60227_2.phpt
        main/SAPI.c

Bugs:
https://bugs.php.net/60227

Changed paths:
  M  ext/standard/tests/general_functions/bug60227_1.phpt
  M  ext/standard/tests/general_functions/bug60227_2.phpt
  A  ext/standard/tests/general_functions/bug60227_3.phpt
  A  ext/standard/tests/general_functions/bug60227_4.phpt
  M  main/SAPI.c


Diff:
diff --git a/ext/standard/tests/general_functions/bug60227_1.phpt 
b/ext/standard/tests/general_functions/bug60227_1.phpt
index f95a061..8efe222 100644
--- a/ext/standard/tests/general_functions/bug60227_1.phpt
+++ b/ext/standard/tests/general_functions/bug60227_1.phpt
@@ -10,7 +10,7 @@ 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
+Warning: Header may not contain more than a single header, new line detected 
in %s on line %d
 foo
 --EXPECTHEADERS--
 X-Foo1: a
diff --git a/ext/standard/tests/general_functions/bug60227_2.phpt 
b/ext/standard/tests/general_functions/bug60227_2.phpt
index fd383f3..995c364 100644
--- a/ext/standard/tests/general_functions/bug60227_2.phpt
+++ b/ext/standard/tests/general_functions/bug60227_2.phpt
@@ -7,7 +7,7 @@ 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
+Warning: Header may not contain more than a single header, new line detected 
in %s on line %d
 foo
 --EXPECTHEADERS--
 X-foo: e
diff --git a/ext/standard/tests/general_functions/bug60227_3.phpt 
b/ext/standard/tests/general_functions/bug60227_3.phpt
new file mode 100644
index 0000000..8cba9b8
--- /dev/null
+++ b/ext/standard/tests/general_functions/bug60227_3.phpt
@@ -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
diff --git a/ext/standard/tests/general_functions/bug60227_4.phpt 
b/ext/standard/tests/general_functions/bug60227_4.phpt
new file mode 100644
index 0000000..d5e2573
--- /dev/null
+++ b/ext/standard/tests/general_functions/bug60227_4.phpt
@@ -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
diff --git a/main/SAPI.c b/main/SAPI.c
index 271fd21..936aee9 100644
--- a/main/SAPI.c
+++ b/main/SAPI.c
@@ -590,17 +590,26 @@ SAPI_API int sapi_header_op(sapi_header_op_enum op, void 
*arg TSRMLS_DC)
                        return FAILURE;
                }
        } else {
-               /* new line safety check */
-               char *s = header_line;
-               while (s = strpbrk(s, "\n\r")) {
-                       if (s[1] == ' ' || s[1] == '\t') {
-                               /* RFC 2616 allows new lines if followed by SP 
or HT */
-                               s++;
-                               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;
+                       }
+                       if (header_line[i] == '\0') {
+                               efree(header_line);
+                               sapi_module.sapi_error(E_WARNING, "Header may 
not contain NUL bytes");
+                               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;
                }
        }


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

Reply via email to