Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

urlparse does not correctly handle signs, underscores, and whitespace in port numbers #96035

Closed
Assignees
JelleZijlstra
Labels
3.10only security fixes3.11only security fixes3.12only security fixesstdlibStandard Library Python modules in the Lib/ directorytriagedThe issue has been accepted as valid by a triager.type-bugAn unexpected behavior, bug, or error
@kenballus

Description

@kenballus

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 moreASCII digits representing a decimal number no greater than$2^{16} − 1$.

"""1

The bug

This is the port string parsing code fromLib/urllib/parse.py:166-176:

defport(self):port=self._hostinfo[1]ifportisnotNone:try:port=int(port,10)exceptValueError:message=f'Port could not be cast to integer value as{port!r}'raiseValueError(message)fromNoneifnot (0<=port<=65535):raiseValueError("Port out of range 0-65535")returnport

This will erroneously validate strings"-0" andf"+{x}" for any value ofx in the valid range. Given that+ and- are not digits, this behavior is in violation of both specifications.

This bug is easily reproducible with the following snippet:

fromurllib.parseimporturlparseurl1=urlparse("http://python.org:-0")url2=urlparse("http://python.org:+80")print(url1.port)# prints 0, but error is expectedprint(url2.port)# prints 80, but error is expected

Happy to submit a PR, but don't want to step on any toes over at#25774.

My environment

  • CPython version tested on:
    • 3.10.6
  • Operating system and architecture:
    • Arch Linux x86_64

Footnotes

  1. Given that this isurlparse and noturiparse, it seems appropriate that we do not accept port numbers outsiderange(2**16), even though such numbers are allowed by RFC 3986.

Metadata

Metadata

Assignees

Labels

3.10only security fixes3.11only security fixes3.12only security fixesstdlibStandard Library Python modules in the Lib/ directorytriagedThe issue has been accepted as valid by a triager.type-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp