-
Notifications
You must be signed in to change notification settings - Fork 779
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
Unable to verify headers using @sendgrid/eventwebhook lib #1322
Comments
I am having this same problem - I am on the same version of sendgrid-nodejs and I used the sample code from https://www.twilio.com/docs/runtime/quickstart/validate-webhook-requests-from-sendgrid |
Has anyone found a fix for this? I'm also having the same issues. |
Do the changes in the linked PR help to validate the payload? |
From my experience, it is very important that the request body is not parsed as JSON or string, and is the raw Buffer! This is the reason that the "hack" with The correct way to do it is to make sure that For example, this is the setup I have and is working 100% (I cleaned it up a bit to share it here). const unless = require("express-unless");
const bodyParser = require("body-parser");
const { EventWebhook, EventWebhookHeader } = require("@sendgrid/eventwebhook");
// Important: apply body-parser json for all endpoints EXCEPT the webhook!
const parse_json = bodyParser.json();
parse_json.unless = unless;
app.use(
parse_json.unless({
path: ["/api/webhooks/sendgrid"],
})
);
app.post(
"/api/webhooks/sendgrid",
bodyParser.raw({ type: "application/json" }), // Important: parse body as raw Buffer
(req, res) => {
console.log("Is Buffer?", Buffer.isBuffer(req.body)); // must return true !
const signature = req.get(EventWebhookHeader.SIGNATURE());
const timestamp = req.get(EventWebhookHeader.TIMESTAMP());
const eventWebhook = new EventWebhook();
const key = eventWebhook.convertPublicKeyToECDSA(pubkey);
if (eventWebhook.verifySignature(key, req.body, signature, timestamp)) {
const events = JSON.parse(req.body);
// TODO: handle events
res.sendStatus(204);
} else {
res.status(401).json({ message: "Invalid signature" });
}
}
);
|
@danmana And also do we have to use the body-parser package? It seems it is deprecated and is now included in express package. |
Yes, we are in fact "overriding" the middleware for the webhook. This is a global middleware that applies to all paths, except the webhook - see unless. app.use(
parse_json.unless({
path: ["/api/webhooks/sendgrid"],
})
); This is a middleware applied just to the webhook path (nothing to override as we excluded this path from the global middleware using unless). app.post(
"/api/webhooks/sendgrid",
bodyParser.raw({ type: "application/json" }), // Important: parse body as raw Buffer Yes you can replace |
Issue Summary
When attempting to use the @sendgrid/eventwebhook methods to verify Sendgrid signed event headers the method
verifySignature()
always returns false. Below code is written as an expressJS middleware function to run when the webhook sends a POST call to our endpoint.Steps to Reproduce
Expected response from method is true
Code Snippet
Technical details:
The text was updated successfully, but these errors were encountered: