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

fix: add webhook signature and clean up inquiry payload checks #280

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

shaunwarman
Copy link
Contributor

Checklist

  • I have ensured my pull request is not behind the main or master branch of the original repository.
  • I have rebased all commits where necessary so that reviewing this pull request can be done without having to merge it first.
  • I have written a commit message that passes commitlint linting.
  • I have ensured that my code changes pass linting tests.
  • I have ensured that my code changes pass unit tests.
  • I have described my pull request and the reasons for code changes along with context if necessary.


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']) {
Copy link
Contributor

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'])) {

parsed.headers.has('X-Auto-Respond') ||
parsed.headers.has('X-AutoRespond') ||
(parsed.headers.has('Precedence') &&
headers.hasHeader('List-Id') ||
Copy link
Contributor

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.

const config = require('#config');
const env = require('#config/env');
const { Inquiries, Users } = require('#models');

const webhookSignatureKey = process.env.WEBHOOK_SIGNATURE_KEY;
Copy link
Contributor

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;

)
);
}

Copy link
Contributor

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);
}

Copy link
Contributor

@titanism titanism left a 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.

@titanism
Copy link
Contributor

🙏 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants