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

feat: verify signature from event webhook #1136

Merged
merged 3 commits into from
Jun 4, 2020
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
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ node_js:
- 10
env:
- version=6
- version=7
- version=8
- version=10
- version=lts
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ We welcome direct contributions to the sendgrid-nodejs code base. Thank you!

##### Prerequisites #####

- Node.js version 6, 7 or 8
- Node.js version 6, 8 or >=10
- Please see [package.json](https://github.com/sendgrid/sendgrid-nodejs/tree/master/package.json)

##### Initial setup: #####
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/webhooks-docker/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ We welcome direct contributions to the sendgrid-nodejs code base. Thank you!

##### Prerequisites #####

- Node.js version 6, 7 or 8
- Node.js versions 6, 8, or >=10
- Please see [package.json](https://github.com/sendgrid/sendgrid-nodejs/tree/master/package.json)

##### Initial setup: #####
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"test:helpers": "babel-node ./node_modules/istanbul/lib/cli cover ./node_modules/mocha/bin/_mocha \"packages/helpers/**/*.spec.js\"",
"test:client": "babel-node ./node_modules/istanbul/lib/cli cover ./node_modules/mocha/bin/_mocha \"packages/client/**/*.spec.js\"",
"test:mail": "babel-node ./node_modules/istanbul/lib/cli cover ./node_modules/mocha/bin/_mocha \"packages/mail/**/*.spec.js\"",
"test:eventwebhook": "babel-node ./node_modules/istanbul/lib/cli cover ./node_modules/mocha/bin/_mocha \"packages/eventwebhook/**/*.spec.js\"",
"test:inbound": "babel-node ./node_modules/istanbul/lib/cli cover ./node_modules/mocha/bin/_mocha \"packages/inbound-mail-parser/**/*.spec.js\"",
"test:contact": "babel-node ./node_modules/istanbul/lib/cli cover ./node_modules/mocha/bin/_mocha \"packages/contact-importer/**/*.spec.js\"",
"test:files": "babel-node ./node_modules/istanbul/lib/cli cover ./node_modules/mocha/bin/_mocha \"test/files.spec.js\"",
Expand Down
2 changes: 1 addition & 1 deletion packages/client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ To be notified when this package is updated, please subscribe to email [notifica

## Prerequisites

- Node.js version 6, 7 or 8
- Node.js version 6, 8 or >=10
- A Twilio SendGrid account, [sign up for free](https://sendgrid.com/free?source=sendgrid-nodejs) to send up to 40,000 emails for the first 30 days or check out [our pricing](https://sendgrid.com/pricing?source=sendgrid-nodejs).

## Obtain an API Key
Expand Down
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"main": "index.js",
"engines": {
"node": ">=6.0.0"
"node": "6.* || 8.* || >=10.*"
eshanholtz marked this conversation as resolved.
Show resolved Hide resolved
},
"dependencies": {
"@sendgrid/helpers": "^7.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/contact-importer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"main": "src/importer.js",
"engines": {
"node": ">=6.0.0"
"node": "6.* || 8.* || >=10.*"
},
"publishConfig": {
"access": "public"
Expand Down
3 changes: 3 additions & 0 deletions packages/eventwebhook/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import EventWebhook = require('./src/eventwebhook');

export = EventWebhook;
5 changes: 5 additions & 0 deletions packages/eventwebhook/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

const EventWebhook = require('./src/eventwebhook');

module.exports = EventWebhook;
28 changes: 28 additions & 0 deletions packages/eventwebhook/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@sendgrid/eventwebhook",
"description": "Twilio SendGrid NodeJS Event Webhook",
"version": "1.0.0",
"author": "Twilio SendGrid <[email protected]> (sendgrid.com)",
"contributors": [
"Elise Shanholtz <[email protected]>"
],
"license": "MIT",
"homepage": "https://sendgrid.com",
"repository": {
"type": "git",
"url": "git://github.com/sendgrid/sendgrid-nodejs.git"
},
"publishConfig": {
"access": "public"
},
"main": "src/eventwebhook.js",
"engines": {
"node": "6.* || 8.* || >=10.*"
},
"dependencies": {
"@starkbank/ecdsa": "^0.0.3"
},
"tags": [
"sendgrid"
]
}
22 changes: 22 additions & 0 deletions packages/eventwebhook/src/eventwebhook.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {PublicKey} from "@starkbank/ecdsa";

declare class EventWebhook {
/**
*
* @param {string} publicKey verification key under Mail Settings
* @return {PublicKey} A public key using the ECDSA algorithm
*/
convertPublicKeyToECDSA(publicKey: string): PublicKey;

/**
*
* @param {PublicKey} publicKey elliptic curve public key
* @param {object|string} payload event payload in the request body
* @param {string} signature value obtained from the 'X-Twilio-Email-Event-Webhook-Signature' header
* @param {string} timestamp value obtained from the 'X-Twilio-Email-Event-Webhook-Timestamp' header
* @return {Boolean} true or false if signature is valid
*/
verifySignature(publicKey: PublicKey, payload: object|string, signature: string, timestamp: string): boolean;
}

export = EventWebhook;
35 changes: 35 additions & 0 deletions packages/eventwebhook/src/eventwebhook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const ecdsa = require('@starkbank/ecdsa');
const Ecdsa = ecdsa.Ecdsa;
const Signature = ecdsa.Signature;
const PublicKey = ecdsa.PublicKey;

class EventWebhook {
/**
*
eshanholtz marked this conversation as resolved.
Show resolved Hide resolved
* @param {string} publicKey verification key under Mail Settings
* @return {PublicKey} A public key using the ECDSA algorithm
*/
convertPublicKeyToECDSA(publicKey) {
return PublicKey.fromPem(publicKey);
}

/**
*
* @param {PublicKey} publicKey elliptic curve public key
* @param {Object|string} payload event payload in the request body
* @param {string} signature value obtained from the 'X-Twilio-Email-Event-Webhook-Signature' header
* @param {string} timestamp value obtained from the 'X-Twilio-Email-Event-Webhook-Timestamp' header
* @return {Boolean} true or false if signature is valid
*/
verifySignature(publicKey, payload, signature, timestamp) {
let timestampPayload = typeof payload === 'object' ? JSON.stringify(payload) : payload;
timestampPayload = timestamp + timestampPayload;
const decodedSignature = Signature.fromBase64(signature);

return Ecdsa.verify(timestampPayload, decodedSignature, publicKey);
}
}

module.exports = EventWebhook;
65 changes: 65 additions & 0 deletions packages/eventwebhook/src/eventwebhook.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const EventWebhook = require('./eventwebhook');

describe('EventWebhook', () => {
const PUBLIC_KEY = 'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEEDr2LjtURuePQzplybdC+u4CwrqDqBaWjcMMsTbhdbcwHBcepxo7yAQGhHPTnlvFYPAZFceEu/1FwCM/QmGUhA==';
const SIGNATURE = 'MEUCIQCtIHJeH93Y+qpYeWrySphQgpNGNr/U+UyUlBkU6n7RAwIgJTz2C+8a8xonZGi6BpSzoQsbVRamr2nlxFDWYNH2j/0=';
const TIMESTAMP = '1588788367';
const PAYLOAD = {
event: 'test_event',
category: 'example_payload',
message_id: 'message_id',
};

describe('#verifySignature()', () => {
it('should verify a valid signature', () => {
expect(verify(
PUBLIC_KEY,
PAYLOAD,
SIGNATURE,
TIMESTAMP
)).to.be.true;
});

it('should reject for invalid key', () => {
expect(verify(
'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEqTxd43gyp8IOEto2LdIfjRQrIbsd4SXZkLW6jDutdhXSJCWHw8REntlo7aNDthvj+y7GjUuFDb/R1NGe1OPzpA==',
PAYLOAD,
SIGNATURE,
TIMESTAMP
)).to.be.false;
});

it('should reject for bad payload', () => {
expect(verify(
PUBLIC_KEY,
'payload',
SIGNATURE,
TIMESTAMP
)).to.be.false;
});

it('should reject for bad signature', () => {
expect(verify(
PUBLIC_KEY,
PAYLOAD,
'MEUCIQCtIHJeH93Y+qpYeWrySphQgpNGNr/U+UyUlBkU6n7RAwIgJTz2C+8a8xonZGi6BpSzoQsbVRamr2nlxFDWYNH3j/0=',
TIMESTAMP
)).to.be.false;
});

it('should reject for bad timestamp', () => {
expect(verify(
PUBLIC_KEY,
PAYLOAD,
SIGNATURE,
'timestamp'
)).to.be.false;
});
});
});

function verify(publicKey, payload, signature, timestamp) {
const ew = new EventWebhook();
const key = ew.convertPublicKeyToECDSA(publicKey);
return ew.verifySignature(key, payload, signature, timestamp);
}
2 changes: 1 addition & 1 deletion packages/inbound-mail-parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ To be notified when this package is updated, please subscribe to email [notifica

## Prerequisites

- Node.js version 6, 7 or 8
- Node.js version 6, 8 or >=10
- A Twilio SendGrid account, [sign up for free](https://sendgrid.com/free?source=sendgrid-nodejs) to send up to 40,000 emails for the first 30 days or check out [our pricing](https://sendgrid.com/pricing?source=sendgrid-nodejs).

## Obtain an API Key
Expand Down
2 changes: 1 addition & 1 deletion packages/inbound-mail-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"main": "src/parser.js",
"engines": {
"node": ">=6.0.0"
"node": "6.* || 8.* || >=10.*"
},
"dependencies": {
"@sendgrid/helpers": "^7.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/mail/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ To be notified when this package is updated, please subscribe to email [notifica

## Prerequisites

- Node.js version 6, 7 or 8
- Node.js version 6, 8 or >=10
- A Twilio SendGrid account, [sign up for free](https://sendgrid.com/free?source=sendgrid-nodejs) to send up to 40,000 emails for the first 30 days or check out [our pricing](https://sendgrid.com/pricing?source=sendgrid-nodejs).

## Obtain an API Key
Expand Down
2 changes: 1 addition & 1 deletion packages/mail/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"main": "index.js",
"engines": {
"node": ">=6.0.0"
"node": "6.* || 8.* || >=10.*"
},
"publishConfig": {
"access": "public"
Expand Down
2 changes: 1 addition & 1 deletion packages/subscription-widget/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
"access": "public"
},
"engines": {
"node": ">=6.0.0"
"node": "6.* || 8.* || >=10.*"
}
}
13 changes: 13 additions & 0 deletions test/typescript/eventwebhook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import EventWebhook = require('@sendgrid/eventwebhook');

var ew = new EventWebhook();
const PUBLIC_KEY = "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEEDr2LjtURuePQzplybdC+u4CwrqDqBaWjcMMsTbhdbcwHBcepxo7yAQGhHPTnlvFYPAZFceEu/1FwCM/QmGUhA==";
const SIGNATURE = "MEUCIQCtIHJeH93Y+qpYeWrySphQgpNGNr/U+UyUlBkU6n7RAwIgJTz2C+8a8xonZGi6BpSzoQsbVRamr2nlxFDWYNH2j/0=";
const TIMESTAMP = "1588788367";
const PAYLOAD = {
event: 'test_event',
category: 'example_payload',
message_id: 'message_id',
};
var key = ew.convertPublicKeyToECDSA(PUBLIC_KEY);
console.log(ew.verifySignature(key, PAYLOAD, SIGNATURE, TIMESTAMP));