Skip to content

Commit

Permalink
fix: don't DOS on nth key (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skn0tt authored Dec 5, 2024
1 parent 4e670bd commit 5debdda
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/accessDeep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { isMap, isArray, isPlainObject, isSet } from './is.js';
import { includes } from './util.js';

const getNthKey = (value: Map<any, any> | Set<any>, n: number): any => {
if (n > value.size) throw new Error('index out of bounds');
const keys = value.keys();
while (n > 0) {
keys.next();
Expand Down
12 changes: 12 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1229,3 +1229,15 @@ test('dedupe=true on a large complicated schema', () => {
expect(nondedupedOut).toEqual(deserialized);
expect(dedupedOut).toEqual(deserialized);
});

test('doesnt iterate to keys that dont exist', () => {
const robbyBubble = { id: 5 };
const highscores = new Map([[robbyBubble, 5000]]);
const objectWithReferentialEquality = { highscores, topScorer: robbyBubble };
const res = SuperJSON.serialize(objectWithReferentialEquality);

expect(res.meta.referentialEqualities.topScorer).toEqual(['highscores.0.0']);
res.meta.referentialEqualities.topScorer = ['highscores.99999.0'];

expect(() => SuperJSON.deserialize(res)).toThrowError('index out of bounds');
});

0 comments on commit 5debdda

Please sign in to comment.