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

TypeError [ERR_INVALID_ARG_TYPE] webhook when the request type is application/json #617

Closed
neekey opened this issue Oct 12, 2020 · 7 comments
Labels
type: bug bug in the library

Comments

@neekey
Copy link

neekey commented Oct 12, 2020

Issue Summary

I was using Twilio Studio setting up an appointment reminder, for the confirmation widget, I setup the request and content as below:

image

and my server code is simply like below:

app.post('/webhook/twilio/sms/confirm_appointment', twilio.webhook(authToken, {}), (req, res) => {
    // Twilio Messaging URL - receives incoming messages from Twilio
    console.log('sms received', req.body);
    res.send(200);
  });

When the confirmation callback hit my server, Twilio webhook throws:

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type object
app_1   |     at Function.from (buffer.js:232:9)
app_1   |     at getExpectedBodyHash (/app/node_modules/twilio/lib/webhooks/webhooks.js:84:20)
app_1   |     at validateBody (/app/node_modules/twilio/lib/webhooks/webhooks.js:116:22)
app_1   |     at validateRequestWithBody (/app/node_modules/twilio/lib/webhooks/webhooks.js:133:5)
app_1   |     at validateExpressRequest (/app/node_modules/twilio/lib/webhooks/webhooks.js:171:12)
app_1   |     at hook (/app/node_modules/twilio/lib/webhooks/webhooks.js:253:21)
app_1   |     at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
app_1   |     at next (/app/node_modules/express/lib/router/route.js:137:13)
app_1   |     at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
app_1   |     at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
app_1   | Mon, 12 Oct 2020 06:41:23 GMT express deprecated res.send(status, body): Use res.status(status).send(body) instead at src/middlewares/errorHandling.ts:16:13

I checked the twilio source code, mainly caused by this function:

function getExpectedBodyHash(body) {
  return crypto
    .createHash('sha256')
    .update(Buffer.from(body, 'utf-8'))
    .digest('hex');
}

obviously if the request type is application/json, express will parse the request.body to an object right away, which causes the issue.

By changing the request type to "Form URL Encoded" fixes my issue:

image

Steps to Reproduce

  1. Set up Twilio webhook middleware in your express server
  2. Set up a confirmation callback request to your server with request type to application/json

Technical details:

  • twilio-node version: twilio@3.49.4
  • node version: 10.x
@thinkingserious thinkingserious added status: help wanted requesting help from the community type: bug bug in the library labels Oct 14, 2020
@thinkingserious
Copy link
Contributor

Hello @neekey,

Thank you for taking the time to report this!

I was able to reproduce this error locally. This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.

With best regards,

Elmer

@ajbraus
Copy link

ajbraus commented Dec 23, 2021

+1 I'm having this issue in production on Heroku. Was working fine, and then just cropped up after I sanitized phone numbers to be formatted like this: +13334445555, which I thought was the ideal format. It works from localhost. I double checked my config vars and they are correct in production. Not sure why it would be different in production than in local.

  twilio.messages
    .create({
      body: `Here\'s your code: ${user.twoFactorAuthToken}. It expires in 10 minutes.`,
      from: process.env.TWILIO_SENDER_NUMBER,
      to: user.phone
    })
    .then(message => console.log("Twilio sent: ", message.sid))
    .catch(err => console.log("Twilio error: ", err));
2021-12-23T19:06:27.622584+00:00 app[web.1]: Creating a signature for the request...
2021-12-23T19:06:27.626309+00:00 app[web.1]: Twilio error:  TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be of type string or an instance of ArrayBuffer, Buffer, TypedArray, DataView, KeyObject, or CryptoKey. Received undefined
2021-12-23T19:06:27.626310+00:00 app[web.1]: at new NodeError (node:internal/errors:371:5)
2021-12-23T19:06:27.626310+00:00 app[web.1]: at prepareSecretKey (node:internal/crypto/keys:570:11)
2021-12-23T19:06:27.626311+00:00 app[web.1]: at new Hmac (node:internal/crypto/hash:132:9)
2021-12-23T19:06:27.626311+00:00 app[web.1]: at Object.createHmac (node:crypto:162:10)
2021-12-23T19:06:27.626311+00:00 app[web.1]: at createSignature (/app/src/sumsub.js:22:28)
2021-12-23T19:06:27.626312+00:00 app[web.1]: at processTicksAndRejections (node:internal/process/task_queues:96:5) {
2021-12-23T19:06:27.626313+00:00 app[web.1]: code: 'ERR_INVALID_ARG_TYPE'
2021-12-23T19:06:27.626314+00:00 app[web.1]: }

I updated to "twilio": "^3.72.0" and still not working.

@ajbraus
Copy link

ajbraus commented Jan 3, 2022

@ajbraus
Copy link

ajbraus commented Jan 11, 2022

@thinkingserious This problem is still blocking my production sending SMS's. Any progress?

@ajbraus
Copy link

ajbraus commented Apr 17, 2022

Still busted

@ealvesss
Copy link

I'm having issue in production with it. Any progress?

@childish-sambino
Copy link
Contributor

Fixed by #665

@childish-sambino childish-sambino removed the status: help wanted requesting help from the community label Nov 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug bug in the library
Projects
None yet
Development

No branches or pull requests

5 participants