howdyai / botkit

Botkit is an open source developer tool for building chat bots, apps and custom integrations for major messaging platforms.
MIT License
11.49k stars 2.28k forks source link

Sparkbot "limit_to_domain" config will raise an exception when receiving a message #1208

Closed JavaCS3 closed 6 years ago

JavaCS3 commented 6 years ago

Botkit version: 0.6 OS: MacOS (It's not related to OS)

var controller = Botkit.sparkbot({
    debug: true,
    log: true,
    limit_to_domain: ['XXXX.com'],  // enable this
    // limit_to_org: 'my_cisco_org_id',
    public_address: process.env.public_address,
    ciscospark_access_token: process.env.access_token,
   ...
});

When bot received any message from cisco spark. It will get

An error occured in the ingest middleware:  TypeError: Cannot read property 'toLowerCase' of undefined
    at Ware.limitUsers (/SECRET/node_modules/botkit/lib/CiscoSparkbot.js:164:34)
    at Ware.<anonymous> (/SECRET/node_modules/wrap-fn/index.js:45:19)
    at next (/SECRET/node_modules/ware/lib/index.js:85:20)
    at Ware.run (/SECRET/node_modules/ware/lib/index.js:88:3)
    at Object.Botkit.botkit.ingest (/SECRET/node_modules/botkit/lib/CoreBot.js:72:34)
    at Object.Sparkbot.controller.handleWebhookPayload (/SECRET/node_modules/botkit/lib/CiscoSparkbot.js:322:20)
    at /SECRET/components/routes/incoming_webhooks.js:17:20
    at Layer.handle [as handle_request] (/SECRET/node_modules/express/lib/router/layer.js:95:5)
    at next (/SECRET/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/SECRET/node_modules/express/lib/router/route.js:112:3)

I have found the root cause. It looks like Cisco Spark API have changed. Cisco Spark sent

{
    "id": "XXXXX",
    "name": "Cisco Spark bot created with Botkit, override me before going to production",
    "targetUrl": "https://XXXXX/ciscospark/receive",
    "resource": "messages",
    "event": "created",
    "orgId": "XXXXX",
    "createdBy": "XXXXX",
    "appId": "XXXXX",
    "ownedBy": "creator",
    "status": "active",
    "created": "2018-01-02T08:40:09.105Z",
    "actorId": "XXXXX",
    "data": {
        "id": "XXXXX",
        "roomId": "XXXXX",
        "roomType": "direct",
        "personId": "XXXXX",
        "personEmail": "XXXXX@XXXX.com",
        "created": "2018-01-12T07:59:43.433Z"
    }
}

There's no "user" field in the message. In this code It will acquire "user" field. So it raised an exception.

jcmusslin commented 6 years ago

I did change the part of code that was testing limit_to_domain in order to log a warning in CiscoSparkbot.js and now its seems running OK.


if (controller.config.limit_to_domain) {
            var domains = [];
            if (typeof(controller.config.limit_to_domain) == 'string') {
                domains = [controller.config.limit_to_domain];
            } else {
                domains = controller.config.limit_to_domain;
            }
            var allowed = false;
            var userEmail = message.data.personEmail;
            for (var d = 0; d < domains.length; d++) {
                if (userEmail.includes(domains[d]) || userEmail.includes('@sparkbot.io')) {
                    allowed = true; 
                }
            }
            if (!allowed) {
                console.log('*** WARNING *** : this message came from ' + userEmail + ' a domain that is outside of the allowed list', controller.config.limit_to_domain);
                // this message came from a domain that is outside of the allowed list.
                return false;
            }
        }```