T09 · Insecure Skill Coding Practices
Error
- Location
references/api.md:134- Finding
Webhook Authentication Fails Open When the Signature Header Is Missing
- Content
View full analysis
{ const sig = req.headers['x-merso-signature']; if (sig && !verifyMersoWebhook(req.body, sig, process.env.MERSO_API_KEY)) { return res.status(401).json({ error: 'Invalid signature' }); } // process event }); ``` ### Technical Analysis The authentication condition rejects a request only when the `X-Merso-Signature` header is present and invalid: ```js if (sig && !verifyMersoWebhook(...)) ``` When the header is absent, `sig` is falsy, the verification function is never called, and execution proceeds to payment-event processing. Authentication therefore fails open. The documented integration flow grants an item or license after receiving a successful payment webhook. If an implementation follows this example without an additional authoritative payment verification step, an unauthenticated remote party could submit a forged `payment.succeeded` event. The comparison function has a related availability weakness. Node.js `crypto.timingSafeEqual` requires both buffers to have equal lengths. A malformed signature of an unexpected length can throw an exception. Without controlled error handling, repeated malformed requests could produce server errors and contribute to denial of service. ### Attack Path 1. The attacker discovers or predicts the publicly accessible payment webhook endpoint. 2. The attacker obtains or guesses a valid-looking pa ...[truncated 1554 chars]- Remediation
View remediation
{ const sig = req.headers['x-merso-signature']; if (typeof sig !== 'string') { return res.status(401).json({ error: 'Missing signature' }); } let valid = false; try { valid = verifyMersoWebhook( req.body, sig, process.env.MERSO_WEBHOOK_SECRET ); } catch { valid = false; } if (!valid) { return res.status(401).json({ error: 'Invalid signature' }); } // Validate and process the authenticated event. } ); ``` 2. **Validate signature encoding and length before comparison.** ```js const crypto = require('crypto'); function verifyMersoWebhook(rawBody, signatureHeader, secret) { if ( !Buffer.isBuffer(rawBody) || typeof signatureHeader !== 'string' || typeof secret !== 'string' || !/^[a-fA-F0-9]{64}$/.test(signatureHeader) ) { return false; } const expected = crypto .createHmac('sha256', secret) .update(rawBody) .digest(); const supplied = Buffer.from(signatureHeader, 'hex'); return supplied.length === expected.length && crypto.timingSafeEqual(expected, supplied); } ``` 3. **Use the provider’s formally documented webhook-signature protocol.** Confirm the signature header, signing secret, digest encoding, signed payload format, timestamp handling, and replay-prevention requirements with Merso. Prefer a dedicated webhook secret rather than reusing the general API key. 4. **Verify payment state independently before changing entitlements.** For every success or refund event, query the authenticated `GET /verify-payment-intent/:id` endpoint and require the authoritative response to match the webhook. 5. **Bind each event to a local pending payment reco ...[truncated 1118 chars]
