From 5a68e6503ee1f9c2ad33b5fcedfe7f6f9137e4c3 Mon Sep 17 00:00:00 2001 From: Abraham Ingersoll <586805+aberoham@users.noreply.github.com> Date: Sun, 15 Nov 2026 19:24:41 +0530 Subject: [PATCH 1/4] zone_record: PUT checks the rdata it changes PUT only checked the merged record when the type or a packed field changed, so an A record took address 1.2.3. A PUT carrying any rdata field now runs the same check as create() on the merged record. A PUT without one, such as a ttl edit, is not checked. A record that fails the check, such as one from v2 or an earlier PUT, still takes a ttl-only edit. Any PUT carrying its rdata, a whole record sent back included, is refused unless it leaves the record valid. json and toml create() skip the check, and PUT writes an owner without it, so both can still store such a record. checkRecord required an address of every type without a column map, such as RRSIG and SIG. json and toml store those types, so the wider check would have refused every rdata edit to them. It now requires a main field only of a type that has one. mysql has no columns for those types, and refuses them by name. The check replaced the owner with a placeholder, so an RRSIG signed by its own owner failed. An absolute owner is now checked as given, as create() does. The mysql store dropped an SRV priority of 0 when reading, so a valid SRV read back invalid and every rdata edit to it would be refused. It now keeps that 0, as json and toml do. --- CHANGELOG.md | 3 ++ lib/zone_record/store/base.js | 23 ++++++++----- lib/zone_record/store/file.js | 2 +- lib/zone_record/store/mysql.js | 17 +++++++--- lib/zone_record/test/index.js | 60 ++++++++++++++++++++++++++++++++++ lib/zone_record/test/mysql.js | 33 +++++++++++++++++-- routes/zone_record.test.js | 12 +++++++ 7 files changed, 134 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4898566..a69190e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/). ### Unreleased +- 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 - fix: a user created without names reads back, lists and logs in on mysql - fix: file stores draw permission ids from one sequence, and renumber repeated ids at startup - fix: file store user, group and permission reads hide revoked permissions, and a regrant gets a new id diff --git a/lib/zone_record/store/base.js b/lib/zone_record/store/base.js index 5db0516..bd550c7 100644 --- a/lib/zone_record/store/base.js +++ b/lib/zone_record/store/base.js @@ -95,18 +95,23 @@ export function checkRecord(record) { 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 = RR.getMap(record.type)?.address ?? 'address' + const main = + RR.getMap(record.type)?.address ?? (rdataFields(record.type).includes('address') ? 'address' : undefined) if (typeof main === 'string' && [undefined, null, ''].includes(record[main])) { throw new StoreInvalidError(`${record.type}: ${main} is required`) } } -// A type change, or a change to fields packed into one column, must leave a -// valid record. The placeholder owner lets a legacy relative owner through. -export function checkPut(fromType, record, changes) { - const packed = Object.values(RR.getMap(record.type) ?? {}) - .filter(Array.isArray) - .flat() - if (record.type === fromType && !packed.some((f) => f in changes)) return - checkRecord({ ...record, owner: 'put.invalid.', ttl: record.ttl ?? 0 }) +// The placeholder owner lets a legacy relative owner through. +const asStored = (record) => ({ + ...record, + owner: record.owner?.endsWith('.') ? record.owner : 'put.invalid.', + ttl: record.ttl ?? 0, +}) + +// A type change, or any rdata field sent, must leave a valid record. +export function checkPut(current, record, changes) { + if (record.type !== current.type || rdataFields(record.type).some((f) => f in changes)) { + checkRecord(asStored(record)) + } } diff --git a/lib/zone_record/store/file.js b/lib/zone_record/store/file.js index 0f2fd14..88d5785 100644 --- a/lib/zone_record/store/file.js +++ b/lib/zone_record/store/file.js @@ -99,7 +99,7 @@ class ZoneRecordRepoFile extends ZoneRecordBase { if (idx === -1) return false const target = mergeChanges(records[idx], args) - checkPut(records[idx].type, target, args) + checkPut(records[idx], target, args) records[idx] = target return true }) diff --git a/lib/zone_record/store/mysql.js b/lib/zone_record/store/mysql.js index c35123a..52a3b62 100644 --- a/lib/zone_record/store/mysql.js +++ b/lib/zone_record/store/mysql.js @@ -1,9 +1,9 @@ import * as RR from '@nictool/dns-resource-record' import { applyMap, getMap, unApplyMap } from '@nictool/dns-resource-record' -import ZoneRecordBase, { checkPut, checkRecord, mergeChanges } from './base.js' +import ZoneRecordBase, { checkPut, checkRecord, mergeChanges, rdataFields } from './base.js' import Mysql from '../../mysql.js' -import { idConflict, StoreConflictError } from '../../store/error.js' +import { idConflict, StoreConflictError, StoreInvalidError } from '../../store/error.js' import { mapToDbColumn } from '../../util.js' const zrDbMap = { id: 'nt_zone_record_id', zid: 'nt_zone_id', owner: 'name' } @@ -24,7 +24,14 @@ const columns = new Set([ ]) const boolFields = ['deleted'] const keepZeroWeightFor = new Set(['SRV', 'URI']) -const keepZeroPriorityFor = new Set(['HTTPS', 'SVCB', 'URI']) +const keepZeroPriorityFor = new Set(['HTTPS', 'SRV', 'SVCB', 'URI']) + +// a type with no rdata in the address column has no columns here to hold it +function checkStorable(type) { + if (getMap(type)?.address === undefined && !rdataFields(type).includes('address')) { + throw new StoreInvalidError(`${type} records can't be stored in mysql`) + } +} const sortByColumn = { id: 'nt_zone_record_id', owner: 'name', type: 'type_id', ttl: 'ttl' } @@ -49,6 +56,7 @@ class ZoneRecordMySQL extends ZoneRecordBase { } checkRecord(args.ttl === undefined ? { ...args, default: { ttl: 0 } } : args) + checkStorable(args.type) args = objectToDb(args) @@ -146,7 +154,8 @@ class ZoneRecordMySQL extends ZoneRecordBase { const type = changes.type ?? current.type const target = mergeChanges(current, changes) - checkPut(current.type, target, changes) + checkPut(current, target, changes) + if (type !== current.type) checkStorable(type) const before = objectToDb(current) // Several fields can share a column, so pack the whole merged record. const after = objectToDb(target) diff --git a/lib/zone_record/test/index.js b/lib/zone_record/test/index.js index 88ef020..88bb90e 100644 --- a/lib/zone_record/test/index.js +++ b/lib/zone_record/test/index.js @@ -2,9 +2,11 @@ import fs from 'node:fs' import path from 'node:path' import assert from 'node:assert/strict' +import * as RR from '@nictool/dns-resource-record' import { describe, it, after, before } from 'node:test' import ZoneRecord from '../index.js' +import { storeType } from '../../config.js' after(async () => { // await ZoneRecord.destroy({ id: testCase.id }) @@ -165,9 +167,57 @@ describe('zone_record', function () { await ZoneRecord.destroy({ id: base.id }) await ZoneRecord.create(soa) await assert.rejects(ZoneRecord.put({ id: base.id, serial: 'abc' }), { code: 'STORE_INVALID' }) + await assert.rejects(ZoneRecord.put({ id: base.id, serial: [soa.serial] }), { code: 'STORE_INVALID' }) assert.equal((await ZoneRecord.get({ id: base.id }))[0].serial, soa.serial) }) + it('takes a whole SOA sent back with only a new ttl', async () => { + const [stored] = await ZoneRecord.get({ id: base.id }) + assert.ok(await ZoneRecord.put({ ...stored, ttl: stored.ttl + 1 })) + assert.equal((await ZoneRecord.get({ id: base.id }))[0].ttl, stored.ttl + 1) + }) + + it('refuses an address the type cannot hold', async () => { + await ZoneRecord.destroy({ id: base.id }) + await ZoneRecord.create({ ...base, type: 'A', address: '203.0.113.7' }) + await assert.rejects(ZoneRecord.put({ id: base.id, address: '1.2.3' }), { code: 'STORE_INVALID' }) + assert.equal((await ZoneRecord.get({ id: base.id }))[0].address, '203.0.113.7') + }) + + it('takes the whole record sent back with only a new ttl', async () => { + await ZoneRecord.destroy({ id: base.id }) + await ZoneRecord.create({ ...base, type: 'A', address: '203.0.113.7' }) + const [stored] = await ZoneRecord.get({ id: base.id }) + assert.ok(await ZoneRecord.put({ ...stored, ttl: 600 })) + assert.equal((await ZoneRecord.get({ id: base.id }))[0].ttl, 600) + }) + + it('reads back an SRV with priority 0 and refuses an IP as its target', async () => { + const srv = { ...base, type: 'SRV', priority: 0, weight: 0, port: 5060, target: 'sip.example.com.' } + await ZoneRecord.destroy({ id: base.id }) + await ZoneRecord.create(srv) + const [stored] = await ZoneRecord.get({ id: base.id }) + assert.equal(stored.priority, 0) + assert.equal(stored.weight, 0) + await assert.rejects(ZoneRecord.put({ id: base.id, target: '1.2.3.4' }), { code: 'STORE_INVALID' }) + assert.equal((await ZoneRecord.get({ id: base.id }))[0].target, 'sip.example.com.') + }) + + it('checks an RRSIG edit against the real owner', async () => { + const rrsig = { ...new RR.RRSIG(null).getCanonical(), id: base.id, zid: base.zid } + delete rrsig.class + await ZoneRecord.destroy({ id: base.id }) + if (storeType() === 'mysql' && RR.getMap('RRSIG').address === undefined) { + await assert.rejects(ZoneRecord.create(rrsig), /can't be stored in mysql/) + return + } + await ZoneRecord.create(rrsig) + assert.ok(await ZoneRecord.put({ id: base.id, signature: 'BAUG' })) + await assert.rejects(ZoneRecord.put({ id: base.id, 'signers name': 'other.example.' }), { + code: 'STORE_INVALID', + }) + }) + it('takes a NAPTR with an empty regexp', async () => { const [before] = await ZoneRecord.get({ id: naptr.id }) const { regexp, ...rest } = before @@ -179,6 +229,16 @@ describe('zone_record', function () { await ZoneRecord.create(naptr) }) + it('refuses the text "undefined" for a field the record lacks', async () => { + if (storeType() === 'mysql') return // mysql create refuses a NAPTR without flags + const { flags, ...rest } = naptr + await ZoneRecord.destroy({ id: naptr.id }) + await ZoneRecord.create(rest) + await assert.rejects(ZoneRecord.put({ id: naptr.id, flags: 'undefined' }), { code: 'STORE_INVALID' }) + await ZoneRecord.destroy({ id: naptr.id }) + await ZoneRecord.create({ ...rest, flags }) + }) + it('refuses a NAPTR field holding the column delimiter', async () => { const [before] = await ZoneRecord.get({ id: naptr.id }) await assert.rejects(ZoneRecord.put({ id: naptr.id, service: "SIP+D2U','x" }), { diff --git a/lib/zone_record/test/mysql.js b/lib/zone_record/test/mysql.js index ed174e2..d8a1589 100644 --- a/lib/zone_record/test/mysql.js +++ b/lib/zone_record/test/mysql.js @@ -1,11 +1,29 @@ import assert from 'node:assert/strict' import { describe, it, before, after } from 'node:test' +import * as RR from '@nictool/dns-resource-record' + import ZoneRecord from '../index.js' +import { checkRecord, rdataFields } from '../store/base.js' const id = 60303 const record = { id, zid: 4096, owner: 'legacy.example.com.', ttl: 300, type: 'A', address: '203.0.113.10' } +// valid rdata of a type the library maps nothing into address for, while one is left +function unheldRdata() { + for (const type of Object.keys(RR.typeMap).filter((t) => isNaN(t) && RR[t])) { + if (RR.getMap(type)?.address !== undefined || rdataFields(type).includes('address')) continue + const rdata = new RR[type](null).getCanonical() + for (const f of ['owner', 'ttl', 'class']) delete rdata[f] + try { + checkRecord({ ...rdata, owner: record.owner, ttl: record.ttl }) + return rdata + } catch { + // a type its own rules refuse here can't show the mysql refusal + } + } +} + before(async () => { await ZoneRecord.destroy({ id }) await ZoneRecord.create(record) @@ -25,6 +43,12 @@ describe('zone_record (mysql)', function () { assert.equal(zr.preference, 10) }) + it('refuses a type change into a type mysql cannot hold', async (t) => { + const rdata = unheldRdata() + if (!rdata) return t.skip('every type has a column map') + await assert.rejects(ZoneRecord.put({ id, ...rdata }), /can't be stored in mysql/) + }) + it('refuses a create with incomplete rdata as invalid', async () => { await assert.rejects(ZoneRecord.create({ ...record, id: id + 1, type: 'MX' }), { code: 'STORE_INVALID' }) }) @@ -76,12 +100,17 @@ describe('zone_record (mysql)', function () { assert.equal(zr.params, undefined) }) - it('edits a legacy SRV whose priority is NULL', async () => { + it('edits a stored SRV whose priority is NULL only into a valid record', async () => { await ZoneRecord.put({ id, type: 'SRV', priority: 5, weight: 5, port: 5060, target: 'sip.example.com.' }) await ZoneRecord.mysql.execute('UPDATE nt_zone_record SET priority = NULL WHERE nt_zone_record_id = ?', [ id, ]) - assert.ok(await ZoneRecord.put({ id, port: 5061 })) + const [stored] = await ZoneRecord.get({ id }) + assert.ok(await ZoneRecord.put({ id, ttl: stored.ttl + 1 })) + assert.equal((await ZoneRecord.get({ id }))[0].ttl, stored.ttl + 1) + await assert.rejects(ZoneRecord.put({ ...stored, ttl: stored.ttl + 2 }), { code: 'STORE_INVALID' }) + await assert.rejects(ZoneRecord.put({ id, port: 5061 }), { code: 'STORE_INVALID' }) + assert.ok(await ZoneRecord.put({ id, priority: 5, port: 5061 })) assert.equal((await ZoneRecord.get({ id }))[0].port, 5061) }) diff --git a/routes/zone_record.test.js b/routes/zone_record.test.js index 5e77e1a..9f4ec62 100644 --- a/routes/zone_record.test.js +++ b/routes/zone_record.test.js @@ -219,6 +219,18 @@ describe('zone_record routes', () => { assert.equal((await ZoneRecord.get({ id: testZoneRecordId }))[0].type, 'A') }) + it(`PUT /zone_record/${testZoneRecordId} refuses an address the type can't hold`, async () => { + const res = await server.inject({ + method: 'PUT', + url: `/zone_record/${testZoneRecordId}`, + headers: auth.headers, + payload: { address: '1.2.3' }, + }) + + assert.equal(res.statusCode, 400) + assert.equal((await ZoneRecord.get({ id: testZoneRecordId }))[0].address, '198.51.100.99') + }) + it(`PUT /zone_record/${testZoneRecordId} refuses a TXT with no data`, async () => { const res = await server.inject({ method: 'PUT', From f3466abf5438db0de1980e32f20bff3a6e0820ba Mon Sep 17 00:00:00 2001 From: Abraham Ingersoll <586805+aberoham@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:40:34 +0530 Subject: [PATCH 2/4] zone_record: send the RRSIG test record as the API takes it The schema takes the covered type as text, so the test sends 'A' and the times as stamps. The test then passes before and after dns-resource-record maps RRSIG into mysql's columns. --- lib/zone_record/test/index.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/zone_record/test/index.js b/lib/zone_record/test/index.js index 88bb90e..27e675f 100644 --- a/lib/zone_record/test/index.js +++ b/lib/zone_record/test/index.js @@ -204,7 +204,15 @@ describe('zone_record', function () { }) it('checks an RRSIG edit against the real owner', async () => { - const rrsig = { ...new RR.RRSIG(null).getCanonical(), id: base.id, zid: base.zid } + // as the API takes it: the covered type and times as text + const rrsig = { + ...new RR.RRSIG(null).getCanonical(), + 'type covered': 'A', + 'signature expiration': '20030212123200', + 'signature inception': '20030113123200', + id: base.id, + zid: base.zid, + } delete rrsig.class await ZoneRecord.destroy({ id: base.id }) if (storeType() === 'mysql' && RR.getMap('RRSIG').address === undefined) { From a2b3b9ff617ff07f8476ab5f714f2050c07c2c3a Mon Sep 17 00:00:00 2001 From: Abraham Ingersoll <586805+aberoham@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:03:31 +0530 Subject: [PATCH 3/4] zone_record: check only the rdata a PUT changes The web UI sends every rdata field on every save, so a TTL edit to a record stored invalid was refused. A field sent back as stored is no longer a change. One helper names a type's main field for checkRecord and mysql. The RRSIG and NAPTR checks are now unit tests of checkPut, so every store runs them. --- lib/zone_record/store/base.js | 17 ++++++++----- lib/zone_record/store/base.test.js | 41 ++++++++++++++++++++++++++++++ lib/zone_record/store/mysql.js | 4 +-- lib/zone_record/test/index.js | 35 ------------------------- lib/zone_record/test/mysql.js | 9 ++++--- routes/zone_record.test.js | 14 ++++++++++ 6 files changed, 74 insertions(+), 46 deletions(-) create mode 100644 lib/zone_record/store/base.test.js diff --git a/lib/zone_record/store/base.js b/lib/zone_record/store/base.js index bd550c7..437d926 100644 --- a/lib/zone_record/store/base.js +++ b/lib/zone_record/store/base.js @@ -1,3 +1,5 @@ +import { isDeepStrictEqual } from 'node:util' + import * as RR from '@nictool/dns-resource-record' import { StoreInvalidError } from '../../store/error.js' @@ -53,6 +55,11 @@ export function rdataFields(type) { return (RR[type]?.rdataFields ?? ['address']).map((f) => (Array.isArray(f) ? f[0] : f)) } +// the field a type's rdata can't do without, if it has one +export function mainField(type) { + return RR.getMap(type)?.address ?? (rdataFields(type).includes('address') ? 'address' : undefined) +} + // what a type change takes off the record: the old type's rdata, and the raw // columns a legacy row can hold without its type mapping them function retiredFields(type) { @@ -95,8 +102,7 @@ export function checkRecord(record) { 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 = - RR.getMap(record.type)?.address ?? (rdataFields(record.type).includes('address') ? 'address' : undefined) + const main = mainField(record.type) if (typeof main === 'string' && [undefined, null, ''].includes(record[main])) { throw new StoreInvalidError(`${record.type}: ${main} is required`) } @@ -109,9 +115,8 @@ const asStored = (record) => ({ ttl: record.ttl ?? 0, }) -// A type change, or any rdata field sent, must leave a valid record. +// A type change, or a change to any rdata field, must leave a valid record. export function checkPut(current, record, changes) { - if (record.type !== current.type || rdataFields(record.type).some((f) => f in changes)) { - checkRecord(asStored(record)) - } + const changed = (f) => f in changes && !isDeepStrictEqual(changes[f], current[f]) + if (record.type !== current.type || rdataFields(record.type).some(changed)) checkRecord(asStored(record)) } diff --git a/lib/zone_record/store/base.test.js b/lib/zone_record/store/base.test.js new file mode 100644 index 0000000..207e080 --- /dev/null +++ b/lib/zone_record/store/base.test.js @@ -0,0 +1,41 @@ +import fs from 'node:fs' + +import assert from 'node:assert/strict' +import { describe, it } from 'node:test' + +import * as RR from '@nictool/dns-resource-record' + +import { checkPut, mergeChanges } from './base.js' + +function put(current, changes) { + const target = mergeChanges(current, changes) + checkPut(current, target, changes) + return target +} + +describe('zone_record checkPut', () => { + // as the API takes it: the covered type and times as text + const rrsig = { + ...new RR.RRSIG(null).getCanonical(), + 'type covered': 'A', + 'signature expiration': '20030212123200', + 'signature inception': '20030113123200', + } + delete rrsig.class + + it('checks an RRSIG edit against its own owner', () => { + assert.equal(put(rrsig, { signature: 'BAUG' }).signature, 'BAUG') + assert.throws(() => put(rrsig, { 'signers name': 'other.example.' }), { code: 'STORE_INVALID' }) + }) + + it('takes rdata sent back as stored, even when it is invalid', () => { + const a = { id: 1, zid: 1, owner: 'a.example.com.', ttl: 300, type: 'A', address: '1.2.3' } + assert.equal(put(a, { ...a, ttl: 600 }).ttl, 600) + assert.throws(() => put(a, { ...a, address: '1.2.4' }), { code: 'STORE_INVALID' }) + }) + + it('refuses the text "undefined" for a field the record lacks', () => { + const { flags, ...naptr } = JSON.parse(fs.readFileSync('lib/zone_record/test/rrs/naptr.json')) + assert.throws(() => put(naptr, { flags: 'undefined' }), { code: 'STORE_INVALID' }) + }) +}) diff --git a/lib/zone_record/store/mysql.js b/lib/zone_record/store/mysql.js index 52a3b62..003ed20 100644 --- a/lib/zone_record/store/mysql.js +++ b/lib/zone_record/store/mysql.js @@ -1,6 +1,6 @@ import * as RR from '@nictool/dns-resource-record' import { applyMap, getMap, unApplyMap } from '@nictool/dns-resource-record' -import ZoneRecordBase, { checkPut, checkRecord, mergeChanges, rdataFields } from './base.js' +import ZoneRecordBase, { checkPut, checkRecord, mainField, mergeChanges } from './base.js' import Mysql from '../../mysql.js' import { idConflict, StoreConflictError, StoreInvalidError } from '../../store/error.js' @@ -28,7 +28,7 @@ const keepZeroPriorityFor = new Set(['HTTPS', 'SRV', 'SVCB', 'URI']) // a type with no rdata in the address column has no columns here to hold it function checkStorable(type) { - if (getMap(type)?.address === undefined && !rdataFields(type).includes('address')) { + if (mainField(type) === undefined) { throw new StoreInvalidError(`${type} records can't be stored in mysql`) } } diff --git a/lib/zone_record/test/index.js b/lib/zone_record/test/index.js index 27e675f..117f99a 100644 --- a/lib/zone_record/test/index.js +++ b/lib/zone_record/test/index.js @@ -2,11 +2,9 @@ import fs from 'node:fs' import path from 'node:path' import assert from 'node:assert/strict' -import * as RR from '@nictool/dns-resource-record' import { describe, it, after, before } from 'node:test' import ZoneRecord from '../index.js' -import { storeType } from '../../config.js' after(async () => { // await ZoneRecord.destroy({ id: testCase.id }) @@ -203,29 +201,6 @@ describe('zone_record', function () { assert.equal((await ZoneRecord.get({ id: base.id }))[0].target, 'sip.example.com.') }) - it('checks an RRSIG edit against the real owner', async () => { - // as the API takes it: the covered type and times as text - const rrsig = { - ...new RR.RRSIG(null).getCanonical(), - 'type covered': 'A', - 'signature expiration': '20030212123200', - 'signature inception': '20030113123200', - id: base.id, - zid: base.zid, - } - delete rrsig.class - await ZoneRecord.destroy({ id: base.id }) - if (storeType() === 'mysql' && RR.getMap('RRSIG').address === undefined) { - await assert.rejects(ZoneRecord.create(rrsig), /can't be stored in mysql/) - return - } - await ZoneRecord.create(rrsig) - assert.ok(await ZoneRecord.put({ id: base.id, signature: 'BAUG' })) - await assert.rejects(ZoneRecord.put({ id: base.id, 'signers name': 'other.example.' }), { - code: 'STORE_INVALID', - }) - }) - it('takes a NAPTR with an empty regexp', async () => { const [before] = await ZoneRecord.get({ id: naptr.id }) const { regexp, ...rest } = before @@ -237,16 +212,6 @@ describe('zone_record', function () { await ZoneRecord.create(naptr) }) - it('refuses the text "undefined" for a field the record lacks', async () => { - if (storeType() === 'mysql') return // mysql create refuses a NAPTR without flags - const { flags, ...rest } = naptr - await ZoneRecord.destroy({ id: naptr.id }) - await ZoneRecord.create(rest) - await assert.rejects(ZoneRecord.put({ id: naptr.id, flags: 'undefined' }), { code: 'STORE_INVALID' }) - await ZoneRecord.destroy({ id: naptr.id }) - await ZoneRecord.create({ ...rest, flags }) - }) - it('refuses a NAPTR field holding the column delimiter', async () => { const [before] = await ZoneRecord.get({ id: naptr.id }) await assert.rejects(ZoneRecord.put({ id: naptr.id, service: "SIP+D2U','x" }), { diff --git a/lib/zone_record/test/mysql.js b/lib/zone_record/test/mysql.js index d8a1589..0d996ea 100644 --- a/lib/zone_record/test/mysql.js +++ b/lib/zone_record/test/mysql.js @@ -4,7 +4,7 @@ import { describe, it, before, after } from 'node:test' import * as RR from '@nictool/dns-resource-record' import ZoneRecord from '../index.js' -import { checkRecord, rdataFields } from '../store/base.js' +import { checkRecord, mainField } from '../store/base.js' const id = 60303 const record = { id, zid: 4096, owner: 'legacy.example.com.', ttl: 300, type: 'A', address: '203.0.113.10' } @@ -12,7 +12,7 @@ const record = { id, zid: 4096, owner: 'legacy.example.com.', ttl: 300, type: 'A // valid rdata of a type the library maps nothing into address for, while one is left function unheldRdata() { for (const type of Object.keys(RR.typeMap).filter((t) => isNaN(t) && RR[t])) { - if (RR.getMap(type)?.address !== undefined || rdataFields(type).includes('address')) continue + if (mainField(type) !== undefined) continue const rdata = new RR[type](null).getCanonical() for (const f of ['owner', 'ttl', 'class']) delete rdata[f] try { @@ -108,7 +108,10 @@ describe('zone_record (mysql)', function () { const [stored] = await ZoneRecord.get({ id }) assert.ok(await ZoneRecord.put({ id, ttl: stored.ttl + 1 })) assert.equal((await ZoneRecord.get({ id }))[0].ttl, stored.ttl + 1) - await assert.rejects(ZoneRecord.put({ ...stored, ttl: stored.ttl + 2 }), { code: 'STORE_INVALID' }) + assert.ok(await ZoneRecord.put({ ...stored, ttl: stored.ttl + 2 })) + const [edited] = await ZoneRecord.get({ id }) + assert.equal(edited.ttl, stored.ttl + 2) + assert.equal(edited.priority, stored.priority) await assert.rejects(ZoneRecord.put({ id, port: 5061 }), { code: 'STORE_INVALID' }) assert.ok(await ZoneRecord.put({ id, priority: 5, port: 5061 })) assert.equal((await ZoneRecord.get({ id }))[0].port, 5061) diff --git a/routes/zone_record.test.js b/routes/zone_record.test.js index 9f4ec62..a9f1467 100644 --- a/routes/zone_record.test.js +++ b/routes/zone_record.test.js @@ -231,6 +231,20 @@ describe('zone_record routes', () => { assert.equal((await ZoneRecord.get({ id: testZoneRecordId }))[0].address, '198.51.100.99') }) + it(`PUT /zone_record/${testZoneRecordId} takes the record as the web UI sends it`, async () => { + const [stored] = await ZoneRecord.get({ id: testZoneRecordId }) + const { zid, owner, type, address } = stored + const res = await server.inject({ + method: 'PUT', + url: `/zone_record/${testZoneRecordId}`, + headers: auth.headers, + payload: { zid, owner, type, ttl: stored.ttl + 1, address }, + }) + + assert.equal(res.statusCode, 200) + assert.equal((await ZoneRecord.get({ id: testZoneRecordId }))[0].ttl, stored.ttl + 1) + }) + it(`PUT /zone_record/${testZoneRecordId} refuses a TXT with no data`, async () => { const res = await server.inject({ method: 'PUT', From 1645a1a6df931deb21e2b838f0b07394669e7542 Mon Sep 17 00:00:00 2001 From: Abraham Ingersoll <586805+aberoham@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:21:05 +0530 Subject: [PATCH 4/4] zone_record: check a relative owner under its zone json and toml store a relative owner as given, and v2 rows on mysql hold one too. The PUT check put the placeholder in its place. An RRSIG owned by www in example.com then failed its labels and signer checks on any signature edit. --- lib/zone_record/store/base.js | 23 +++++++++++++++-------- lib/zone_record/store/base.test.js | 28 ++++++++++++++++++---------- lib/zone_record/store/file.js | 4 ++-- lib/zone_record/store/mysql.js | 2 +- lib/zone_record/test/mysql.js | 12 ++++++++++++ 5 files changed, 48 insertions(+), 21 deletions(-) diff --git a/lib/zone_record/store/base.js b/lib/zone_record/store/base.js index 437d926..281685b 100644 --- a/lib/zone_record/store/base.js +++ b/lib/zone_record/store/base.js @@ -108,15 +108,22 @@ export function checkRecord(record) { } } -// The placeholder owner lets a legacy relative owner through. -const asStored = (record) => ({ - ...record, - owner: record.owner?.endsWith('.') ? record.owner : 'put.invalid.', - ttl: record.ttl ?? 0, -}) +async function zoneName(zid) { + const { default: Zone } = await import('../../zone/index.js') + return (await Zone.get({ id: zid }))[0]?.zone +} + +// A relative owner sits under its zone. The placeholder stands in when the zone is gone. +function asStored(record, zone) { + let owner = record.owner + if (!owner?.endsWith('.')) owner = zone ? `${owner}.${zone}.` : 'put.invalid.' + return { ...record, owner, ttl: record.ttl ?? 0 } +} // A type change, or a change to any rdata field, must leave a valid record. -export function checkPut(current, record, changes) { +export async function checkPut(current, record, changes, zoneOf = zoneName) { const changed = (f) => f in changes && !isDeepStrictEqual(changes[f], current[f]) - if (record.type !== current.type || rdataFields(record.type).some(changed)) checkRecord(asStored(record)) + if (record.type === current.type && !rdataFields(record.type).some(changed)) return + const zone = record.owner?.endsWith('.') ? undefined : await zoneOf(record.zid) + checkRecord(asStored(record, zone)) } diff --git a/lib/zone_record/store/base.test.js b/lib/zone_record/store/base.test.js index 207e080..374c72f 100644 --- a/lib/zone_record/store/base.test.js +++ b/lib/zone_record/store/base.test.js @@ -7,9 +7,9 @@ import * as RR from '@nictool/dns-resource-record' import { checkPut, mergeChanges } from './base.js' -function put(current, changes) { +async function put(current, changes, zone) { const target = mergeChanges(current, changes) - checkPut(current, target, changes) + await checkPut(current, target, changes, async () => zone) return target } @@ -23,19 +23,27 @@ describe('zone_record checkPut', () => { } delete rrsig.class - it('checks an RRSIG edit against its own owner', () => { - assert.equal(put(rrsig, { signature: 'BAUG' }).signature, 'BAUG') - assert.throws(() => put(rrsig, { 'signers name': 'other.example.' }), { code: 'STORE_INVALID' }) + it('checks an RRSIG edit against its own owner', async () => { + assert.equal((await put(rrsig, { signature: 'BAUG' })).signature, 'BAUG') + await assert.rejects(put(rrsig, { 'signers name': 'other.example.' }), { code: 'STORE_INVALID' }) }) - it('takes rdata sent back as stored, even when it is invalid', () => { + it('checks an RRSIG edit against a relative owner under its zone', async () => { + const www = { ...rrsig, owner: 'www', labels: 3 } + assert.equal((await put(www, { signature: 'BAUG' }, 'example.com')).signature, 'BAUG') + await assert.rejects(put(www, { 'signers name': 'other.example.' }, 'example.com'), { + code: 'STORE_INVALID', + }) + }) + + it('takes rdata sent back as stored, even when it is invalid', async () => { const a = { id: 1, zid: 1, owner: 'a.example.com.', ttl: 300, type: 'A', address: '1.2.3' } - assert.equal(put(a, { ...a, ttl: 600 }).ttl, 600) - assert.throws(() => put(a, { ...a, address: '1.2.4' }), { code: 'STORE_INVALID' }) + assert.equal((await put(a, { ...a, ttl: 600 })).ttl, 600) + await assert.rejects(put(a, { ...a, address: '1.2.4' }), { code: 'STORE_INVALID' }) }) - it('refuses the text "undefined" for a field the record lacks', () => { + it('refuses the text "undefined" for a field the record lacks', async () => { const { flags, ...naptr } = JSON.parse(fs.readFileSync('lib/zone_record/test/rrs/naptr.json')) - assert.throws(() => put(naptr, { flags: 'undefined' }), { code: 'STORE_INVALID' }) + await assert.rejects(put(naptr, { flags: 'undefined' }), { code: 'STORE_INVALID' }) }) }) diff --git a/lib/zone_record/store/file.js b/lib/zone_record/store/file.js index 88d5785..fe4cf1f 100644 --- a/lib/zone_record/store/file.js +++ b/lib/zone_record/store/file.js @@ -94,12 +94,12 @@ class ZoneRecordRepoFile extends ZoneRecordBase { async put(args) { if (!args.id) return false - return this.file.mutate('zone_record', (records) => { + return this.file.mutate('zone_record', async (records) => { const idx = records.findIndex((r) => r.id === args.id) if (idx === -1) return false const target = mergeChanges(records[idx], args) - checkPut(records[idx], target, args) + await checkPut(records[idx], target, args) records[idx] = target return true }) diff --git a/lib/zone_record/store/mysql.js b/lib/zone_record/store/mysql.js index 003ed20..ff259b0 100644 --- a/lib/zone_record/store/mysql.js +++ b/lib/zone_record/store/mysql.js @@ -154,7 +154,7 @@ class ZoneRecordMySQL extends ZoneRecordBase { const type = changes.type ?? current.type const target = mergeChanges(current, changes) - checkPut(current, target, changes) + await checkPut(current, target, changes) if (type !== current.type) checkStorable(type) const before = objectToDb(current) // Several fields can share a column, so pack the whole merged record. diff --git a/lib/zone_record/test/mysql.js b/lib/zone_record/test/mysql.js index 0d996ea..b9463a5 100644 --- a/lib/zone_record/test/mysql.js +++ b/lib/zone_record/test/mysql.js @@ -35,6 +35,18 @@ after(async () => { }) describe('zone_record (mysql)', function () { + it('edits a legacy relative owner, checked under its zone', async () => { + await ZoneRecord.mysql.execute('UPDATE nt_zone_record SET name = ? WHERE nt_zone_record_id = ?', [ + 'legacy', + id, + ]) + assert.ok(await ZoneRecord.put({ id, address: '203.0.113.11' })) + const [zr] = await ZoneRecord.get({ id }) + assert.equal(zr.owner, 'legacy') + assert.equal(zr.address, '203.0.113.11') + await ZoneRecord.put({ id, owner: record.owner, address: record.address }) + }) + it('drops a legacy priority on a type change', async () => { await ZoneRecord.mysql.execute('UPDATE nt_zone_record SET priority = 5 WHERE nt_zone_record_id = ?', [id]) assert.ok(await ZoneRecord.put({ id, type: 'MX', preference: 10, exchange: 'mail.example.com.' }))