From b0e0e4f24b91c9be6a9bb005dc1f683d3b563bd6 Mon Sep 17 00:00:00 2001 From: Leo Kettmeir Date: Wed, 11 Jan 2023 21:31:14 +0100 Subject: [PATCH] fix: don't panic on resolveDns if unsupported record type is specified (#17336) Fixes #14373 --- cli/tests/testdata/run/resolve_dns.ts | 7 +++ cli/tests/testdata/run/resolve_dns.ts.out | 1 + cli/tests/testdata/run/resolve_dns.zone.in | 3 +- ext/net/ops.rs | 64 ++++++++++++++-------- 4 files changed, 51 insertions(+), 24 deletions(-) diff --git a/cli/tests/testdata/run/resolve_dns.ts b/cli/tests/testdata/run/resolve_dns.ts index bde479c9ae..ae6ed70a42 100644 --- a/cli/tests/testdata/run/resolve_dns.ts +++ b/cli/tests/testdata/run/resolve_dns.ts @@ -62,3 +62,10 @@ try { } thrown for not-found-example.com`, ); } + +try { + // @ts-ignore testing invalid overloads + await Deno.resolveDns("example.com", "SSHFP", nameServer); +} catch (e) { + console.log(e.message); +} diff --git a/cli/tests/testdata/run/resolve_dns.ts.out b/cli/tests/testdata/run/resolve_dns.ts.out index 2fe10cb9aa..f41dc68bcb 100644 --- a/cli/tests/testdata/run/resolve_dns.ts.out +++ b/cli/tests/testdata/run/resolve_dns.ts.out @@ -23,3 +23,4 @@ SRV TXT [["I","am","a","txt","record"],["I","am","another","txt","record"],["I am a different","txt record"],["key=val"]] Error NotFound thrown for not-found-example.com +Provided record type is not supported diff --git a/cli/tests/testdata/run/resolve_dns.zone.in b/cli/tests/testdata/run/resolve_dns.zone.in index c351a1de9d..94d5b9fc37 100644 --- a/cli/tests/testdata/run/resolve_dns.zone.in +++ b/cli/tests/testdata/run/resolve_dns.zone.in @@ -28,4 +28,5 @@ alias CNAME cname PTR alias _service._tcp SRV 0 100 1234 srv @ IN NAPTR 10 0 "s" "SIPS+D2T" "" _sips._tcp.example.com. -@ IN NAPTR 10 0 "s" RELAY:turn.udp "" _turn._udp.example.com. \ No newline at end of file +@ IN NAPTR 10 0 "s" RELAY:turn.udp "" _turn._udp.example.com. +@ IN SSHFP 1 1 436C6F7564666C diff --git a/ext/net/ops.rs b/ext/net/ops.rs index 5f832a4b9e..259703d175 100644 --- a/ext/net/ops.rs +++ b/ext/net/ops.rs @@ -484,7 +484,7 @@ where let resolver = AsyncResolver::tokio(config, opts)?; - let results = resolver + resolver .lookup(query, record_type) .await .map_err(|e| { @@ -501,10 +501,8 @@ where } })? .iter() - .filter_map(rdata_to_return_record(record_type)) - .collect(); - - Ok(results) + .filter_map(|rdata| rdata_to_return_record(record_type)(rdata).transpose()) + .collect::, AnyError>>() } #[op] @@ -531,10 +529,10 @@ pub fn op_set_keepalive( fn rdata_to_return_record( ty: RecordType, -) -> impl Fn(&RData) -> Option { +) -> impl Fn(&RData) -> Result, AnyError> { use RecordType::*; - move |r: &RData| -> Option { - match ty { + move |r: &RData| -> Result, AnyError> { + let record = match ty { A => r.as_a().map(ToString::to_string).map(DnsReturnRecord::A), AAAA => r .as_aaaa() @@ -614,9 +612,14 @@ fn rdata_to_return_record( .collect(); DnsReturnRecord::Txt(texts) }), - // TODO(magurotuna): Other record types are not supported - _ => todo!(), - } + _ => { + return Err(custom_error( + "NotSupported", + "Provided record type is not supported", + )) + } + }; + Ok(record) } } @@ -646,7 +649,7 @@ mod tests { let func = rdata_to_return_record(RecordType::A); let rdata = RData::A(Ipv4Addr::new(127, 0, 0, 1)); assert_eq!( - func(&rdata), + func(&rdata).unwrap(), Some(DnsReturnRecord::A("127.0.0.1".to_string())) ); } @@ -655,14 +658,20 @@ mod tests { fn rdata_to_return_record_aaaa() { let func = rdata_to_return_record(RecordType::AAAA); let rdata = RData::AAAA(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); - assert_eq!(func(&rdata), Some(DnsReturnRecord::Aaaa("::1".to_string()))); + assert_eq!( + func(&rdata).unwrap(), + Some(DnsReturnRecord::Aaaa("::1".to_string())) + ); } #[test] fn rdata_to_return_record_aname() { let func = rdata_to_return_record(RecordType::ANAME); let rdata = RData::ANAME(Name::new()); - assert_eq!(func(&rdata), Some(DnsReturnRecord::Aname("".to_string()))); + assert_eq!( + func(&rdata).unwrap(), + Some(DnsReturnRecord::Aname("".to_string())) + ); } #[test] @@ -674,7 +683,7 @@ mod tests { vec![KeyValue::new("account", "123456")], )); assert_eq!( - func(&rdata), + func(&rdata).unwrap(), Some(DnsReturnRecord::Caa { critical: false, tag: "issue".to_string(), @@ -687,7 +696,10 @@ mod tests { fn rdata_to_return_record_cname() { let func = rdata_to_return_record(RecordType::CNAME); let rdata = RData::CNAME(Name::new()); - assert_eq!(func(&rdata), Some(DnsReturnRecord::Cname("".to_string()))); + assert_eq!( + func(&rdata).unwrap(), + Some(DnsReturnRecord::Cname("".to_string())) + ); } #[test] @@ -695,7 +707,7 @@ mod tests { let func = rdata_to_return_record(RecordType::MX); let rdata = RData::MX(MX::new(10, Name::new())); assert_eq!( - func(&rdata), + func(&rdata).unwrap(), Some(DnsReturnRecord::Mx { preference: 10, exchange: "".to_string() @@ -715,7 +727,7 @@ mod tests { Name::new(), )); assert_eq!( - func(&rdata), + func(&rdata).unwrap(), Some(DnsReturnRecord::Naptr { order: 1, preference: 2, @@ -731,14 +743,20 @@ mod tests { fn rdata_to_return_record_ns() { let func = rdata_to_return_record(RecordType::NS); let rdata = RData::NS(Name::new()); - assert_eq!(func(&rdata), Some(DnsReturnRecord::Ns("".to_string()))); + assert_eq!( + func(&rdata).unwrap(), + Some(DnsReturnRecord::Ns("".to_string())) + ); } #[test] fn rdata_to_return_record_ptr() { let func = rdata_to_return_record(RecordType::PTR); let rdata = RData::PTR(Name::new()); - assert_eq!(func(&rdata), Some(DnsReturnRecord::Ptr("".to_string()))); + assert_eq!( + func(&rdata).unwrap(), + Some(DnsReturnRecord::Ptr("".to_string())) + ); } #[test] @@ -754,7 +772,7 @@ mod tests { 0, )); assert_eq!( - func(&rdata), + func(&rdata).unwrap(), Some(DnsReturnRecord::Soa { mname: "".to_string(), rname: "".to_string(), @@ -772,7 +790,7 @@ mod tests { let func = rdata_to_return_record(RecordType::SRV); let rdata = RData::SRV(SRV::new(1, 2, 3, Name::new())); assert_eq!( - func(&rdata), + func(&rdata).unwrap(), Some(DnsReturnRecord::Srv { priority: 1, weight: 2, @@ -792,7 +810,7 @@ mod tests { &[0xe3, 0x81, 0x82], // "あ" in UTF-8 ])); assert_eq!( - func(&rdata), + func(&rdata).unwrap(), Some(DnsReturnRecord::Txt(vec![ "foo".to_string(), "bar".to_string(),