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/curly-challenges-deny-empty-id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'mppx': patch
---

Rejected empty Payment challenge IDs during construction and deserialization.
33 changes: 33 additions & 0 deletions src/Challenge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ describe('from', () => {
expect(challenge.expires).toBe('2025-01-06T12:00:00.000Z')
})

test('error: rejects empty id', () => {
expect(() =>
Challenge.from({
id: '',
realm: 'api.example.com',
method: 'tempo',
intent: 'charge',
request: { amount: '1000000' },
}),
).toThrow()
})

// ---------------------------------------------------------------------------
// HMAC Challenge ID Test Vectors
//
Expand Down Expand Up @@ -417,6 +429,22 @@ describe('fromMethod', () => {
}),
).toThrow()
})

test('error: rejects explicit empty id instead of falling back to secretKey', () => {
expect(() =>
Challenge.fromMethod(Methods.charge, {
id: '',
secretKey: 'my-secret',
realm: 'api.example.com',
request: {
amount: '1',
currency: '0x20c0000000000000000000000000000000000001',
decimals: 6,
recipient: '0x742d35Cc6634C0532925a3b844Bc9e7595f8fE00',
},
} as any),
).toThrow()
})
})

describe('serialize', () => {
Expand Down Expand Up @@ -635,6 +663,11 @@ describe('deserialize', () => {
'Payment id="a", realm="api", method="tempo", intent="charge", request="e30", ="value"',
error: 'Malformed auth-param.',
},
{
name: 'empty id',
header: 'Payment id="", realm="api", method="tempo", intent="charge", request="e30"',
error: 'Invalid input',
},
])('error: throws for $name', ({ header, error }) => {
expect(() => Challenge.deserialize(header)).toThrow(error)
})
Expand Down
4 changes: 2 additions & 2 deletions src/Challenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const Schema = z.object({
/** Optional expiration timestamp (ISO 8601). */
expires: z.optional(z.datetime()),
/** Unique challenge identifier (HMAC-bound). */
id: z.string(),
id: z.string().check(z.minLength(1)),
/** Intent type (e.g., "charge", "session"). */
intent: z.string(),
/** Payment method (e.g., "tempo", "stripe"). */
Expand Down Expand Up @@ -241,7 +241,7 @@ export function fromMethod<const method extends Method.Method>(
const request = PaymentRequest.fromMethod(method, parameters.request)

return from({
...(id ? { id } : { secretKey }),
...(id !== undefined ? { id } : { secretKey }),
realm,
method: methodName,
intent: intent,
Expand Down
Loading