Skip to content
Open
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
10 changes: 9 additions & 1 deletion src/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ function isObject(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null;
}

function isNonEmptySignedField(receipt: Record<string, unknown>, field: string): boolean {
const value = receipt[field];
if (value === undefined || value === null) {
return false;
}
return true;
}

export async function verifyReceipt(receipt: unknown, options: VerifyOptions): Promise<VerifyResult> {
if (!isObject(receipt)) {
return {
Expand All @@ -74,7 +82,7 @@ export async function verifyReceipt(receipt: unknown, options: VerifyOptions): P
}

for (const field of REQUIRED_SIGNED_FIELDS) {
if (!(field in receipt)) {
if (!isNonEmptySignedField(receipt, field)) {
return {
verified: false,
exitCode: 3,
Expand Down
16 changes: 16 additions & 0 deletions tests/verify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,20 @@ describe('verifyReceipt', () => {
expect(result.errorCode).toBe('SIGNATURE_INVALID');
}
});

it('returns malformed when a required signed field is null', async () => {
const receipt = await loadJson('valid.json') as Record<string, unknown>;
// The canonicalizer omits null values from signing bytes, but the
// previous required-field check only tested `field in receipt`,
// so a key whose value is null satisfied presence and produced a
// verified result against a tampered requestJson scope.
receipt.requestJson = null;
const result = await verifyReceipt(receipt, { keyFile, noNetwork: true });
expect(result.verified).toBe(false);
if (!result.verified) {
expect(result.exitCode).toBe(3);
expect(result.errorCode).toBe('MALFORMED_RECEIPT');
expect(result.errorMessage.toLowerCase()).toContain('requestjson');
}
});
});