Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/bak/raw.ts

Raw
import type { BakArchive } from "./archive.js";

const PAGE_SOURCE_BYTES = 16 * 1024;
const MAX_CREDENTIAL_CHARS = 4096;

/** Match JSON string spellings too: compact reads redact after JSON decoding. */
function credentialPattern(secret: string): RegExp {
	const encoded = [...secret]
		.map((character) => {
			const canonical = RegExp.escape(
				Buffer.from(JSON.stringify(character).slice(1, -1)).toString("latin1"),
			);
			const unicode = character
				.split("")
				.map((unit) => {
					const hex = unit
						.charCodeAt(0)
						.toString(16)
						.padStart(4, "0")
						.replace(
							/[a-f]/g,
							(letter) => `[${letter}${letter.toUpperCase()}]`,
						);
					return `\\\\u${hex}`;
				})
				.join("");
			return `(?:${canonical}|${unicode}${character === "/" ? `|${RegExp.escape("\\/")}` : ""})`;
		})
		.join("");
	const literal = RegExp.escape(Buffer.from(secret).toString("latin1"));
	return new RegExp(`(?:${encoded}|${literal})`, "g");
}

export interface RawWindow {
	text: string;
	endByteExclusive: number;
	totalBytes: number;
	etag: string;
}

/** No JSON parsing or line buffering: even media-sized entries remain inspectable. */
export async function readRawWindow(
	archive: Pick<BakArchive, "readSessionRange">,
	hostId: string,
	sessionId: string,
	offset: number,
	secrets: readonly string[],
	etag?: string,
	signal?: AbortSignal,
): Promise<RawWindow> {
	if (
		!Number.isSafeInteger(offset) ||
		offset < 0 ||
		offset > Number.MAX_SAFE_INTEGER - 128 * 1024
	)
		throw new Error("Invalid raw archive byte offset");
	if (
		secrets.length > 16 ||
		secrets.some((secret) => secret.length > MAX_CREDENTIAL_CHARS)
	)
		throw new Error(
			"Configured credentials exceed raw inspection redaction limits",
		);
	// Overlap covers the longest JSON-escaped spelling of a credential on either side.
	// Mask before clipping, so arbitrary offsets cannot reconstruct split credentials.
	const overlap = Math.max(4, ...secrets.map((secret) => secret.length * 6));
	const start = Math.max(0, offset - overlap);
	const range = await archive.readSessionRange(hostId, sessionId, {
		start,
		endInclusive: offset + PAGE_SOURCE_BYTES + overlap + 3,
		...(etag === undefined ? {} : { etag }),
		...(signal === undefined ? {} : { signal }),
	});
	if (offset > range.totalBytes)
		throw new Error("Raw archive byte offset is outside the object");
	const original = Buffer.from(range.body);
	const text = original.toString("latin1");
	const masked = Buffer.from(original);
	for (const secret of secrets) {
		if (!secret) continue;
		const pattern = credentialPattern(secret);
		for (let match = pattern.exec(text); match; match = pattern.exec(text)) {
			masked.fill(42, match.index, match.index + match[0].length);
			// Find overlapping occurrences, including overlaps between different credentials.
			pattern.lastIndex = match.index + 1;
		}
	}
	const first = offset - start;
	let end = Math.min(offset + PAGE_SOURCE_BYTES, range.totalBytes) - start;
	while (end > first && ((masked[end] ?? 0) & 0xc0) === 0x80) end--;
	const decoded = new TextDecoder("utf-8", {
		fatal: true,
		ignoreBOM: true,
	}).decode(masked.subarray(first, end));
	return {
		text: decoded,
		endByteExclusive: start + end,
		totalBytes: range.totalBytes,
		etag: range.etag,
	};
}