Skip to content

Commit deae3c2

Browse files
committed
fix: escape "strings" slot in records
1 parent c05660e commit deae3c2

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

tests/unit/core/test_zf.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,45 @@ def test_zf_delete_a_record(app_with_single_zone, zfzone_common_data):
396396
assert True
397397

398398

399+
def test_zf_delete_txt_record(app_with_single_zone, zfzone_common_data):
400+
zone_name = str(zfzone_common_data.origin)
401+
record_name = "@"
402+
record_type = "TXT"
403+
record_ttl = "86400"
404+
record_data = {"strings": "This domain name is reserved for use in documentation"}
405+
record_index = 0
406+
407+
with app_with_single_zone.app_context():
408+
existing_records = get_records(
409+
zone_name=zone_name, record_name=record_name, record_type=record_type
410+
)
411+
assert len(existing_records) == 1
412+
# pylint: disable=protected-access
413+
assert record_data["strings"] == dns.rdata._escapify(
414+
existing_records[0][0].strings[0]
415+
)
416+
# pylint: enable=protected-access
417+
418+
with app_with_single_zone.app_context():
419+
_ = delete_record(
420+
record_name=record_name,
421+
record_type=record_type,
422+
record_data=record_data,
423+
record_ttl=record_ttl,
424+
record_index=record_index,
425+
zone_name=zone_name,
426+
)
427+
428+
with app_with_single_zone.app_context():
429+
try:
430+
_ = get_records(
431+
zone_name=zone_name, record_name=record_name, record_type=record_type
432+
)
433+
assert False
434+
except NotFound:
435+
assert True
436+
437+
399438
def test_zf_delete_single_record_under_rrset(app_with_single_zone, zfzone_common_data):
400439
zone_name = str(zfzone_common_data.origin)
401440
record_name = "@"

zoneforge/core/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,8 @@ def record_to_response(records: list[dns.rrset.RRset]) -> dict:
382382
for slot in record_slots:
383383
property_value = getattr(rdata, slot)
384384
# perform any necessary transformations
385-
if (
386-
property_value is not None
387-
): # needs to be explicitly checked for None since dns.name.Name for a root record is evaluated to False (len=0)
385+
# needs to be explicitly checked for None since dns.name.Name for a root record is evaluated to False (len=0)
386+
if property_value is not None:
388387
if slot == "strings":
389388
txt = ""
390389
prefix = ""
@@ -435,7 +434,10 @@ def request_to_rdata(
435434
# construct rdata class values from string in the proper order. We could pass these to a constructor as kwargs, but we don't currently have client side rdata slot typing
436435
rdata_str = ""
437436
for rdata_slot in get_rdata_class_slots(record_type):
438-
rdata_str += f"{record_data[rdata_slot]} "
437+
slot_data = record_data[rdata_slot]
438+
if rdata_slot == "strings":
439+
slot_data = f'"{slot_data}"'
440+
rdata_str += f"{slot_data} "
439441
rdata = dns.rdata.from_text(rdclass=record_class, rdtype=record_type, tok=rdata_str)
440442

441443
if record_comment:

0 commit comments

Comments
 (0)