Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Buffer instances in Webhook.constructEvent #523

Merged
merged 1 commit into from
Nov 23, 2018
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
11 changes: 8 additions & 3 deletions lib/Webhooks.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
'use strict';

var Buffer = require('safe-buffer').Buffer;
var crypto = require('crypto');

var utils = require('./utils');
var Error = require('./Error');

var Webhook = {
DEFAULT_TOLERANCE: 300,
DEFAULT_TOLERANCE: 300, // 5 minutes

constructEvent: function(payload, header, secret, tolerance) {
var jsonPayload = JSON.parse(payload);

this.signature.verifyHeader(payload, header, secret, tolerance || Webhook.DEFAULT_TOLERANCE);

var jsonPayload = JSON.parse(payload);
return jsonPayload;
},
};
Expand All @@ -25,6 +27,9 @@ var signature = {
},

verifyHeader: function(payload, header, secret, tolerance) {
payload = Buffer.isBuffer(payload) ? payload.toString('utf8') : payload;
header = Buffer.isBuffer(header) ? header.toString('utf8') : header;

var details = parseHeader(header, this.EXPECTED_SCHEME);

if (!details || details.timestamp === -1) {
Expand Down
9 changes: 9 additions & 0 deletions test/Webhook.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var stripe = require('../testUtils').getSpyableStripe();
var expect = require('chai').expect;
var Buffer = require('safe-buffer').Buffer;

var EVENT_PAYLOAD = {
id: 'evt_test_webhook',
Expand Down Expand Up @@ -125,6 +126,14 @@ describe('Webhooks', function() {

expect(stripe.webhooks.signature.verifyHeader(EVENT_PAYLOAD_STRING, header, SECRET)).to.equal(true);
});

it('should accept Buffer instances for the payload and header', function() {
var header = generateHeaderString({
timestamp: (Date.now() / 1000),
});

expect(stripe.webhooks.signature.verifyHeader(new Buffer(EVENT_PAYLOAD_STRING), new Buffer(header), SECRET, 10)).to.equal(true);
});
});
});

Expand Down