diff --git a/CHANGELOG.md b/CHANGELOG.md index 31cbb62..01877f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/). ### Unreleased +- fix(json, toml): zone record create checks the record, and an unchanged PUT answers false, as on mysql - 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 diff --git a/lib/zone_record/store/file.js b/lib/zone_record/store/file.js index fe4cf1f..0a38e16 100644 --- a/lib/zone_record/store/file.js +++ b/lib/zone_record/store/file.js @@ -1,7 +1,11 @@ +import { isDeepStrictEqual } from 'node:util' + import FileStore, { claimId, destroyMatch, removeRows } from '../../store/file.js' import { idConflict } from '../../store/error.js' -import ZoneRecordBase, { checkPut, mergeChanges } from './base.js' +import ZoneRecordBase, { checkPut, checkRecord, mergeChanges } from './base.js' + +const plain = (record) => JSON.parse(JSON.stringify(record)) class ZoneRecordRepoFile extends ZoneRecordBase { constructor(args = {}) { @@ -21,8 +25,9 @@ class ZoneRecordRepoFile extends ZoneRecordBase { return keep(idConflict('zone record', args.id, options)) } - args.id = claimId(records, data, args.id) if (args.ttl === undefined) args.ttl = 0 + checkRecord(args) + args.id = claimId(records, data, args.id) records.push(args) return args.id @@ -99,6 +104,8 @@ class ZoneRecordRepoFile extends ZoneRecordBase { if (idx === -1) return false const target = mergeChanges(records[idx], args) + // smol-toml parses tables without a prototype, so compare plain copies + if (isDeepStrictEqual(plain(target), plain(records[idx]))) return false await checkPut(records[idx], target, args) records[idx] = target return true diff --git a/lib/zone_record/test/index.js b/lib/zone_record/test/index.js index 117f99a..667ec52 100644 --- a/lib/zone_record/test/index.js +++ b/lib/zone_record/test/index.js @@ -34,6 +34,19 @@ describe('zone_record', function () { } }) + it('CREATE refuses a record the type cannot hold', async () => { + const base = { id: 60002, zid: 4096, owner: 'invalid.example.com.', ttl: 300 } + await ZoneRecord.destroy({ id: base.id }) + for (const record of [ + { ...base, type: 'A', address: '1.2.3' }, + { ...base, type: 'MX' }, + { ...base, owner: 'invalid', type: 'A', address: '203.0.113.45' }, + ]) { + await assert.rejects(ZoneRecord.create(record), { code: 'STORE_INVALID' }) + } + assert.deepEqual(await ZoneRecord.get({ id: base.id }), []) + }) + for (const rrType of fs.readdirSync('lib/zone_record/test/rrs')) { // if (rrType !== 'tlsa.json') continue describe(`${path.basename(rrType, '.json').toUpperCase()}`, function () { @@ -81,6 +94,11 @@ describe('zone_record', function () { assert.ok(await ZoneRecord.put({ id: testCase.id, ttl: testCase.ttl })) }) + it('PUT answers false when nothing changes', async () => { + assert.equal(await ZoneRecord.put({ id: testCase.id, ttl: testCase.ttl }), false) + assert.equal(await ZoneRecord.put({ ...testCase }), false) + }) + describe('DELETE', async () => { it('deletes a zr', async () => { assert.ok(await ZoneRecord.delete({ id: testCase.id })) diff --git a/lib/zone_record/test/mysql.js b/lib/zone_record/test/mysql.js index b9463a5..1b7a29b 100644 --- a/lib/zone_record/test/mysql.js +++ b/lib/zone_record/test/mysql.js @@ -61,10 +61,6 @@ describe('zone_record (mysql)', function () { 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' }) - }) - it('leaves fields the new type does not map out of the update', async () => { assert.ok(await ZoneRecord.put({ id, type: 'A', address: '203.0.113.11', port: 443 })) const [zr] = await ZoneRecord.get({ id })