php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80067 Omitting the port in bindto setting results in "Failed to parse address" error
Submitted: 2020-09-06 21:17 UTC Modified: 2020-09-12 11:35 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:0 (0.0%)
From: me at derrabus dot de Assigned: cmb (profile)
Status: Closed Package: Sockets related
PHP Version: 7.4.10 OS: macOS 10.15
Private report: No CVE-ID: None
 [2020-09-06 21:17 UTC] me at derrabus dot de
Description:
------------
Before php 7.4.10, it was possible to provide a "bindto" setting without a port via the socket stream context. With 7.4.10, a warning is raised and the stream is not opened.

This breaks Symfony's HttpClient component, see https://github.com/symfony/symfony/issues/38081 for details.

The attached test script reproduces the regression. Executing it on php 7.4.9 produces the expected result. On php 7.4.10, the script fails. If you change the "bindto" setting to "0:0", the script passes on both versions, 7.4.9 and 7.4.10.

Test script:
---------------
<?php

$context = stream_context_create(['socket' => ['bindto' => '0']]);
var_dump(file_get_contents('https://httpbin.org/get', false, $context));


Expected result:
----------------
string(200) "{
  "args": {}, 
  "headers": {
    "Host": "httpbin.org"
  }, 
  "origin": "1.2.3.4", 
  "url": "https://httpbin.org/get"
}


Actual result:
--------------
Warning: file_get_contents(https://httpbin.org/get): failed to open stream: Failed to parse address "0" in /app/test.php on line 4
bool(false)


Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-09-06 21:20 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2020-09-06 21:20 UTC] requinix@php.net
The documentation doesn't actually say that "0" is allowed.

https://www.php.net/manual/en/context.socket.php
> Used to specify the IP address (either IPv4 or IPv6) and/or the port number that
> PHP will use to access the network. The syntax is ip:port for IPv4 addresses,
> and [ip]:port for IPv6 addresses. Setting the IP or the port to 0 will let the
> system choose the IP and/or port.
Thus the allowed values are "0:0" and ":0".
 [2020-09-06 21:49 UTC] me at derrabus dot de
> The documentation doesn't actually say that "0" is allowed.

Your quote says:

> the IP address (either IPv4 or IPv6) and/or the port number

So my understanding is, I can provide either just the IP address or just the port or both. Omitting the port seems valid to me. Your suggestion ":0" also triggers a warning by the way, although the actual operation succeeds.

But this discussion is a bit beside the point: if providing the port had always been mandatory, I would not file a bug ticket here. But it wasn't until recently. My point is that this change is quite heavy for a bugfix release and it breaks a widely used library.
 [2020-09-06 21:57 UTC] nikic@php.net
Possibly caused by https://github.com/php/php-src/pull/5903.
 [2020-09-06 22:28 UTC] requinix@php.net
-Status: Feedback +Status: Open
 [2020-09-06 22:28 UTC] requinix@php.net
> So my understanding is, I can provide either just the IP address or just the
> port or both.
We could blame poor phrasing on the docs' part, but the very next sentence says:

> The syntax is ip:port for IPv4 addresses, and [ip]:port for IPv6 addresses.

To me, that indicates a specification which will supersede just about anything else that might be stated.

This is also when I notice that I totally misunderstood that second syntax. The [] brackets are required because it's for IPv6 - they don't mean the address half is optional.
So actually, according to the docs, the two syntaxes would be "0:0" for IPv4 and "[0]:0" for IPv6.

Not saying that "0" / "[0]" and ":0" / "[]:0" should not be allowed; I don't care either way, though I don't see why they shouldn't be. I'm just questioning why Symfony was relying on undocumented behavior.

Naturally, assuming this break does get fixed, the docs need to be updated.
 [2020-09-07 03:18 UTC] twosee@php.net
It is indeed caused by https://github.com/php/php-src/pull/5903 ...
As a bugfix, the patch is correct, I think.
If we want to revert it to the previous behavior, we should change the implementation of parse_ip_address_ex instead of reverting the patch.
But I don’t think it is a good practice to use "0"...
 [2020-09-07 08:40 UTC] me at derrabus dot de
> But I don’t think it is a good practice to use "0"

Probably. If that zero is too obscure, we can do the same with an actual IP address assigned to the machine you're running the code on, like 192.168.0.1 for example.

* Setting bindto to "192.168.0.1:0" will work on 7.4.9 and 7.4.10.
* Setting bindto to "192.168.0.1" will work on 7.4.9 and break on 7.4.10.

So, omitting the port is what isn't possible anymore.
 [2020-09-07 08:58 UTC] requinix@php.net
> If that zero is too obscure, we can do the same with an actual IP address
> assigned to the machine you're running the code on,
Except that has a different meaning. I don't think there are too many good cases for binding to all interfaces either, but PHP shouldn't disallow it just because it could be misused.
 [2020-09-07 11:48 UTC] me at derrabus dot de
> Except that has a different meaning.

Exactly, but the issue is the same. My fear was that we're focusing too much on that obscure zero, so I wanted to demonstrate that the zero isn't really the issue here.
 [2020-09-09 12:32 UTC] cmb@php.net
The following pull request has been associated:

Patch Name: Fix #80067: Omitting the port in bindto setting errors
On GitHub:  https://github.com/php/php-src/pull/6104
Patch:      https://github.com/php/php-src/pull/6104.patch
 [2020-09-09 12:32 UTC] cmb@php.net
-Assigned To: +Assigned To: cmb
 [2020-09-11 13:03 UTC] cmb@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=dfb3a799140d7d526c0ab77be437b663bfac9cc4
Log: Fix #80067: Omitting the port in bindto setting errors
 [2020-09-11 13:03 UTC] cmb@php.net
-Status: Assigned +Status: Closed
 [2020-09-12 11:35 UTC] me at derrabus dot de
I cannot reproduce the issue on master anymore. Thank you for the quick fix!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 09:01:32 2024 UTC