diff --git a/CHANGELOG.md b/CHANGELOG.md index a69190e..31cbb62 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 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 diff --git a/lib/zone_record/store/base.js b/lib/zone_record/store/base.js index 281685b..e422c30 100644 --- a/lib/zone_record/store/base.js +++ b/lib/zone_record/store/base.js @@ -82,25 +82,41 @@ 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. +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 { + return packed.find((f) => asText(copy[f]) !== asText(record[f])) ?? packed[0] + } + return packed.find((f) => asText(stored.get(f)) !== asText(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..b194dc9 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,46 @@ 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('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" }), { + code: 'STORE_INVALID', + 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/, + }) + }) })