Node.js based forum software built for the modern web
fix(activitypub): validate webfinger subject hostname to prevent cache poisoning
Julian Lamd3c82555a09b
diff --git a/src/activitypub/helpers.js b/src/activitypub/helpers.jsindex 18dc506a80..588464caa0 100644--- a/src/activitypub/helpers.js+++ b/src/activitypub/helpers.js@@ -147,10 +147,42 @@ Helpers.query = async (id) => {let { subject, publicKey } = body;// Fix missing schemeif (!subject.startsWith('acct:') && !subject.startsWith('did:')) {- subject = `acct:${subject}`;+ try {+ new URL(subject);+ } catch (e) {+ subject = `acct:${subject}`;+ }+ }++ // Validate that the subject's hostname matches the queried hostname.+ let subjectUrl;+ try {+ subjectUrl = new URL(subject);+ } catch (e) {+ // Invalid URL — reject the response+ return false;}++ // Extract hostname from the subject.+ let subjectHostname;+ if (subjectUrl.protocol === 'acct:') {+ // Parse acct:user@hostname from the opaque part+ const opaque = subjectUrl.pathname;+ const atIndex = opaque.lastIndexOf('@');+ if (atIndex === -1) {+ // No @ in acct: subject — malformed+ return false;+ }+ subjectHostname = opaque.slice(atIndex + 1);+ } else {+ subjectHostname = subjectUrl.hostname;+ }+ if (subjectHostname !== hostname) {+ return false;+ }+const payload = { subject, username, hostname, actorUri, publicKey, _raw: body };- const claimedId = new URL(subject).pathname;+ const claimedId = subjectUrl.pathname;webfingerCache.set(claimedId, payload);if (claimedId !== id) {webfingerCache.set(id, payload);diff --git a/test/activitypub.js b/test/activitypub.jsindex 73c2a33276..5f3553c146 100644--- a/test/activitypub.js+++ b/test/activitypub.js@@ -99,7 +99,59 @@ describe('ActivityPub integration', () => {describe('Helpers', () => {describe('.query()', () => {+ let webfingerCache;+ let originalGet;+ beforeEach(() => {+ webfingerCache = activitypub.helpers._webfingerCache;+ webfingerCache.reset();+ originalGet = request.get;+ });++ afterEach(() => {+ webfingerCache.reset();+ request.get = originalGet;+ });++ it('should return false when the subject hostname does not match the queried hostname', async () => {+ request.get = async () => ({+ response: { statusCode: 200 },+ body: {+ subject: 'https://evil.com/actor',+ links: [{ rel: 'self', type: 'application/activity+json', href: 'https://evil.com/actor' }],+ },+ });++ const result = await activitypub.helpers.query('[email protected]');+ assert.strictEqual(result, false);+ });++ it('should succeed when the subject hostname matches the queried hostname', async () => {+ request.get = async () => ({+ response: { statusCode: 200 },+ body: {+ subject: 'https://trusted.com/actor',+ links: [{ rel: 'self', type: 'application/activity+json', href: 'https://trusted.com/actor' }],+ },+ });++ const result = await activitypub.helpers.query('[email protected]');+ assert(result);+ assert.strictEqual(result.actorUri, 'https://trusted.com/actor');+ });++ it('should succeed for acct: subjects with matching hostname', async () => {+ request.get = async () => ({+ response: { statusCode: 200 },+ body: {+ subject: 'acct:[email protected]',+ links: [{ rel: 'self', type: 'application/activity+json', href: 'https://trusted.com/actor' }],+ },+ });++ const result = await activitypub.helpers.query('[email protected]');+ assert(result);+ });});describe('.generateKeys()', () => {