express-all-interactions example uses old (and undefined) verification token
Description
The express-all-interactions example has mostly switched to using the signing secret for validating requests from Slack. However, the endpoint for the slash commands (function slackSlashCommand of server.js) still uses the undefined constant slackVerificationToken which does not exist anywhere else in the code, which is a bug. It is also not obvious how the slash command endpoint should use the signing secret to validate the request.
What type of issue is this? (place an x in one of the [ ])
- [x] bug
Requirements
- [x] I've read and understood the Contributing guidelines and have done my best effort to follow them.
- [x] I've read and agree to the Code of Conduct.
- [x] I've searched for any related issues and avoided creating a duplicate issue.
Bug Report
The file in question is examples/express-all-interactions/server.js, and more specifically line 217, where slackVerificationToken is used.
Expected result:
Some example code for doing request verification for slash command endpoints would be nice, since it is currently not obvious how this is done. If a body-parser such as the urlencode-parser is used, the req.body property will not be the raw body which is required in the HMAC-calculations.
On the other hand, if a raw-body parser is used, the parsing must be done manually which is cumbersome. Perhaps some convenience middleware performing both validation and parsing could be exported from @slack/interactive-messages or @slack/events-api since the validation is done internally already, just not for slash command endpoints.
thanks for catching this one @zozs, you're totally right.
in the @slack/events-api package, which is very similar to this one, we shipped an update which exposes a verfiyRequestSignature() function. i think a good way to solve this problem would be to do something similar in this package (😞 code repetition - it can be factored out into a common dependency package later if we'd like) and then utilize that function to verify the signature.
what do you think?
Great! I hadn't noticed that functionality. I agree that the function helps a lot and it would be a nice bonus to have it in the interactive-messages sdk as well, even though just importing the event-api is a viable solution as well if one isn't scared of some extra dependencies.
@aoberoi I've tested the example and received the error that token is undefined at line 217. This issue seems to be related to my error. When I removed the req.body.token === slackVerificationToken from the if statement, and used body-parser because the req.body was empty, it seemed to work properly.
const bodyParser = require('body-parser');
// Use Body Parser
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
And then I ran into errors mentioned in this issue ('Error: Parsing request body prohibits request signature verification'). Adding the bodyParser to Express after adding the Middleware fixes it, though. Is this something to add to the example perhaps? Right now the example breaks and it's a bit vague why it doesn't work.
@ErwinLengkeek yep, it's more confusing than i like right now.
so an express router's job is essentially to plug middleware and handlers into specific paths of URLs. it's really good at this job, and we should leverage it to solve the "where does my body-parser go?" problem. unfortunately, most people learn how to use express and body-parser by rote copy and paste, and don't realize that you don't have to mount it using app.use(...) (which puts it in the path of all handlers, no matter which path is being requested), but instead it can be put specifically on just the slash command handler (so that it doesn't interfere with the raw body parsing needed for this package's middleware).
ideally, we'd just place the body parsing middleware as the between the two arguments here:
https://github.com/slackapi/node-slack-interactive-messages/blob/2cfc05805c79a62279ef828e3a6bd345805d8719/examples/express-all-interactions/server.js#L28
@aoberoi Gotcha! I've put a PR fixing this and completely removing the mention of the verification token as well (comments/.env file).
I wasn't aware myself that you could put the body-parser into the route itself! Learn something new every day 👍
Following on from @ErwinLengkeek
Error: Parsing request body prohibits request signature verification
Is fixable by ordering from This
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({ extended: false }));
this.app.use('/actions', this.slackInteractions.expressMiddleware());
To
this.app.use('/actions', this.slackInteractions.expressMiddleware());
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({ extended: false }));
Just to be clear, we agree that the express-all-interactions example is working properly, right?
@aoberoi it appears all is good 👍 just documentation needs to clarify the section above about ordering the bodyParser and Middleware