Skip to content
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
5 changes: 4 additions & 1 deletion api/app/controllers/api/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ def post(self):
# rtype no need to be validated & no need to check its length
# `get_typeid` will raise error for non existing rtype
validator.validate(rtype, rdata)
validator.validate("owner", owner)
if rtype == "srv":
validator.validate("owner-srv", owner)
else:
validator.validate("owner", owner)
except Exception as e:
return response(422, message=f"{e}")

Expand Down
28 changes: 28 additions & 0 deletions api/app/helpers/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,33 @@ def check_hypen(label):
raise ValueError("Bad OWNER")


def is_valid_srv_owner(owner):
"""Check if it's a valid srv owner.

Rules:
- must contain service and protocol name
- can't contain the zone name
- enfore the service name to be prefixed with _
- enfore the protocol to be prefixed with _
"""

if "." not in owner:
raise ValueError("Bad OWNER")

labels = owner.split(".")
if len(labels) < 2:
raise ValueError("Bad OWNER")

service_name = labels[0]
protocol_type = labels[1]
if not service_name.startswith("_"):
raise ValueError("Service name must start with underscore")
if not protocol_type.startswith("_"):
raise ValueError("Protocol type must start with underscore")

is_valid_owner(owner)


functions = {
"A": is_valid_ip,
"AAAA": is_valid_ip,
Expand All @@ -171,6 +198,7 @@ def check_hypen(label):
"TXT": is_valid_txt,
"SRV": is_valid_srv,
"OWNER": is_valid_owner,
"OWNER-SRV": is_valid_srv_owner,
}


Expand Down
14 changes: 14 additions & 0 deletions api/tests/unit/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ def test_valid_owner():
validator.is_valid_owner("a" * 256)


def test_valid_srv_owner():
validator.is_valid_srv_owner("_xmpp._tcp")

with pytest.raises(Exception):
# owner ends with dot
validator.is_valid_srv_owner("_xmpp._tcp.example.com.")
with pytest.raises(Exception):
# service name doesn't prefixed with _
validator.is_valid_srv_owner("xmpp._tcp")
with pytest.raises(Exception):
# protocol type doesn't prefixed with _
validator.is_valid_srv_owner("_xmpp.tcp")


def test_validate_func():
# validator exists
validator.validate("A", "192.0.2.1")
Expand Down