From e98f505ae3bbbddf50353777fa858663194046a2 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Tue, 14 May 2024 11:18:56 +0700 Subject: [PATCH] Added body parsing to the frontend ref https://linear.app/tryghost/issue/MOM-73 We need to add body parsing middleware here, so that NestJS has access to it. We also attach the rawBody which is used to validate the HTTP Signatures --- ghost/core/core/frontend/web/site.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ghost/core/core/frontend/web/site.js b/ghost/core/core/frontend/web/site.js index 8a8fea58d1..4b8188915a 100644 --- a/ghost/core/core/frontend/web/site.js +++ b/ghost/core/core/frontend/web/site.js @@ -22,6 +22,7 @@ const shared = require('../../server/web/shared'); const errorHandler = require('@tryghost/mw-error-handler'); const mw = require('./middleware'); const labs = require('../../shared/labs'); +const bodyParser = require('body-parser'); const STATIC_IMAGE_URL_PREFIX = `/${urlUtils.STATIC_IMAGE_URL_PREFIX}`; const STATIC_MEDIA_URL_PREFIX = `/${constants.STATIC_MEDIA_URL_PREFIX}`; @@ -50,6 +51,21 @@ module.exports = function setupSiteApp(routerConfig) { // enable CORS headers (allows admin client to hit front-end when configured on separate URLs) siteApp.use(mw.cors); + const jsonParser = bodyParser.json({ + type: ['application/activity+json', 'application/ld+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'], + // TODO: The @RawBody decorator in nest isn't working without this atm... + verify: function (req, res, buf) { + req.rawBody = buf; + } + }); + siteApp.use(async function nestBodyParser(req, res, next) { + if (labs.isSet('NestPlayground') || labs.isSet('ActivityPub')) { + jsonParser(req, res, next); + return; + } + return next(); + }); + siteApp.use(async function nestApp(req, res, next) { if (labs.isSet('NestPlayground') || labs.isSet('ActivityPub')) { const originalExpressApp = req.app;