node-slack-interactive-messages icon indicating copy to clipboard operation
node-slack-interactive-messages copied to clipboard

Slack Node SDK Proxy Issue for signing verification

Open JanmejayPattanaik opened this issue 7 years ago • 4 comments

Description

Hi Team,

I am building an APP using Node Slack SDK. I am able to authenticate the user by using auth.test URL and also able to post start up message using im.open or chat.postMessage in the slack.

However, in Proxy environment, I am getting the following error. I am not getting the error message in the no-Proxy setup.

{ Error: Slack request signing verification failed
   at verifyRequestSignature (/opt/app-root/src/node_modules/@slack/events-api/dist/http-handler.js:84:18)
   at /opt/app-root/src/node_modules/@slack/events-api/dist/http-handler.js:189:11
   at <anonymous>
   at process._tickCallback (internal/process/next_tick.js:189:7)
 code: 'SLACKHTTPHANDLER_REQUEST_SIGNATURE_VERIFICATION_FAILURE' } 

Slack package details :

"@slack/client": "^4.8.0", "@slack/events-api": "^2.1.1", "@slack/interactive-messages": "^1.0.2",

Sample Code:

const { WebClient } = require('@slack/client');
const axios = require('axios');
const r = require('request');
const HttpsProxyAgent = require('https-proxy-agent');
const proxyUrl = `${process.env.proxyUrl}:${process.env.proxyPort}`;
var request = r.defaults({'proxy': proxyUrl});

 web: new WebClient(process.env.SLACK_BOT_TOKEN, { agent: new HttpsProxyAgent(proxyUrl) }),

getAuthorizedUser() {
    const AuthStr = 'Bearer '.concat(process.env.SLACK_CLIENT_TOKEN);
    let options  = {
      url:'https://slack.com/api/auth.test?token='+process.env.SLACK_CLIENT_TOKEN,
      headers:{
        'Content-Type':'application/json',
        'Authorization': AuthStr
      },
      json: true
    }

    var context = this;
    request.get(options, function(err, res, body) {  
        if(err){
          console.log("error in code ::: ", err);
        }        
    })
    .on('data', function(data) {
      var locData = JSON.parse(Buffer.from(data).toString());
      context.approver_user_id = locData.user_id;
      context.decisionseeker = locData.user_id;
      context.adminUser();
    })
}

adminUser() {
    var text = `Welcome to app! I'm here to help your team`;
    
    this.web.im.open({user:this.approver_user_id})
      .then((res) => {
        this.web.chat.postMessage({channel:res.channel.id, text, as_user: true, parse: 'full'})}
      )
      .catch((err) => {
        console.log("admin user error code ::: ", err);
      });
  },

As per the Version 4 guide i am using SLACK_SIGNING_SECRET.

What type of issue is this? (place an x in one of the [ ])

  • [ ] bug
  • [ ] enhancement (feature request)
  • [x ] question
  • [ x] documentation related
  • [ x] testing related
  • [x ] discussion

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

Filling out the following details about bugs will help us solve your issue sooner.

Reproducible in:

@slack/client version: 4.8.0 node version:8.9.4 OS version(s): Windows 10

Steps to reproduce:

1. 2. 3.

Expected result:

What you expected to happen

Actual result:

What actually happened

Attachments:

Logs, screenshots, screencast, sample project, funny gif, etc.

JanmejayPattanaik avatar Jan 14 '19 16:01 JanmejayPattanaik

As we discussed before, this error shouldn’t have anything to do with proxy setup.

As per the Version 4 guide i am using SLACK_SIGNING_SECRET.

Your sample code doesn’t show how you are initializing the events API adapter (I don’t see require('@slack/events-api') in this code or how you attach the adapter to your server).

Also, the signing secret is not in the @slack/client v4 migration guide. This error is not related to that package.

aoberoi avatar Jan 15 '19 16:01 aoberoi

Hi Ankur, My events API adapter is invoked in a different file ( index.js ). I am sharing the sample code for the events API invocation in this.

I am not facing any issues while using no Proxy environment ( using ngRok URLs ), where as when i am trying to deploy the code in a more controlled environment ( proxy ), the code is not working.

Sample Code : index.js

require('dotenv').config();

const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');
const slackEventsApi = require('@slack/events-api');
const {createMessageAdapter} = require('@slack/interactive-messages');
const slackBot= require('./src/slackBot');

const slackEvents = slackEventsApi.createEventAdapter(process.env.SLACK_SIGNING_SECRET, {
  includeBody: true
});

slackBot.getAuthorizedUser();

const app = express();
app.use('/slack/events', slackEvents.expressMiddleware());

slackEvents.on('message', (event) => {
  if (event.subtype === 'bot_message' || event.subtype === 'message_changed' || event.subtype === 'message_deleted') {
    return;
  }
  slackBot.postMessage(event.user, event.channel, event.text);
});

slackEvents.on('error', console.error);

const slackMessages = createMessageAdapter(process.env.SLACK_SIGNING_SECRET, {
  includeBody: true
});
app.use('/slack/actions', slackMessages.expressMiddleware());

slackBot.js:

const { WebClient } = require('@slack/client');
const axios = require('axios');
const r = require('request');
const HttpsProxyAgent = require('https-proxy-agent');
const proxyUrl = `${process.env.proxyUrl}:${process.env.proxyPort}`;
var request = r.defaults({'proxy': proxyUrl});

 web: new WebClient(process.env.SLACK_BOT_TOKEN, { agent: new HttpsProxyAgent(proxyUrl) }),

getAuthorizedUser() {
    const AuthStr = 'Bearer '.concat(process.env.SLACK_CLIENT_TOKEN);
    let options  = {
      url:'https://slack.com/api/auth.test?token='+process.env.SLACK_CLIENT_TOKEN,
      headers:{
        'Content-Type':'application/json',
        'Authorization': AuthStr
      },
      json: true
    }

    var context = this;
    request.get(options, function(err, res, body) {  
        if(err){
          console.log("error in code ::: ", err);
        }        
    })
    .on('data', function(data) {
      var locData = JSON.parse(Buffer.from(data).toString());
      context.approver_user_id = locData.user_id;
      context.decisionseeker = locData.user_id;
      context.adminUser();
    })
}

adminUser() {
    var text = `Welcome to app! I'm here to help your team`;
    
    this.web.im.open({user:this.approver_user_id})
      .then((res) => {
        this.web.chat.postMessage({channel:res.channel.id, text, as_user: true, parse: 'full'})}
      )
      .catch((err) => {
        console.log("admin user error code ::: ", err);
      });
  },

JanmejayPattanaik avatar Jan 16 '19 08:01 JanmejayPattanaik

@JanmejayPattanaik I'm sorry we haven't gotten back to you in so long. Have you been able to figure this issue out yet? We've shipped a few updates since we last spoke, so if its not yet resolved, and you're not using the latest version (v1.1.1), maybe you can try that out.

As far as I can tell, the code in index.js looks correct. Is there a reverse proxy that's forwarding the incoming messages to this app in the environment where its not working? That could be a problem, since reverse proxies might modify the data in the request, and then it would be impossible to verify the request signature.

aoberoi avatar Mar 07 '19 22:03 aoberoi

@JanmejayPattanaik I'm closing this issue out, but please feel free to open an issue at https://github.com/slackapi/node-slack-sdk/issues if you're still running into this problem.

shaydewael avatar Mar 28 '19 23:03 shaydewael