fix(SEC-WEB-30+31+36): Validate JSON.parse/localStorage deserialization
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

Add runtime type validation after all JSON.parse calls in the web app to
prevent runtime crashes from corrupted or tampered storage data. Creates a
shared safeJsonParse utility with type guard functions for each data shape
(Message[], ChatOverlayState, LayoutConfigRecord). All four affected
callsites now validate parsed data and fall back to safe defaults on
mismatch.

Files changed:
- apps/web/src/lib/utils/safe-json.ts (new utility)
- apps/web/src/lib/utils/safe-json.test.ts (25 tests)
- apps/web/src/hooks/useChat.ts (deserializeMessages)
- apps/web/src/hooks/useChat.test.ts (3 new corruption tests)
- apps/web/src/hooks/useChatOverlay.ts (loadState)
- apps/web/src/hooks/useChatOverlay.test.ts (3 new corruption tests)
- apps/web/src/components/chat/ConversationSidebar.tsx (ideaToConversation)
- apps/web/src/lib/hooks/useLayout.ts (layout loading)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Jason Woltje
2026-02-06 15:46:58 -06:00
parent 6d92251fc1
commit 14b547d468
8 changed files with 516 additions and 22 deletions

View File

@@ -64,6 +64,7 @@ describe("useChatOverlay", () => {
});
it("should handle invalid localStorage data gracefully", () => {
vi.spyOn(console, "warn").mockImplementation(() => undefined);
localStorageMock.setItem("chatOverlayState", "invalid json");
const { result } = renderHook(() => useChatOverlay());
@@ -71,6 +72,37 @@ describe("useChatOverlay", () => {
expect(result.current.isOpen).toBe(false);
expect(result.current.isMinimized).toBe(false);
});
it("should fall back to defaults when localStorage has wrong shape", () => {
vi.spyOn(console, "warn").mockImplementation(() => undefined);
// Valid JSON but wrong shape
localStorageMock.setItem("chatOverlayState", JSON.stringify({ isOpen: "yes", wrong: true }));
const { result } = renderHook(() => useChatOverlay());
expect(result.current.isOpen).toBe(false);
expect(result.current.isMinimized).toBe(false);
});
it("should fall back to defaults when localStorage has null value parsed", () => {
vi.spyOn(console, "warn").mockImplementation(() => undefined);
localStorageMock.setItem("chatOverlayState", "null");
const { result } = renderHook(() => useChatOverlay());
expect(result.current.isOpen).toBe(false);
expect(result.current.isMinimized).toBe(false);
});
it("should fall back to defaults when localStorage has array instead of object", () => {
vi.spyOn(console, "warn").mockImplementation(() => undefined);
localStorageMock.setItem("chatOverlayState", JSON.stringify([true, false]));
const { result } = renderHook(() => useChatOverlay());
expect(result.current.isOpen).toBe(false);
expect(result.current.isMinimized).toBe(false);
});
});
describe("open", () => {