From d4f7dc3300a04b45800bd5331bd8282ce1dd0ea0 Mon Sep 17 00:00:00 2001 From: Sebastian Lorenz Date: Fri, 9 Oct 2026 09:02:21 +0200 Subject: [PATCH] Add TLSA records to DNS services (#8953) --- .changeset/net-dns-tlsa.md | 8 +++ packages/effect/src/Schema.ts | 38 +++++++++- packages/effect/src/net/Dns.ts | 70 +++++++++++++++++-- packages/effect/test/net/Dns.test.ts | 27 +++++++ packages/effect/typetest/net/Dns.tst.ts | 3 + packages/platform/bun/src/BunDns.ts | 3 + packages/platform/deno/src/DenoDns.ts | 9 ++- packages/platform/node-shared/src/NodeDns.ts | 18 ++++- .../node-shared/test/Dns.test-utils.ts | 24 +++++++ 9 files changed, 187 insertions(+), 13 deletions(-) create mode 100644 .changeset/net-dns-tlsa.md diff --git a/.changeset/net-dns-tlsa.md b/.changeset/net-dns-tlsa.md new file mode 100644 index 0000000000..f65cf1d3c2 --- /dev/null +++ b/.changeset/net-dns-tlsa.md @@ -0,0 +1,8 @@ +--- +"effect": patch +"@effect/platform-node-shared": patch +"@effect/platform-bun": patch +"@effect/platform-deno": patch +--- + +Add TLSA records to `effect/net/Dns`, with a `Schema.DnsTlsaRecord` schema whose JSON codec encodes the certificate association data as hex. The Node.js `Dns` service queries them with `resolveTlsa` (Node.js 22.15 or 23.9 and later); Bun, Deno, and older Node.js versions fail TLSA queries with `Unsupported`. diff --git a/packages/effect/src/Schema.ts b/packages/effect/src/Schema.ts index a7643587b4..f1f83dcf41 100644 --- a/packages/effect/src/Schema.ts +++ b/packages/effect/src/Schema.ts @@ -13828,6 +13828,8 @@ const dnsCanonicalAddress = ( }) )) +const dnsUint8 = Int.check(isBetween({ minimum: 0, maximum: 0xff })) + const dnsUint16 = Int.check(isBetween({ minimum: 0, maximum: 0xffff })) const dnsUint32 = Int.check(isBetween({ minimum: 0, maximum: 0xffffffff })) @@ -14093,6 +14095,36 @@ export const DnsSrvRecord: DnsSrvRecord = dnsRecordOfType("SRV", { weight: dnsUint16 }) +/** + * Type-level representation of {@link DnsTlsaRecord}. + * + * @stability experimental + * @category models + * @since 4.0.0 + */ +export interface DnsTlsaRecord extends declare { + readonly "Rebuild": DnsTlsaRecord +} + +/** + * Schema for already-constructed TLS authentication (`TLSA`) DNS records. + * + * **Details** + * + * The default JSON serializer encodes the certificate association `data` as a + * hex string. + * + * @stability experimental + * @category schemas + * @since 4.0.0 + */ +export const DnsTlsaRecord: DnsTlsaRecord = dnsRecordOfType("TLSA", { + certUsage: dnsUint8, + selector: dnsUint8, + matchingType: dnsUint8, + data: Uint8ArrayFromHex +}) + /** * Type-level representation of {@link DnsTxtRecord}. * @@ -14133,6 +14165,7 @@ export interface DnsRecord extends DnsPtrRecord, DnsSoaRecord, DnsSrvRecord, + DnsTlsaRecord, DnsTxtRecord ] > @@ -14146,8 +14179,8 @@ export interface DnsRecord extends * **Details** * * The default JSON serializer encodes records as objects tagged by record type, - * with addresses and names as canonical strings and SOA timers as whole - * seconds, such as + * with addresses and names as canonical strings, SOA timers as whole seconds, + * and TLSA data as hex, such as * `{ "_tag": "SRV", "target": "db.internal", "port": 5432, "priority": 10, "weight": 5 }`. * Decoding accepts only canonical addresses and normalized domain names, and * reports invalid fields with their path. @@ -14167,6 +14200,7 @@ export const DnsRecord: DnsRecord = Union([ DnsPtrRecord, DnsSoaRecord, DnsSrvRecord, + DnsTlsaRecord, DnsTxtRecord ]).annotate({ identifier: "DnsRecord" }) diff --git a/packages/effect/src/net/Dns.ts b/packages/effect/src/net/Dns.ts index 0f648d28d2..0665a65354 100644 --- a/packages/effect/src/net/Dns.ts +++ b/packages/effect/src/net/Dns.ts @@ -17,6 +17,7 @@ import * as Context from "../Context.ts" import * as Data from "../Data.ts" import * as Duration from "../Duration.ts" import * as Effect from "../Effect.ts" +import * as Hex from "../encoding/Hex.ts" import * as Equal from "../Equal.ts" import * as Hash from "../Hash.ts" import * as Inspectable from "../Inspectable.ts" @@ -180,6 +181,37 @@ export interface Srv extends RecordProto<"SRV"> { readonly weight: number } +/** + * A TLS authentication record (RFC 6698), which pins the certificate or public + * key that a TLS service presents, for DNS-Based Authentication of Named + * Entities (DANE). Records are published at a name made of the service's port + * and protocol, such as `_443._tcp.example.com`. + * + * **Details** + * + * `certUsage` says how the record constrains the certificate chain (0 to 3: + * PKIX-TA, PKIX-EE, DANE-TA, DANE-EE), `selector` whether `data` matches the + * full certificate (0) or its public key (1), and `matchingType` whether + * `data` holds the selected content itself (0) or its SHA-256 (1) or SHA-512 + * (2) digest. Unassigned values are kept as they are. + * + * **Gotchas** + * + * `data` is a copy of the bytes the record was made from, but typed arrays + * cannot be frozen; treat it as read-only, because changing it changes the + * record's equality. + * + * @stability experimental + * @category models + * @since 4.0.0 + */ +export interface Tlsa extends RecordProto<"TLSA"> { + readonly certUsage: number + readonly selector: number + readonly matchingType: number + readonly data: Uint8Array +} + /** * A text record. DNS stores text as one or more character strings, which are * kept as separate chunks; protocols such as SPF read them joined, for example @@ -214,7 +246,7 @@ export interface Txt extends RecordProto<"TXT"> { * @category models * @since 4.0.0 */ -export type DnsRecord = A | Aaaa | Caa | Cname | Mx | Naptr | Ns | Ptr | Soa | Srv | Txt +export type DnsRecord = A | Aaaa | Caa | Cname | Mx | Naptr | Ns | Ptr | Soa | Srv | Tlsa | Txt /** * The type of a DNS record, such as `"A"` or `"SRV"`. @@ -223,7 +255,19 @@ export type DnsRecord = A | Aaaa | Caa | Cname | Mx | Naptr | Ns | Ptr | Soa | S * @category models * @since 4.0.0 */ -export type RecordType = "A" | "AAAA" | "CAA" | "CNAME" | "MX" | "NAPTR" | "NS" | "PTR" | "SOA" | "SRV" | "TXT" +export type RecordType = + | "A" + | "AAAA" + | "CAA" + | "CNAME" + | "MX" + | "NAPTR" + | "NS" + | "PTR" + | "SOA" + | "SRV" + | "TLSA" + | "TXT" /** * The record value for a record type. @@ -245,6 +289,7 @@ export type RecordFor = Extract = T extends RecordType ? Omit, keyof RecordProto> : never +const isUint8 = (u: unknown): boolean => Number.isInteger(u) && (u as number) >= 0 && (u as number) <= 0xff const isUint16 = (u: unknown): boolean => Number.isInteger(u) && (u as number) >= 0 && (u as number) <= 0xffff const isUint32 = (u: unknown): boolean => Number.isInteger(u) && (u as number) >= 0 && (u as number) <= 0xffffffff const isString = (u: unknown): u is string => typeof u === "string" @@ -302,6 +347,12 @@ const recordFields: { priority: isUint16, weight: isUint16 }, + TLSA: { + certUsage: isUint8, + selector: isUint8, + matchingType: isUint8, + data: (u) => u instanceof Uint8Array && u.length > 0 + }, TXT: { chunks: (u) => Array.isArray(u) && u.length > 0 && u.every(isString) } @@ -360,9 +411,10 @@ const RecordPrototype = { * Every field is checked at runtime: addresses must belong to the record's * address family, names must be normalized domain names, priorities, weights, * and ports must be 16-bit unsigned integers, the SOA serial and timers must be - * 32-bit unsigned integers (the timers in whole seconds), TXT records need at - * least one chunk, and CAA property tags must be alphanumeric. Fields that do - * not belong to the record type are ignored. + * 32-bit unsigned integers (the timers in whole seconds), the TLSA fields must + * be 8-bit unsigned integers with non-empty `data`, which is copied, TXT + * records need at least one chunk, and CAA property tags must be alphanumeric. + * Fields that do not belong to the record type are ignored. * * **Example** (Creating a service record) * @@ -399,7 +451,11 @@ export const makeRecord = ( if (!check(value)) { return Result.fail(new NetAddress.NetAddressError({ input: fields, message: `invalid ${type} record ${key}` })) } - self[key] = Array.isArray(value) ? Object.freeze([...value]) : value + self[key] = Array.isArray(value) + ? Object.freeze([...value]) + : value instanceof Uint8Array + ? new Uint8Array(value) + : value } return Result.succeed(Object.freeze(self)) @@ -493,6 +549,8 @@ export const formatRecord = (self: DnsRecord): string => { } ${Duration.toSeconds(self.expire)} ${Duration.toSeconds(self.minimum)}` case "SRV": return `SRV ${self.priority} ${self.weight} ${self.port} ${self.target}` + case "TLSA": + return `TLSA ${self.certUsage} ${self.selector} ${self.matchingType} ${Hex.encode(self.data).toUpperCase()}` case "TXT": return `TXT ${self.chunks.map(quote).join(" ")}` } diff --git a/packages/effect/test/net/Dns.test.ts b/packages/effect/test/net/Dns.test.ts index d91b5652e5..003bfd8dc1 100644 --- a/packages/effect/test/net/Dns.test.ts +++ b/packages/effect/test/net/Dns.test.ts @@ -10,6 +10,8 @@ const name = Host.domainNameFromStringUnsafe const srv = (target: string, priority: number, weight: number) => Dns.makeRecordUnsafe("SRV", { target: name(target), port: 5432, priority, weight }) +const tlsa = (data: Uint8Array) => Dns.makeRecordUnsafe("TLSA", { certUsage: 3, selector: 1, matchingType: 1, data }) + describe("Dns", () => { describe("records", () => { it("rejects invalid fields", () => { @@ -21,6 +23,21 @@ describe("Dns", () => { assert.isTrue( Result.isFailure(Dns.makeRecord("A", { address: ip("2001:db8::1") as NetAddress.Ipv4Address })) ) + assert.isTrue( + Result.isFailure( + Dns.makeRecord("TLSA", { certUsage: 256, selector: 1, matchingType: 1, data: new Uint8Array([1]) }) + ) + ) + assert.isTrue( + Result.isFailure(Dns.makeRecord("TLSA", { certUsage: 3, selector: 1, matchingType: 1, data: new Uint8Array() })) + ) + }) + + it("copies TLSA data", () => { + const data = new Uint8Array([0xab, 0xcd]) + const record = tlsa(data) + data[0] = 0 + assert.deepStrictEqual(record.data, new Uint8Array([0xab, 0xcd])) }) it("formats records in presentation format", () => { @@ -33,6 +50,7 @@ describe("Dns", () => { Dns.formatRecord(Dns.makeRecordUnsafe("PTR", { host: "Office Printer._ipp._tcp.local." })), "PTR Office\\032Printer._ipp._tcp.local." ) + assert.strictEqual(Dns.formatRecord(tlsa(new Uint8Array([0x0a, 0xbc, 0xde]))), "TLSA 3 1 1 0ABCDE") }) it("implements equality and hashing", () => { @@ -40,6 +58,10 @@ describe("Dns", () => { assert.isTrue(Equal.equals(a, srv("db.internal", 10, 5))) assert.strictEqual(Hash.hash(a), Hash.hash(srv("db.internal", 10, 5))) assert.isFalse(Equal.equals(a, srv("db.internal", 10, 6))) + const b = tlsa(new Uint8Array([1, 2])) + assert.isTrue(Equal.equals(b, tlsa(new Uint8Array([1, 2])))) + assert.strictEqual(Hash.hash(b), Hash.hash(tlsa(new Uint8Array([1, 2])))) + assert.isFalse(Equal.equals(b, tlsa(new Uint8Array([1, 3])))) }) it("builds reverse lookup names", () => { @@ -74,6 +96,11 @@ describe("Dns", () => { assert.deepStrictEqual(Schema.encodeSync(codec)(soa), json) assert.isTrue(Equal.equals(Schema.decodeUnknownSync(codec)(json), soa)) assert.throws(() => Schema.decodeUnknownSync(codec)({ _tag: "CNAME", target: "Example.com" })) + const record = tlsa(new Uint8Array([0x0a, 0xbc])) + const tlsaJson = { _tag: "TLSA", certUsage: 3, selector: 1, matchingType: 1, data: "0abc" } + assert.deepStrictEqual(Schema.encodeSync(codec)(record), tlsaJson) + assert.isTrue(Equal.equals(Schema.decodeUnknownSync(codec)(tlsaJson), record)) + assert.throws(() => Schema.decodeUnknownSync(codec)({ ...tlsaJson, data: "" })) }) }) diff --git a/packages/effect/typetest/net/Dns.tst.ts b/packages/effect/typetest/net/Dns.tst.ts index 057699b5b3..2d5763e513 100644 --- a/packages/effect/typetest/net/Dns.tst.ts +++ b/packages/effect/typetest/net/Dns.tst.ts @@ -21,6 +21,9 @@ describe("Dns", () => { expect(dns.resolve("db.example.com", "SRV")).type.toBe< Effect.Effect, Dns.DnsError> >() + expect(dns.resolve("_443._tcp.example.com", "TLSA")).type.toBe< + Effect.Effect, Dns.DnsError> + >() }) it("keeps platform inputs and stored record names branded", () => { diff --git a/packages/platform/bun/src/BunDns.ts b/packages/platform/bun/src/BunDns.ts index db9afc75db..0be4a30cf6 100644 --- a/packages/platform/bun/src/BunDns.ts +++ b/packages/platform/bun/src/BunDns.ts @@ -14,6 +14,9 @@ * a record made of several strings arrives as several TXT records whose chunks * cannot be reassembled (https://github.com/oven-sh/bun/issues/44692). * + * Bun's resolver cannot query TLSA records, so TLSA queries fail with + * `Unsupported`. + * * @stability experimental * @since 4.0.0 */ diff --git a/packages/platform/deno/src/DenoDns.ts b/packages/platform/deno/src/DenoDns.ts index 0fcdf9d9ac..367a1faf34 100644 --- a/packages/platform/deno/src/DenoDns.ts +++ b/packages/platform/deno/src/DenoDns.ts @@ -14,8 +14,9 @@ * same error as a missing name, so refused queries and server failures * (`REFUSED`, `SERVFAIL`, `FORMERR`, `NOTIMP`) fail with `NotFound` instead of * `Refused`, `ServerFailure`, `InvalidResponse`, or `Unsupported`. Server - * failures are therefore not reported as temporary. Queries require the - * `--allow-net` permission. + * failures are therefore not reported as temporary. `Deno.resolveDns` cannot + * query TLSA records, so TLSA queries fail with `Unsupported`. Queries require + * the `--allow-net` permission. * * @stability experimental * @since 4.0.0 @@ -28,7 +29,7 @@ import * as Effect from "effect/Effect" import * as Layer from "effect/Layer" import * as Dns from "effect/net/Dns" import * as NetAddress from "effect/net/NetAddress" -import type * as Result from "effect/Result" +import * as Result from "effect/Result" /** * Options for the Deno `Dns` service. @@ -132,6 +133,8 @@ const queries: { priority: srv.priority, weight: srv.weight })), + TLSA: async (name) => + Result.fail(new Dns.DnsError({ reason: "Unsupported", method: "resolve", hostname: name, recordType: "TLSA" })), TXT: async (name, options) => NodeDns.recordsFromResolver(name, "TXT", await Deno.resolveDns(name, "TXT", options), (chunks) => ({ chunks: chunks.map(NodeDns.utf8FromLatin1) as unknown as Arr.NonEmptyReadonlyArray diff --git a/packages/platform/node-shared/src/NodeDns.ts b/packages/platform/node-shared/src/NodeDns.ts index f7deb6cbdb..60673abf9c 100644 --- a/packages/platform/node-shared/src/NodeDns.ts +++ b/packages/platform/node-shared/src/NodeDns.ts @@ -8,8 +8,10 @@ * * Unrepresentable records, such as invalid `Host.DomainName` values, are * skipped. If every record is skipped, the query fails with `InvalidResponse`. - * `make` decodes Node.js's Latin-1 TXT and CAA strings as UTF-8. Other runtimes - * reuse `lookup`, `resolver`, and the conversion helpers to build their services. + * `make` decodes Node.js's Latin-1 TXT and CAA strings as UTF-8. TLSA queries + * need Node.js 22.15 or 23.9 and later, and fail with `Unsupported` on runtimes + * without `resolveTlsa`. Other runtimes reuse `lookup`, `resolver`, and the + * conversion helpers to build their services. * * @stability experimental * @since 4.0.0 @@ -305,6 +307,18 @@ const queries: { priority: srv.priority, weight: srv.weight })), + // `resolveTlsa` was added in Node.js 22.15 and 23.9; Bun and Deno lack it. + TLSA: async (resolver, name) => + typeof resolver.resolveTlsa === "function" + ? recordsFromResolver(name, "TLSA", await resolver.resolveTlsa(name), (tlsa) => ({ + certUsage: tlsa.certUsage, + selector: tlsa.selector, + matchingType: tlsa.match, + data: new Uint8Array(tlsa.data) + })) + : Result.fail( + new Dns.DnsError({ reason: "Unsupported", method: "resolve", hostname: name, recordType: "TLSA" }) + ), TXT: async (resolver, name) => recordsFromResolver(name, "TXT", await resolver.resolveTxt(name), (chunks) => ({ chunks: chunks as unknown as Arr.NonEmptyReadonlyArray diff --git a/packages/platform/node-shared/test/Dns.test-utils.ts b/packages/platform/node-shared/test/Dns.test-utils.ts index a7cf68ee31..c9955c8bc1 100644 --- a/packages/platform/node-shared/test/Dns.test-utils.ts +++ b/packages/platform/node-shared/test/Dns.test-utils.ts @@ -1,10 +1,12 @@ import { assert, describe, it } from "@effect/vitest" import * as Duration from "effect/Duration" import * as Effect from "effect/Effect" +import * as Hex from "effect/encoding/Hex" import * as Equal from "effect/Equal" import * as Dns from "effect/net/Dns" import * as Host from "effect/net/Host" import * as NetAddress from "effect/net/NetAddress" +import * as Result from "effect/Result" import type * as Scope from "effect/Scope" import * as NodeDnsApi from "node:dns" import * as Fs from "node:fs/promises" @@ -52,6 +54,14 @@ const expected: { readonly [K in Dns.RecordType]: ReadonlyArray }) ], SRV: [Dns.makeRecordUnsafe("SRV", { target: name("db1.example.test."), port: 5432, priority: 10, weight: 5 })], + TLSA: [ + Dns.makeRecordUnsafe("TLSA", { + certUsage: 3, + selector: 1, + matchingType: 1, + data: Result.getOrThrow(Hex.decode("38a88126a15ae8e643ce9447c3ce9a874ea0e05255d07ee12227809edbe5c7f1")) + }) + ], TXT: [Dns.makeRecordUnsafe("TXT", { chunks: ["v=spf1 ", "-all"] })] } @@ -98,6 +108,7 @@ $TTL 300 ns1 IN A 192.0.2.53 www IN CNAME example.test. _pg._tcp IN SRV 10 5 5432 db1.example.test. +_443._tcp IN TLSA 3 1 1 38a88126a15ae8e643ce9447c3ce9a874ea0e05255d07ee12227809edbe5c7f1 bad-mx IN MX 10 bad\\032host.example.test. ` @@ -216,6 +227,19 @@ export const describeDnsServer = ( assertRecords(yield* resolver.resolve(name("_pg._tcp.example.test"), "SRV"), expected.SRV) })) + // Bun's resolver and `Deno.resolveDns` cannot query TLSA records. + it.effect("queries TLSA records where the runtime supports them", () => + Effect.gen(function*() { + const query = (yield* dns()).resolve(name("_443._tcp.example.test"), "TLSA") + if (isBun || isDeno) { + const error = yield* Effect.flip(query) + assert.strictEqual(error.reason, "Unsupported") + assert.strictEqual(error.recordType, "TLSA") + } else { + assertRecords(yield* query, expected.TLSA) + } + })) + // Bun returns each character string of a TXT record as a separate record, // so the chunks of a record cannot be reassembled: // https://github.com/oven-sh/bun/issues/44692