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

[FIX] Incoming integrations being able to trigger an empty message with a GET #9576

Merged
merged 1 commit into from
Apr 10, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 68 additions & 46 deletions packages/rocketchat-integrations/server/api/api.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,62 @@
/* globals Api Meteor Restivus logger processWebhookMessage*/
/* globals Meteor Restivus logger processWebhookMessage*/
// TODO: remove globals

import _ from 'underscore';
import s from 'underscore.string';
import vm from 'vm';
import moment from 'moment';

const Api = new Restivus({
enableCors: true,
apiPath: 'hooks/',
auth: {
user() {
const payloadKeys = Object.keys(this.bodyParams);
const payloadIsWrapped = (this.bodyParams && this.bodyParams.payload) && payloadKeys.length === 1;
if (payloadIsWrapped && this.request.headers['content-type'] === 'application/x-www-form-urlencoded') {
try {
this.bodyParams = JSON.parse(this.bodyParams.payload);
} catch ({message}) {
return {
error: {
statusCode: 400,
body: {
success: false,
error: message
}
}
};
}
}

this.integration = RocketChat.models.Integrations.findOne({
_id: this.request.params.integrationId,
token: decodeURIComponent(this.request.params.token)
});

if (!this.integration) {
logger.incoming.info('Invalid integration id', this.request.params.integrationId, 'or token', this.request.params.token);

return {
error: {
statusCode: 404,
body: {
success: false,
error: 'Invalid integration id or token provided.'
}
}
};
}

const user = RocketChat.models.Users.findOne({
_id: this.integration.userId
});

return { user };
}
}
});

const compiledScripts = {};
function buildSandbox(store = {}) {
const sandbox = {
Expand Down Expand Up @@ -41,78 +92,45 @@ function buildSandbox(store = {}) {

function getIntegrationScript(integration) {
const compiledScript = compiledScripts[integration._id];
if ((compiledScript != null) && +compiledScript._updatedAt === +integration._updatedAt) {
if (compiledScript && +compiledScript._updatedAt === +integration._updatedAt) {
return compiledScript.script;
}

const script = integration.scriptCompiled;
const {sandbox, store} = buildSandbox();
const { sandbox, store } = buildSandbox();
try {
logger.incoming.info('Will evaluate script of Trigger', integration.name);
logger.incoming.debug(script);

const vmScript = vm.createScript(script, 'script.js');
vmScript.runInNewContext(sandbox);
if (sandbox.Script != null) {
if (sandbox.Script) {
compiledScripts[integration._id] = {
script: new sandbox.Script(),
store,
_updatedAt: integration._updatedAt
};

return compiledScripts[integration._id].script;
}
} catch ({stack}) {
} catch ({ stack }) {
logger.incoming.error('[Error evaluating Script in Trigger', integration.name, ':]');
logger.incoming.error(script.replace(/^/gm, ' '));
logger.incoming.error('[Stack:]');
logger.incoming.error(stack.replace(/^/gm, ' '));
throw RocketChat.API.v1.failure('error-evaluating-script');
}
if (sandbox.Script == null) {

if (!sandbox.Script) {
logger.incoming.error('[Class "Script" not in Trigger', integration.name, ']');
throw RocketChat.API.v1.failure('class-script-not-found');
}
}

Api = new Restivus({
enableCors: true,
apiPath: 'hooks/',
auth: {
user() {
const payloadKeys = Object.keys(this.bodyParams);
const payloadIsWrapped = (this.bodyParams && this.bodyParams.payload) && payloadKeys.length === 1;
if (payloadIsWrapped && this.request.headers['content-type'] === 'application/x-www-form-urlencoded') {
try {
this.bodyParams = JSON.parse(this.bodyParams.payload);
} catch ({message}) {
return {
error: {
statusCode: 400,
body: {
success: false,
error: message
}
}
};
}
}
this.integration = RocketChat.models.Integrations.findOne({
_id: this.request.params.integrationId,
token: decodeURIComponent(this.request.params.token)
});
if (this.integration == null) {
logger.incoming.info('Invalid integration id', this.request.params.integrationId, 'or token', this.request.params.token);
return;
}
const user = RocketChat.models.Users.findOne({
_id: this.integration.userId
});
return {user};
}
}
});

function createIntegration(options, user) {
logger.incoming.info('Add integration', options.name);
logger.incoming.debug(options);

Meteor.runAsUser(user._id, function() {
switch (options['event']) {
case 'newMessageOnChannel':
Expand Down Expand Up @@ -142,18 +160,22 @@ function createIntegration(options, user) {
});
}
});

return RocketChat.API.v1.success();
}

function removeIntegration(options, user) {
logger.incoming.info('Remove integration');
logger.incoming.debug(options);

const integrationToRemove = RocketChat.models.Integrations.findOne({
urls: options.target_url
});

Meteor.runAsUser(user._id, () => {
return Meteor.call('deleteOutgoingIntegration', integrationToRemove._id);
});

return RocketChat.API.v1.success();
}

Expand All @@ -176,7 +198,7 @@ function executeIntegrationRest() {
emoji: this.integration.emoji
};

if (this.integration.scriptEnabled === true && this.integration.scriptCompiled && this.integration.scriptCompiled.trim() !== '') {
if (this.integration.scriptEnabled && this.integration.scriptCompiled && this.integration.scriptCompiled.trim() !== '') {
let script;
try {
script = getIntegrationScript(this.integration);
Expand Down Expand Up @@ -240,7 +262,7 @@ function executeIntegrationRest() {

// TODO: Turn this into an option on the integrations - no body means a success
// TODO: Temporary fix for https://github.com/RocketChat/Rocket.Chat/issues/7770 until the above is implemented
if (!this.bodyParams) {
if (!this.bodyParams || (_.isEmpty(this.bodyParams) && !this.integration.scriptEnabled)) {
// return RocketChat.API.v1.failure('body-empty');
return RocketChat.API.v1.success();
}
Expand Down