Node.js based forum software built for the modern web
fix(activitypub): enforce attributedTo origin check in inbox handlers
Julian Lam870ec669f30f
diff --git a/src/activitypub/inbox.js b/src/activitypub/inbox.jsindex d6fc9fd345..269484278c 100644--- a/src/activitypub/inbox.js+++ b/src/activitypub/inbox.js@@ -42,10 +42,37 @@ inbox._reject = function (type, object, target, senderType = 'uid', id = 0) {inbox.create = async (req) => {const { object, actor } = req.body;+ // 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/inbox.create] attributedTo origin mismatch (${attributedToHostname} !== ${actorHostname}).`);+ return null;+ }+ } catch (e) {+ return null;+ }+ }+ }+// Alternative logic for non-public objectsconst isPublic = publiclyAddressed([...(object.to || []), ...(object.cc || [])]);if (!isPublic) {- return await activitypub.notes.assertPrivate(object, actor);+ return await activitypub.notes.assertPrivate(object);}// Category sync, remove when cross-posting available@@ -164,6 +191,33 @@ inbox.update = async (req) => {throw new Error('[[error:activitypub.origin-mismatch]]');}+ // 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/inbox.update] attributedTo origin mismatch (${attributedToHostname} !== ${actorHostname}).`);+ return null;+ }+ } catch (e) {+ return null;+ }+ }+ }+switch (true) {case activitypub._constants.acceptedPostTypes.includes(object.type): {const [isNote, isMessage] = await Promise.all([@@ -207,7 +261,7 @@ inbox.update = async (req) => {default: {if (!isPublic) {- return await activitypub.notes.assertPrivate(object, actor);+ return await activitypub.notes.assertPrivate(object);}const { cids } = await activitypub.actors.getFollowers(actor);diff --git a/src/activitypub/notes.js b/src/activitypub/notes.jsindex f2ab339bb9..6b608d0834 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, actor) => {+Notes.assertPrivate = async (object) => {// Given an object, adds it to an existing chat or creates a new chat otherwise// todo: context stuff@@ -424,33 +424,6 @@ Notes.assertPrivate = async (object, actor) => {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) => {