1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-29 16:30:56 -05:00

fix: don't panic on resolveDns if unsupported record type is specified (#17336)

Fixes #14373
This commit is contained in:
Leo Kettmeir 2023-01-11 21:31:14 +01:00 committed by David Sherret
parent f74c44efd6
commit 32e9299e32
4 changed files with 51 additions and 24 deletions

View file

@ -62,3 +62,10 @@ try {
} thrown for not-found-example.com`, } 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);
}

View file

@ -23,3 +23,4 @@ SRV
TXT TXT
[["I","am","a","txt","record"],["I","am","another","txt","record"],["I am a different","txt record"],["key=val"]] [["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 Error NotFound thrown for not-found-example.com
Provided record type is not supported

View file

@ -28,4 +28,5 @@ alias CNAME cname
PTR alias PTR alias
_service._tcp SRV 0 100 1234 srv _service._tcp SRV 0 100 1234 srv
@ IN NAPTR 10 0 "s" "SIPS+D2T" "" _sips._tcp.example.com. @ IN NAPTR 10 0 "s" "SIPS+D2T" "" _sips._tcp.example.com.
@ IN NAPTR 10 0 "s" RELAY:turn.udp "" _turn._udp.example.com. @ IN NAPTR 10 0 "s" RELAY:turn.udp "" _turn._udp.example.com.
@ IN SSHFP 1 1 436C6F7564666C

View file

@ -484,7 +484,7 @@ where
let resolver = AsyncResolver::tokio(config, opts)?; let resolver = AsyncResolver::tokio(config, opts)?;
let results = resolver resolver
.lookup(query, record_type) .lookup(query, record_type)
.await .await
.map_err(|e| { .map_err(|e| {
@ -501,10 +501,8 @@ where
} }
})? })?
.iter() .iter()
.filter_map(rdata_to_return_record(record_type)) .filter_map(|rdata| rdata_to_return_record(record_type)(rdata).transpose())
.collect(); .collect::<Result<Vec<DnsReturnRecord>, AnyError>>()
Ok(results)
} }
#[op] #[op]
@ -531,10 +529,10 @@ pub fn op_set_keepalive(
fn rdata_to_return_record( fn rdata_to_return_record(
ty: RecordType, ty: RecordType,
) -> impl Fn(&RData) -> Option<DnsReturnRecord> { ) -> impl Fn(&RData) -> Result<Option<DnsReturnRecord>, AnyError> {
use RecordType::*; use RecordType::*;
move |r: &RData| -> Option<DnsReturnRecord> { move |r: &RData| -> Result<Option<DnsReturnRecord>, AnyError> {
match ty { let record = match ty {
A => r.as_a().map(ToString::to_string).map(DnsReturnRecord::A), A => r.as_a().map(ToString::to_string).map(DnsReturnRecord::A),
AAAA => r AAAA => r
.as_aaaa() .as_aaaa()
@ -614,9 +612,14 @@ fn rdata_to_return_record(
.collect(); .collect();
DnsReturnRecord::Txt(texts) 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 func = rdata_to_return_record(RecordType::A);
let rdata = RData::A(Ipv4Addr::new(127, 0, 0, 1)); let rdata = RData::A(Ipv4Addr::new(127, 0, 0, 1));
assert_eq!( assert_eq!(
func(&rdata), func(&rdata).unwrap(),
Some(DnsReturnRecord::A("127.0.0.1".to_string())) Some(DnsReturnRecord::A("127.0.0.1".to_string()))
); );
} }
@ -655,14 +658,20 @@ mod tests {
fn rdata_to_return_record_aaaa() { fn rdata_to_return_record_aaaa() {
let func = rdata_to_return_record(RecordType::AAAA); let func = rdata_to_return_record(RecordType::AAAA);
let rdata = RData::AAAA(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); 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] #[test]
fn rdata_to_return_record_aname() { fn rdata_to_return_record_aname() {
let func = rdata_to_return_record(RecordType::ANAME); let func = rdata_to_return_record(RecordType::ANAME);
let rdata = RData::ANAME(Name::new()); 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] #[test]
@ -674,7 +683,7 @@ mod tests {
vec![KeyValue::new("account", "123456")], vec![KeyValue::new("account", "123456")],
)); ));
assert_eq!( assert_eq!(
func(&rdata), func(&rdata).unwrap(),
Some(DnsReturnRecord::Caa { Some(DnsReturnRecord::Caa {
critical: false, critical: false,
tag: "issue".to_string(), tag: "issue".to_string(),
@ -687,7 +696,10 @@ mod tests {
fn rdata_to_return_record_cname() { fn rdata_to_return_record_cname() {
let func = rdata_to_return_record(RecordType::CNAME); let func = rdata_to_return_record(RecordType::CNAME);
let rdata = RData::CNAME(Name::new()); 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] #[test]
@ -695,7 +707,7 @@ mod tests {
let func = rdata_to_return_record(RecordType::MX); let func = rdata_to_return_record(RecordType::MX);
let rdata = RData::MX(MX::new(10, Name::new())); let rdata = RData::MX(MX::new(10, Name::new()));
assert_eq!( assert_eq!(
func(&rdata), func(&rdata).unwrap(),
Some(DnsReturnRecord::Mx { Some(DnsReturnRecord::Mx {
preference: 10, preference: 10,
exchange: "".to_string() exchange: "".to_string()
@ -715,7 +727,7 @@ mod tests {
Name::new(), Name::new(),
)); ));
assert_eq!( assert_eq!(
func(&rdata), func(&rdata).unwrap(),
Some(DnsReturnRecord::Naptr { Some(DnsReturnRecord::Naptr {
order: 1, order: 1,
preference: 2, preference: 2,
@ -731,14 +743,20 @@ mod tests {
fn rdata_to_return_record_ns() { fn rdata_to_return_record_ns() {
let func = rdata_to_return_record(RecordType::NS); let func = rdata_to_return_record(RecordType::NS);
let rdata = RData::NS(Name::new()); 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] #[test]
fn rdata_to_return_record_ptr() { fn rdata_to_return_record_ptr() {
let func = rdata_to_return_record(RecordType::PTR); let func = rdata_to_return_record(RecordType::PTR);
let rdata = RData::PTR(Name::new()); 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] #[test]
@ -754,7 +772,7 @@ mod tests {
0, 0,
)); ));
assert_eq!( assert_eq!(
func(&rdata), func(&rdata).unwrap(),
Some(DnsReturnRecord::Soa { Some(DnsReturnRecord::Soa {
mname: "".to_string(), mname: "".to_string(),
rname: "".to_string(), rname: "".to_string(),
@ -772,7 +790,7 @@ mod tests {
let func = rdata_to_return_record(RecordType::SRV); let func = rdata_to_return_record(RecordType::SRV);
let rdata = RData::SRV(SRV::new(1, 2, 3, Name::new())); let rdata = RData::SRV(SRV::new(1, 2, 3, Name::new()));
assert_eq!( assert_eq!(
func(&rdata), func(&rdata).unwrap(),
Some(DnsReturnRecord::Srv { Some(DnsReturnRecord::Srv {
priority: 1, priority: 1,
weight: 2, weight: 2,
@ -792,7 +810,7 @@ mod tests {
&[0xe3, 0x81, 0x82], // "あ" in UTF-8 &[0xe3, 0x81, 0x82], // "あ" in UTF-8
])); ]));
assert_eq!( assert_eq!(
func(&rdata), func(&rdata).unwrap(),
Some(DnsReturnRecord::Txt(vec![ Some(DnsReturnRecord::Txt(vec![
"foo".to_string(), "foo".to_string(),
"bar".to_string(), "bar".to_string(),