Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Squawk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,9 @@ export default function createStore<T>(initialState: Required<T>, useReduxDevToo
() =>
new Proxy(localPending, {
get(_, prop) {
contexts.current.add(prop as StoreProp);
if (typeof prop === "string") {
contexts.current.add(prop as StoreProp);
}
return localPending[prop as StoreProp];
}
}),
Expand Down Expand Up @@ -382,7 +384,9 @@ export default function createStore<T>(initialState: Required<T>, useReduxDevToo
() =>
new Proxy(localState, {
get(_, prop) {
contexts.current.add(prop as StoreProp);
if (typeof prop === "string") {
contexts.current.add(prop as StoreProp);
}
return localState[prop as StoreProp];
}
}),
Expand Down
60 changes: 60 additions & 0 deletions src/__tests__/Squawk.proxy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import createStore from "../Squawk";

Check warning on line 1 in src/__tests__/Squawk.proxy.test.ts

View workflow job for this annotation

GitHub Actions / build

Run autofix to sort these imports!
import * as React from "react";

// Mock React hooks to simulate hook environment
jest.mock("react", () => {
const actualReact = jest.requireActual("react");
return {
...actualReact,
useState: jest.fn((init) => [typeof init === 'function' ? init() : init, jest.fn()]),

Check warning on line 9 in src/__tests__/Squawk.proxy.test.ts

View workflow job for this annotation

GitHub Actions / build

Replace `'function'` with `"function"`
useRef: jest.fn((init) => ({ current: init })),
useMemo: jest.fn((factory) => factory()),
useEffect: jest.fn(),

Check warning on line 12 in src/__tests__/Squawk.proxy.test.ts

View workflow job for this annotation

GitHub Actions / build

Delete `,`
};
});

describe("Squawk proxy vulnerability", () => {
it("does not crash or add Symbol properties to contexts in usePending", () => {
// Clear mocks between tests
jest.clearAllMocks();

const store = createStore({ foo: "bar", baz: 123 });
const pending = store.usePending();

// Access a Symbol property on the returned proxy
const sym = Symbol("test");
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const value = pending[sym];
expect(value).toBeUndefined();

// Since we mocked useRef, we can check its calls
const useRefCalls = (React.useRef as jest.Mock).mock.calls;
// useIfMounted has 2 useRef calls, usePending has 1
const contextsRef = useRefCalls[2][0]; // explicitContexts set

expect(contextsRef).toBeInstanceOf(Set);
expect(contextsRef.has(sym)).toBe(false);
});

it("does not crash or add Symbol properties to contexts in useSquawk", () => {
// Clear mocks between tests
jest.clearAllMocks();

const store = createStore({ foo: "bar", baz: 123 });
const state = store.useSquawk();

// Access a Symbol property on the returned proxy
const sym = Symbol("test");
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const value = state[sym];
expect(value).toBeUndefined();

const useRefCalls = (React.useRef as jest.Mock).mock.calls;
const contextsRef = useRefCalls[2][0]; // explicitContexts set

expect(contextsRef).toBeInstanceOf(Set);
expect(contextsRef.has(sym)).toBe(false);
});
});
Loading