-
Notifications
You must be signed in to change notification settings - Fork 118
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
fix: add webhook signature and clean up inquiry payload checks #280
base: master
Are you sure you want to change the base?
fix: add webhook signature and clean up inquiry payload checks #280
Conversation
app/controllers/api/v1/inquiries.js
Outdated
|
||
ctx.logger.info('creating inquiry from webhook'); | ||
|
||
// TODO: Add support for webhook payload signature: | ||
// https://stackoverflow.com/questions/68885086/how-to-create-signed-webhook-requests-in-nodejs/68885281#68885281 | ||
if (!requestHeaders['X-Webhook-Signature']) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-if (!requestHeaders['X-Webhook-Signature']) {
+if (!_.isObject(requestHeaders) || !isSANB(requestHeaders['X-Webhook-Signature'])) {
app/controllers/api/v1/inquiries.js
Outdated
parsed.headers.has('X-Auto-Respond') || | ||
parsed.headers.has('X-AutoRespond') || | ||
(parsed.headers.has('Precedence') && | ||
headers.hasHeader('List-Id') || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need all this, you can just do stuff like:
headers.hasHeader('auto-submitted')
All lowercase works, it's case insensitive - that's the whole reason we use new Headers
instead of this massive ||
conditional so we can do case insensitive key matches.
app/controllers/api/v1/inquiries.js
Outdated
const config = require('#config'); | ||
const env = require('#config/env'); | ||
const { Inquiries, Users } = require('#models'); | ||
|
||
const webhookSignatureKey = process.env.WEBHOOK_SIGNATURE_KEY; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not correct using of @ladjs/env
. Try to never use process
if possible to get process.env
.
Instead add this:
+const env = require('#config/env');
const config = require('#config');
And then use const webhookSignatureKey = env.WEBHOOK_SIGNATURE_KEY;
) | ||
); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be wrapped, e.g.
if (isSANB(webhookSignatureKey)) {
// all the code like `const webhookSignature = ...` goes inside here
} else {
// if there wasn't a key detected in the `.env` file then we should alert admins
const err = new TypeError('Webhook signature key missing, did you forget to add it to .env?');
err.isCodeBug = true;
logger.fatal(err);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comments for changes.
🙏 👏 |
78e0fc5
to
0a1b2ce
Compare
Checklist