Title: urlparse does not correctly handle signs, underscores, and whitespace in port numbers · Issue #96035 · python/cpython · GitHub
Open Graph Title: urlparse does not correctly handle signs, underscores, and whitespace in port numbers · Issue #96035 · python/cpython
X Title: urlparse does not correctly handle signs, underscores, and whitespace in port numbers · Issue #96035 · python/cpython
Description: Background RFC 3986 (spec for URIs) defines a valid port string with the following grammar rule: port = *DIGIT Here's the WHATWG URL spec definition: """ A URL-port string must be one of the following: the empty string one or more ASCII ...
Open Graph Description: Background RFC 3986 (spec for URIs) defines a valid port string with the following grammar rule: port = *DIGIT Here's the WHATWG URL spec definition: """ A URL-port string must be one of the follow...
X Description: Background RFC 3986 (spec for URIs) defines a valid port string with the following grammar rule: port = *DIGIT Here's the WHATWG URL spec definition: """ A URL-port string must b...
Opengraph URL: https://github.com/python/cpython/issues/96035
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"urlparse does not correctly handle signs, underscores, and whitespace in port numbers","articleBody":"# Background\r\nRFC 3986 (spec for URIs) defines a valid port string with the following grammar rule:\r\n- `port = *DIGIT`\r\n\r\nHere's the WHATWG URL spec definition:\r\n\"\"\"\r\nA URL-port string must be one of the following:\r\n- the empty string\r\n- one or more [ASCII digits](https://infra.spec.whatwg.org/#ascii-digit) representing a decimal number no greater than $2^{16} − 1$.\r\n\r\n\"\"\"[^1]\r\n[^1]:Given that this is `urlparse` and not `uriparse`, it seems appropriate that we do not accept port numbers outside `range(2**16)`, even though such numbers are allowed by RFC 3986.\r\n\r\n# The bug\r\nThis is the port string parsing code from `Lib/urllib/parse.py:166-176`:\r\n```python\r\ndef port(self):\r\n port = self._hostinfo[1]\r\n if port is not None:\r\n try:\r\n port = int(port, 10)\r\n except ValueError:\r\n message = f'Port could not be cast to integer value as {port!r}'\r\n raise ValueError(message) from None\r\n if not ( 0 \u003c= port \u003c= 65535):\r\n raise ValueError(\"Port out of range 0-65535\")\r\n return port\r\n```\r\nThis will erroneously validate strings `\"-0\"` and `f\"+{x}\"` for any value of `x` in the valid range. Given that `+` and `-` are not digits, this behavior is in violation of both specifications.\r\n\r\nThis bug is easily reproducible with the following snippet:\r\n```python\r\nfrom urllib.parse import urlparse\r\nurl1 = urlparse(\"http://python.org:-0\")\r\nurl2 = urlparse(\"http://python.org:+80\")\r\nprint(url1.port) # prints 0, but error is expected\r\nprint(url2.port) # prints 80, but error is expected\r\n```\r\nHappy to submit a PR, but don't want to step on any toes over at #25774.\r\n\r\n# My environment\r\n- CPython version tested on:\r\n - 3.10.6\r\n- Operating system and architecture:\r\n - Arch Linux x86_64","author":{"url":"https://github.com/kenballus","@type":"Person","name":"kenballus"},"datePublished":"2022-08-16T20:45:48Z","url":"https://github.com/python/cpython/issues/96035"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:7fe1a58f-2063-7005-72dd-e5a2596091e2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B798:23DB5F:118FCEC:1609774:6B02459F |
| html-safe-nonce | 94d0dceb3b55b5bb46c85689f9001e6b7f15dc5b8f929b6a237096dc8858c8d0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNzk4OjIzREI1RjoxMThGQ0VDOjE2MDk3NzQ6NkIwMjQ1OUYiLCJ2aXNpdG9yX2lkIjoiMjQwNzQ1NjkxMjEyMDYyODYzOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 9ee63eb359e0dca4ba1234216b4e27d3ed683613927858fc0ab491471a2965a3 |
| hovercard-subject-tag | issue:1340863065 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/96035/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b416d79f62773d67bc07c632c4741a0d6a2b0ee14d71d5c30bb17d0d10099a29/python/cpython/issues/96035 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b416d79f62773d67bc07c632c4741a0d6a2b0ee14d71d5c30bb17d0d10099a29/python/cpython/issues/96035 |
| og:image:alt | Background RFC 3986 (spec for URIs) defines a valid port string with the following grammar rule: port = *DIGIT Here's the WHATWG URL spec definition: """ A URL-port string must be one of the follow... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kenballus |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width