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

Picker should handle middleware errors #3

Open
rubenhelsloot opened this issue Jul 18, 2022 · 2 comments
Open

Picker should handle middleware errors #3

rubenhelsloot opened this issue Jul 18, 2022 · 2 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@rubenhelsloot
Copy link

Thank you for reviving this package!

I wanted to ask to open issue #12 here too, because it's highly relevant.

Situation

I maintain an application that accepts incoming webhooks from Shopify. The data on that webhook is sent in JSON format, so I've registered a bodyparser.json() middleware in Picker. Normally, I then check that the hash of the JSON body is the same as the value of one of the request headers. This is also described in their docs.

However, when the body size is too large, req.body is empty with no warning whatsoever. Calculating the hmac fails and the webhook is aborted. But the true error happened way earlier in the request, namely when the middleware parsing failed. Ideally, I'd want to have been notified on parse error, not later.

A subset of my code:

Picker.middleware(bodyParser.json({
  verify: (req, res, buf) => {
    req.rawBody = buf;
  },
}));

/**
 * Cryptomagically check the webhook shared secret to make sure the
 * webhook originates from Shopify
 */
const checkShopifyWebhookSecret = (req, country) => {
  const webhookSecret = Meteor.settings.shopifyWebhookSecret[country]
  const hmac = req.headers['x-shopify-hmac-sha256']

  let success = true;
  try {
    // Create a hash using the body and our key
    const hash = crypto
      .createHmac('sha256', webhookSecret)
      .update(req.rawBody, 'utf8', 'hex')
      .digest('base64')

    // check if webhook is valid
    success = hash === hmac;
  } catch (e) {
    logger.info("Unable to validate webhook", { body: req.body, error: e });
    success = false;
  }

  if (!success) throw new Error('invalid-webhook-request');
}
@StorytellerCZ StorytellerCZ added bug Something isn't working enhancement New feature or request labels Dec 31, 2022
@StorytellerCZ
Copy link
Member

@rubenhelsloot thank you for raising this issues. I have added it to my todos and hopefully will get a chance to look at it in the coming months.

@perbergland
Copy link

Just stumbled on this problem myself but Picker seems to be going away in favour of (express core functionality?) in Meteor 3 so maybe not worth fixing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
Development

No branches or pull requests

3 participants