// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2026 Bryan Joshua Pedini
import type { RecordType } from './records';
import type { Rng } from './rng';
export const SQL_INJECTION = "' OR '1'='1' -- ";
export const SCRIPT = '';
export const PAYLOADS = [
SQL_INJECTION,
SCRIPT,
`${SCRIPT}${SQL_INJECTION}`,
"x'); DROP TABLE users; --",
'" OR ""="',
'">
',
"admin'--",
'1 UNION SELECT username FROM users',
] as const;
export const AUDIT_FILTER_KEYS = ['zone', 'actor', 'operation', 'kind', 'outcome', 'before_id', 'limit'] as const;
export const QUERY_KEYS = [...AUDIT_FILTER_KEYS, 'deleted'] as const;
export interface FuzzRecord {
name: unknown;
type: RecordType;
ttl: unknown;
data: Record;
}
interface Field {
valid: readonly unknown[];
hostile: readonly unknown[];
}
const INTEGERS_HOSTILE: readonly unknown[] = [65536, 2147483648, -1, '10', 1.5, null, SQL_INJECTION];
const U16: Field = { valid: [0, 1, 10, 65535], hostile: INTEGERS_HOSTILE };
const TTL: Field = { valid: [0, 1, 300, 86400, 2147483647], hostile: [2147483648, -1, '300', 1.5, null, SQL_INJECTION] };
const TEXT: Field = {
valid: ["it's", 'a;b;c', 'bold', 'say "hi"', SQL_INJECTION, SCRIPT, 'plain text', 'x'.repeat(300), '">
'],
hostile: ['', 'back\\slash', 'tab\there', 'café', 'nul\u0000byte', 'x'.repeat(4097), 7, null],
};
const IPV4: Field = { valid: ['192.0.2.55', '198.51.100.7', '203.0.113.200'], hostile: ['2001:db8::55', '256.0.0.1', '192.0.2.055', '::ffff:192.0.2.1', SQL_INJECTION, SCRIPT, '', 3221225985] };
const IPV6: Field = { valid: ['2001:db8::55', '2001:db8:0:0:0:0:0:56', '2001:DB8::57'], hostile: ['192.0.2.55', '::ffff:192.0.2.1', '2001:db8::g', SQL_INJECTION, SCRIPT, '', 1] };
const CAA_TAG: Field = { valid: ['issue', 'issuewild', 'iodef'], hostile: ['bad tag', SCRIPT, SQL_INJECTION, '', 'x'.repeat(256), 1] };
const CAA_FLAG: Field = { valid: [0, 128], hostile: [256, -1, '0', SQL_INJECTION, null] };
const SOA_TIMER: Field = { valid: [60, 3600, 604800, 2147483647], hostile: [0, -1, 2147483648, 4294967296, '3600', null, SQL_INJECTION] };
function hosts(zone: string): Field {
return {
valid: ['target.example.net.', `www.${zone}`, 'mail.example.net'],
hostile: ['single-label', SQL_INJECTION, SCRIPT, '-bad-.example.net.', 'a..b.example.net.', '', `${'x'.repeat(64)}.example.net.`, 'under score!.example.net.', 5],
};
}
function fields(zone: string, type: RecordType): Record {
const host = hosts(zone);
switch (type) {
case 'A':
return { ip: IPV4 };
case 'AAAA':
return { ip: IPV6 };
case 'CNAME':
case 'NS':
return { host };
case 'MX':
return { host: { valid: ['mail.example.net.', `mx.${zone}`], hostile: host.hostile }, preference: U16 };
case 'TXT':
return { text: TEXT };
case 'SRV':
return { priority: U16, weight: U16, port: U16, target: { valid: ['sip.example.net.', '.'], hostile: host.hostile } };
case 'CAA':
return { flag: CAA_FLAG, tag: CAA_TAG, value: { valid: TEXT.valid.filter((t) => t !== '' && String(t).length < 256), hostile: TEXT.hostile } };
case 'SOA':
return {
ns: { valid: ['ns1.example.net.', `ns1.${zone}`], hostile: host.hostile },
MBox: { valid: ['hostmaster.example.net.', `hostmaster.${zone}`], hostile: [...host.hostile, 'host@master@example.net'] },
refresh: SOA_TIMER,
retry: SOA_TIMER,
expire: SOA_TIMER,
minttl: SOA_TIMER,
};
}
}
function freshName(rng: Rng, type: RecordType, seq: number): string {
const label = `f${seq}${rng.string(5)}`;
const choices = type === 'SRV' ? [`_${label}._tcp`, `_${label}._udp`] : [label, `${label}.sub`, label.toUpperCase()];
if (type !== 'NS' && type !== 'SRV') choices.push(`*.${label}`);
if (type === 'NS' || type === 'MX' || type === 'TXT' || type === 'CAA') choices.push('@');
return rng.pick(choices);
}
const NAME_HOSTILE: readonly unknown[] = [SQL_INJECTION, SCRIPT, '', 'bad name', 'x'.repeat(64), '*bad', 'nul\u0000', 7, null];
export function fuzzRecord(rng: Rng, zone: string, type: RecordType, seq: number, mutate: boolean): FuzzRecord {
const spec = fields(zone, type);
const data: Record = {};
for (const [key, field] of Object.entries(spec)) data[key] = rng.pick(field.valid);
const record: FuzzRecord = { name: type === 'SOA' ? '@' : freshName(rng, type, seq), type, ttl: rng.pick(TTL.valid), data };
if (!mutate) return record;
const keys = Object.keys(spec);
const target = rng.weighted([
['name', 2],
['ttl', 2],
['data', 6],
['missing', 1],
['extra', 1],
]);
if (target === 'name') record.name = rng.pick(NAME_HOSTILE);
else if (target === 'ttl') record.ttl = rng.pick(TTL.hostile);
else if (target === 'missing') delete data[rng.pick(keys)];
else if (target === 'extra') data[rng.pick(['serial', 'Ttl', 'comment', 'ip', 'host'])] = rng.pick(PAYLOADS);
else {
const key = rng.pick(keys);
data[key] = rng.pick(spec[key].hostile);
}
return record;
}
export function soaBody(record: FuzzRecord): Record {
const d = record.data;
return { ns: d.ns, mbox: d.MBox, ttl: record.ttl, refresh: d.refresh, retry: d.retry, expire: d.expire, minttl: d.minttl };
}