Bug #79265 [Com]: Improper injection of Host header when using fopen for http requests

From: Date: Wed, 12 Feb 2020 16:50:54 +0000
Subject: Bug #79265 [Com]: Improper injection of Host header when using fopen for http requests
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-225537@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79265&edit=1 ID: 79265 Comment by: miguelxpn2 at gmail dot com Reported by: miguelxpn2 at gmail dot com Summary: Improper injection of Host header when using fopen for http requests Status: Open Type: Bug Package: HTTP related Operating System: Linux PHP Version: master-Git-2020-02-12 (Git) Block user comment: N Private report: N New Comment: I dug into the source code to find out why this happens. This seems to be caused by an improper search of the Host: header to find out whether we should inject it or not. Inside http_fopen_wrapper.c we use strstr to search an occurrence of the string 'host:' in the headers, and if that occurrence is in the beginning of a line then we set the have_header variable with HTTP_HEADER_HOST flag. If you try to send the Host header after another header that contains the string host: in the value of the header that flag won't be set and the default header will be injected. A fix would be to check for every occurence of the host: string instead of just getting the first one. Previous Comments: ------------------------------------------------------------------------ [2020-02-12 16:47:37] miguelxpn2 at gmail dot com Description: ------------ If there is any header in the http request that contains the string 'host:' in their value before the actual host header the requests ends up being sent with two headers Host, the one specified by the user and the default one that's injected in case the header wasn't present. Test script: --------------- <?php $opts = array( 'http'=>array( 'method'=>"GET", 'header'=>"RandomHeader: localhost:8080\r\n" . "Cookie: foo=bar\r\n" . "Host: testcustomheader \r\n" ) ); $context = stream_context_create($opts); $fp = fopen('http://myserver.com', 'r', false, $context); fpassthru($fp); Expected result: ---------------- The raw headers should only contain the host header specified by the user that was sent in the context. Actual result: -------------- We get a faulty request with two Host headers instead: GET / HTTP/1.0 Host: myserver.com RandomHeader: localhost:8080 Cookie: foo=bar Host: testcustomhostheader ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=79265&edit=1

« previous php.bugs (#225537) next »