Skip to content

Commit

Permalink
Merge pull request #523 from stripe/ob-webhook-convert-buffer
Browse files Browse the repository at this point in the history
Handle Buffer instances in Webhook.constructEvent
  • Loading branch information
ob-stripe authored Nov 23, 2018
2 parents 324b972 + e226be5 commit 55ebb45
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
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

0 comments on commit 55ebb45

Please sign in to comment.