From d4e86cab47e829ed03c12030abc2acb8da8f9548 Mon Sep 17 00:00:00 2001 From: Peter Cai Date: Tue, 6 Apr 2021 07:29:57 +0800 Subject: [PATCH] client: try to parse upstream response and skip malformed DNS response * For some reason, some of our upstreams return malformed DNS response sometimes. --- src/client.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/client.rs b/src/client.rs index de420ac..a7ed97c 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,9 +1,13 @@ use crate::cache::DnsCache; use crate::r#override::OverrideResolver; -use domain::base::{ - iana::{Opcode, Rcode}, - rdata::UnknownRecordData, - Dname, Message, MessageBuilder, ParsedDname, Question, Record, ToDname, +use domain::{ + base::{ + iana::{Opcode, Rcode}, + octets::Parser, + rdata::{ParseRecordData, UnknownRecordData}, + Dname, Message, MessageBuilder, ParsedDname, Question, Record, ToDname, + }, + rdata::AllRecordData, }; use js_sys::{ArrayBuffer, Uint8Array}; use wasm_bindgen_futures::JsFuture; @@ -173,6 +177,19 @@ impl Client { record.data().data().to_vec(), ), ); + // Try to parse the record; if failed, fail this entire query + let parsed_record_data: Result>>, _> = + AllRecordData::parse_data( + owned_record.rtype(), + &mut Parser::from_ref(owned_record.data().data().as_slice()), + ); + + if let Err(_) = parsed_record_data { + return Err("Failed to parse response from upstream".to_string()); + } else if let Ok(None) = parsed_record_data { + return Err("Upstream did not respond our query".to_string()); + } + ret.push(owned_record); } Ok(ret)