diff options
author | Leo Kettmeir <crowlkats@toaxl.com> | 2023-01-11 21:31:14 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-11 21:31:14 +0100 |
commit | b0e0e4f24b91c9be6a9bb005dc1f683d3b563bd6 (patch) | |
tree | 196043a170306869daccb85363c248580bc5b695 /ext/net/ops.rs | |
parent | 6ee5563f6860758a18f35cee412d5fe3e139cf7c (diff) |
fix: don't panic on resolveDns if unsupported record type is specified (#17336)
Fixes #14373
Diffstat (limited to 'ext/net/ops.rs')
-rw-r--r-- | ext/net/ops.rs | 64 |
1 files changed, 41 insertions, 23 deletions
diff --git a/ext/net/ops.rs b/ext/net/ops.rs index 5f832a4b9..259703d17 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::<Result<Vec<DnsReturnRecord>, AnyError>>() } #[op] @@ -531,10 +529,10 @@ pub fn op_set_keepalive( fn rdata_to_return_record( ty: RecordType, -) -> impl Fn(&RData) -> Option<DnsReturnRecord> { +) -> impl Fn(&RData) -> Result<Option<DnsReturnRecord>, AnyError> { use RecordType::*; - move |r: &RData| -> Option<DnsReturnRecord> { - match ty { + move |r: &RData| -> Result<Option<DnsReturnRecord>, 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(), |