Node.js based forum software built for the modern web
fix(activitypub): reject actors with spoofed id hostname
Julian Lamc0d6d0e20767
diff --git a/src/activitypub/actors.js b/src/activitypub/actors.jsindex f964afa324..fa0bda2909 100644--- a/src/activitypub/actors.js+++ b/src/activitypub/actors.js@@ -139,6 +139,17 @@ Actors.assert = async (ids, options = {}) => {try {activitypub.helpers.log(`[activitypub/actors] Processing ${id}`);const actor = (typeof id === 'object' && id.hasOwnProperty('id')) ? id : await activitypub.get('uid', 0, id, { cache: process.env.CI === 'true' });++ // Verify actor.id hostname matches the queried URL's hostname (prevent spoofed id overwrite)+ if (typeof id === 'string') {+ const queriedHost = new URL(id).hostname;+ const actorHost = new URL(actor.id).hostname;+ if (queriedHost !== actorHost) {+ activitypub.helpers.log(`[activitypub/actors] Actor id hostname mismatch: queried ${queriedHost}, got ${actorHost}`);+ return null;+ }+ }+// webfinger backreference checkconst { hostname: domain } = new URL(id);const { actorUri: canonicalId } = await activitypub.helpers.query(`${actor.preferredUsername}@${domain}`);@@ -321,6 +332,16 @@ Actors.assertGroup = async (ids, options = {}) => {activitypub.helpers.log(`[activitypub/actors] Processing group ${id}`);const actor = (typeof id === 'object' && id.hasOwnProperty('id')) ? id : await activitypub.get('uid', 0, id, { cache: process.env.CI === 'true' });+ // Verify actor.id hostname matches the queried URL's hostname (prevent spoofed id overwrite)+ if (typeof id === 'string') {+ const queriedHost = new URL(id).hostname;+ const actorHost = new URL(actor.id).hostname;+ if (queriedHost !== actorHost) {+ activitypub.helpers.log(`[activitypub/actors] Group id hostname mismatch: queried ${queriedHost}, got ${actorHost}`);+ return null;+ }+ }+// webfinger backreference checkconst { hostname: domain } = new URL(id);const { actorUri: canonicalId } = await activitypub.helpers.query(`${actor.preferredUsername}@${domain}`);diff --git a/test/activitypub/actors.js b/test/activitypub/actors.jsindex 4adf161447..249f1ce60f 100644--- a/test/activitypub/actors.js+++ b/test/activitypub/actors.js@@ -79,6 +79,68 @@ describe('as:Person (Actor asserton)', () => {});describe('less happy paths', () => {+ describe('actor.id hostname mismatch (spoofed id)', () => {+ it('should reject an actor whose id hostname does not match the queried URL', async () => {+ const queriedId = `https://example.org/user/${utils.generateUUID()}`;+ const spoofedId = `https://malicious.org/user/${utils.generateUUID()}`;++ // Cache a spoofed actor: queried from example.org but id points to malicious.org+ activitypub._cache.set(`0;${queriedId}`, {+ '@context': 'https://www.w3.org/ns/activitystreams',+ id: spoofedId,+ url: spoofedId,+ type: 'Person',+ name: 'spoofed',+ preferredUsername: 'spoofed',+ inbox: `${spoofedId}/inbox`,+ outbox: `${spoofedId}/outbox`,+ publicKey: {+ id: `${spoofedId}#key`,+ owner: spoofedId,+ publicKeyPem: 'somekey',+ },+ });++ const result = await activitypub.actors.assert([queriedId]);+ assert.deepStrictEqual(result, []);++ // Ensure no user data was created for either the queried or spoofed id+ const queriedExists = await db.exists(`userRemote:${queriedId}`);+ const spoofedExists = await db.exists(`userRemote:${spoofedId}`);+ assert.strictEqual(queriedExists, false);+ assert.strictEqual(spoofedExists, false);+ });++ it('should reject a group whose id hostname does not match the queried URL', async () => {+ const queriedId = `https://example.org/group/${utils.generateUUID()}`;+ const spoofedId = `https://malicious.org/group/${utils.generateUUID()}`;++ activitypub._cache.set(`0;${queriedId}`, {+ '@context': 'https://www.w3.org/ns/activitystreams',+ id: spoofedId,+ url: spoofedId,+ type: 'Group',+ name: 'spoofed group',+ preferredUsername: 'spoofed-group',+ inbox: `${spoofedId}/inbox`,+ outbox: `${spoofedId}/outbox`,+ publicKey: {+ id: `${spoofedId}#key`,+ owner: spoofedId,+ publicKeyPem: 'somekey',+ },+ });++ const result = await activitypub.actors.assertGroup([queriedId]);+ assert.deepStrictEqual(result, []);++ const queriedExists = await db.exists(`categoryRemote:${queriedId}`);+ const spoofedExists = await db.exists(`categoryRemote:${spoofedId}`);+ assert.strictEqual(queriedExists, false);+ assert.strictEqual(spoofedExists, false);+ });+ });+describe('actor with `preferredUsername` that is not all lowercase', () => {it('should save a handle-to-uid association', async () => {const preferredUsername = 'nameWITHCAPS';