Skip to content

Node.js based forum software built for the modern web

feat: normalization middleware to ensure incoming objects' data types are as expected (simplifies later logic)

Julian Lam13c048a51536
diff --git a/src/middleware/activitypub.js b/src/middleware/activitypub.js
index f7183f6f36..8913339ea4 100644
--- a/src/middleware/activitypub.js
+++ b/src/middleware/activitypub.js
@@ -137,6 +137,21 @@ middleware.resolveObjects = async function (req, res, next) {
next();
};
+middleware.normalize = async function (req, res, next) {
+ // Normalizes the received data structure
+ const { body } = req;
+ const { object } = body;
+
+ // Ensure `to` and `cc` are arrays in the object
+ ['to', 'cc'].forEach((prop) => {
+ if (object[prop] && typeof object[prop] === 'string') {
+ object[prop] = [object[prop]];
+ }
+ });
+
+ next();
+};
+
middleware.configureResponse = async function (req, res, next) {
res.header('Content-Type', 'application/activity+json');
next();
diff --git a/src/routes/activitypub.js b/src/routes/activitypub.js
index 9a378b41b1..dadff0bdf4 100644
--- a/src/routes/activitypub.js
+++ b/src/routes/activitypub.js
@@ -21,6 +21,7 @@ module.exports = function (app, middleware, controllers) {
const inboxMiddlewares = [
middleware.activitypub.assertPayload,
middleware.activitypub.resolveObjects,
+ middleware.activitypub.normalize,
];
app.get('/actor', middlewares, controllers.activitypub.actors.application);