Skip to content

Commit

Permalink
[BUGFIX] Update validator to allow hostname only entries #386
Browse files Browse the repository at this point in the history
One more attempt to fix the edge cases for validating hostnames. We want to allow bare hostnames and fqdn, but not allow any other special characters like an accidental port or path part

References #383 #380
  • Loading branch information
kfdm authored Feb 4, 2022
2 parents f4ec674 + ad53bae commit f962eaf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
38 changes: 26 additions & 12 deletions promgen/tests/test_host_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

from django.urls import reverse

from promgen import forms, models
from promgen import models, validators
from promgen.tests import PromgenTest


class HostTests(PromgenTest):
def setUp(self):
self.force_login(username="demo")

# For our first two tests, we just want to make sure that both newline
# separated and comma separated work, but are not necessarily testing
# valid/invalid hostnames
def test_newline(self):
self.client.post(
reverse("hosts-add", args=[1]),
Expand All @@ -28,14 +31,25 @@ def test_comma(self):
)
self.assertCount(models.Host, 3, "Expected 3 hosts")

def test_invalid(self):
form = forms.HostForm(
{
"hosts": """
foo/bar/baz
not-a-valid:host
"""
}
)
self.assertFalse(form.is_valid(), "Form uses invalid hosts")
self.assertEquals(form.errors, {"__all__": ["Invalid hostname foo/bar/baz"]})
# Within our new host code, the hosts are split (by newline or comma) and then
# individually tested. Here we will test our validator on specific entries that
# should pass or fail
def test_validators(self):
# Hostname only should be valid
validators.hostname("bare-hostname")
# FQDN should be valid
validators.hostname("fqdn.example.com")
# UPPERCASE and trailing dot should also be fine
validators.hostname("FQDN.with.trailing.dot.example.com.")
# Hostname cannot contain underscore
with self.assertRaises(validators.ValidationError):
validators.hostname("underscore_in_hostname")
# Hostname should not include port
with self.assertRaises(validators.ValidationError):
validators.hostname("invalid:host")
# Hostname should not be a url (no scheme)
with self.assertRaises(validators.ValidationError):
validators.hostname("http://example.com")
# Hostnames should not contain a path component
with self.assertRaises(validators.ValidationError):
validators.hostname("example.com/path")
8 changes: 7 additions & 1 deletion promgen/validators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright (c) 2017 LINE Corporation
# These sources are released under the terms of the MIT license: see LICENSE

import re

from dateutil import parser

from django.core.exceptions import ValidationError
Expand Down Expand Up @@ -36,10 +38,14 @@
+ URLValidator.ipv6_re
+ "|"
+ URLValidator.host_re
+ "|"
+ URLValidator.hostname_re
+ ")$",
message="Invalid hostname %(value)s",
message="Invalid hostname: %(value)s",
flags=re.IGNORECASE,
)


def datetime(value):
try:
parser.parse(value)
Expand Down

0 comments on commit f962eaf

Please sign in to comment.