From 591b76898bae734d2214302ccc2c60d01547eec0 Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Mon, 16 Nov 2026 18:14:18 -0500 Subject: [PATCH 1/4] zone_record: compare packed fields as the RR class reads them checkRecord required each packed field back from its shared column byte for byte. dns-resource-record #72 packs RRSIG as BIND text the way NicTool 2 stores it, which rewrites a type number as its mnemonic, times in seconds as YYYYMMDDHHmmSS stamps, and drops spaces from the signature. The api schema accepts all three, so such an RRSIG was refused as "can't be stored as given". Since #95, any PUT that changes rdata runs this check, and the UI sends every rdata field. Both the record and its packed round trip now go through the RR class, and their fields are compared. A value that splits differently, such as a NAPTR field holding the column delimiter, is still refused. Cross-package: needed before the dns-resource-record release with #72, which ^2.0.0 picks up. --- CHANGELOG.md | 1 + lib/zone_record/store/base.js | 40 ++++++++++++++++++++---------- lib/zone_record/store/base.test.js | 29 +++++++++++++++++++++- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a69190e..1d15cc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/). ### Unreleased +- fix: zone record checks compare packed fields as the record class reads them, so an RRSIG sent with a type number or times in seconds is taken - fix: zone record PUT checks the record when its rdata changes - fix(mysql): an SRV record with priority 0 reads back with its priority - fix: zone record checks require an address only of types that have one, and use the record's owner diff --git a/lib/zone_record/store/base.js b/lib/zone_record/store/base.js index 281685b..6de13d3 100644 --- a/lib/zone_record/store/base.js +++ b/lib/zone_record/store/base.js @@ -82,25 +82,39 @@ export function mergeChanges(current, changes) { return target } +// A packed field must come back out of its shared column as the same rdata. +// Packing may rewrite its text (RRSIG stores a type mnemonic and YYYYMMDDHHmmSS +// times), so compare what the RR class makes of each. +function garbledField(record, rr) { + const map = RR.getMap(record.type) + const packed = Object.values(map ?? {}) + .filter(Array.isArray) + .flat() + if (!packed.length) return + + const copy = { ...record } + RR.applyMap(copy, map) + RR.unApplyMap(copy, map) + let stored + try { + stored = new RR[record.type](copy) + } catch { + const changed = packed.find((f) => String(copy[f] ?? '') !== String(record[f] ?? '')) + return changed ?? packed[0] + } + return packed.find((f) => !isDeepStrictEqual(stored.get(f), rr.get(f))) +} + export function checkRecord(record) { if (RR.typeMap[record.type] === undefined) throw new StoreInvalidError(`unknown type ${record.type}`) + let rr try { - new RR[record.type](record) + rr = new RR[record.type](record) } catch (err) { throw new StoreInvalidError(err.message.split('\n')[0]) } - // packed fields must come back out of their shared column unchanged - const map = RR.getMap(record.type) - const packed = Object.values(map ?? {}) - .filter(Array.isArray) - .flat() - if (packed.length) { - const copy = { ...record } - RR.applyMap(copy, map) - RR.unApplyMap(copy, map) - const garbled = packed.find((f) => String(copy[f] ?? '') !== String(record[f] ?? '')) - if (garbled) throw new StoreInvalidError(`${record.type}: ${garbled} can't be stored as given`) - } + const garbled = garbledField(record, rr) + if (garbled) throw new StoreInvalidError(`${record.type}: ${garbled} can't be stored as given`) // dns-resource-record 1.x accepts an empty TXT data or CAA value, which no read accepts const main = mainField(record.type) if (typeof main === 'string' && [undefined, null, ''].includes(record[main])) { diff --git a/lib/zone_record/store/base.test.js b/lib/zone_record/store/base.test.js index 374c72f..64df72b 100644 --- a/lib/zone_record/store/base.test.js +++ b/lib/zone_record/store/base.test.js @@ -5,7 +5,7 @@ import { describe, it } from 'node:test' import * as RR from '@nictool/dns-resource-record' -import { checkPut, mergeChanges } from './base.js' +import { checkPut, checkRecord, mergeChanges } from './base.js' async function put(current, changes, zone) { const target = mergeChanges(current, changes) @@ -46,4 +46,31 @@ describe('zone_record checkPut', () => { const { flags, ...naptr } = JSON.parse(fs.readFileSync('lib/zone_record/test/rrs/naptr.json')) await assert.rejects(put(naptr, { flags: 'undefined' }), { code: 'STORE_INVALID' }) }) + + it('takes an RRSIG sent whole with a type number and times in seconds', async () => { + const rrsig = { ...new RR.RRSIG(null).getCanonical() } + delete rrsig.class + const later = { ...rrsig, 'signature expiration': rrsig['signature expiration'] + 86400 } + assert.equal((await put(rrsig, later))['signature expiration'], later['signature expiration']) + }) +}) + +describe('zone_record checkRecord', () => { + const rrsig = { ...new RR.RRSIG(null).getCanonical() } + delete rrsig.class + + it('takes packed fields that store as the same rdata', () => { + checkRecord(rrsig) + checkRecord({ ...rrsig, 'type covered': 'TYPE1' }) + const { signature } = rrsig + checkRecord({ ...rrsig, signature: `${signature.slice(0, 20)} ${signature.slice(20)}` }) + }) + + it('refuses packed fields that store as different rdata', () => { + const naptr = JSON.parse(fs.readFileSync('lib/zone_record/test/rrs/naptr.json')) + assert.throws(() => checkRecord({ ...naptr, service: "SIP+D2U','x" }), { + code: 'STORE_INVALID', + message: /NAPTR: service can't be stored as given/, + }) + }) }) From 53df1dfc54b0176cd31c728814dbf6e639c40666 Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Mon, 16 Nov 2026 18:14:56 -0500 Subject: [PATCH 2/4] changelog: shorten the RRSIG check entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d15cc3..31cbb62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/). ### Unreleased -- fix: zone record checks compare packed fields as the record class reads them, so an RRSIG sent with a type number or times in seconds is taken +- fix: zone record checks take an RRSIG with a type number or times in seconds - fix: zone record PUT checks the record when its rdata changes - fix(mysql): an SRV record with priority 0 reads back with its priority - fix: zone record checks require an address only of types that have one, and use the record's owner From bba1333addf14b984c92d8cb5fba8652d4014529 Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Mon, 16 Nov 2026 18:17:57 -0500 Subject: [PATCH 3/4] zone_record: a missing packed field matches an empty one The previous commit compared the RR class's values strictly, so a NAPTR created without a regexp, which unpacks as '', was refused on mysql. The byte comparison it replaced treated the two alike. --- lib/zone_record/store/base.js | 8 +++++--- lib/zone_record/store/base.test.js | 6 ++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/zone_record/store/base.js b/lib/zone_record/store/base.js index 6de13d3..e422c30 100644 --- a/lib/zone_record/store/base.js +++ b/lib/zone_record/store/base.js @@ -82,6 +82,9 @@ export function mergeChanges(current, changes) { return target } +// a missing field unpacks as an empty one +const asText = (v) => String(v ?? '') + // A packed field must come back out of its shared column as the same rdata. // Packing may rewrite its text (RRSIG stores a type mnemonic and YYYYMMDDHHmmSS // times), so compare what the RR class makes of each. @@ -99,10 +102,9 @@ function garbledField(record, rr) { try { stored = new RR[record.type](copy) } catch { - const changed = packed.find((f) => String(copy[f] ?? '') !== String(record[f] ?? '')) - return changed ?? packed[0] + return packed.find((f) => asText(copy[f]) !== asText(record[f])) ?? packed[0] } - return packed.find((f) => !isDeepStrictEqual(stored.get(f), rr.get(f))) + return packed.find((f) => asText(stored.get(f)) !== asText(rr.get(f))) } export function checkRecord(record) { diff --git a/lib/zone_record/store/base.test.js b/lib/zone_record/store/base.test.js index 64df72b..4be99cf 100644 --- a/lib/zone_record/store/base.test.js +++ b/lib/zone_record/store/base.test.js @@ -66,6 +66,12 @@ describe('zone_record checkRecord', () => { checkRecord({ ...rrsig, signature: `${signature.slice(0, 20)} ${signature.slice(20)}` }) }) + it('takes a NAPTR without a regexp, which reads back empty', () => { + const { regexp, ...naptr } = JSON.parse(fs.readFileSync('lib/zone_record/test/rrs/naptr.json')) + assert.ok(regexp) + checkRecord(naptr) + }) + it('refuses packed fields that store as different rdata', () => { const naptr = JSON.parse(fs.readFileSync('lib/zone_record/test/rrs/naptr.json')) assert.throws(() => checkRecord({ ...naptr, service: "SIP+D2U','x" }), { From 435d583ef77b30283cfde3cbc8e9326fd4aecee1 Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Mon, 16 Nov 2026 18:21:15 -0500 Subject: [PATCH 4/4] zone_record: test a packed field whose stored rdata no longer builds --- lib/zone_record/store/base.test.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/zone_record/store/base.test.js b/lib/zone_record/store/base.test.js index 4be99cf..b194dc9 100644 --- a/lib/zone_record/store/base.test.js +++ b/lib/zone_record/store/base.test.js @@ -79,4 +79,13 @@ describe('zone_record checkRecord', () => { message: /NAPTR: service can't be stored as given/, }) }) + + it('names the field when the stored rdata is no longer a valid record', () => { + const soa = { ...new RR.SOA(null).getCanonical(), rname: "a','b.example.com." } + delete soa.class + assert.throws(() => checkRecord(soa), { + code: 'STORE_INVALID', + message: /SOA: rname can't be stored as given/, + }) + }) })