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
5 changes: 5 additions & 0 deletions .changeset/seven-moons-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@contentauth/c2pa-web': patch
---

Add null check to avoid potential crashes.
24 changes: 24 additions & 0 deletions packages/c2pa-web/src/lib/settings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ describe('settings', () => {
JSON.stringify({ builder: { generate_c2pa_archive: true } })
);
});

test('should not throw when a settings value is null', async () => {
// typeof null === 'object' in JS — without a null guard this crashes
const settingsString = await settingsToWasmJson({
verify: null as any
});

expect(settingsString).toEqual(
JSON.stringify({ builder: { generate_c2pa_archive: true }, verify: null })
);
});

test('should not throw when a nested settings value is null', async () => {
const settingsString = await settingsToWasmJson({
trust: { userAnchors: null as any }
});

expect(settingsString).toEqual(
JSON.stringify({
builder: { generate_c2pa_archive: true },
trust: { user_anchors: null }
})
);
});
});

describe('trust', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/c2pa-web/src/lib/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function snakeCaseify(object: SettingsObjectType): SettingsObjectType {
const formattedObject = Object.entries(object).reduce(
(formattedObject, [key, val]) => {
formattedObject[snakeCase(key)] =
typeof val === 'object' ? snakeCaseify(val) : val;
typeof val === 'object' && val !== null ? snakeCaseify(val) : val;
Comment thread
tmathern marked this conversation as resolved.
return formattedObject;
},
{} as SettingsObjectType
Expand Down
Loading