test(e2e): set a user password and revoke a delegation from the web ui

- test(e2e): set a user password and revoke a delegation from the web ui
- test(e2e): switch a dynamic soa serial to a managed one through the ui
- test(e2e): match audit ledger routes on the decoded api path
- test(e2e): fuzz injection payloads in paths, filters and record data
This commit is contained in:
bryanpedini committed 2026-11-19 15:46:12 +01:00
1 parent 2a773e740f
commit 1cafcc759e
7 files changed
+433 -28

No files matched your search

+125 -1
View File
@@ -5,7 +5,7 @@ import { test, expect, type Account } from '../harness/fixtures';
import type { ApiClient } from '../harness/api';
import { zp, up } from '../harness/api';
import { parseTable, USAGE_TEXT } from '../harness/cli';
import { waitForZone, waitForZoneGone } from '../harness/dig';
import { servedSerial, waitForZone, waitForZoneGone } from '../harness/dig';
import { runnerAddress, SESSION_COOKIE } from '../harness/env';
import { bulkDeleteBody, expectCheckErrorRowsCarryRaw, expectZoneCreateWarnings, importApplyBody, UNKNOWN_API_OPERATION, ZONE_CREATE_DELEGATIONS_IN } from '../harness/decisions';
import { READ_OPERATIONS } from '../harness/ledger';
@@ -660,6 +660,79 @@ test.describe('core flow', () => {
await servedZone(world, zone);
});
test('a dynamic SOA serial is shown, served as Unix time and replaced by a managed serial through the UI', async ({ world }) => {
test.setTimeout(180_000);
const ui = adminUi;
const dz = world.zoneName('dynamic-');
expect((await adminApi.post('/zones', { name: dz, ns: `ns1.${dz}`, mbox: `hostmaster@${dz}`, extra_ns: [], refresh: 3600, retry: 600, expire: 604800, minttl: 300, ttl: 3600 })).status).toBe(201);
await waitForZone(world.g, dz, { recorder: world.recorder });
const managedSoa = (await world.db.records(dz)).find((r) => r.record_type === 'SOA')!;
const { serial: createdSerial, ...timers } = JSON.parse(managedSoa.content!) as Record<string, string | number>;
expect(createdSerial).toEqual(expect.any(Number));
await world.db.updateRaw(managedSoa.id, { content: JSON.stringify(timers) });
expect(await world.db.soaSerial(dz), 'the SOA has no serial key').toBeNull();
await ui.openZone(dz);
expect((await ui.soaSummary()).Serial).toBe('dynamic (Unix time)');
await expect(ui.page.locator('#soa-managed')).toBeVisible();
await ui.expectRow({ name: '@', type: 'SOA', value: uiValue('SOA', { ...timers, serial: 'dynamic (Unix time)' }) });
expect(await ui.problems()).toContainEqual({ severity: 'warning', code: 'soa_dynamic_serial', where: '(@ SOA)' });
expect((await adminApi.get(`${zp(dz)}/check`)).json.problems).toContainEqual(expect.objectContaining({ severity: 'warning', code: 'soa_dynamic_serial', id: managedSoa.id, name: '', type: 'SOA' }));
await ui.goto('/');
expect(await ui.zonesTable()).toContainEqual(expect.objectContaining({ zone: dz, serial: 'dynamic' }));
const before = Math.floor(Date.now() / 1000);
const unixSerial = await servedSerial(world.g, dz, { recorder: world.recorder });
const after = Math.floor(Date.now() / 1000);
expect(unixSerial, 'CoreDNS serves the Unix time as the dynamic serial').toBeGreaterThanOrEqual(before - 5);
expect(unixSerial).toBeLessThanOrEqual(after + 5);
await served(world, dz, dz, 'SOA', 'dynamic serial');
await ui.openZone(dz);
const rowsBefore = await world.db.records(dz);
const mark = await world.db.auditMaxId();
const t0 = new Date();
const done = ui.page.waitForResponse((r) => new URL(r.url()).pathname.endsWith('/soa') && r.request().method() === 'PUT');
await ui.page.click('#soa-managed');
expect((await done).status()).toBe(200);
await ui.settle();
await ui.expectToast('The zone now uses a managed serial.');
const serial = (await world.db.soaSerial(dz)) as number;
expect([utcDay(t0), utcDay(new Date())], 'the managed serial is YYYYMMDD00 of today').toContain(serial);
const soaRow = await storedRow(world, dz, { name: '', type: 'SOA', ttl: managedSoa.ttl as number, data: { ...timers, serial } });
expect(soaRow.id).toBe(managedSoa.id);
for (const r of rowsBefore.filter((x) => x.id !== managedSoa.id)) expect(await world.db.recordById(r.id)).toEqual(r);
await expect.poll(async () => (await ui.soaSummary()).Serial).toBe(String(serial));
await expect(ui.page.locator('#soa-managed')).toBeHidden();
await ui.expectRow({ name: '@', type: 'SOA', value: uiValue('SOA', { ...timers, serial }) });
expect((await ui.problems()).some((p) => p.code === 'soa_dynamic_serial')).toBe(false);
await served(world, dz, dz, 'SOA', 'managed serial in the answer');
await served(world, dz, `missing.${dz}`, 'TXT', 'managed serial in the authority section');
expect(await servedSerial(world.g, dz, { recorder: world.recorder })).toBe(serial);
const audits = (await auditSince(world, mark)).filter((r) => r.operation === 'soa_update');
expect(audits, 'exactly one soa_update audit row').toHaveLength(1);
expect(audits[0]).toMatchObject({ actor: admin.username, status: 200, error: null, zone: dz, record_id: managedSoa.id, target_user_id: null });
const dynamicRow = rowsBefore.find((r) => r.id === managedSoa.id)!;
expectAuditRecord(parsedJson(audits[0].before_value), dynamicRow, 'soa_update before');
expectAuditRecord(parsedJson(audits[0].after_value), soaRow, 'soa_update after');
const contentOf = (v: unknown) => {
const c = (parsedJson(v) as { content: unknown }).content;
return (typeof c === 'string' ? JSON.parse(c) : c) as Record<string, unknown>;
};
expect(contentOf(audits[0].before_value)).not.toHaveProperty('serial');
expect(contentOf(audits[0].after_value)).toHaveProperty('serial', serial);
const rec: Rec = { name: 'after-managed', type: 'TXT', ttl: 300, data: { text: 'managed serial bump' } };
const change = await begin(world, dz);
await ui.addRecord({ name: rec.name, type: 'TXT', ttl: '300', fields: { text: 'managed serial bump' } }, { expectStatus: 201 });
await threeWay(world, ui, dz, rec, change, 'the first change after the managed serial');
const next = (await world.db.soaSerial(dz)) as number;
if (utcDay(new Date()) === serial) expect(next, 'the next change of the day is YYYYMMDD01').toBe(serial + 1);
expect(await servedSerial(world.g, dz, { recorder: world.recorder })).toBe(next);
await expect.poll(async () => (await ui.soaSummary()).Serial).toBe(String(next));
expect((await adminApi.get(`${zp(dz)}/check`)).json.problems.filter((p: { severity: string }) => p.severity === 'error')).toEqual([]);
await servedZone(world, dz);
});
test('7. users and delegation: roles, zone visibility, revocation, promotion, deletion and the last active admin', async ({ world }) => {
test.setTimeout(600_000);
zoneB = `${world.unique('second-')}.core.test.`;
@@ -811,6 +884,57 @@ test.describe('core flow', () => {
expect((await adminUi.setRoleUi('user')).status).toBe(204);
expect((await u1api.get(zp(zone))).status, 'the delegation applies again after the demotion').toBe(200);
const u2id = (await world.db.user(user2.username))?.id as number;
const hashBefore = (await world.db.user(user2.username))?.password_hash;
expect((await u2api.get('/account')).status, 'the API session of the user is open').toBe(200);
expect((await world.db.sessions(u2id)).length, 'the user has an API and a browser session').toBe(2);
const setPassword = world.password('password of bob set by an admin');
await adminUi.openUser(user2.username);
const pwMark = await world.db.auditMaxId();
const pwSet = await adminUi.setPasswordUi(setPassword);
expect(pwSet.status, JSON.stringify(pwSet.json)).toBe(204);
await adminUi.expectToast(`Password set. The sessions of ${user2.username} ended.`);
const pwAudits = (await auditSince(world, pwMark)).filter((r) => r.operation === 'user_password');
expect(pwAudits, 'exactly one user_password audit row').toHaveLength(1);
const pwAudit = pwAudits[0];
expect(pwAudit).toMatchObject({ user_id: rootId, actor: admin.username, origin: runnerAddress(), status: 204, error: null, zone: null, record_id: null, target_user_id: u2id });
for (const value of [pwAudit.before_value, pwAudit.after_value]) {
const snapshot = parsedJson(value) as Record<string, unknown>;
expect(Object.keys(snapshot).sort()).toEqual(['created_at', 'deleted_at', 'id', 'role', 'username']);
expect(snapshot).toMatchObject({ id: u2id, username: user2.username, role: 'user', deleted_at: null });
}
expect(JSON.stringify(pwAudit)).not.toMatch(/argon2|password_hash/);
expect(JSON.stringify(pwAudit)).not.toContain(setPassword);
expect(await world.db.sessions(u2id), 'setting a password ends every session of the user').toEqual([]);
const u2row = await world.db.user(user2.username);
expect(u2row?.password_hash).toMatch(/^\$argon2id\$v=19\$m=65536,t=3,p=2\$/);
expect(u2row?.password_hash, 'the stored hash changed').not.toBe(hashBefore);
expect((await u2api.get('/account')).status, 'the open API session of the user ended').toBe(401);
u2api.forgetSession();
await u2ui.page.reload();
await expect(u2ui.page).toHaveURL(/\/login/);
u2ui.sessionEnded();
const oldPassword = await (await world.api()).login(user2.username, user2.password);
expect(oldPassword.status, 'the previous password is refused').toBe(401);
user2 = { ...user2, password: setPassword };
const u2next = await world.apiAs(user2);
expect((await u2next.get('/account')).status, 'the password set by the admin works').toBe(200);
await adminUi.openZone(zoneB);
expect((await adminUi.delegateFromZonePage(user2.username)).status).toBe(204);
await expect.poll(() => adminUi.zoneUsers()).toEqual([user2.username]);
expect((await u2next.get(zp(zoneB))).status, 'the delegation from the zone page applies').toBe(200);
const revokeMark = await world.db.auditMaxId();
expect((await adminUi.revokeFromZonePage(user2.username)).status).toBe(204);
await expect.poll(() => adminUi.zoneUsers()).toEqual([]);
const revokes = (await auditSince(world, revokeMark)).filter((r) => r.operation === 'delegation_revoke');
expect(revokes, 'exactly one delegation_revoke audit row').toHaveLength(1);
expect(revokes[0]).toMatchObject({ user_id: rootId, actor: admin.username, status: 204, error: null, zone: zoneB, record_id: null, target_user_id: u2id });
expect(parsedJson(revokes[0].before_value)).toEqual({ zone: zoneB, username: user2.username });
expect(revokes[0].after_value).toBeNull();
expect((await world.db.delegations()).filter((d) => d.zone === zoneB && d.user_id === u2id), 'the delegation row is gone').toEqual([]);
expect((await u2next.get(zp(zoneB))).status, 'a revocation from the zone page takes effect on the next request').toBe(404);
await adminUi.openUser(admin.username);
await expect(adminUi.page.locator('#user-self')).toBeVisible();
await expect(adminUi.page.locator('#role-form')).toHaveCount(0);