Skip to content

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions ext/standard/tests/url/bug80114.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Bug #80114 (parse_url does not accept URLs with port 0)
--FILE--
<?php
var_dump(parse_url('https://example.com:0/'));
?>
--EXPECT--
array(3) {
["scheme"]=>
string(5) "https"
["host"]=>
string(11) "example.com"
["path"]=>
string(1) "/"
}
2 changes: 1 addition & 1 deletion ext/standard/url.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Copy link
Member

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.

Copy link
Member Author

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.

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

Copy link
Member Author

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.

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

ret->port = (unsigned short)port;
} else {
php_url_free(ret);
Expand Down