test(e2e): add playwright harness and test stack
- test(e2e): add playwright harness and test stack - test(e2e): add core flow - fix(ui): preserve same-origin browser form submissions - fix(ui): offer reload inside stale edit dialogs - test(e2e): add persona session and accessibility suites - test(e2e): preserve per-run artifacts and clean pulled images - test(e2e): verify import and audit state across navigation - test(e2e): tighten verification helpers and persona checks - test(e2e): bound response body reads of the browser observer - test: recover review compatibility assertions - test: stabilize audit and user flow assertions
This commit is contained in:
1 parent
a7ba01fc77
commit
57a005c19f
48 files changed
+6239
-6
No files matched your search
@@ -0,0 +1,310 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
// Copyright (C) 2026 Bryan Joshua Pedini
|
||||
|
||||
import { expect } from '@playwright/test';
|
||||
import type { AuditRow, Db } from './mysql';
|
||||
import { REVIEW_FIXES, UNKNOWN_API_OPERATION } from './decisions';
|
||||
import type { SecretRegistry } from './secrets';
|
||||
|
||||
export const READ_OPERATIONS = new Set([
|
||||
'account_read',
|
||||
'zone_list',
|
||||
'zone_read',
|
||||
'record_list',
|
||||
'zone_check',
|
||||
'check_all',
|
||||
'zone_export',
|
||||
'zone_import_preview',
|
||||
'delegation_list',
|
||||
'user_list',
|
||||
'user_read',
|
||||
'audit_read',
|
||||
UNKNOWN_API_OPERATION,
|
||||
]);
|
||||
|
||||
export const CHANGE_OPERATIONS = new Set([
|
||||
'login',
|
||||
'logout',
|
||||
'password_change',
|
||||
'zone_create',
|
||||
'zone_delete',
|
||||
'record_create',
|
||||
'record_update',
|
||||
'record_delete',
|
||||
'record_bulk_delete',
|
||||
'soa_update',
|
||||
'zone_import',
|
||||
'delegation_grant',
|
||||
'delegation_revoke',
|
||||
'user_create',
|
||||
'user_role',
|
||||
'user_password',
|
||||
'user_delete',
|
||||
]);
|
||||
|
||||
type Target = 'zone' | 'record' | 'user' | 'delegation';
|
||||
|
||||
const ROUTES: ReadonlyArray<readonly [string, RegExp, string, Target[]]> = [
|
||||
['GET', /^\/account$/, 'account_read', []],
|
||||
['PUT', /^\/account\/password$/, 'password_change', []],
|
||||
['GET', /^\/zones$/, 'zone_list', []],
|
||||
['POST', /^\/zones$/, 'zone_create', []],
|
||||
['GET', /^\/zones\/([^/]+)$/, 'zone_read', ['zone']],
|
||||
['DELETE', /^\/zones\/([^/]+)$/, 'zone_delete', ['zone']],
|
||||
['GET', /^\/zones\/([^/]+)\/records$/, 'record_list', ['zone']],
|
||||
['POST', /^\/zones\/([^/]+)\/records$/, 'record_create', ['zone']],
|
||||
['POST', /^\/zones\/([^/]+)\/records\/delete$/, 'record_bulk_delete', ['zone']],
|
||||
['PUT', /^\/zones\/([^/]+)\/records\/([^/]+)$/, 'record_update', ['zone', 'record']],
|
||||
['DELETE', /^\/zones\/([^/]+)\/records\/([^/]+)$/, 'record_delete', ['zone', 'record']],
|
||||
['PUT', /^\/zones\/([^/]+)\/soa$/, 'soa_update', ['zone']],
|
||||
['GET', /^\/zones\/([^/]+)\/check$/, 'zone_check', ['zone']],
|
||||
['GET', /^\/check$/, 'check_all', []],
|
||||
['GET', /^\/zones\/([^/]+)\/export$/, 'zone_export', ['zone']],
|
||||
['POST', /^\/zones\/([^/]+)\/import\/preview$/, 'zone_import_preview', ['zone']],
|
||||
['POST', /^\/zones\/([^/]+)\/import\/apply$/, 'zone_import', ['zone']],
|
||||
['GET', /^\/zones\/([^/]+)\/users$/, 'delegation_list', ['zone']],
|
||||
['PUT', /^\/zones\/([^/]+)\/users\/([^/]+)$/, 'delegation_grant', ['zone', 'delegation']],
|
||||
['DELETE', /^\/zones\/([^/]+)\/users\/([^/]+)$/, 'delegation_revoke', ['zone', 'delegation']],
|
||||
['GET', /^\/users$/, 'user_list', []],
|
||||
['POST', /^\/users$/, 'user_create', []],
|
||||
['GET', /^\/users\/([^/]+)$/, 'user_read', ['user']],
|
||||
['PUT', /^\/users\/([^/]+)\/role$/, 'user_role', ['user']],
|
||||
['PUT', /^\/users\/([^/]+)\/password$/, 'user_password', ['user']],
|
||||
['DELETE', /^\/users\/([^/]+)$/, 'user_delete', ['user']],
|
||||
['GET', /^\/audit$/, 'audit_read', []],
|
||||
];
|
||||
|
||||
export interface RouteMatch {
|
||||
operation: string;
|
||||
zone: string | null;
|
||||
recordId: number | null;
|
||||
targetUser: string | null;
|
||||
}
|
||||
|
||||
function decode(segment: string): string | null {
|
||||
try {
|
||||
return decodeURIComponent(segment);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const ZONE_RE = /^(?=.{1,254}$)([a-z0-9_]([a-z0-9_-]{0,61}[a-z0-9_])?\.)+$/;
|
||||
|
||||
export function normalizeZoneParam(raw: string): string | null {
|
||||
const z = raw.toLowerCase();
|
||||
const withDot = z.endsWith('.') ? z : `${z}.`;
|
||||
return ZONE_RE.test(withDot) ? withDot : null;
|
||||
}
|
||||
|
||||
export function matchRoute(method: string, apiPath: string): RouteMatch | null {
|
||||
for (const [m, re, operation, targets] of ROUTES) {
|
||||
if (m !== method.toUpperCase()) continue;
|
||||
const match = re.exec(apiPath);
|
||||
if (!match) continue;
|
||||
const groups = match.slice(1).map((s) => decode(s));
|
||||
const out: RouteMatch = { operation, zone: null, recordId: null, targetUser: null };
|
||||
targets.forEach((t, i) => {
|
||||
const v = groups[i];
|
||||
if (v === null || v === undefined) return;
|
||||
if (t === 'zone') out.zone = normalizeZoneParam(v);
|
||||
if (t === 'record') out.recordId = /^[0-9]{1,9}$/.test(v) ? Number(v) : null;
|
||||
if (t === 'user' || t === 'delegation') out.targetUser = v;
|
||||
});
|
||||
return out;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function sanitizeActor(s: string): string {
|
||||
return [...s]
|
||||
.map((ch) => (ch === ' ' || /[\p{L}\p{M}\p{N}\p{P}\p{S}]/u.test(ch) ? ch : ' '))
|
||||
.slice(0, 64)
|
||||
.join('');
|
||||
}
|
||||
|
||||
export interface ExpectedAudit {
|
||||
operation: string;
|
||||
status: number | null;
|
||||
anyStatus?: boolean;
|
||||
actor: string;
|
||||
origin: string;
|
||||
userIsActor: boolean | null;
|
||||
error?: string | null;
|
||||
zone?: string | null;
|
||||
recordId?: number | null;
|
||||
targetUser?: string | null;
|
||||
optional: boolean;
|
||||
source: string;
|
||||
}
|
||||
|
||||
export interface ApiExchange {
|
||||
method: string;
|
||||
path: string;
|
||||
status: number;
|
||||
actor: string;
|
||||
origin: string;
|
||||
body: string;
|
||||
sessionValid: boolean;
|
||||
crossOrigin?: boolean;
|
||||
optional?: boolean;
|
||||
source: string;
|
||||
}
|
||||
|
||||
function errorOf(body: string): string | null {
|
||||
try {
|
||||
const parsed = JSON.parse(body) as { error?: unknown };
|
||||
return typeof parsed.error === 'string' ? parsed.error : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export class AuditLedger {
|
||||
private expected: ExpectedAudit[] = [];
|
||||
private mark = 0;
|
||||
private pending = new Set<Promise<unknown>>();
|
||||
private skipped = 0;
|
||||
|
||||
constructor(
|
||||
private readonly db: Db,
|
||||
private readonly secrets: SecretRegistry,
|
||||
) {}
|
||||
|
||||
async start(): Promise<void> {
|
||||
await this.settle();
|
||||
this.mark = await this.db.auditMaxId();
|
||||
this.expected = [];
|
||||
}
|
||||
|
||||
track<T>(p: Promise<T>): Promise<T> {
|
||||
this.pending.add(p);
|
||||
p.finally(() => this.pending.delete(p)).catch(() => undefined);
|
||||
return p;
|
||||
}
|
||||
|
||||
async settle(): Promise<void> {
|
||||
while (this.pending.size > 0) await Promise.allSettled([...this.pending]);
|
||||
}
|
||||
|
||||
get expectedCount(): number {
|
||||
return this.expected.length;
|
||||
}
|
||||
|
||||
get skippedCount(): number {
|
||||
return this.skipped;
|
||||
}
|
||||
|
||||
add(e: ExpectedAudit): void {
|
||||
this.expected.push(e);
|
||||
}
|
||||
|
||||
api(x: ApiExchange): void {
|
||||
if (!x.path.startsWith('/api/v1/') && x.path !== '/api/v1') return;
|
||||
const apiPath = x.path.slice('/api/v1'.length).split('?')[0];
|
||||
const route = matchRoute(x.method, apiPath);
|
||||
if (!x.sessionValid || x.status === 401 || x.crossOrigin) {
|
||||
this.skipped++;
|
||||
return;
|
||||
}
|
||||
if (!route || x.status === 405) {
|
||||
this.add(
|
||||
REVIEW_FIXES
|
||||
? { operation: UNKNOWN_API_OPERATION, status: x.status, actor: x.actor, origin: x.origin, userIsActor: true, error: errorOf(x.body) ?? undefined, optional: false, source: x.source }
|
||||
: { operation: '*', status: x.status, actor: x.actor, origin: x.origin, userIsActor: true, optional: true, source: x.source },
|
||||
);
|
||||
return;
|
||||
}
|
||||
const ok = x.status < 400;
|
||||
this.add({
|
||||
operation: route.operation,
|
||||
status: x.status,
|
||||
actor: x.actor,
|
||||
origin: x.origin,
|
||||
userIsActor: true,
|
||||
error: ok ? null : errorOf(x.body),
|
||||
zone: ok ? route.zone : undefined,
|
||||
recordId: ok && route.recordId !== null ? route.recordId : undefined,
|
||||
targetUser: ok ? route.targetUser : undefined,
|
||||
optional: x.optional ?? false,
|
||||
source: x.source,
|
||||
});
|
||||
}
|
||||
|
||||
login(submitted: string, status: number, origin: string, source: string): void {
|
||||
this.add({
|
||||
operation: 'login',
|
||||
status,
|
||||
actor: sanitizeActor(submitted),
|
||||
origin,
|
||||
userIsActor: status === 303 ? true : null,
|
||||
optional: false,
|
||||
source,
|
||||
});
|
||||
}
|
||||
|
||||
logout(actor: string, status: number, origin: string, sessionValid: boolean, source: string): void {
|
||||
if (!sessionValid) {
|
||||
this.skipped++;
|
||||
return;
|
||||
}
|
||||
this.add({ operation: 'logout', status, actor, origin, userIsActor: true, optional: false, source });
|
||||
}
|
||||
|
||||
cli(operation: string, targetUser: string, source: string): void {
|
||||
this.add({ operation, status: null, actor: 'cli', origin: 'local', userIsActor: false, targetUser, error: null, optional: false, source });
|
||||
}
|
||||
|
||||
async verify(label = 'audit rows match the operations performed'): Promise<AuditRow[]> {
|
||||
await this.settle();
|
||||
await new Promise((r) => setTimeout(r, 150));
|
||||
await this.settle();
|
||||
const rows = await this.db.auditSince(this.mark);
|
||||
const userIds = new Map<number, string>();
|
||||
for (const r of rows) {
|
||||
for (const id of [r.user_id, r.target_user_id]) {
|
||||
if (id !== null && !userIds.has(id)) userIds.set(id, (await this.db.usernameById(id)) ?? `#${id}`);
|
||||
}
|
||||
}
|
||||
const used = new Set<number>();
|
||||
const problems: string[] = [];
|
||||
const describe = (r: AuditRow) =>
|
||||
`#${r.id} ${r.operation} ${r.status} actor=${JSON.stringify(r.actor)} origin=${r.origin} zone=${r.zone} record=${r.record_id} target=${r.target_user_id === null ? null : userIds.get(r.target_user_id)} error=${JSON.stringify(r.error)}`;
|
||||
const matches = (e: ExpectedAudit, r: AuditRow): boolean => {
|
||||
if (e.operation !== '*' && r.operation !== e.operation) return false;
|
||||
if (!e.anyStatus && r.status !== e.status) return false;
|
||||
if (r.actor !== e.actor) return false;
|
||||
if (r.origin !== e.origin) return false;
|
||||
if (e.userIsActor === true && (r.user_id === null || userIds.get(r.user_id) !== e.actor)) return false;
|
||||
if (e.userIsActor === false && r.user_id !== null) return false;
|
||||
if (e.error !== undefined && e.error !== null && r.error !== e.error) return false;
|
||||
if (e.error === null && r.error !== null) return false;
|
||||
if (e.zone !== undefined && e.zone !== null && r.zone !== e.zone) return false;
|
||||
if (e.recordId !== undefined && e.recordId !== null && r.record_id !== e.recordId) return false;
|
||||
if (e.targetUser !== undefined && e.targetUser !== null && (r.target_user_id === null || userIds.get(r.target_user_id) !== e.targetUser)) return false;
|
||||
return true;
|
||||
};
|
||||
const ordered = [...this.expected.filter((e) => !e.optional), ...this.expected.filter((e) => e.optional)];
|
||||
for (const e of ordered) {
|
||||
const idx = rows.findIndex((r, i) => !used.has(i) && matches(e, r));
|
||||
if (idx >= 0) used.add(idx);
|
||||
else if (!e.optional) problems.push(`missing: ${e.operation} ${e.status} actor=${JSON.stringify(e.actor)} zone=${e.zone ?? '-'} error=${JSON.stringify(e.error ?? '-')} (${e.source})`);
|
||||
}
|
||||
rows.forEach((r, i) => {
|
||||
if (!used.has(i)) problems.push(`unexpected: ${describe(r)}`);
|
||||
});
|
||||
for (const r of rows) {
|
||||
if (r.status !== null && r.status >= 400 && (r.error === null || r.error === '')) problems.push(`row without error message: ${describe(r)}`);
|
||||
if (r.status !== null && r.status < 400 && r.error !== null) problems.push(`successful row with error: ${describe(r)}`);
|
||||
if (READ_OPERATIONS.has(r.operation) && (r.before_value !== null || r.after_value !== null)) problems.push(`read row with values: ${describe(r)}`);
|
||||
if (r.status !== null && r.status >= 400 && (r.before_value !== null || r.after_value !== null)) problems.push(`failed row with values: ${describe(r)}`);
|
||||
const text = JSON.stringify(r);
|
||||
if (/password_hash|\$argon2id\$/i.test(text)) problems.push(`row with a password hash: ${describe(r)}`);
|
||||
for (const leak of this.secrets.findIn(text)) problems.push(`row with a secret (${leak}): ${describe(r)}`);
|
||||
}
|
||||
expect(problems, label).toEqual([]);
|
||||
if (rows.length > 0) this.mark = rows[rows.length - 1].id;
|
||||
this.expected = [];
|
||||
return rows;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user