|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { unwrapProxy } from "./persist"; |
| 3 | + |
| 4 | +describe("unwrapProxy", () => { |
| 5 | + describe("prototype pollution prevention", () => { |
| 6 | + it("ignores __proto__ key in plain objects", () => { |
| 7 | + const malicious = JSON.parse('{"__proto__": {"polluted": true}, "safe": 1}'); |
| 8 | + const result = unwrapProxy(malicious); |
| 9 | + |
| 10 | + expect(result.safe).toBe(1); |
| 11 | + expect(result.__proto__).toBeUndefined(); |
| 12 | + expect(({} as Record<string, unknown>).polluted).toBeUndefined(); |
| 13 | + }); |
| 14 | + |
| 15 | + it("ignores constructor key", () => { |
| 16 | + const malicious = { constructor: { prototype: { polluted: true } }, safe: 1 }; |
| 17 | + const result = unwrapProxy(malicious); |
| 18 | + |
| 19 | + expect(result.safe).toBe(1); |
| 20 | + expect(result.constructor).toBeUndefined(); |
| 21 | + }); |
| 22 | + |
| 23 | + it("ignores prototype key", () => { |
| 24 | + const malicious = { prototype: { polluted: true }, safe: 1 }; |
| 25 | + const result = unwrapProxy(malicious); |
| 26 | + |
| 27 | + expect(result.safe).toBe(1); |
| 28 | + expect(result.prototype).toBeUndefined(); |
| 29 | + }); |
| 30 | + |
| 31 | + it("filters dangerous keys in nested objects", () => { |
| 32 | + const malicious = { |
| 33 | + nested: JSON.parse('{"__proto__": {"polluted": true}, "valid": 2}'), |
| 34 | + safe: 1, |
| 35 | + }; |
| 36 | + const result = unwrapProxy(malicious); |
| 37 | + |
| 38 | + expect(result.safe).toBe(1); |
| 39 | + expect(result.nested.valid).toBe(2); |
| 40 | + expect(result.nested.__proto__).toBeUndefined(); |
| 41 | + }); |
| 42 | + |
| 43 | + it("result object has null prototype", () => { |
| 44 | + const input = { a: 1 }; |
| 45 | + const result = unwrapProxy(input); |
| 46 | + |
| 47 | + expect(Object.getPrototypeOf(result)).toBeNull(); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe("basic functionality", () => { |
| 52 | + it("handles primitives", () => { |
| 53 | + expect(unwrapProxy(null)).toBeNull(); |
| 54 | + expect(unwrapProxy(undefined)).toBeUndefined(); |
| 55 | + expect(unwrapProxy(42)).toBe(42); |
| 56 | + expect(unwrapProxy("str")).toBe("str"); |
| 57 | + expect(unwrapProxy(true)).toBe(true); |
| 58 | + }); |
| 59 | + |
| 60 | + it("handles arrays", () => { |
| 61 | + const input = [1, { a: 2 }, [3]]; |
| 62 | + const result = unwrapProxy(input); |
| 63 | + |
| 64 | + expect(result).toEqual([1, { a: 2 }, [3]]); |
| 65 | + expect(Array.isArray(result)).toBe(true); |
| 66 | + }); |
| 67 | + |
| 68 | + it("handles nested objects", () => { |
| 69 | + const input = { a: { b: { c: 1 } } }; |
| 70 | + const result = unwrapProxy(input); |
| 71 | + |
| 72 | + expect(result.a.b.c).toBe(1); |
| 73 | + }); |
| 74 | + |
| 75 | + it("handles circular references", () => { |
| 76 | + const obj: Record<string, unknown> = { a: 1 }; |
| 77 | + obj.self = obj; |
| 78 | + |
| 79 | + const result = unwrapProxy(obj); |
| 80 | + |
| 81 | + expect(result.a).toBe(1); |
| 82 | + expect(result.self).toBe(result); |
| 83 | + }); |
| 84 | + |
| 85 | + it("handles Map", () => { |
| 86 | + const input = new Map([["key", { value: 1 }]]); |
| 87 | + const result = unwrapProxy(input); |
| 88 | + |
| 89 | + expect(result instanceof Map).toBe(true); |
| 90 | + expect(result.get("key")).toEqual({ value: 1 }); |
| 91 | + }); |
| 92 | + |
| 93 | + it("handles Set", () => { |
| 94 | + const input = new Set([1, 2, { a: 3 }]); |
| 95 | + const result = unwrapProxy(input); |
| 96 | + |
| 97 | + expect(result instanceof Set).toBe(true); |
| 98 | + expect(result.size).toBe(3); |
| 99 | + }); |
| 100 | + |
| 101 | + it("preserves Date instances", () => { |
| 102 | + const date = new Date("2024-01-01"); |
| 103 | + const result = unwrapProxy(date); |
| 104 | + |
| 105 | + expect(result).toBe(date); |
| 106 | + }); |
| 107 | + |
| 108 | + it("preserves RegExp instances", () => { |
| 109 | + const regex = /test/gi; |
| 110 | + const result = unwrapProxy(regex); |
| 111 | + |
| 112 | + expect(result).toBe(regex); |
| 113 | + }); |
| 114 | + |
| 115 | + it("preserves Error instances", () => { |
| 116 | + const error = new Error("test"); |
| 117 | + const result = unwrapProxy(error); |
| 118 | + |
| 119 | + expect(result).toBe(error); |
| 120 | + }); |
| 121 | + }); |
| 122 | +}); |
0 commit comments