Skip to content

Fix stream_context_set_option() mutating the default context#22235

Open
nicolas-grekas wants to merge 1 commit into
php:masterfrom
nicolas-grekas:fix-stream-context-default-leak
Open

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

Conversation

@nicolas-grekas
Copy link
Copy Markdown
Contributor

@nicolas-grekas nicolas-grekas commented Jun 5, 2026

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.
Sign up for free to 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.

1 participant