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
3 changes: 2 additions & 1 deletion src/canonicalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ function sortKeys(obj: unknown): unknown {
return obj.map(sortKeys);
}

const sorted: Record<string, unknown> = {};
// Preserve "__proto__" as signed data instead of invoking Object.prototype's setter.
const sorted: Record<string, unknown> = Object.create(null) as Record<string, unknown>;
const keys = Object.keys(obj as Record<string, unknown>).sort();

for (const key of keys) {
Expand Down
18 changes: 18 additions & 0 deletions tests/verify.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { readFile } from 'node:fs/promises';
import { resolve } from 'node:path';
import { sign } from 'node:crypto';
import { describe, expect, it } from 'vitest';
import { canonicalizeReceiptBytes } from '../src/canonicalize.js';
import { verifyReceipt } from '../src/verify.js';

async function loadJson(name: string): Promise<unknown> {
Expand All @@ -9,6 +11,7 @@ async function loadJson(name: string): Promise<unknown> {
}

const keyFile = resolve(process.cwd(), 'tests/fixtures/public-key.pem');
const privateKeyFile = resolve(process.cwd(), 'tests/fixtures/private-key.pem');

describe('verifyReceipt', () => {
it('verifies a valid receipt', async () => {
Expand Down Expand Up @@ -59,4 +62,19 @@ describe('verifyReceipt', () => {
expect(result.errorCode).toBe('SIGNATURE_INVALID');
}
});

it('rejects tampering with a nested __proto__ request field', async () => {
const receipt = await loadJson('valid.json') as Record<string, unknown>;
const privateKey = await readFile(privateKeyFile, 'utf8');
receipt.requestJson = JSON.parse('{"action":"deploy:production","__proto__":{"scope":"before"}}');
receipt.signatureValue = sign(null, canonicalizeReceiptBytes(receipt), privateKey).toString('base64');
receipt.requestJson = JSON.parse('{"action":"deploy:production","__proto__":{"scope":"after"}}');

const result = await verifyReceipt(receipt, { keyFile, noNetwork: true });
expect(result.verified).toBe(false);
if (!result.verified) {
expect(result.exitCode).toBe(1);
expect(result.errorCode).toBe('SIGNATURE_INVALID');
}
});
});