fix(dns): use presentation values for TXT records

Use the same optional value field for every DNS record type. Remove the
TXT-specific segments message and update configuration, management, CLI,
frontend types and examples to use the shared RDATA contract.

Decode quoted DNS decimal escapes as octets and require valid UTF-8 in
the existing tokenizer, avoiding Hickory 0.25.2's Unicode digit
shifting. Preserve ordered and empty TXT strings, and validate their
255-byte limit after decoding.

Keep value at protobuf field 4 and reserve the removed txt field. Extend
existing parsing, wire roundtrip and configuration tests.
This commit is contained in:
KKRainbow
2026-11-10 12:55:50 +08:00
parent 361c323f20
commit 3ccdaae9ab
14 changed files with 140 additions and 158 deletions
+10 -10
View File
@@ -16,7 +16,7 @@ records = [
{ name = "nas", type = "A", value = "10.0.0.8" },
{ name = "nas", type = "AAAA", value = "fd00::8" },
{ name = "files", type = "CNAME", value = "nas" },
{ name = "info", type = "TXT", txt = { segments = ["local records"] } },
{ name = "info", type = "TXT", value = '"local records"' },
]
[[dns.zone]]
@@ -71,28 +71,28 @@ change its meaning.
## TXT records
TXT uses `txt.segments` instead of `value`:
TXT uses the same `value` field as other record types:
```toml
[[dns.zone.records]]
name = "info"
type = "TXT"
txt = { segments = ['quote" slash\ ;', "", "tail"] }
value = '"quote\" slash\\ ;" "" "tail"'
```
Each array element becomes one UTF-8 DNS character-string of at most 255 bytes.
There must be at least one segment; an empty segment is allowed. Segments are
Each quoted string becomes one UTF-8 DNS character-string of at most 255 bytes.
There must be at least one string; an empty string is allowed. Strings are
ordered and are never split or combined automatically. To configure two TXT
records, add two record objects.
Segments use only TOML or JSON escaping, with no second layer of DNS text
escaping. `value` and `txt` are mutually exclusive. TXT must use `txt`, and other
types must use `value`.
The example uses a TOML literal string to preserve DNS quotes and escapes.
DNS presentation escaping still applies inside `value`, as it does for HINFO
and NAPTR. JSON and TOML basic strings also apply their own escaping rules.
## Configuration and management
The file and management API share `core_config.DnsConfigRaw`, including the
record enum and the `value | txt` oneof. Management JSON uses the same field names
record enum and the `value` field. Management JSON uses the same field names
and transparent collection arrays:
```json
@@ -103,7 +103,7 @@ and transparent collection arrays:
"ttl": 60,
"records": [
{"name": "nas", "type": "A", "value": "192.0.2.1"},
{"name": "info", "type": "TXT", "txt": {"segments": ["first", "second"]}}
{"name": "info", "type": "TXT", "value": "\"first\" \"second\""}
]
}]
}
+2 -2
View File
@@ -754,7 +754,7 @@ origin = 'example.test.'
ttl = 60
records = [
{ name = '@', type = 'MX', value = '10 mail' },
{ name = 'info', type = 'TXT', ttl = 0, txt = { segments = ['quote" slash\ ;', '', 'tail'] } },
{ name = 'info', type = 'TXT', ttl = 0, value = '"quote\" slash\\ ;" "" "tail"' },
]
"#,
] {
@@ -825,7 +825,7 @@ records = [
"[[dns.zone]]\norigin = 'example.test.'\nttl = -1\n",
"[[dns.zone]]\norigin = 'example.test.'\nrecords = [{ name = '@', type = 'A', value = '192.0.2.1', txt = { segments = ['x'] } }]\n",
"[[dns.zone]]\norigin = 'example.test.'\nrecords = [{ name = '@', type = 'UNKNOWN', value = 'x' }]\n",
"[[dns.zone]]\norigin = 'example.test.'\nrecords = [{ name = '@', type = 'TXT', value = 'x' }]\n",
"[[dns.zone]]\norigin = 'example.test.'\nrecords = [{ name = '@', type = 'TXT', value = '' }]\n",
"[[dns.zone]]\norigin = 'example.test.'\nrecords = ['@ IN A 192.0.2.1']\n",
] {
assert!(
+93 -89
View File
@@ -3,13 +3,11 @@
use std::collections::{BTreeMap, BTreeSet};
use anyhow::{Context, bail, ensure};
use easytier_proto::core_config::{
DnsConfigRaw, DnsRecordRaw, DnsRecordType, DnsZoneConfigRaw, dns_record_raw::Data,
};
use easytier_proto::core_config::{DnsConfigRaw, DnsRecordRaw, DnsRecordType, DnsZoneConfigRaw};
use hickory_proto::{
rr::{
LowerName, Name, RData, Record, RecordSet, RecordType, RrKey,
rdata::{HTTPS, SVCB, TXT},
rdata::{HTTPS, SVCB},
},
serialize::{
binary::{BinEncodable, BinEncoder, EncodeMode},
@@ -107,21 +105,16 @@ pub fn compile_zone(input: &DnsZoneConfigRaw) -> anyhow::Result<DnsZone> {
.entry(key)
.or_insert_with(|| (record.ttl(), BTreeMap::new()));
ensure!(*ttl == record.ttl(), "{path}.ttl: RRset TTLs must match");
let data_field = if record_type == RecordType::TXT {
"txt"
} else {
"value"
};
let mut bytes = Vec::new();
let mut encoder = BinEncoder::with_mode(&mut bytes, EncodeMode::Signing);
encoder.set_canonical_names(true);
record
.data()
.emit(&mut encoder)
.with_context(|| format!("{path}.{data_field}"))?;
.with_context(|| format!("{path}.value"))?;
ensure!(
!values.contains_key(&bytes),
"{path}.{data_field}: duplicate RDATA"
"{path}.value: duplicate RDATA"
);
ensure!(
values.is_empty()
@@ -129,7 +122,7 @@ pub fn compile_zone(input: &DnsZoneConfigRaw) -> anyhow::Result<DnsZone> {
record_type,
RecordType::CNAME | RecordType::ANAME | RecordType::SOA
),
"{path}.{data_field}: {record_type} permits only one value"
"{path}.value: {record_type} permits only one value"
);
values.insert(bytes, record);
}
@@ -169,36 +162,24 @@ fn compile_record(input: &DnsRecordRaw, origin: &Name, ttl: u32) -> anyhow::Resu
"type: record type is required"
);
let record_type: RecordType = kind.as_str_name().parse().context("type")?;
let rdata = match (&input.data, record_type) {
(Some(Data::Txt(txt)), RecordType::TXT) => {
let value = input.value.as_deref().context("value: RDATA is required")?;
let rdata = parse_value(record_type, value, origin).context("value")?;
if let RData::TXT(txt) = &rdata {
ensure!(
!txt.txt_data().is_empty(),
"value: at least one TXT character-string is required"
);
for (index, segment) in txt.txt_data().iter().enumerate() {
ensure!(
!txt.segments.is_empty(),
"txt.segments: at least one segment is required"
segment.len() <= 255,
"value: TXT character-string {index} exceeds 255 UTF-8 bytes"
);
for (index, segment) in txt.segments.iter().enumerate() {
ensure!(
segment.len() <= 255,
"txt.segments[{index}]: exceeds 255 UTF-8 bytes"
);
}
RData::TXT(TXT::new(txt.segments.clone()))
}
(Some(Data::Value(value)), kind) if kind != RecordType::TXT => {
parse_value(kind, value, origin).context("value")?
}
(Some(Data::Txt(_)), _) => bail!("txt: only TXT records accept txt.segments"),
(Some(Data::Value(_)), _) => bail!("value: TXT records require txt.segments"),
(None, RecordType::TXT) => bail!("txt: at least one segment is required"),
(None, _) => bail!("value: RDATA is required"),
};
}
let record = Record::from_rdata(name, ttl, rdata);
record.to_bytes().with_context(|| {
if record_type == RecordType::TXT {
"txt.segments: invalid DNS wire encoding"
} else {
"value: invalid DNS wire encoding"
}
})?;
record
.to_bytes()
.context("value: invalid DNS wire encoding")?;
Ok(record)
}
@@ -307,8 +288,8 @@ fn validate_alpn_member(value: &str) -> anyhow::Result<()> {
}
/// Tokenize one RDATA, retaining the iterator for strict consumption checks.
/// Hickory's lexer is private; its TXT parser decodes individual quoted strings
/// using the same escaping rules as the previous zone-file input.
/// Decode quoted strings here because Hickory 0.25.2 treats decimal escapes as
/// shifted Unicode digits rather than DNS octets.
fn rdata_tokens(value: &str) -> anyhow::Result<Vec<String>> {
let mut chars = value.char_indices().peekable();
let mut tokens = Vec::new();
@@ -327,41 +308,38 @@ fn rdata_tokens(value: &str) -> anyhow::Result<Vec<String>> {
ch if ch.is_whitespace() => {}
';' | '$' => bail!("zone directives and comments are not supported in RDATA"),
'"' => {
let mut end = None;
let mut closed = false;
let mut token = Vec::new();
let mut steps = 0;
while let Some((index, ch)) = chars.next() {
if ch == '\\' {
let (_, escaped) = chars.next().context("incomplete RDATA escape")?;
if escaped.is_numeric() {
ensure!(
chars.next().is_some() && chars.next().is_some(),
"incomplete numeric RDATA escape"
);
}
steps += 1;
continue;
}
while let Some((_, ch)) = chars.next() {
if ch == '"' {
end = Some(index + 1);
closed = true;
break;
}
steps += 1;
ensure!(steps <= 4093, "RDATA token exceeds parser limit");
if ch == '\\' {
let (_, escaped) = chars.next().context("incomplete RDATA escape")?;
ensure!(!escaped.is_control(), "control character in RDATA escape");
if escaped.is_ascii_digit() {
let mut octet = escaped.to_digit(10).unwrap();
for _ in 0..2 {
let (_, digit) =
chars.next().context("incomplete numeric RDATA escape")?;
ensure!(digit.is_ascii_digit(), "invalid numeric RDATA escape");
octet = octet * 10 + digit.to_digit(10).unwrap();
}
ensure!(octet <= 255, "numeric RDATA escape exceeds 255");
token.push(octet as u8);
} else {
token.extend_from_slice(escaped.encode_utf8(&mut [0; 4]).as_bytes());
}
} else {
token.extend_from_slice(ch.encode_utf8(&mut [0; 4]).as_bytes());
}
}
let end = end.context("unclosed RDATA quoted string")?;
let token = &value[start..end];
// The dependency's lexer asserts after 4096 steps per token.
// Values that exceed that bound must fail rather than panic.
ensure!(steps <= 4093, "RDATA token exceeds parser limit");
// A leading blank enters the same lexer state as RDATA after
// a zone-file header, including its maximum token length.
let RData::TXT(decoded) =
RData::try_from_str(RecordType::TXT, &format!(" {token}"))?
else {
unreachable!("the TXT parser returns TXT");
};
for segment in decoded.txt_data() {
tokens.push(String::from_utf8(segment.to_vec())?);
}
ensure!(closed, "unclosed RDATA quoted string");
tokens.push(String::from_utf8(token)?);
}
ch => {
ensure!(!ch.is_control(), "control character in RDATA");
@@ -390,7 +368,6 @@ fn rdata_tokens(value: &str) -> anyhow::Result<Vec<String>> {
#[cfg(test)]
mod tests {
use super::*;
use easytier_proto::core_config::DnsTxtData;
use hickory_proto::serialize::binary::BinDecodable;
fn record(kind: DnsRecordType, value: &str) -> DnsRecordRaw {
@@ -398,7 +375,7 @@ mod tests {
name: "service".into(),
r#type: kind as i32,
ttl: None,
data: Some(Data::Value(value.into())),
value: Some(value.into()),
}
}
@@ -436,6 +413,7 @@ mod tests {
(Sshfp, "1 1 ABCD"),
(Svcb, "1 target alpn=h2 port=443"),
(Tlsa, "3 1 1 ABCD"),
(Txt, "\"hello world\" \"tail\""),
] {
let mut input = record(kind, value);
if kind == Soa {
@@ -470,12 +448,19 @@ mod tests {
}
#[test]
fn txt_segments_are_literal_ordered_utf8_strings() {
let segments = vec!["quote\" slash\\ ;\n".into(), String::new(), "x".repeat(255)];
let mut input = record(DnsRecordType::Txt, "");
input.data = Some(Data::Txt(DnsTxtData {
segments: segments.clone(),
}));
fn txt_presentation_preserves_ordered_utf8_strings() {
let segments = [
"quote\" slash\\ ;\n".to_owned(),
String::new(),
"x".repeat(255),
"é".repeat(127),
"é".to_owned(),
];
let value = format!(
r#""quote\" slash\\ ;\010" "" "{}" "{}" "\195\169""#,
segments[2], segments[3]
);
let mut input = record(DnsRecordType::Txt, &value);
let parsed = compile_zone(&zone(vec![input.clone()])).unwrap();
let record = parsed
.records
@@ -496,8 +481,17 @@ mod tests {
.map(|s| s.as_bytes().to_vec().into_boxed_slice())
.collect::<Vec<_>>()
);
for segments in [vec![], vec!["x".repeat(256)], vec!["é".repeat(128)]] {
input.data = Some(Data::Txt(DnsTxtData { segments }));
for value in [
String::new(),
" ".to_owned(),
format!("\"{}\"", "x".repeat(256)),
format!("\"{}\"", "é".repeat(128)),
r#""\256""#.to_owned(),
r#""\01""#.to_owned(),
r#""\0x0""#.to_owned(),
r#""\255""#.to_owned(),
] {
input.value = Some(value);
assert!(compile_zone(&zone(vec![input.clone()])).is_err());
}
}
@@ -566,7 +560,6 @@ mod tests {
format!("0 custom \"{}\"", "a".repeat(4092)),
format!("0 \"custom\"\"{}\"", "a".repeat(4093)),
format!("0 custom \"{}\"", "\\a".repeat(3000)),
r#"0 custom "a\077b""#.into(),
] {
let content = format!("$ORIGIN example.test.\n$TTL 60\nservice IN CAA {value}\n");
let (_, existing) = Parser::new(content, None, Some(origin.clone()))
@@ -580,6 +573,23 @@ mod tests {
&format!("0 custom \"{}\"", "a".repeat(4094)),
);
assert!(compile_zone(&zone(vec![input])).is_err());
let parsed = compile_zone(&zone(vec![record(
DnsRecordType::Caa,
r#"0 custom "a\077b""#,
)]))
.unwrap();
assert_eq!(
parsed
.records
.values()
.next()
.unwrap()
.records_without_rrsigs()
.next()
.unwrap()
.data(),
&RData::parse(RecordType::CAA, ["0", "custom", "aMb"].into_iter(), None).unwrap()
);
for target in [r"host\065label.example.test.", r"host\-label.example.test."] {
let expected =
RData::parse(RecordType::CNAME, [target].into_iter(), Some(&origin)).unwrap();
@@ -628,17 +638,11 @@ mod tests {
input.r#type = kind;
assert!(compile_zone(&zone(vec![input])).is_err());
}
for data in [
None,
Some(Data::Txt(DnsTxtData {
segments: vec!["x".into()],
})),
] {
let mut input = record(DnsRecordType::A, "192.0.2.1");
input.data = data;
for kind in [DnsRecordType::A, DnsRecordType::Txt] {
let mut input = record(kind, "192.0.2.1");
input.value = None;
assert!(compile_zone(&zone(vec![input])).is_err());
}
assert!(compile_zone(&zone(vec![record(DnsRecordType::Txt, "\"x\"")])).is_err());
let mut outside = record(DnsRecordType::A, "192.0.2.1");
outside.name = "other.invalid.".into();
assert!(
@@ -65,7 +65,7 @@ domain = "MESH.test"
origin = "RECORDS.test"
ttl = 60
records = [
{ name = "info", type = "TXT", ttl = 0, txt = { segments = ["space quote\" slash\\ semicolon;", "tail"] } },
{ name = "info", type = "TXT", ttl = 0, value = '"space quote\" slash\\ semicolon;" "tail"' },
{ name = "alias", type = "CNAME", value = "info" },
]
"#,
+1 -3
View File
@@ -803,9 +803,7 @@ network_secret = "network-secret"
vec![easytier_proto::core_config::DnsRecordRaw {
name: "saved".to_owned(),
r#type: easytier_proto::core_config::DnsRecordType::A as i32,
data: Some(easytier_proto::core_config::dns_record_raw::Data::Value(
"192.0.2.10".to_owned(),
)),
value: Some("192.0.2.10".to_owned()),
..Default::default()
}]
.into(),
@@ -823,11 +823,7 @@ mod dns_tests {
vec![easytier_proto::core_config::DnsRecordRaw {
name: "host".to_owned(),
r#type: easytier_proto::core_config::DnsRecordType::A as i32,
data: Some(
easytier_proto::core_config::dns_record_raw::Data::Value(
"not-an-ip".to_owned(),
),
),
value: Some("not-an-ip".to_owned()),
..Default::default()
}]
.into(),
+4 -10
View File
@@ -102,14 +102,8 @@ message DnsRecordRaw {
string name = 1;
DnsRecordType type = 2;
optional uint32 ttl = 3;
oneof data {
// One RDATA in presentation format, without an owner, TTL, class or type.
string value = 4;
DnsTxtData txt = 5;
}
}
message DnsTxtData {
// One UTF-8 character-string per segment, without DNS text escaping.
repeated string segments = 1;
// One RDATA in presentation format, without an owner, TTL, class or type.
optional string value = 4;
reserved 5;
reserved "txt";
}
@@ -174,7 +174,7 @@ function allFieldFixture() {
ttl: 0,
records: [
{ name: 'nas', type: 'A', value: '192.0.2.1' },
{ name: 'info', type: 'TXT', ttl: 0, txt: { segments: ['quote" slash\\ ;', '', 'tail'] } },
{ name: 'info', type: 'TXT', ttl: 0, value: String.raw`"quote\" slash\\ ;" "" "tail"` },
],
}] },
enable_private_mode: true,
@@ -625,7 +625,7 @@ for (const dns of [
{ zone: [{ origin: 'example.test.', ttl: 0, records: [] }] },
{ zone: [{ origin: 'example.test.', records: [
{ name: '@', type: 'MX', value: '10 mail' },
{ name: 'info', type: 'TXT', txt: { segments: ['first', '', 'second'] } },
{ name: 'info', type: 'TXT', value: '"first" "" "second"' },
] }] },
]) {
const normalized = normalizeNetworkConfig({ ...DEFAULT_NETWORK_CONFIG(), dns });
@@ -58,16 +58,11 @@ export const PEER_PROTOCOLS: { [proto: string]: number } = {
// Configuration JSON uses transparent repeated wrappers, as Rust Serde does.
// Derive the value types from the protocol schema rather than copying fields.
type OneofJson<T> = T extends { oneofKind: keyof T }
? { [Field in Exclude<keyof T, 'oneofKind'>]: ConfigJson<T[Field]> }
: never
type ConfigJson<T> = T extends { items: Array<infer Item> }
? ConfigJson<Item>[]
: T extends DnsRecordRaw
? ConfigJson<Omit<T, 'data' | 'type'>>
? ConfigJson<Omit<T, 'type'>>
& { type: DnsRecordType | keyof typeof DnsRecordType }
& OneofJson<T['data']>
: T extends object ? { [Key in keyof T]: ConfigJson<T[Key]> } : T
export type DnsConfig = ConfigJson<NonNullable<ProtoNetworkConfig['dns']>>
+12 -4
View File
@@ -1676,7 +1676,7 @@ mod tests {
desired["dns"]["zone"][0]["records"] = json!([
{"name": "nas", "type": "A", "value": "192.0.2.20"},
{"name": "@", "type": "TXT", "txt": {"segments": ["updated through web"]}}
{"name": "@", "type": "TXT", "value": "\"updated through web\""}
]);
*webhook_state.config_revision.write().await = "dns-updated".to_owned();
mgr.reconcile_managed_network_configs(
@@ -1699,12 +1699,20 @@ mod tests {
.as_ref()
.unwrap()
.items;
assert!(records.iter().any(|record| matches!(&record.data, Some(easytier::proto::core_config::dns_record_raw::Data::Value(value)) if value == "192.0.2.20")));
assert!(!records.iter().any(|record| matches!(&record.data, Some(easytier::proto::core_config::dns_record_raw::Data::Value(value)) if value == "192.0.2.10")));
assert!(
records
.iter()
.any(|record| matches!(&record.data, Some(easytier::proto::core_config::dns_record_raw::Data::Txt(txt)) if txt.segments == ["updated through web"]))
.any(|record| record.value.as_deref() == Some("192.0.2.20"))
);
assert!(
!records
.iter()
.any(|record| record.value.as_deref() == Some("192.0.2.10"))
);
assert!(
records
.iter()
.any(|record| record.value.as_deref() == Some("\"updated through web\""))
);
desired["dns"]["zone"] = json!([]);
@@ -1783,9 +1783,7 @@ mod tests {
easytier::proto::core_config::DnsRecordRaw {
name: "nas".to_owned(),
r#type: kind as i32,
data: Some(easytier::proto::core_config::dns_record_raw::Data::Value(
value.to_owned(),
)),
value: Some(value.to_owned()),
..Default::default()
}
}
+5 -6
View File
@@ -806,7 +806,7 @@ mod tests {
origin = "records.test"
records = [
{ name = "nas", type = "A", value = "192.0.2.10" },
{ name = "info", type = "TXT", txt = { segments = ["literal ; \" \\", "tail"] } },
{ name = "info", type = "TXT", value = '"literal ; \" \\" "tail"' },
]
"#,
)
@@ -817,11 +817,10 @@ records = [
records[0].r#type,
easytier_proto::core_config::DnsRecordType::A as i32
);
assert!(matches!(
&records[1].data,
Some(easytier_proto::core_config::dns_record_raw::Data::Txt(txt))
if txt.segments == ["literal ; \" \\", "tail"]
));
assert_eq!(
records[1].value.as_deref(),
Some(r#""literal ; \" \\" "tail""#)
);
assert!(parse_dns_file("hostname = 'unrelated'\n[dns]\nzone = []").is_err());
assert!(parse_dns_file("").is_err());
}
@@ -661,9 +661,7 @@ mod tests {
use super::*;
use crate::proto::api::instance::Route;
use easytier_core::config::toml::{DnsConfig, DnsZoneConfig};
use easytier_proto::core_config::{
DnsRecordRaw, DnsRecordType, DnsTxtData, dns_record_raw::Data,
};
use easytier_proto::core_config::{DnsRecordRaw, DnsRecordType};
use hickory_proto::{
op::{Message, Query},
rr::{RData, RecordType},
@@ -694,13 +692,7 @@ mod tests {
name: name.into(),
r#type: kind as i32,
ttl,
data: Some(if kind == DnsRecordType::Txt {
Data::Txt(DnsTxtData {
segments: vec![value.into()],
})
} else {
Data::Value(value.into())
}),
value: Some(value.into()),
}
}
@@ -841,7 +833,7 @@ mod tests {
record("specific", DnsRecordType::Cname, "nas.sub", None),
record("missing", DnsRecordType::Cname, "absent", None),
record("cycle", DnsRecordType::Cname, "cycle.example.test.", None),
record("info", DnsRecordType::Txt, "local record", None),
record("info", DnsRecordType::Txt, "\"local record\"", None),
],
),
(
+4 -6
View File
@@ -1,4 +1,4 @@
use easytier_proto::core_config::{DnsRecordRaw, DnsRecordType, dns_record_raw::Data};
use easytier_proto::core_config::{DnsRecordRaw, DnsRecordType};
use std::net::{Ipv4Addr, SocketAddr};
use std::str::FromStr as _;
use std::sync::Arc;
@@ -144,7 +144,7 @@ async fn test_magic_dns_server_instance() {
name: "svc".into(),
r#type: DnsRecordType::A as i32,
ttl: None,
data: Some(Data::Value("10.0.0.9".into())),
value: Some("10.0.0.9".into()),
}]
.into(),
),
@@ -288,7 +288,7 @@ async fn test_magic_dns_update_replaces_records_for_same_client() {
name: "svc".into(),
r#type: DnsRecordType::A as i32,
ttl: None,
data: Some(Data::Value("10.0.0.9".into())),
value: Some("10.0.0.9".into()),
}]
.into(),
),
@@ -510,9 +510,7 @@ async fn test_magic_dns_runtime_clients_republish_after_election() {
vec![easytier_proto::core_config::DnsRecordRaw {
name: name.to_owned(),
r#type: easytier_proto::core_config::DnsRecordType::A as i32,
data: Some(easytier_proto::core_config::dns_record_raw::Data::Value(
ip.to_owned(),
)),
value: Some(ip.to_owned()),
..Default::default()
}]
.into(),