[php-src] PHP-8.4.26: Fix GHSA-fpwc-w8rq-cr92: strip credentials from user headers on cross-origin redirects
| From: | Alexandre Daubois via Jakub Zelenka | Date: | Tue, 22 Sep 2026 14:40:48 +0000 |
| Subject: | [php-src] PHP-8.4.26: Fix GHSA-fpwc-w8rq-cr92: strip credentials from user headers on cross-origin redirects | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-141433@lists.php.net to get a copy of this message | ||
Author: Alexandre Daubois (alexandre-daubois)
Committer: Jakub Zelenka (bukka)
Pusher: SakiTakamachi
Date: 2026-09-19T16:13:18+02:00
Commit: https://github.com/php/php-src/commit/93eb2fd6b99b075dd09793f893fe554568b39390
Raw diff: https://github.com/php/php-src/commit/93eb2fd6b99b075dd09793f893fe554568b39390.diff
Fix GHSA-fpwc-w8rq-cr92: strip credentials from user headers on cross-origin redirects
Authorization, Cookie and Proxy-Authorization set through the http context
were forwarded verbatim when follow_location sent the request to another
origin. They are now stripped from the user header bag whenever the
redirect target differs in scheme, host or port, including when the header
name is repeated or the bag uses malformed line endings.
Co-authored-by: Jakub Zelenka <bukka@php.net>
Changed paths:
A ext/standard/tests/http/ghsa-fpwc-w8rq-cr92.phpt
M ext/standard/http_fopen_wrapper.c
M ext/standard/tests/http/bug61548.phpt
Diff:
diff --git a/ext/standard/http_fopen_wrapper.c b/ext/standard/http_fopen_wrapper.c
index 891c1b23955f..2cc9e081ae7d 100644
--- a/ext/standard/http_fopen_wrapper.c
+++ b/ext/standard/http_fopen_wrapper.c
@@ -81,27 +81,70 @@
#define HTTP_WRAPPER_HEADER_INIT 1
#define HTTP_WRAPPER_REDIRECTED 2
#define HTTP_WRAPPER_KEEP_METHOD 4
+#define HTTP_WRAPPER_STRIP_AUTH 8
+static char *next_header_line(char *line)
+{
+ while (*line != '\0' && *line != '\r' && *line !=
'\n') {
+ line++;
+ }
+ if (*line == '\r') {
+ line++;
+ }
+ if (*line == '\n') {
+ line++;
+ }
+
+ return line;
+}
+
+/* Removes every line whose header name matches, along with the folded
+ * continuation lines carrying the rest of its value. 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 *header_start = header_bag + (lc_header_start - lc_header_bag);
- char *lc_eol = strchr(lc_header_start, '\n');
+ size_t name_len = strlen(lc_header_name);
+ char *lc_line = lc_header_bag;
- if (lc_eol) {
- char *eol = header_start + (lc_eol - lc_header_start);
- size_t eollen = strlen(lc_eol);
+ while (*lc_line != '\0') {
+ if (strncmp(lc_line, lc_header_name, name_len) != 0) {
+ lc_line = next_header_line(lc_line);
+ continue;
+ }
- memmove(lc_header_start, lc_eol+1, eollen);
- memmove(header_start, eol+1, eollen);
- } else {
- *lc_header_start = '\0';
- *header_start = '\0';
+ /* the whitespace RFC 7230 forbids before the colon is tolerated by some
+ * servers, so it must not hide the header from us either */
+ const char *lc_colon = lc_line + name_len;
+ while (*lc_colon == ' ' || *lc_colon == '\t') {
+ lc_colon++;
+ }
+
+ if (*lc_colon != ':') {
+ lc_line = next_header_line(lc_line);
+ continue;
}
+
+ char *lc_next = next_header_line(lc_line);
+ while (*lc_next == ' ' || *lc_next == '\t') {
+ lc_next = next_header_line(lc_next);
+ }
+
+ if (*lc_next == '\0') {
+ /* drop the preceding line break too, or the one appended after the bag
+ * would close the header block early */
+ while (lc_line > lc_header_bag
+ && (*(lc_line - 1) == '\r' || *(lc_line - 1) == '\n')) {
+ --lc_line;
+ }
+ }
+
+ size_t tail_len = strlen(lc_next) + 1;
+ char *line = header_bag + (lc_line - lc_header_bag);
+
+ memmove(line, header_bag + (lc_next - lc_header_bag), tail_len);
+ memmove(lc_line, lc_next, tail_len);
}
}
@@ -697,8 +740,23 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
if (!header_init && !redirect_keep_method) {
/* strip POST headers on redirect */
- strip_header(user_headers, t, "content-length:");
- strip_header(user_headers, t, "content-type:");
+ strip_header(user_headers, t, "content-length");
+ 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:")) {
@@ -1100,14 +1158,22 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
header_info.location = NULL;
}
- php_url_free(resource);
- /* check for invalid redirection URLs */
- if ((resource = php_url_parse(new_path)) == NULL) {
+ php_url *new_resource = php_url_parse(new_path);
+ if (new_resource == NULL) {
php_stream_wrapper_log_error(wrapper, options, "Invalid redirect URL! %s", new_path);
efree(new_path);
goto out;
}
+ int default_port = use_ssl ? 443 : 80;
+ bool same_origin = zend_string_equals_ci(resource->scheme, new_resource->scheme)
+ && zend_string_equals_ci(resource->host, new_resource->host)
+ && (resource->port ? resource->port : default_port)
+ == (new_resource->port ? new_resource->port : default_port);
+
+ php_url_free(resource);
+ resource = new_resource;
+
#define CHECK_FOR_CNTRL_CHARS(val) { \
if (val) { \
unsigned char *s, *e; \
@@ -1129,7 +1195,10 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
CHECK_FOR_CNTRL_CHARS(resource->pass);
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/bug61548.phpt b/ext/standard/tests/http/bug61548.phpt
index 5f21b3769dd8..ba46e65704f3 100644
--- a/ext/standard/tests/http/bug61548.phpt
+++ b/ext/standard/tests/http/bug61548.phpt
@@ -55,7 +55,6 @@ Connection: close
First:1
Second:2
-
POST / HTTP/1.1
Host: %s:%d
Connection: close
@@ -69,7 +68,6 @@ Connection: close
First:1
Second:2
-
POST / HTTP/1.1
Host: %s:%d
Connection: close
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..5ccc88b3cc4d
--- /dev/null
+++ b/ext/standard/tests/http/ghsa-fpwc-w8rq-cr92.phpt
@@ -0,0 +1,159 @@
+--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));
+
+/* a stripped header that was last in the bag must not leave a trailing line break
+ * behind, or the request body would be pushed out of the request */
+$ctx = stream_context_create(['http' => [
+ 'method' => 'POST',
+ 'content' => 'hello=world',
+ 'header' => "X-Custom: keep-me\r\nAuthorization: Bearer SECRET",
+ 'follow_location' => 1,
+]]);
+
+$captureH = null;
+['pid' => $pidH, 'uri' => $uriH] = http_server([
+ "data://text/plain,HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK",
+], $captureH);
+
+$captureG = null;
+['pid' => $pidG, 'uri' => $uriG] = http_server([
+ "data://text/plain,HTTP/1.1 307 Temporary Redirect\r\nLocation:
$uriH/second\r\nContent-Length: 0\r\n\r\n",
+], $captureG);
+
+var_dump(@file_get_contents($uriG . '/first', false, $ctx));
+
+http_server_kill($pidG);
+http_server_kill($pidH);
+
+rewind($captureH);
+echo "--- credential header last in the bag (307) ---\n";
+echo preg_replace('/^Host:.*$/m', 'Host: ...', stream_get_contents($captureH));
+
+echo "--- malformed header bags ---\n";
+foreach ([
+ 'folded value ' => "Authorization:\r\n Bearer SECRET\r\nX-Custom:
keep-me",
+ 'folded value (tab)' => "Authorization:\r\n\tBearer SECRET\r\nX-Custom:
keep-me",
+ 'lone CR ' => "X-Custom: keep-me\rAuthorization: Bearer
SECRET",
+ 'space before colon' => "Authorization : Bearer SECRET\r\nX-Custom:
keep-me",
+ 'tab before colon ' => "Authorization\t: Bearer SECRET\r\nX-Custom:
keep-me",
+] as $label => $header) {
+ $ctx = stream_context_create(['http' => ['header' => $header,
'follow_location' => 1]]);
+
+ $captureF = null;
+ ['pid' => $pidF, 'uri' => $uriF] = http_server([
+ "data://text/plain,HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK",
+ ], $captureF);
+
+ $captureE = null;
+ ['pid' => $pidE, 'uri' => $uriE] = http_server([
+ "data://text/plain,HTTP/1.1 302 Found\r\nLocation: $uriF/second\r\nContent-Length:
0\r\n\r\n",
+ ], $captureE);
+
+ file_get_contents($uriE . '/first', false, $ctx);
+
+ http_server_kill($pidE);
+ http_server_kill($pidF);
+
+ rewind($captureF);
+ $request = stream_get_contents($captureF);
+ printf(" %s SECRET leaked: %d, X-Custom kept: %d\n", $label,
+ str_contains($request, 'SECRET'), str_contains($request, 'X-Custom'));
+}
+?>
+--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
+string(2) "OK"
+--- credential header last in the bag (307) ---
+POST /second HTTP/1.1
+Host: ...
+Connection: close
+Content-Length: 11
+X-Custom: keep-me
+Content-Type: application/x-www-form-urlencoded
+
+hello=world--- malformed header bags ---
+ folded value SECRET leaked: 0, X-Custom kept: 1
+ folded value (tab) SECRET leaked: 0, X-Custom kept: 1
+ lone CR SECRET leaked: 0, X-Custom kept: 1
+ space before colon SECRET leaked: 0, X-Custom kept: 1
+ tab before colon SECRET leaked: 0, X-Custom kept: 1