Skip to content

Node.js based forum software built for the modern web

fix(activitypub): enforce attributedTo origin check in inbox handlers

Julian Lamf93507a6404c
diff --git a/src/activitypub/inbox.js b/src/activitypub/inbox.js
index 0625aaf9b4..9b426eb881 100644
--- a/src/activitypub/inbox.js
+++ b/src/activitypub/inbox.js
@@ -42,6 +42,33 @@ 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 objects
const isPublic = publiclyAddressed([...(object.to || []), ...(object.cc || [])]);
if (!isPublic) {
@@ -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([