Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ext/net): add CAA DNS record support #14624

Merged
merged 4 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions cli/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2953,6 +2953,7 @@ declare namespace Deno {
| "A"
| "AAAA"
| "ANAME"
| "CAA"
| "CNAME"
| "MX"
| "NAPTR"
Expand All @@ -2974,6 +2975,13 @@ declare namespace Deno {
};
}

/** If `resolveDns` is called with "CAA" record type specified, it will return an array of this interface. */
export interface CAARecord {
critical: boolean;
tag: string;
value: string;
}

/** If `resolveDns` is called with "MX" record type specified, it will return an array of this interface. */
export interface MXRecord {
preference: number;
Expand Down Expand Up @@ -3015,6 +3023,12 @@ declare namespace Deno {
options?: ResolveDnsOptions,
): Promise<string[]>;

export function resolveDns(
query: string,
recordType: "CAA",
options?: ResolveDnsOptions,
): Promise<CAARecord[]>;

export function resolveDns(
query: string,
recordType: "MX",
Expand Down Expand Up @@ -3068,6 +3082,7 @@ declare namespace Deno {
options?: ResolveDnsOptions,
): Promise<
| string[]
| CAARecord[]
| MXRecord[]
| NAPTRRecord[]
| SOARecord[]
Expand Down
33 changes: 19 additions & 14 deletions cli/tests/testdata/resolve_dns.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
const nameServer = { nameServer: { ipAddr: "127.0.0.1", port: 4553 } };

const [a, aaaa, aname, cname, mx, naptr, ns, ptr, soa, srv, txt] = await Promise
.all([
Deno.resolveDns("www.example.com", "A", nameServer),
Deno.resolveDns("www.example.com", "AAAA", nameServer),
Deno.resolveDns("www.example.com", "ANAME", nameServer),
Deno.resolveDns("alias.example.com", "CNAME", nameServer),
Deno.resolveDns("example.com", "MX", nameServer),
Deno.resolveDns("example.com", "NAPTR", nameServer),
Deno.resolveDns("example.com", "NS", nameServer),
Deno.resolveDns("1.2.3.4.IN-ADDR.ARPA.", "PTR", nameServer),
Deno.resolveDns("example.com", "SOA", nameServer),
Deno.resolveDns("_service._tcp.example.com", "SRV", nameServer),
Deno.resolveDns("example.com", "TXT", nameServer),
]);
const [a, aaaa, aname, caa, cname, mx, naptr, ns, ptr, soa, srv, txt] =
await Promise
.all([
Deno.resolveDns("www.example.com", "A", nameServer),
Deno.resolveDns("www.example.com", "AAAA", nameServer),
Deno.resolveDns("www.example.com", "ANAME", nameServer),
Deno.resolveDns("example.com", "CAA", nameServer),
Deno.resolveDns("alias.example.com", "CNAME", nameServer),
Deno.resolveDns("example.com", "MX", nameServer),
Deno.resolveDns("example.com", "NAPTR", nameServer),
Deno.resolveDns("example.com", "NS", nameServer),
Deno.resolveDns("1.2.3.4.IN-ADDR.ARPA.", "PTR", nameServer),
Deno.resolveDns("example.com", "SOA", nameServer),
Deno.resolveDns("_service._tcp.example.com", "SRV", nameServer),
Deno.resolveDns("example.com", "TXT", nameServer),
]);

console.log("A");
console.log(JSON.stringify(a));
Expand All @@ -24,6 +26,9 @@ console.log(JSON.stringify(aaaa));
console.log("ANAME");
console.log(JSON.stringify(aname));

console.log("CAA");
console.log(JSON.stringify(caa));

console.log("CNAME");
console.log(JSON.stringify(cname));

Expand Down
2 changes: 2 additions & 0 deletions cli/tests/testdata/resolve_dns.ts.out
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ AAAA
["1:2:3:4:5:6:7:8"]
ANAME
["aname.example.com."]
CAA
[{"critical":false,"tag":"issue","value":"ca.example.net"},{"critical":false,"tag":"issue","value":"ca2.example.net; account=123456"},{"critical":false,"tag":"issuewild","value":";"},{"critical":false,"tag":"iodef","value":"mailto:security@example.com"},{"critical":true,"tag":"tbs","value":"Unknown"}]
CNAME
["cname.example.com."]
MX
Expand Down
5 changes: 5 additions & 0 deletions cli/tests/testdata/resolve_dns.zone.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
600 ; RETRY
3600000; EXPIRE
60) ; MINIMUM
@ IN CAA 0 issue "ca.example.net"
@ IN CAA 0 issue "ca2.example.net; account=123456"
@ IN CAA 0 issuewild ";"
@ IN CAA 0 iodef "mailto:security@example.com"
@ IN CAA 128 tbs "Unknown"
NS ns1.ns.com.
NS ns2.ns.com.
NS ns3.ns.com.
Expand Down
49 changes: 49 additions & 0 deletions ext/net/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use std::rc::Rc;
use tokio::net::TcpListener;
use tokio::net::TcpStream;
use tokio::net::UdpSocket;
use trust_dns_proto::rr::rdata::caa::Value;
use trust_dns_proto::rr::record_data::RData;
use trust_dns_proto::rr::record_type::RecordType;
use trust_dns_resolver::config::NameServerConfigGroup;
Expand Down Expand Up @@ -574,6 +575,11 @@ pub enum DnsReturnRecord {
A(String),
Aaaa(String),
Aname(String),
Caa {
critical: bool,
tag: String,
value: String,
},
Cname(String),
Mx {
preference: u16,
Expand Down Expand Up @@ -740,6 +746,29 @@ fn rdata_to_return_record(
.as_aname()
.map(ToString::to_string)
.map(DnsReturnRecord::Aname),
CAA => r.as_caa().map(|caa| DnsReturnRecord::Caa {
critical: caa.issuer_critical(),
tag: caa.tag().to_string(),
value: match caa.value() {
Value::Issuer(name, key_values) => {
let mut s = String::new();

if let Some(name) = name {
s.push_str(&format!("{}", name));
} else if name.is_none() && key_values.is_empty() {
s.push(';');
}

for key_value in key_values {
s.push_str(&format!("; {}", key_value));
}

s
}
Value::Url(url) => url.to_string(),
Value::Unknown(data) => String::from_utf8(data.to_vec()).unwrap(),
},
}),
CNAME => r
.as_cname()
.map(ToString::to_string)
Expand Down Expand Up @@ -803,6 +832,8 @@ mod tests {
use std::net::Ipv4Addr;
use std::net::Ipv6Addr;
use std::path::Path;
use trust_dns_proto::rr::rdata::caa::KeyValue;
use trust_dns_proto::rr::rdata::caa::CAA;
use trust_dns_proto::rr::rdata::mx::MX;
use trust_dns_proto::rr::rdata::naptr::NAPTR;
use trust_dns_proto::rr::rdata::srv::SRV;
Expand Down Expand Up @@ -835,6 +866,24 @@ mod tests {
assert_eq!(func(&rdata), Some(DnsReturnRecord::Aname("".to_string())));
}

#[test]
fn rdata_to_return_record_caa() {
let func = rdata_to_return_record(RecordType::CAA);
let rdata = RData::CAA(CAA::new_issue(
false,
Some(Name::parse("example.com", None).unwrap()),
vec![KeyValue::new("account", "123456")],
));
assert_eq!(
func(&rdata),
Some(DnsReturnRecord::Caa {
critical: false,
tag: "issue".to_string(),
value: "example.com; account=123456".to_string(),
})
);
}

#[test]
fn rdata_to_return_record_cname() {
let func = rdata_to_return_record(RecordType::CNAME);
Expand Down