Node.js based forum software built for the modern web
fix(activitypub): harden webfinger query input validation
Julian Lamd6f95f142dd2
diff --git a/src/activitypub/helpers.js b/src/activitypub/helpers.jsindex 95a99e57f2..1a658d0224 100644--- a/src/activitypub/helpers.js+++ b/src/activitypub/helpers.js@@ -21,6 +21,9 @@ const activitypub = require('.');// \w only matches ASCII, so match unicode letters/numbers/marks explicitly to support non-ASCII handlesconst webfingerRegex = /^(@|acct:)?[\p{L}\p{N}\p{M}_.-]+@.+$/u;+// WebFinger username charset — used to validate actor.preferredUsername before it+// is reflected into a WebFinger query target (audit F-2)+const webfingerUserRegex = /^[\p{L}\p{N}\p{M}_.-]+$/u;const webfingerCache = ttl({name: 'ap-webfinger-cache',max: 5000,@@ -131,6 +134,17 @@ Helpers.query = async (id, { strict = true } = {}) => {username = username.trim();hostname = hostname.trim();+ // Reject hostnames that do not round-trip through the URL parser (ports, paths,+ // spaces, ...) — otherwise they would steer the WebFinger request to an+ // unintended host or path (audit F-2)+ try {+ if (new URL(`https://${hostname}/`).hostname.toLowerCase() !== hostname.toLowerCase()) {+ return false;+ }+ } catch (e) {+ return false;+ }+const cached = webfingerCache.get(id);// A cached entry that carries a hostname only answers queries for the domain// it was fetched from — entries stored under this key while querying a@@ -254,7 +268,14 @@ Helpers.verifyActorWebfinger = async (actorId, actor) => {}const idHostname = new URL(actorId).hostname;- const preferredUsername = actor.preferredUsername || 'user';+ // preferredUsername is reflected verbatim into the WebFinger query target+ // (`username@hostname`); restrict it to the WebFinger username charset so an+ // attacker-supplied value cannot redirect the query to another host (audit F-2)+ const preferredUsername = typeof actor.preferredUsername === 'string' ?+ actor.preferredUsername.trim() : '';+ if (!webfingerUserRegex.test(preferredUsername)) {+ return { ok: false, splitDomain: false, canonicalHandle: null, reason: 'no-backreference' };+ }// Step 1: Backreference — non-strict query on domain B (the actor's id host).// This allows the WebFinger subject to point elsewhere (split-domain forward target).diff --git a/test/activitypub/split-domain.js b/test/activitypub/split-domain.jsindex c0153ec463..e0b9a4b30b 100644--- a/test/activitypub/split-domain.js+++ b/test/activitypub/split-domain.js@@ -535,3 +535,84 @@ describe('WebFinger cache poisoning (audit F-1)', () => {assert.equal(verdict.reason, 'subject-mismatch');});});++// ============================================================================++describe('preferredUsername / hostname validation (audit F-2)', () => {+ let originalGet;+ let getCalled;++ beforeEach(Helpers.reset);++ afterEach(() => {+ if (originalGet) {+ request.get = originalGet;+ originalGet = null;+ }+ });++ const spyGet = () => {+ getCalled = 0;+ originalGet = request.get;+ request.get = async () => {+ getCalled += 1;+ return {+ body: {},+ response: { statusCode: 404, headers: { 'content-type': 'application/jrd+json' } },+ };+ };+ };++ it('does not issue a webfinger request for an attacker-shaped preferredUsername', async () => {+ const { actorUri } = Helpers.genSplitDomain();+ // preferredUsername containing @ / port would put attacker-controlled+ // values into the query's hostname slot+ const actor = Helpers.seedActor(actorUri, { preferredUsername: '[email protected]:8443' });+ spyGet();++ const verdict = await activitypub.helpers.verifyActorWebfinger(actorUri, actor);+ assert.ok(verdict);+ assert.equal(verdict.ok, false);+ assert.equal(verdict.reason, 'no-backreference');+ assert.equal(getCalled, 0);+ });++ it('rejects missing or non-string preferredUsername without a webfinger request', async () => {+ const { actorUri } = Helpers.genSplitDomain();+ const noUsername = Helpers.seedActor(actorUri, { preferredUsername: undefined });+ delete noUsername.preferredUsername;+ const badType = Helpers.seedActor(actorUri, { preferredUsername: 12345 });+ spyGet();++ const verdictA = await activitypub.helpers.verifyActorWebfinger(actorUri, noUsername);+ assert.ok(verdictA);+ assert.equal(verdictA.ok, false);+ assert.equal(verdictA.reason, 'no-backreference');++ const verdictB = await activitypub.helpers.verifyActorWebfinger(actorUri, badType);+ assert.ok(verdictB);+ assert.equal(verdictB.ok, false);+ assert.equal(verdictB.reason, 'no-backreference');+ assert.equal(getCalled, 0);+ });++ it('query() rejects port-bearing and path-bearing hostnames before any request', async () => {+ spyGet();+ const r1 = await activitypub.helpers.query('[email protected]:8443', { strict: false });+ const r2 = await activitypub.helpers.query('user@b/c', { strict: false });+ const r3 = await activitypub.helpers.query('user@space host', { strict: false });+ assert.equal(r1, false);+ assert.equal(r2, false);+ assert.equal(r3, false);+ assert.equal(getCalled, 0);+ });++ it('query() still fetches normally for plain hostnames', async () => {+ const { domainB } = Helpers.genSplitDomain();+ spyGet();+ const result = await activitypub.helpers.query(`user@${domainB}`, { strict: false });+ // mock returns 404 → false, but the request must have been made+ assert.equal(result, false);+ assert.equal(getCalled, 1);+ });+});