- 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
109 lines
3.5 KiB
TypeScript
109 lines
3.5 KiB
TypeScript
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
// Copyright (C) 2026 Bryan Joshua Pedini
|
|
|
|
import crypto from 'node:crypto';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { SESSION_COOKIE } from './env';
|
|
|
|
const TOKEN_CANDIDATE = /(?<![A-Za-z0-9_-])[A-Za-z0-9_-]{43}(?![A-Za-z0-9_-])/g;
|
|
const COOKIE_PAIR = new RegExp(`(${SESSION_COOKIE}=)([^;\\s"'&]*)`, 'g');
|
|
const JSON_SECRET = /("(?:password|current|repeat)"\s*:\s*)"(?:[^"\\]|\\.)*"/g;
|
|
const FORM_SECRET = /((?:^|[?&\s])(?:password|current)=)[^&\s]*/g;
|
|
const HASH = /\$argon2id\$[^\s"']+/g;
|
|
|
|
export function redactionFor(secret: string): string {
|
|
return `[REDACTED:${crypto.createHash('sha256').update(secret, 'utf8').digest('hex').slice(0, 8)}]`;
|
|
}
|
|
|
|
export function redactText(text: string, secrets: Iterable<string>): string {
|
|
let out = text;
|
|
const list = [...new Set(secrets)].filter((s) => s.length >= 4).sort((a, b) => b.length - a.length);
|
|
for (const s of list) {
|
|
if (out.includes(s)) out = out.split(s).join(redactionFor(s));
|
|
}
|
|
out = out.replace(COOKIE_PAIR, (_m, k: string) => `${k}[REDACTED]`);
|
|
out = out.replace(JSON_SECRET, (_m, k: string) => `${k}"[REDACTED]"`);
|
|
out = out.replace(FORM_SECRET, (_m, k: string) => `${k}[REDACTED]`);
|
|
out = out.replace(HASH, '[REDACTED:hash]');
|
|
out = out.replace(TOKEN_CANDIDATE, (m) => redactionFor(m));
|
|
return out;
|
|
}
|
|
|
|
export function redactValue<T>(value: T, secrets: Iterable<string>): T {
|
|
const list = [...secrets];
|
|
const walk = (v: unknown): unknown => {
|
|
if (typeof v === 'string') return redactText(v, list);
|
|
if (Array.isArray(v)) return v.map(walk);
|
|
if (Buffer.isBuffer(v)) return redactText(v.toString('utf8'), list);
|
|
if (v && typeof v === 'object') {
|
|
const o: Record<string, unknown> = {};
|
|
for (const [k, x] of Object.entries(v)) {
|
|
const lower = k.toLowerCase();
|
|
o[k] = lower === 'cookie' || lower === 'set-cookie' || lower === 'password' || lower === 'current' ? '[REDACTED]' : walk(x);
|
|
}
|
|
return o;
|
|
}
|
|
return v;
|
|
};
|
|
return walk(value) as T;
|
|
}
|
|
|
|
export class SecretRegistry {
|
|
private readonly secrets = new Map<string, string>();
|
|
|
|
constructor(private readonly persistFile?: string) {}
|
|
|
|
register(secret: string, label = 'secret'): string {
|
|
if (!secret || this.secrets.has(secret)) return secret;
|
|
this.secrets.set(secret, label);
|
|
if (this.persistFile) {
|
|
fs.mkdirSync(path.dirname(this.persistFile), { recursive: true, mode: 0o700 });
|
|
fs.appendFileSync(this.persistFile, JSON.stringify(secret) + '\n', { mode: 0o600 });
|
|
}
|
|
return secret;
|
|
}
|
|
|
|
all(): string[] {
|
|
return [...this.secrets.keys()];
|
|
}
|
|
|
|
labelled(): Array<[string, string]> {
|
|
return [...this.secrets.entries()];
|
|
}
|
|
|
|
redact(text: string): string {
|
|
return redactText(text, this.secrets.keys());
|
|
}
|
|
|
|
redactValue<T>(value: T): T {
|
|
return redactValue(value, this.secrets.keys());
|
|
}
|
|
|
|
findIn(text: string): string[] {
|
|
return this.labelled()
|
|
.filter(([secret]) => secret.length >= 8 && text.includes(secret))
|
|
.map(([secret, label]) => `${label} ${redactionFor(secret)}`);
|
|
}
|
|
}
|
|
|
|
export function loadPersistedSecrets(dir: string): string[] {
|
|
const out = new Set<string>();
|
|
let files: string[] = [];
|
|
try {
|
|
files = fs.readdirSync(dir);
|
|
} catch {
|
|
return [];
|
|
}
|
|
for (const f of files) {
|
|
try {
|
|
for (const line of fs.readFileSync(path.join(dir, f), 'utf8').split('\n')) {
|
|
if (line.trim()) out.add(JSON.parse(line) as string);
|
|
}
|
|
} catch {
|
|
continue;
|
|
}
|
|
}
|
|
return [...out];
|
|
}
|