Files
Netcatty/infrastructure/persistence/localStorageAdapter.ts
T
bincxzandClaude Opus 4.6 be36ccd167 fix: address security, correctness, and performance issues from code review
Security: add validateSender to agent IPC handlers, remove CODEX_API_KEY
and NODE_PATH from SAFE_ENV_KEYS, validate URL scheme before openExternal,
add backtick substitution to command blocklist, add 10MB buffer limits.

Bugs: fix runExternalAgentTurn timeout hang, fix limitConcurrency undefined
entries on error, treat null exitCode as failure, enforce maxBytes for SFTP reads.

Performance: debounce addMessageToSession persistence, cache compiled
blocklist regexes, add QuotaExceededError handling for localStorage writes.

UI: use local onKeyDown for PermissionDialog, move CRITICAL_PATHS to module level.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-16 00:18:45 +08:00

68 lines
1.9 KiB
TypeScript

const safeParse = <T>(value: string | null): T | null => {
if (!value) return null;
try {
return JSON.parse(value) as T;
} catch {
return null;
}
};
/**
* Safely write to localStorage, catching QuotaExceededError.
* Returns true if the write succeeded, false if storage quota was exceeded.
*/
function safeSetItem(key: string, value: string): boolean {
try {
localStorage.setItem(key, value);
return true;
} catch (err) {
if (
err instanceof DOMException &&
(err.name === 'QuotaExceededError' || err.code === 22)
) {
console.warn(
`[localStorageAdapter] QuotaExceededError writing key "${key}" (${value.length} chars). Data was not persisted.`,
);
return false;
}
throw err; // Re-throw unexpected errors
}
}
export const localStorageAdapter = {
read<T>(key: string): T | null {
return safeParse<T>(localStorage.getItem(key));
},
write<T>(key: string, value: T): boolean {
return safeSetItem(key, JSON.stringify(value));
},
readString(key: string): string | null {
return localStorage.getItem(key);
},
writeString(key: string, value: string): boolean {
return safeSetItem(key, value);
},
readBoolean(key: string): boolean | null {
const value = localStorage.getItem(key);
if (value === null) return null;
if (value === "true") return true;
if (value === "false") return false;
return null;
},
writeBoolean(key: string, value: boolean): boolean {
return safeSetItem(key, value ? "true" : "false");
},
readNumber(key: string): number | null {
const value = localStorage.getItem(key);
if (!value) return null;
const num = parseInt(value, 10);
return isNaN(num) ? null : num;
},
writeNumber(key: string, value: number): boolean {
return safeSetItem(key, String(value));
},
remove(key: string) {
localStorage.removeItem(key);
},
};