From 079b41e6085419dc8ba75ceb4da93580b45c8d92 Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Wed, 3 Oct 2018 00:47:03 +0200 Subject: [PATCH] Added body parser to web/api/v2 express app refs #9866 - req.body is undefined if we don't use the body parser - the content API only offers "fetch" endpoints, but if a component/module in Ghost relies on req.body being present, it can crash - e.g. the authentication service checks for the existence of client_id + client_secret in req.query or req.body - we could theoretically change it from `if (!req.body.client_id` to `if (req.body && !req.body.client_id)`, but that makes the code very hard to read + maintain - we will use the body parser for the content API now - req.body will be {} --- core/server/web/api/v2/content/app.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/server/web/api/v2/content/app.js b/core/server/web/api/v2/content/app.js index e8e1dca7e8..d945551e3f 100644 --- a/core/server/web/api/v2/content/app.js +++ b/core/server/web/api/v2/content/app.js @@ -1,5 +1,6 @@ const debug = require('ghost-ignition').debug('api'); const boolParser = require('express-query-boolean'); +const bodyParser = require('body-parser'); const express = require('express'); const shared = require('../../../shared'); const routes = require('./routes'); @@ -10,6 +11,9 @@ module.exports = function setupApiApp() { // API middleware + // @NOTE: req.body is undefined if we don't use this parser, this can trouble if components rely on req.body being present + apiApp.use(bodyParser.json({limit: '1mb'})); + // Query parsing apiApp.use(boolParser());