[php-src] PHP-8.5.11: ext/standard: strip credentials from user headers on cross-origin redirects

From: Date: Tue, 22 Sep 2026 13:26:05 +0000
Subject: [php-src] PHP-8.5.11: ext/standard: strip credentials from user headers on cross-origin redirects
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-141416@lists.php.net to get a copy of this message
Author: Alexandre Daubois (alexandre-daubois) Committer: Jakub Zelenka (bukka) Pusher: edorian Date: 2026-09-20T14:40:36+02:00 Commit: https://github.com/php/php-src/commit/8f79b50b995797badd6934ef215f8394f8590584 Raw diff: https://github.com/php/php-src/commit/8f79b50b995797badd6934ef215f8394f8590584.diff ext/standard: strip credentials from user headers on cross-origin redirects Changed paths: A ext/standard/tests/http/ghsa-fpwc-w8rq-cr92.phpt M ext/standard/http_fopen_wrapper.c Diff: diff --git a/ext/standard/http_fopen_wrapper.c b/ext/standard/http_fopen_wrapper.c index 202178e41ffd..b7a05e2942d8 100644 --- a/ext/standard/http_fopen_wrapper.c +++ b/ext/standard/http_fopen_wrapper.c @@ -83,27 +83,36 @@ #define HTTP_WRAPPER_HEADER_INIT 1 #define HTTP_WRAPPER_REDIRECTED 2 #define HTTP_WRAPPER_KEEP_METHOD 4 +#define HTTP_WRAPPER_STRIP_AUTH 8 +/* Removes every line whose header name matches. Neither a repeated header nor an + * occurrence of the name inside another header's value may leave the real header + * behind, as that would defeat HTTP_WRAPPER_STRIP_AUTH. */ static inline void strip_header(char *header_bag, char *lc_header_bag, const char *lc_header_name) { - char *lc_header_start = strstr(lc_header_bag, lc_header_name); - if (lc_header_start - && (lc_header_start == lc_header_bag || *(lc_header_start-1) == '\n') - ) { + char *lc_header_start = lc_header_bag; + + while ((lc_header_start = strstr(lc_header_start, lc_header_name))) { + if (lc_header_start != lc_header_bag && *(lc_header_start-1) != '\n') { + lc_header_start += strlen(lc_header_name); + continue; + } + char *header_start = header_bag + (lc_header_start - lc_header_bag); char *lc_eol = strchr(lc_header_start, '\n'); - if (lc_eol) { - char *eol = header_start + (lc_eol - lc_header_start); - size_t eollen = strlen(lc_eol); - - memmove(lc_header_start, lc_eol+1, eollen); - memmove(header_start, eol+1, eollen); - } else { + if (!lc_eol) { *lc_header_start = '\0'; *header_start = '\0'; + return; } + + char *eol = header_start + (lc_eol - lc_header_start); + size_t eollen = strlen(lc_eol); + + memmove(lc_header_start, lc_eol+1, eollen); + memmove(header_start, eol+1, eollen); } } @@ -704,6 +713,21 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, strip_header(user_headers, t, "content-type:"); } + if (flags & HTTP_WRAPPER_STRIP_AUTH) { + strip_header(user_headers, t, "authorization:"); + strip_header(user_headers, t, "cookie:"); + if (!use_proxy) { + strip_header(user_headers, t, "proxy-authorization:"); + } + } + + if (*user_headers == '\0') { + /* everything got stripped, keeping the empty bag would append a + * stray CRLF and end the header block early */ + efree(user_headers); + user_headers = NULL; + } + if (check_has_header(t, "user-agent:")) { have_header |= HTTP_HEADER_USER_AGENT; } @@ -1101,14 +1125,24 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, header_info.location = NULL; } - php_uri_struct_free(resource); - /* check for invalid redirection URLs */ - if ((resource = php_uri_parse_to_struct(uri_parser, new_path, strlen(new_path), PHP_URI_COMPONENT_READ_MODE_RAW, true)) == NULL) { + php_uri *new_resource = php_uri_parse_to_struct(uri_parser, new_path, strlen(new_path), PHP_URI_COMPONENT_READ_MODE_RAW, true); + if (new_resource == NULL) { php_stream_wrapper_log_error(wrapper, options, "Invalid redirect URL! %s", new_path); efree(new_path); goto out; } + zend_long default_port = use_ssl ? 443 : 80; + bool same_origin = resource->scheme && new_resource->scheme + && zend_string_equals_ci(resource->scheme, new_resource->scheme) + && resource->host && new_resource->host + && zend_string_equals_ci(resource->host, new_resource->host) + && (resource->port ? resource->port : default_port) + == (new_resource->port ? new_resource->port : default_port); + + php_uri_struct_free(resource); + resource = new_resource; + #define CHECK_FOR_CNTRL_CHARS(val) { \ if (val) { \ unsigned char *s, *e; \ @@ -1130,7 +1164,10 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, CHECK_FOR_CNTRL_CHARS(resource->password); CHECK_FOR_CNTRL_CHARS(resource->path); } - int new_flags = HTTP_WRAPPER_REDIRECTED; + int new_flags = HTTP_WRAPPER_REDIRECTED | (flags & HTTP_WRAPPER_STRIP_AUTH); + if (!same_origin) { + new_flags |= HTTP_WRAPPER_STRIP_AUTH; + } if (response_code == 307 || response_code == 308) { /* RFC 7538 specifies that status code 308 does not allow changing the request method from POST to GET. * RFC 7231 does the same for status code 307. diff --git a/ext/standard/tests/http/ghsa-fpwc-w8rq-cr92.phpt b/ext/standard/tests/http/ghsa-fpwc-w8rq-cr92.phpt new file mode 100644 index 000000000000..21a8b911a34b --- /dev/null +++ b/ext/standard/tests/http/ghsa-fpwc-w8rq-cr92.phpt @@ -0,0 +1,85 @@ +--TEST-- +GHSA-fpwc-w8rq-cr92: strip credentials from user headers on cross-origin redirect +--INI-- +allow_url_fopen=1 +--SKIPIF-- +<?php require 'server.inc'; http_server_skipif(); ?> +--FILE-- +<?php +require 'server.inc'; + +function count_header(string $requests, string $name): int { + return preg_match_all('/^' . preg_quote($name, '/') . ':/mi', $requests); +} + +function report(string $label, string $requests): void { + echo $label, "\n"; + foreach (['Authorization', 'Cookie', 'Proxy-Authorization', 'X-Custom'] as $name) { + echo " $name: ", count_header($requests, $name), "\n"; + } +} + +$ctx = stream_context_create(['http' => [ + 'header' => "Authorization: Bearer SECRET\r\n" + . "Cookie: sid=abc\r\n" + . "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n" + . "X-Custom: keep-me", + 'follow_location' => 1, +]]); + +/* server B listens on a different port than server A, so the hop from A to B is + * cross-origin; B then redirects to itself: credentials must stay withheld for + * that same-origin hop too */ +$captureB = null; +['pid' => $pidB, 'uri' => $uriB] = http_server([ + "data://text/plain,HTTP/1.1 302 Found\r\nLocation: /second\r\nContent-Length: 0\r\n\r\n", + "data://text/plain,HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK", +], $captureB); + +$captureA = null; +['pid' => $pidA, 'uri' => $uriA] = http_server([ + "data://text/plain,HTTP/1.1 302 Found\r\nLocation: $uriB/first\r\nContent-Length: 0\r\n\r\n", +], $captureA); + +var_dump(file_get_contents($uriA . '/src', false, $ctx)); + +http_server_kill($pidA); +http_server_kill($pidB); + +rewind($captureA); +rewind($captureB); +report('--- origin A (1 request) ---', stream_get_contents($captureA)); +report('--- origin B (2 requests) ---', stream_get_contents($captureB)); + +/* same origin throughout: credentials must be sent on both hops */ +$captureC = null; +['pid' => $pidC, 'uri' => $uriC] = http_server([ + "data://text/plain,HTTP/1.1 302 Found\r\nLocation: /next\r\nContent-Length: 0\r\n\r\n", + "data://text/plain,HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK", +], $captureC); + +var_dump(file_get_contents($uriC . '/first', false, $ctx)); + +http_server_kill($pidC); + +rewind($captureC); +report('--- origin C (2 requests) ---', stream_get_contents($captureC)); +?> +--EXPECT-- +string(2) "OK" +--- origin A (1 request) --- + Authorization: 1 + Cookie: 1 + Proxy-Authorization: 1 + X-Custom: 1 +--- origin B (2 requests) --- + Authorization: 0 + Cookie: 0 + Proxy-Authorization: 0 + X-Custom: 2 +string(2) "OK" +--- origin C (2 requests) --- + Authorization: 2 + Cookie: 2 + Proxy-Authorization: 2 + X-Custom: 2

« previous php.cvs (#141416) next »