Skip to content

bugfix: domain name validation. #951

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pytz = "*"
requests = "^2"
xmlschema = "^2"
"zope.interface" = {optional = true, version = "*"}
validators = "^0.34.0"

[tool.poetry.extras]
s2repoze = ["paste", "repoze-who", "zope-interface"]
Expand Down
7 changes: 5 additions & 2 deletions src/saml2/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import time
from urllib.parse import urlparse

import validators

from saml2 import time_util


Expand Down Expand Up @@ -420,6 +422,7 @@ def valid_instance(instance):


def valid_domain_name(dns_name):
m = re.match(r"^[a-z0-9]+([-.]{ 1 }[a-z0-9]+).[a-z]{2,5}(:[0-9]{1,5})?(\/.)?$", dns_name, re.I)
if not m:
value = validators.domain(dns_name)
if not value:
raise ValueError("Not a proper domain name")
return value
51 changes: 51 additions & 0 deletions tests/test_13_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from saml2.validate import valid_any_uri
from saml2.validate import valid_anytype
from saml2.validate import valid_duration
from saml2.validate import valid_domain_name
from saml2.validate import valid_instance
from saml2.validate import valid_non_negative_integer
from saml2.validate import valid_string
Expand Down Expand Up @@ -146,3 +147,53 @@ def test_valid_address():
assert valid_address("[2001:8003:5555:9999:555a:5555:c77:d5c5")
with raises(NotValid):
assert valid_address("[[2001:8003:5555:9999:555a:5555:c77:d5c5]")


def test_valid_domain_name():
assert valid_domain_name("api.my-domain.com")
assert valid_domain_name("auth.admin.domain.com")
assert valid_domain_name("auth.domain.com")
assert valid_domain_name("auth.domain.com")
assert valid_domain_name("lk.domain.com")
assert valid_domain_name("domain.com")
assert valid_domain_name("domain.lu")
assert valid_domain_name("auth-domain.com")
assert valid_domain_name("auth-admin.domain-uero.xyz")
assert valid_domain_name("auth.lk.d.sr.mydomain.com")
assert valid_domain_name("123example.com")

with raises(ValueError):
valid_domain_name("")

with raises(ValueError):
valid_domain_name("123.123.123.123")

with raises(ValueError):
valid_domain_name("123.123.123.123:80")

with raises(ValueError):
valid_domain_name("123.123.123.123:8000")

with raises(ValueError):
valid_domain_name("auth_domain.com")

with raises(ValueError):
valid_domain_name("example-.com")

with raises(ValueError):
valid_domain_name("[email protected]")

with raises(ValueError):
valid_domain_name("exaple.c")

with raises(ValueError):
valid_domain_name("example.com:")

with raises(ValueError):
valid_domain_name("example..com")

with raises(ValueError):
valid_domain_name("example.com123")

with raises(ValueError):
valid_domain_name("example.com.")