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

Conversation

cmb69
Copy link
Member

@cmb69 cmb69 commented Sep 17, 2020

URIs with a 0 port are generally valid, so parse_url() should
recognize such URIs, but still report the port as missing.

URIs with a 0 port are generally valid, so `parse_url()` should
recognize such URIs, but still report the port as missing.
@@ -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

How about this way, and it also needs to be applied to `//example.com:0`
@cmb69 cmb69 added the Bug label Sep 19, 2020
Copy link
Member

@nikic nikic left a 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.

@cmb69
Copy link
Member Author

cmb69 commented Sep 20, 2020

Oh, that makes sense. Thanks!

@php-pulls php-pulls closed this in 81b2f3e Sep 20, 2020
@cmb69 cmb69 deleted the cmb/80114 branch September 20, 2020 13:39
@deguif deguif mentioned this pull request Oct 20, 2020
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants