Skip to content

Commit

Permalink
Add null checks to jsonPrune (#186)
Browse files Browse the repository at this point in the history
Previously, json-prune and its related scriptlets silently broke webpages if null or undefined was present in objects passed to pruners. This fixes that.
  • Loading branch information
anfragment authored Dec 24, 2024
1 parent 99977c2 commit de6ecf3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/scriptlet/bundle.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion scriptlets/src/helpers/jsonPrune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function createPrune(propsToRemove: string, requiredProps?: string, stack
}

function prunePath(obj: any, path: PropPath) {
if (path.length === 0) {
if (path.length === 0 || obj == null) {
return;
}

Expand Down Expand Up @@ -83,6 +83,9 @@ function prunePath(obj: any, path: PropPath) {
}

function matchesPath(obj: any, path: PropPath): boolean {
if (obj == null) {
return false;
}
if (path.length === 0) {
return true;
}
Expand Down
6 changes: 6 additions & 0 deletions scriptlets/src/json-prune.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,10 @@ describe('json-prune', () => {
const obj = JSON.parse('{"a": 123}');
expect(obj).toEqual({});
});

test('"null" gets parsed to null', () => {
jsonPrune('test');

expect(JSON.parse('null')).toEqual(null);
});
});

0 comments on commit de6ecf3

Please sign in to comment.