From 8bb9586a9bfd5f17eac44fb09b63761bcd1f95f2 Mon Sep 17 00:00:00 2001 From: SebastiaanYN Date: Thu, 23 Jul 2026 16:01:54 +0200 Subject: [PATCH] Reject empty TXT records in `Txt::parse` RFC 1035 section 3.3.14 requires "one or more" character strings, so rdlength=0 is invalid. `check_slice` (used by `from_octets` / `from_slice`) already enforced this; `parse` did not, allowing construction of a `Txt` that panics in `as_flat_slice`. Replace the manual `CharStr::skip` loop with a single `check_slice` call so all construction paths share the same validation. The zero-length unknown-rdata test now uses TYPE65534 instead of TXT since zero-length TXT is correctly invalid. --- src/rdata/rfc1035/txt.rs | 13 ++++++++----- test-data/zonefiles/unknown-zero-length.yaml | 4 ++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/rdata/rfc1035/txt.rs b/src/rdata/rfc1035/txt.rs index 1a7e3e9c8..00b34740c 100644 --- a/src/rdata/rfc1035/txt.rs +++ b/src/rdata/rfc1035/txt.rs @@ -185,12 +185,9 @@ impl Txt { Octs: AsRef<[u8]>, { let len = parser.remaining(); - LongRecordData::check_len(len)?; let text = parser.parse_octets(len)?; - let mut tmp = Parser::from_ref(text.as_ref()); - while tmp.remaining() != 0 { - CharStr::skip(&mut tmp)? - } + Txt::check_slice(text.as_ref()) + .map_err(|e| ParseError::Form(e.into()))?; Ok(Txt(text)) } @@ -1125,6 +1122,12 @@ mod test { ); } + #[test] + fn txt_empty_rejected() { + let mut parser = Parser::from_static(b""); + assert!(Txt::<&[u8]>::parse(&mut parser).is_err()); + } + #[test] fn txt_display() { fn cmp(input: &[u8], output: &str) { diff --git a/test-data/zonefiles/unknown-zero-length.yaml b/test-data/zonefiles/unknown-zero-length.yaml index c98725233..980e820b3 100644 --- a/test-data/zonefiles/unknown-zero-length.yaml +++ b/test-data/zonefiles/unknown-zero-length.yaml @@ -1,10 +1,10 @@ origin: example.com. zonefile: | - example.com. 3600 IN TXT \# 0 + example.com. 3600 IN TYPE65534 \# 0 result: - owner: example.com. class: IN ttl: 3600 data: !Unknown - rtype: Txt + rtype: TYPE65534 data: