Skip to content
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

Improper validation of the port in URL.with_port #746

Closed
1 task done
decorator-factory opened this issue Jul 2, 2022 · 4 comments
Closed
1 task done

Improper validation of the port in URL.with_port #746

decorator-factory opened this issue Jul 2, 2022 · 4 comments

Comments

@decorator-factory
Copy link

decorator-factory commented Jul 2, 2022

Describe the bug

URL.with_port doesn't fully validate its argument. It is possible to set a boolean port, a negative port, or a port greater than 65535. Additionally, if the provided port is 0, it is treated as None (i.e. removes the port).

Notably, a bool port or a port out of range will cause an error when trying to retrieve the port:

from yarl import URL
url = URL("http://example.com")
true_url = url.with_port(True)
print(true_url.port)  # ValueError here

To Reproduce

  1. Install yarl==1.7.2
  2. Run the following code:
from yarl import URL
url = URL("http://example.com")
print(url.with_port(True))
print(url.with_port(0))
print(url.with_port(-1))
print(url.with_port(99999))

Expected behavior

  1. yarl.URL("http://example.com").with_port(True) raises a TypeError
  2. yarl.URL("http://example.com").with_port(0) raises a ValueError
  3. yarl.URL("http://example.com").with_port(-1) raises a ValueError
  4. yarl.URL("http://example.com").with_port(99999) raises a ValueError

Logs/tracebacks

Output of the code in To Reproduce

http://example.com:True
http://example.com
http://example.com:-1
http://example.com:99999

Python Version

CPython 3.9.8

multidict Version

6.0.2

yarl Version

1.7.2

OS

Manjaro Linux

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct
@webknjaz
Copy link
Member

webknjaz commented Dec 3, 2022

Thanks! Feel free to send a PR with tests for these cases.

@dereckt
Copy link
Contributor

dereckt commented Dec 5, 2022

Hi, I'm looking into this issue. Almost got a PR ready.

@dereckt
Copy link
Contributor

dereckt commented Dec 5, 2022

Hi I've created a draft for the PR, though I'm unsure as how to handle the case with the invalid port 0. How should port 0 be handled?

@asvetlov
Copy link
Member

Fixed by #793

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants