Skip to content

Url type port limit not checked #1654

@flapili

Description

@flapili
pydantic version: 1.5.1
pydantic compiled: False
install path: /opt/fastapi/backend/venv/lib/python3.8/site-packages/pydantic
python version: 3.8.3 (default, Jun 21 2020, 00:03:19)  [GCC 8.3.0]
platform: Linux-4.19.0-9-amd64-x86_64-with-glibc2.28
optional deps. installed: ['email-validator']

Hi,

The port limit is not checked by URLs types:

>>> from pydantic import BaseModel, stricturl
>>> class Test(BaseModel):
	url: stricturl(allowed_schemes=["tcp"])

	
>>> Test(url="tcp://127.0.0.1:100000000000000000000000000000")
Test(url=UrlValue('tcp://127.0.0.1:100000000000000000000000000000', scheme='tcp', host='127.0.0.1', host_type='ipv4', port='100000000000000000000000000000'))

Excepted result: raise an error,

my current workaround

>>> from pydantic import BaseModel, stricturl, validator
>>> from pydantic.networks import url_regex
>>> class Test(BaseModel):
	url: stricturl(allowed_schemes=["tcp"])
	@validator("url")
	def validate_port(cls, v):
		port = url_regex().match(v).group("port")
		if port is None:
		    return v

		if int(port) > 2**16:
			raise ValueError("port overflow")
		return v

	
>>> Test(url="tcp://127.0.0.1:900000")
Traceback (most recent call last):
  File "<pyshell#84>", line 1, in <module>
    Test(url="tcp://127.0.0.1:900000")
  File "pydantic\main.py", line 338, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for Test
url
  port overflow (type=value_error)

Metadata

Metadata

Assignees

No one assigned

    Labels

    changeSuggested alteration to Pydantic, not a new feature nor a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions