Skip to content

Commit

Permalink
[chores] Added custom validation for hostname
Browse files Browse the repository at this point in the history
The hostname validation has been adapted from jsonschema~=3.2.0
(jsonschema._format.is_host_name). The newer versions of
jsonschema enforces FQDN validation which is not always
required in OpenWISP. E.g. setting up hostname of a device.
  • Loading branch information
pandafy committed Mar 10, 2022
1 parent 7e3ea2b commit 98d2ea4
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions netjsonconfig/backends/base/backend.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import gzip
import ipaddress
import json
import re
import tarfile
from collections import OrderedDict
from copy import deepcopy
Expand All @@ -13,6 +14,8 @@
from ...schema import DEFAULT_FILE_MODE
from ...utils import evaluate_vars, merge_config

_host_name_re = re.compile(r"^[A-Za-z0-9][A-Za-z0-9\.\-]{1,255}$")


class BaseBackend(object):
"""
Expand Down Expand Up @@ -131,6 +134,24 @@ def _cidr_notation(value):
assert False, str(e)
return True

@draft4_format_checker.checks('hostname', JsonSchemaError)
def _is_hostname(value):
"""
The hostname validation has been taken from jsonschema~=3.2.0
(jsonschema._format.is_host_name). The newer versions of
jsonschema enforces FQDN validation which is not always
required in OpenWISP. E.g. setting up hostname of a device.
"""
if not isinstance(value, str):
return True
if not _host_name_re.match(value):
return False
components = value.split(".")
for component in components:
if len(component) > 63:
return False
return True

def validate(self):
try:
Draft4Validator(self.schema, format_checker=draft4_format_checker).validate(
Expand Down

0 comments on commit 98d2ea4

Please sign in to comment.