Skip to content

Fix stream_context_set_option() mutating the default context - #22235

Merged
bukka merged 1 commit into
php:masterfrom
nicolas-grekas:fix-stream-context-default-leak
Jun 14, 2026
Merged

Fix stream_context_set_option() mutating the default context#22235
bukka merged 1 commit into
php:masterfrom
nicolas-grekas:fix-stream-context-default-leak

Conversation

@nicolas-grekas

@nicolas-grekasnicolas-grekas commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Since #20524, _php_stream_open_wrapper_ex() attaches the implicitly substituted default context to context-less streams. A later stream_context_set_option() on such a stream then mutates the global default context, leaking options into every other context-less stream (regression from 8.5):

$a = fopen('php://memory', 'r+');
stream_context_set_option($a, 'http', 'filename', 'test.txt');
var_dump(stream_context_get_options(fopen('php://memory', 'r+')));
// 8.5: [], 8.6: ['http' => ['filename' => 'test.txt']]

Fixed by attaching only explicitly provided contexts. Stream errors are unaffected: they already fall back to the default context when the stream has none.

Since phpGH-20524, _php_stream_open_wrapper_ex() attaches the context to a
stream that has none, including the implicitly substituted default
context. Sharing the default context by reference let a later
stream_context_set_option() on the stream mutate the global default
context, leaking options into every other context-less stream.
Only attach explicitly provided contexts. Stream errors already fall
back to the default context when the stream has none, so error handling
is unaffected.
@bukka
bukka merged commit 92128ac into php:masterJun 14, 2026
18 checks passed
@nicolas-grekas
nicolas-grekas deleted the fix-stream-context-default-leak branch June 15, 2026 05:04
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@nicolas-grekas@bukka