Files
Multi-Debrid-Downloader/src/main/renderer-settings.ts
T
Sucukdeluxe 26d62a9337 Harden renderer account IPC boundary
Move secret-bearing account and settings state behind main-process boundaries for Task 1B. Renderer snapshots now expose RendererSettings plus safe account metadata instead of full AppSettings, with provider tokens, passwords, API keys, archive passwords, and notification URLs excluded from renderer-bound state. Add write-only account create, replace, update-secret, and delete IPC commands, validate renderer settings updates against the safe shape, and keep account command results limited to safe settings, safe accounts, and stable account IDs.

Preserve existing account behavior while removing renderer secret access: blank replace secrets retain stored main-process secrets, Mega-Debrid API/Web pools stay mode-specific, Debrid-Link key metadata migrates by stable key ID, and delete/enable operations target stable account or provider identities. Remove obsolete renderer-side account status helpers from the old settings snapshot flow.

Add focused coverage for all supported renderer account kinds, secret-free UiSnapshot serialization, preload account command forwarding, malformed payload error sanitization, Mega-Debrid preferApi preservation, Debrid-Link key metadata migration, account edit safety, settings UI, debug server settings payloads, link export, and visual fixtures.
2026-08-11 23:22:44 +02:00

84 lines
2.7 KiB
TypeScript

import type { AppSettings, RendererSettingsUpdate } from "../shared/types";
import { createRendererSettings } from "./renderer-state";
const DERIVED_KEYS = new Set(["archivePasswordListConfigured", "notifyUrlConfigured", "configuredProviders"]);
const WRITE_ONLY_KEYS = new Set(["archivePasswordList", "notifyUrl"]);
const MAX_SETTINGS_PAYLOAD_BYTES = 1_000_000;
function invalid(): never {
throw new Error("Settings-Payload ist ungültig");
}
function validateJsonValue(value: unknown, depth = 0): void {
if (depth > 8) {
invalid();
}
if (value === null || typeof value === "string" || typeof value === "boolean") {
return;
}
if (typeof value === "number") {
if (!Number.isFinite(value)) invalid();
return;
}
if (Array.isArray(value)) {
if (value.length > 10_000) invalid();
value.forEach((entry) => validateJsonValue(entry, depth + 1));
return;
}
if (!value || typeof value !== "object" || Object.getPrototypeOf(value) !== Object.prototype) {
invalid();
}
const entries = Object.entries(value as Record<string, unknown>);
if (entries.length > 10_000) invalid();
entries.forEach(([, entry]) => validateJsonValue(entry, depth + 1));
}
function validateTopLevelType(value: unknown, expected: unknown): void {
if (Array.isArray(expected)) {
if (!Array.isArray(value)) invalid();
return;
}
if (expected && typeof expected === "object") {
if (!value || typeof value !== "object" || Array.isArray(value) || Object.getPrototypeOf(value) !== Object.prototype) invalid();
return;
}
if (typeof value !== typeof expected) {
invalid();
}
}
export function validateRendererSettingsUpdate(value: unknown, current: AppSettings): RendererSettingsUpdate {
if (!value || typeof value !== "object" || Array.isArray(value) || Object.getPrototypeOf(value) !== Object.prototype) {
invalid();
}
let serialized = "";
try {
serialized = JSON.stringify(value);
} catch {
invalid();
}
if (Buffer.byteLength(serialized, "utf8") > MAX_SETTINGS_PAYLOAD_BYTES) {
invalid();
}
const safe = createRendererSettings(current) as unknown as Record<string, unknown>;
const input = value as Record<string, unknown>;
const output: Record<string, unknown> = {};
for (const [key, entry] of Object.entries(input)) {
if (DERIVED_KEYS.has(key)) {
continue;
}
if (WRITE_ONLY_KEYS.has(key)) {
if (typeof entry !== "string" || entry.length > 100_000) invalid();
output[key] = entry;
continue;
}
if (!(key in safe)) {
invalid();
}
validateTopLevelType(entry, safe[key]);
validateJsonValue(entry);
output[key] = entry;
}
return output as RendererSettingsUpdate;
}