Browse Source

Validations to ensure Record.name and Zone.name have no whitespace

pull/1004/head
Ross McFarland 3 years ago
parent
commit
cfa7abaee5
No known key found for this signature in database GPG Key ID: 943B179E15D3B22A
6 changed files with 39 additions and 1 deletions
  1. +4
    -0
      CHANGELOG.md
  2. +4
    -0
      octodns/record/base.py
  3. +1
    -1
      octodns/record/exception.py
  4. +3
    -0
      octodns/zone.py
  5. +22
    -0
      tests/test_octodns_record.py
  6. +5
    -0
      tests/test_octodns_zone.py

+ 4
- 0
CHANGELOG.md View File

@ -1,3 +1,7 @@
## v1.0.0.rc2 - 2023-??-?? -
* Record and Zone validation now ensures there's no whitespace in names
## v1.0.0.rc0 - 2023-05-16 - First of the ones
#### Noteworthy changes


+ 4
- 0
octodns/record/base.py View File

@ -41,6 +41,10 @@ class Record(EqualityTupleMixin):
# convert the error into a reason
reasons.append(str(e))
name = str(name)
if ' ' in name or '\t' in name:
reasons.append('invalid record, whitespace is not allowed')
fqdn = f'{name}.{zone.name}' if name else zone.name
try:
_type = data['type']


+ 1
- 1
octodns/record/exception.py View File

@ -13,7 +13,7 @@ class ValidationError(RecordException):
@classmethod
def build_message(cls, fqdn, reasons):
reasons = '\n - '.join(reasons)
return f'Invalid record {idna_decode(fqdn)}\n - {reasons}'
return f'Invalid record "{idna_decode(fqdn)}"\n - {reasons}'
def __init__(self, fqdn, reasons):
super().__init__(self.build_message(fqdn, reasons))


+ 3
- 0
octodns/zone.py View File

@ -28,6 +28,9 @@ class Zone(object):
def __init__(self, name, sub_zones):
if not name[-1] == '.':
raise Exception(f'Invalid zone name {name}, missing ending dot')
elif ' ' in name or '\t' in name:
raise Exception(f'Invalid zone name {name}, whitespace not allowed')
# internally everything is idna
self.name = idna_encode(str(name)) if name else name
# we'll keep a decoded version around for logs and errors


+ 22
- 0
tests/test_octodns_record.py View File

@ -398,6 +398,28 @@ class TestRecordValidation(TestCase):
zone = Zone('unit.tests.', [])
def test_base(self):
# no spaces
for name in (
' ',
' leading',
'trailing ',
'in the middle',
'\t',
'\tleading',
'trailing\t',
'in\tthe\tmiddle',
):
with self.assertRaises(ValidationError) as ctx:
Record.new(
self.zone,
name,
{'ttl': 300, 'type': 'A', 'value': '1.2.3.4'},
)
reason = ctx.exception.reasons[0]
self.assertEqual(
'invalid record, whitespace is not allowed', reason
)
# name = '@'
with self.assertRaises(ValidationError) as ctx:
name = '@'


+ 5
- 0
tests/test_octodns_zone.py View File

@ -186,6 +186,11 @@ class TestZone(TestCase):
Zone('not.allowed', [])
self.assertTrue('missing ending dot' in str(ctx.exception))
def test_whitespace(self):
with self.assertRaises(Exception) as ctx:
Zone('space not allowed.', [])
self.assertTrue('whitespace not allowed' in str(ctx.exception))
def test_sub_zones(self):
# NS for exactly the sub is allowed


Loading…
Cancel
Save