Skip to content

Node.js based forum software built for the modern web

fix(activitypub): normalize webfinger cache keys and search handles

Julian Lam958cd2d6b709
diff --git a/src/activitypub/helpers.js b/src/activitypub/helpers.js
index a0f3f58717..2469114de6 100644
--- a/src/activitypub/helpers.js
+++ b/src/activitypub/helpers.js
@@ -131,6 +131,20 @@ Helpers.isWebfinger = (value) => {
return false;
};
+// Normalized webfinger cache key: username case is preserved (usernames may
+// be case-sensitive), hostname is lowercased (hostnames are case-insensitive;
+// URI ids normalize via the URL parser). All webfinger cache read/write paths
+// must use this so case variants can never produce divergent entries (audit F-7)
+Helpers._webfingerKey = (id) => {
+ if (Helpers.isUri(id)) {
+ const uri = new URL(id);
+ return `${uri.pathname || uri.href}@${uri.hostname}`;
+ }
+
+ const [username, hostname] = String(id).split('@');
+ return `${(username || '').trim()}@${(hostname || '').trim().toLowerCase()}`;
+};
+
Helpers.query = async (id, { strict = true } = {}) => {
const isUri = Helpers.isUri(id);
// username@host ids use acct: URI schema
@@ -154,12 +168,15 @@ Helpers.query = async (id, { strict = true } = {}) => {
return false;
}
+ // Normalize the cache key (hostnames are case-insensitive, audit F-7)
+ const key = Helpers._webfingerKey(id);
+
// Short-circuit recent transport-level failures for this exact id (audit F-4)
- if (failedWebfingerQueryCache.has(id)) {
+ if (failedWebfingerQueryCache.has(key)) {
return false;
}
- const cached = webfingerCache.get(id);
+ const cached = webfingerCache.get(key);
// 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
// different domain must not be served (cross-domain cache poisoning, audit
@@ -185,12 +202,12 @@ Helpers.query = async (id, { strict = true } = {}) => {
timeout: 5000,
}));
} catch (e) {
- failedWebfingerQueryCache.set(id, true);
+ failedWebfingerQueryCache.set(key, true);
return false;
}
if (response.statusCode !== 200) {
- failedWebfingerQueryCache.set(id, true);
+ failedWebfingerQueryCache.set(key, true);
return false;
}
@@ -273,7 +290,7 @@ Helpers.query = async (id, { strict = true } = {}) => {
// let a response served by one domain answer WebFinger queries about a
// *different* domain's handle, which the non-strict backref read in
// verifyActorWebfinger would then trust (audit F-1)
- webfingerCache.set(id, payload);
+ webfingerCache.set(key, payload);
return payload;
};
diff --git a/src/user/search.js b/src/user/search.js
index cdb917c687..01682ff230 100644
--- a/src/user/search.js
+++ b/src/user/search.js
@@ -50,9 +50,17 @@ module.exports = function (User) {
} else {
const assertion = await activitypub.actors.assert([handle || data.query]);
if (assertion === true) {
- // Actor already exists; resolve UID from webfinger cache
- const cached = handle ? activitypub.helpers._webfingerCache.get(handle) : null;
- uids = cached ? [cached.actorUri] : [handle ? await User.getUidByUserslug(handle) : query];
+ // Actor already exists; prefer the persisted handle:uid mapping
+ // over the webfinger cache, which can hold a stale entry for a
+ // re-resolved handle (audit F-7)
+ const resolved = handle ? await db.getObjectField('handle:uid', handle.toLowerCase()) : null;
+ if (resolved) {
+ uids = [resolved];
+ } else {
+ const wfKey = handle ? activitypub.helpers._webfingerKey(handle) : false;
+ const cached = wfKey ? activitypub.helpers._webfingerCache.get(wfKey) : null;
+ uids = cached ? [cached.actorUri] : [handle ? await User.getUidByUserslug(handle) : query];
+ }
} else if (Array.isArray(assertion) && assertion.length) {
uids = assertion.map(u => u.id);
}
diff --git a/test/activitypub/split-domain.js b/test/activitypub/split-domain.js
index f705f0ed22..a6a53daae4 100644
--- a/test/activitypub/split-domain.js
+++ b/test/activitypub/split-domain.js
@@ -800,3 +800,24 @@ describe('Webfinger failure negative cache (audit F-4)', () => {
assert.equal(getCalled, 2);
});
});
+
+// ============================================================================
+
+describe('Webfinger cache key normalization (audit F-7)', () => {
+ beforeEach(Helpers.reset);
+
+ it('serves cached entries for case variants of the hostname', async () => {
+ const { domainB, username, actorUri } = Helpers.genSplitDomain();
+ Helpers.seedSameDomainWebfinger(domainB, username, actorUri);
+ // Uppercase hostname variant of the same handle must hit the same key
+ const result = await activitypub.helpers.query(`${username}@${domainB.toUpperCase()}`);
+ assert.ok(result);
+ assert.equal(result.actorUri, actorUri);
+ });
+
+ it('normalizes keys with lowercase hostnames and preserved username case', () => {
+ assert.equal(activitypub.helpers._webfingerKey('[email protected]'), '[email protected]');
+ assert.equal(activitypub.helpers._webfingerKey('[email protected]'), '[email protected]');
+ assert.equal(activitypub.helpers._webfingerKey('https://Example.COM/user'), '/[email protected]');
+ });
+});
diff --git a/test/user/search.js b/test/user/search.js
index a648fb7a61..b19bc26866 100644
--- a/test/user/search.js
+++ b/test/user/search.js
@@ -158,6 +158,30 @@ describe('User.search()', () => {
const hasRemote = result.users.some(u => u.uid === remoteActorUri);
assert.ok(hasRemote, 'exact webfinger should resolve to remote user');
});
+
+ it('prefers the persisted handle:uid mapping over a stale webfinger cache entry', async () => {
+ const hostname = new URL(remoteActorUri).hostname;
+ const webfinger = `charlie_remote@${hostname}`;
+ // A stale cache entry resolves the handle to a different, already-crawled URI
+ const staleUri = `https://${hostname}/uid/stale_charlie`;
+ await db.sortedSetAdd('usersRemote:lastCrawled', Date.now(), staleUri);
+ activitypub.helpers._webfingerCache.set(webfinger, {
+ actorUri: staleUri,
+ subject: `acct:charlie_remote@${hostname}`,
+ username: 'charlie_remote',
+ hostname,
+ });
+
+ const result = await user.search({
+ query: webfinger,
+ searchBy: 'username',
+ uid: localUid,
+ });
+
+ assert.ok(result.users.length >= 1, 'should find the user');
+ const hasRemote = result.users.some(u => u.uid === remoteActorUri);
+ assert.ok(hasRemote, 'stale webfinger cache entry must not override the persisted handle:uid mapping (audit F-7)');
+ });
});
describe('search by fullname', () => {