From 2a91cdb0667b47fbebc778a80bb96dcf865f9def Mon Sep 17 00:00:00 2001 From: Daniel McCarney Date: Mon, 15 Apr 2024 23:11:24 -0400 Subject: [PATCH] proto: fix parse of arbitrary keys in pres. syntax Previously the `FromStr` impl for `SvcParamKey` had support for parsing the "arbitrary key" presentation syntax where a key can be specified "keyNNNNN", where NNNNN is the numeric value of the key type without leading zeros. The existing code would pull out the numeric component into a `u16` and then use the `TryFrom` impl for `SvcParamKey` to get the key. However, the `TryFrom` impl for `SvcParamKey` was using the IANA service parameter keys registry to map from u16s to `SvcParamKey`. Values 0..6 are mapped to the known key entries. The reserved range (65280-65534) was mapped to `SvcParamKey::Key`, and 65535 was mapped to `SvcParamKey::Key65535`. This makes sense when mapping an arbitrary u16, but when we are parsing a "keyNNNNN" presentation syntax item, we want to represent it as `Key(NNNNN)`, no matter if it is/isn't a registered key. This commit fixes this behaviour, constructing a `SvcParamKey::Key()` entry when parsing the arbitrary key presentation syntax, avoiding `TryFrom`. With this change in place the two arbitrary key test vectors can be included in the svcb test vector unit test. [0] https://datatracker.ietf.org/doc/html/rfc9460#name-initial-contents --- crates/proto/src/rr/rdata/svcb.rs | 4 +--- crates/proto/src/serialize/txt/rdata_parsers/svcb.rs | 5 +---- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/crates/proto/src/rr/rdata/svcb.rs b/crates/proto/src/rr/rdata/svcb.rs index ce36595f..dc1a7c12 100644 --- a/crates/proto/src/rr/rdata/svcb.rs +++ b/crates/proto/src/rr/rdata/svcb.rs @@ -307,9 +307,7 @@ impl std::str::FromStr for SvcParamKey { ))) })?; - let key_value = u16::from_str(key_value)?; - let key = SvcParamKey::from(key_value); - Ok(key) + Ok(SvcParamKey::Key(u16::from_str(key_value)?)) } let key = match s { diff --git a/crates/proto/src/serialize/txt/rdata_parsers/svcb.rs b/crates/proto/src/serialize/txt/rdata_parsers/svcb.rs index ac008882..d66f4def 100644 --- a/crates/proto/src/serialize/txt/rdata_parsers/svcb.rs +++ b/crates/proto/src/serialize/txt/rdata_parsers/svcb.rs @@ -452,7 +452,7 @@ mod tests { // NOTE: In each case the test vector from the RFC was augmented with a TTL (42 in each // case). The parser requires this but the test vectors do not include it. - let vectors: [TestVector; 6] = [ + let vectors: [TestVector; 8] = [ // https://datatracker.ietf.org/doc/html/rfc9460#appendix-D.1 // Figure 2: AliasMode TestVector { @@ -479,8 +479,6 @@ mod tests { priority: 16, params: vec![(SvcParamKey::Port, SvcParamValue::Port(53))], }, - /* - * TODO(XXX): ParseError { kind: Message("Bad Key type or unsupported, see generic key option, e.g. key1234"), backtrack: None } // Figure 5: A Generic Key and Unquoted Value TestVector { record: "example.com. 42 SVCB 1 foo.example.com. key667=hello", @@ -503,7 +501,6 @@ mod tests { SvcParamValue::Unknown(Unknown(b"hello\\210qoo".into())), )], }, - */ // Figure 7: Two Quoted IPv6 Hints TestVector { record: r#"example.com. 42 SVCB 1 foo.example.com. (ipv6hint="2001:db8::1,2001:db8::53:1")"#,