`verifyRequestSignature` does not create a signature that's the same as slack's
Description
verifyRequestSignature does not seem to be working, maybe doesn't work very well with the body-parser package
What type of issue is this? (place an x in one of the [ ])
- [x] bug
- [ ] enhancement (feature request)
- [ ] question
- [ ] documentation related
- [ ] testing related
- [ ] discussion
Requirements (place an x in each of the [ ])
- [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
verifyRequestSignature does not seem to be working, either debug steps should be created or an example snippet. Here is my code:
const { verifyRequestSignature } = require('@slack/events-api');
const { SLACK_SIGNING_SECRET } = require('../config');
const receiveWebhook = (botCtrl) => async (req, res) => {
const { body } = req;
const ts = req.headers['x-slack-request-timestamp'];
const signature = req.headers['x-slack-signature'];
payload = qs.stringify(body, { format: 'RFC1738' });
return verifyRequestSignature({
signingSecret: SLACK_SIGNING_SECRET,
requestSignature: signature,
requestTimestamp: ts,
body: payload,
});
// always returns false
}
My node.js express code looks like this:
const bodyParser = require('body-parser');
const compression = require('compression');
const cookieParser = require('cookie-parser');
const cors = require('cors');
const express = require('express');
const methodOverride = require('method-override');
const morgan = require('morgan');
const path = require('path');
const { errorHandler: bodyErrorHandler } = require('bodymen');
const winston = require('./lib/logger');
const { error } = require('./api/responses');
module.exports = (apiRoot, routes) => {
const app = express();
app.use(cors());
app.use(compression());
app.use(morgan('combined', { stream: winston.stream }));
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(
methodOverride((req) => {
if (req.body && typeof req.body === 'object' && '_method' in req.body) {
const method = req.body._method;
delete req.body._method;
return method;
}
}),
);
app.get('/favicon.ico', (req, res) => res.sendStatus(204));
app.use(apiRoot, routes);
app.use(bodyErrorHandler());
app.use((req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
app.use((err, req, res, next) => {
if (err.status) {
next(err);
} else {
console.error(err);
error(res, err.message);
}
});
return app;
};
Reproducible in:
@slack/events-api version: 2.1.1
node version: v10.12.0
OS version(s): macOS 10.12.6
Steps to reproduce:
- Set up Slack Events API, and node.js express server, and entrypoint for slack events http request
- Import the verification code above
- Install app to your slack workspace
- Go to the bot in your slack workspace and send a DM
- Look at node.js logs and see it fail to verify the payload
Expected result:
verifyRequestSignature should return true
Actual result:
verifyRequestSignature returns false
Attachments:
None, just code.
I found the issue. You need to take the body exactly as Slack provides (in utf-8 Buffer form) and not using qs.stringify (if you look at the raw body, it looks more like a JSON.stringify())
Creating the signature with this works.