-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix GH-17976: CRLF injection via from and user_agent in HTTP wrapper #21658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iliaal
wants to merge
1
commit into
php:master
Choose a base branch
from
iliaal:fix/gh-17976-crlf-injection-http-wrapper
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| --TEST-- | ||
| GH-17976 (CRLF injection via from and user_agent INI settings in HTTP wrapper) | ||
| --INI-- | ||
| allow_url_fopen=1 | ||
| --FILE-- | ||
| <?php | ||
| $serverCode = <<<'CODE' | ||
| $ctxt = stream_context_create([ | ||
| "socket" => ["tcp_nodelay" => true] | ||
| ]); | ||
|
|
||
| $server = stream_socket_server( | ||
| "tcp://127.0.0.1:0", $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $ctxt); | ||
| phpt_notify_server_start($server); | ||
|
|
||
| for ($i = 0; $i < 4; $i++) { | ||
| $conn = stream_socket_accept($server); | ||
| $result = fread($conn, 4096); | ||
| fwrite($conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n" . base64_encode($result)); | ||
| fclose($conn); | ||
| } | ||
| CODE; | ||
|
|
||
| $clientCode = <<<'CODE' | ||
| // Test 1: from INI with CRLF | ||
| ini_set("from", "test\r\nInjected-From: evil"); | ||
| ini_set("user_agent", "clean_ua"); | ||
| $raw = base64_decode(file_get_contents("http://{{ ADDR }}/")); | ||
| echo (str_contains($raw, "Injected-From:") ? "FAIL" : "OK") . ": from INI\n"; | ||
|
|
||
| // Test 2: user_agent INI with CRLF | ||
| ini_restore("from"); | ||
| ini_set("user_agent", "test\r\nInjected-UA: evil"); | ||
| $raw = base64_decode(file_get_contents("http://{{ ADDR }}/")); | ||
| echo (str_contains($raw, "Injected-UA:") ? "FAIL" : "OK") . ": user_agent INI\n"; | ||
|
|
||
| // Test 3: user_agent context option with CRLF | ||
| ini_restore("user_agent"); | ||
| $ctx = stream_context_create(["http" => [ | ||
| "user_agent" => "test\nInjected-Ctx: evil" | ||
| ]]); | ||
| $raw = base64_decode(file_get_contents("http://{{ ADDR }}/", false, $ctx)); | ||
| echo (str_contains($raw, "Injected-Ctx:") ? "FAIL" : "OK") . ": user_agent context\n"; | ||
|
|
||
| // Test 4: user_agent INI with a NUL byte before the CRLF | ||
| ini_set("user_agent", "ua\0valid\r\nInjected-Nul: evil"); | ||
| $raw = base64_decode(file_get_contents("http://{{ ADDR }}/")); | ||
| echo (str_contains($raw, "Injected-Nul:") ? "FAIL" : "OK") . ": user_agent NUL+CRLF\n"; | ||
| CODE; | ||
|
|
||
| include sprintf("%s/../../../openssl/tests/ServerClientTestCase.inc", __DIR__); | ||
| ServerClientTestCase::getInstance()->run($clientCode, $serverCode); | ||
| ?> | ||
| --EXPECTF-- | ||
| Warning: file_get_contents(): Header From value contains newline characters and has been truncated in %s on line %d | ||
| OK: from INI | ||
|
|
||
| Warning: file_get_contents(): Header User-Agent value contains newline characters and has been truncated in %s on line %d | ||
| OK: user_agent INI | ||
|
|
||
| Warning: file_get_contents(): Header User-Agent value contains newline characters and has been truncated in %s on line %d | ||
| OK: user_agent context | ||
|
|
||
| Warning: file_get_contents(): Header User-Agent value contains newline characters and has been truncated in %s on line %d | ||
| OK: user_agent NUL+CRLF |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside/follow-up, wouldn't it be better to convert the from_address and user_agent file globals to zend_strings to have their length, as currently null bytes will be truncated from those.