diff --git a/apps/meteor/app/api/server/router.ts b/apps/meteor/app/api/server/router.ts index 6176d2644b471..152e454b99ded 100644 --- a/apps/meteor/app/api/server/router.ts +++ b/apps/meteor/app/api/server/router.ts @@ -94,7 +94,7 @@ export class Router< { urlParams: req.params, queryParams: req.query, - bodyParams: req.body, + bodyParams: (req as any).bodyParams || req.body, request: req, response: res, } as any, diff --git a/apps/meteor/app/integrations/server/api/api.js b/apps/meteor/app/integrations/server/api/api.js index 5a2c750c93f9c..80a88b8757c9f 100644 --- a/apps/meteor/app/integrations/server/api/api.js +++ b/apps/meteor/app/integrations/server/api/api.js @@ -240,24 +240,6 @@ function integrationInfoRest() { class WebHookAPI extends APIClass { async authenticatedRoute(request) { - const payloadKeys = Object.keys(request.body); - const payloadIsWrapped = request.body && request.body.payload && payloadKeys.length === 1; - if (payloadIsWrapped && request.headers['content-type'] === 'application/x-www-form-urlencoded') { - try { - request.body = JSON.parse(request.body.payload); - } catch ({ message }) { - return { - error: { - statusCode: 400, - body: { - success: false, - error: message, - }, - }, - }; - } - } - request.integration = await Integrations.findOne({ _id: request.params.integrationId, token: decodeURIComponent(request.params.token), @@ -329,52 +311,27 @@ class WebHookAPI extends APIClass { const Api = new WebHookAPI({ enableCors: true, apiPath: 'hooks/', - auth: { - async 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.request.integration = await Integrations.findOne({ - _id: this.request.params.integrationId, - token: decodeURIComponent(this.request.params.token), - }); - - if (!this.request.integration) { - incomingLogger.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.', - }, - }, - }; - } +// middleware for special requests that are urlencoded but have a json payload (like GitHub webhooks) +Api.router.use((req, res, next) => { + if (req.headers['content-type'] !== 'application/x-www-form-urlencoded') { + return next(); + } - const user = await Users.findOne({ - _id: this.request.integration.userId, - }); + const payloadKeys = Object.keys(req.body); + if (payloadKeys.length !== 1) { + return next(); + } - return { user }; - }, - }, + try { + // need to compose the full payload in this weird way because body-parser thought it was a form + req.bodyParams = JSON.parse(payloadKeys[0] + req.body[payloadKeys[0]]); + return next(); + } catch (e) { + res.writeHead(400); + res.end(JSON.stringify({ success: false, error: e.message })); + } }); Api.addRoute(