-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix #80114: parse_url does not accept URLs with port 0 #6152
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
Conversation
URIs with a 0 port are generally valid, so `parse_url()` should recognize such URIs, but still report the port as missing.
ext/standard/url.c
Outdated
@@ -261,7 +261,7 @@ PHPAPI php_url *php_url_parse_ex(char const *str, size_t length) | |||
memcpy(port_buf, p, (e - p)); | |||
port_buf[e - p] = '\0'; | |||
port = ZEND_STRTOL(port_buf, NULL, 10); | |||
if (port > 0 && port <= 65535) { | |||
if ((port > 0 && port <= 65535) || (port == 0 && *port_buf == '0' && e - p == 1)) { |
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.
Why the additional checks here? If we accept :001
for other port numbers, I don't think zero should get different treatment than that.
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.
The problem is that port==0
also if there is no digit (e.g. http://example.com:foo/
). We could drop the e-p==1
check to be more liberal, though.
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.
Hi, I was just checking how the contributing process with PHP works and found this bug quite interesting for a beginner like me.
Just one question, why not just check if the port >= 0 in this case? In the case that the port is ":foo" I believe it's invalid no? Since the RFC says that the port number should be a decimal number
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.
I suggest that you try that change (checking for port >= 0
) and run the test in ext/standard/tests/url/ – there'll be several failures; check the *.diff files. :) (Hint: ZEND_STROL()
is similar to the (int)
cast in PHP: both ignore any trailing garbage.
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.
Nice I will try to play a bit with the source code, tks man
How about this way, and it also needs to be applied to `//example.com:0`
Another way
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.
It might make more sense to add the test to url.inc instead.
Oh, that makes sense. Thanks! |
URIs with a 0 port are generally valid, so
parse_url()
shouldrecognize such URIs, but still report the port as missing.