From b8613d5ce36b7a0b7f83c35c26b166ef795b16ea Mon Sep 17 00:00:00 2001 From: Miguel Xavier Penha Neto Date: Mon, 24 Feb 2020 02:04:37 -0300 Subject: [PATCH] Fixes #79265: Improper injection of Host header when using fopen for http requests --- ext/standard/http_fopen_wrapper.c | 84 ++++++++++++++++++--------- ext/standard/tests/http/bug79265.phpt | 39 +++++++++++++ 2 files changed, 95 insertions(+), 28 deletions(-) create mode 100644 ext/standard/tests/http/bug79265.phpt diff --git a/ext/standard/http_fopen_wrapper.c b/ext/standard/http_fopen_wrapper.c index 0aeec9115b1f9..0654958323888 100644 --- a/ext/standard/http_fopen_wrapper.c +++ b/ext/standard/http_fopen_wrapper.c @@ -460,41 +460,69 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, strip_header(user_headers, t, "content-type:"); } - if ((s = strstr(t, "user-agent:")) && - (s == t || *(s-1) == '\r' || *(s-1) == '\n' || - *(s-1) == '\t' || *(s-1) == ' ')) { - have_header |= HTTP_HEADER_USER_AGENT; + s = t; + while ((s = strstr(s, "user-agent:"))) { + if (s == t || *(s-1) == '\r' || *(s-1) == '\n' || + *(s-1) == '\t' || *(s-1) == ' ') { + have_header |= HTTP_HEADER_USER_AGENT; + } + s++; } - if ((s = strstr(t, "host:")) && - (s == t || *(s-1) == '\r' || *(s-1) == '\n' || - *(s-1) == '\t' || *(s-1) == ' ')) { - have_header |= HTTP_HEADER_HOST; + + s = t; + while ((s = strstr(s, "host:"))) { + if (s == t || *(s-1) == '\r' || *(s-1) == '\n' || + *(s-1) == '\t' || *(s-1) == ' ') { + have_header |= HTTP_HEADER_HOST; + } + s++; } - if ((s = strstr(t, "from:")) && - (s == t || *(s-1) == '\r' || *(s-1) == '\n' || - *(s-1) == '\t' || *(s-1) == ' ')) { - have_header |= HTTP_HEADER_FROM; + + s = t; + while ((s = strstr(s, "from:"))) { + if (s == t || *(s-1) == '\r' || *(s-1) == '\n' || + *(s-1) == '\t' || *(s-1) == ' ') { + have_header |= HTTP_HEADER_FROM; } - if ((s = strstr(t, "authorization:")) && - (s == t || *(s-1) == '\r' || *(s-1) == '\n' || - *(s-1) == '\t' || *(s-1) == ' ')) { - have_header |= HTTP_HEADER_AUTH; + s++; } - if ((s = strstr(t, "content-length:")) && - (s == t || *(s-1) == '\r' || *(s-1) == '\n' || - *(s-1) == '\t' || *(s-1) == ' ')) { - have_header |= HTTP_HEADER_CONTENT_LENGTH; + + s = t; + while ((s = strstr(s, "authorization:"))) { + if (s == t || *(s-1) == '\r' || *(s-1) == '\n' || + *(s-1) == '\t' || *(s-1) == ' ') { + have_header |= HTTP_HEADER_AUTH; + } + s++; } - if ((s = strstr(t, "content-type:")) && - (s == t || *(s-1) == '\r' || *(s-1) == '\n' || - *(s-1) == '\t' || *(s-1) == ' ')) { - have_header |= HTTP_HEADER_TYPE; + + s = t; + while ((s = strstr(s, "content-length:"))) { + if (s == t || *(s-1) == '\r' || *(s-1) == '\n' || + *(s-1) == '\t' || *(s-1) == ' ') { + have_header |= HTTP_HEADER_CONTENT_LENGTH; + } + s++; } - if ((s = strstr(t, "connection:")) && - (s == t || *(s-1) == '\r' || *(s-1) == '\n' || - *(s-1) == '\t' || *(s-1) == ' ')) { - have_header |= HTTP_HEADER_CONNECTION; + + s = t; + while ((s = strstr(s, "content-type:"))) { + if (s == t || *(s-1) == '\r' || *(s-1) == '\n' || + *(s-1) == '\t' || *(s-1) == ' ') { + have_header |= HTTP_HEADER_TYPE; + } + s++; } + + s = t; + while ((s = strstr(s, "connection:"))) { + if (s == t || *(s-1) == '\r' || *(s-1) == '\n' || + *(s-1) == '\t' || *(s-1) == ' ') { + have_header |= HTTP_HEADER_CONNECTION; + } + s++; + } + /* remove Proxy-Authorization header */ if (use_proxy && use_ssl && (s = strstr(t, "proxy-authorization:")) && (s == t || *(s-1) == '\r' || *(s-1) == '\n' || diff --git a/ext/standard/tests/http/bug79265.phpt b/ext/standard/tests/http/bug79265.phpt new file mode 100644 index 0000000000000..37221f7e5453d --- /dev/null +++ b/ext/standard/tests/http/bug79265.phpt @@ -0,0 +1,39 @@ +--TEST-- +Bug #79265 (Improper injection of Host header when using fopen for http requests) +--INI-- +allow_url_fopen=1 +--SKIPIF-- + +--FILE-- +array( + 'method'=>"GET", + 'header'=>"RandomHeader: localhost:8080\r\n" . + "Cookie: foo=bar\r\n" . + "Host: userspecifiedvalue\r\n" + ) +); +$context = stream_context_create($opts); +$fd = fopen('http://127.0.0.1:12342/', 'rb', false, $context); +fseek($output, 0, SEEK_SET); +echo stream_get_contents($output); +fclose($fd); + +http_server_kill($pid); + +?> +--EXPECT-- +GET / HTTP/1.0 +Connection: close +RandomHeader: localhost:8080 +Cookie: foo=bar +Host: userspecifiedvalue