feat: resolve objects from ids in middleware

This commit is contained in:
Opliko
2024-04-09 23:58:00 +02:00
parent d437d969cc
commit 102c174e03
3 changed files with 110 additions and 11 deletions

View File

@@ -55,8 +55,8 @@ middleware.validate = async function (req, res, next) {
const actorHostname = new URL(actor).hostname;
const objectHostname = new URL(object.id).hostname;
if (actorHostname !== objectHostname) {
winston.verbose('[middleware/activitypub] Origin check failed.');
return res.sendStatus(403);
winston.verbose('[middleware/activitypub] Origin check failed, stripping object down to id.');
req.body.object = [object.id];
}
winston.verbose('[middleware/activitypub] Origin check passed.');
}
@@ -75,6 +75,21 @@ middleware.validate = async function (req, res, next) {
next();
};
middleware.resolveObjects = async function (req, res, next) {
const { object } = req.body;
if (typeof object === 'string' || (Array.isArray(object) && object.every(o => typeof o === 'string'))) {
winston.verbose('[middleware/activitypub] Resolving object(s)...');
try {
req.body.object = await activitypub.helpers.resolveObjects(object);
winston.verbose('[middleware/activitypub] Object(s) successfully resolved.');
} catch (e) {
winston.verbose('[middleware/activitypub] Failed to resolve object(s).');
return res.sendStatus(400);
}
}
next();
};
middleware.configureResponse = async function (req, res, next) {
res.header('Content-Type', 'application/activity+json');
next();