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
17 changes: 15 additions & 2 deletions src/Smartpay/webhooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createHmac } from 'crypto';
import { createHmac, timingSafeEqual } from 'crypto';

import basex from 'base-x';
// eslint-disable-next-line import/no-extraneous-dependencies
Expand Down Expand Up @@ -187,7 +187,20 @@ const webhooksMixin = <T extends Constructor>(Base: T) => {
secret,
});

return signature === calculatedSignature;
const signatureBuffer = Buffer.from(signature, 'utf8');
const calculatedSignatureBuffer = Buffer.from(
calculatedSignature,
'utf8'
);

// Compare in constant time to avoid leaking the expected signature
// through timing differences. timingSafeEqual requires equal-length
// inputs, so a length mismatch is an early (safe) rejection.
if (signatureBuffer.length !== calculatedSignatureBuffer.length) {
return false;
}

return timingSafeEqual(signatureBuffer, calculatedSignatureBuffer);
}

static expressWebhookMiddleware(secret: string | Function) {
Expand Down
35 changes: 35 additions & 0 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,41 @@ test('Verify Webhook Signature Verification function', function testWebhookSigna
t.ok(Smartpay.verifyWebhookSignature({ data, signature, secret }));
});

test('Reject invalid webhook signature', function testWebhookSignatureReject(t) {
t.plan(3);

// eslint-disable-next-line max-len
const data = `1653028612220.{"id":"evt_test_dwPfFKu5iSEKyHR2LFj9Lx","object":"event","createdAt":1653028523052,"test":true,"eventData":{"type":"payment.created","version":"2022-02-18","data":{"id":"payment_test_35LxgmF5KM22XKG38BjpJg","object":"payment","test":true,"createdAt":1653028523020,"updatedAt":1653028523020,"amount":200,"currency":"JPY","order":"order_test_RiYq2rthzRHrkKVGeucSwn","reference":"order_ref_1234567","status":"processed","metadata":{}}}}`;
const secret = 'gybcsjixKyBW2d4z6iNPlaYzHUMtawnodwZt3W0q';
const validSignature =
'68007ada8485ea0ceca7c5e879ae860a50412b7af95ab8e81b32a3e13f3c0832';

// Wrong signature of the same length must be rejected.
const wrongSignature =
`${validSignature.slice(0, -1)}0` === validSignature
? `${validSignature.slice(0, -1)}1`
: `${validSignature.slice(0, -1)}0`;

t.notOk(
Smartpay.verifyWebhookSignature({ data, signature: wrongSignature, secret })
);

// A signature shorter than the expected one must be rejected (and must not
// throw from the constant-time comparison, which requires equal lengths).
t.notOk(
Smartpay.verifyWebhookSignature({ data, signature: 'deadbeef', secret })
);

// A signature longer than the expected one must be rejected.
t.notOk(
Smartpay.verifyWebhookSignature({
data,
signature: `${validSignature}00`,
secret,
})
);
});

test('Test retry policy', async function testRetryPolicy(t) {
t.plan(2);

Expand Down