Skip to content

Node.js based forum software built for the modern web

fix(activitypub): validate attributedTo origin in assertPrivate

Julian Lam8df796ee4934
diff --git a/src/activitypub/inbox.js b/src/activitypub/inbox.js
index f0a1f061dd..835a413446 100644
--- a/src/activitypub/inbox.js
+++ b/src/activitypub/inbox.js
@@ -45,7 +45,7 @@ inbox.create = async (req) => {
// Alternative logic for non-public objects
const isPublic = publiclyAddressed([...(object.to || []), ...(object.cc || [])]);
if (!isPublic) {
- return await activitypub.notes.assertPrivate(object);
+ return await activitypub.notes.assertPrivate(object, actor);
}
// Category sync, remove when cross-posting available
@@ -207,7 +207,7 @@ inbox.update = async (req) => {
default: {
if (!isPublic) {
- return await activitypub.notes.assertPrivate(object);
+ return await activitypub.notes.assertPrivate(object, actor);
}
const { cids } = await activitypub.actors.getFollowers(actor);
diff --git a/src/activitypub/notes.js b/src/activitypub/notes.js
index 6b608d0834..f2ab339bb9 100644
--- a/src/activitypub/notes.js
+++ b/src/activitypub/notes.js
@@ -416,7 +416,7 @@ Notes.assert = async (uid, input, options = { skipChecks: false, queue: false })
}
};
-Notes.assertPrivate = async (object) => {
+Notes.assertPrivate = async (object, actor) => {
// Given an object, adds it to an existing chat or creates a new chat otherwise
// todo: context stuff
@@ -424,6 +424,33 @@ Notes.assertPrivate = async (object) => {
return null;
}
+ // attributedTo must be same-origin
+ if (actor && object.attributedTo) {
+ // Normalize `attributedTo`
+ let { attributedTo } = object;
+ if (typeof attributedTo === 'object' && !Array.isArray(attributedTo)) {
+ attributedTo = attributedTo.id;
+ } else if (Array.isArray(attributedTo)) {
+ attributedTo = attributedTo.find(a => typeof a === 'string' || (typeof a === 'object' && a.id)) || attributedTo.shift();
+ if (typeof attributedTo === 'object') {
+ attributedTo = attributedTo.id;
+ }
+ }
+
+ if (typeof attributedTo === 'string') {
+ try {
+ const actorHostname = typeof actor === 'string' ? new URL(actor).hostname : new URL(actor[0]).hostname;
+ const attributedToHostname = new URL(attributedTo).hostname;
+ if (actorHostname !== attributedToHostname) {
+ activitypub.helpers.log(`[activitypub/notes.assertPrivate] attributedTo origin mismatch (${attributedToHostname} !== ${actorHostname}).`);
+ return null;
+ }
+ } catch (e) {
+ return null;
+ }
+ }
+ }
+
const localUids = [];
const recipients = new Set([...(object.to || []), ...(object.cc || [])]);
await Promise.all(Array.from(recipients).map(async (value) => {
diff --git a/src/middleware/activitypub.js b/src/middleware/activitypub.js
index 08948916f7..1529f209f3 100644
--- a/src/middleware/activitypub.js
+++ b/src/middleware/activitypub.js
@@ -126,6 +126,7 @@ middleware.assertPayload = helpers.try(async function (req, res, next) {
activitypub.helpers.log('[middleware/activitypub] Origin check failed, stripping object down to id.');
req.body.object = [object.id];
}
+
activitypub.helpers.log('[middleware/activitypub] Origin check passed.');
}