From da83636c9a0d76a05dfdcf7fd3a722ab76714828 Mon Sep 17 00:00:00 2001 From: Priyanshubhartistm Date: Tue, 1 Dec 2026 01:48:28 +0530 Subject: [PATCH] fix(wot): lowercase every pubkey in the trust graph and its cache Signed-off-by: Priyanshubhartistm --- .changeset/wot-lowercase-follow-pubkeys.md | 7 ++ src/services/wot-graph-service.ts | 31 +++++-- test/unit/services/wot-graph-service.spec.ts | 97 ++++++++++++++++++++ 3 files changed, 125 insertions(+), 10 deletions(-) create mode 100644 .changeset/wot-lowercase-follow-pubkeys.md diff --git a/.changeset/wot-lowercase-follow-pubkeys.md b/.changeset/wot-lowercase-follow-pubkeys.md new file mode 100644 index 000000000..79e0e0994 --- /dev/null +++ b/.changeset/wot-lowercase-follow-pubkeys.md @@ -0,0 +1,7 @@ +--- +'nostream': patch +--- + +fix(wot): use one lowercase form for every pubkey in the trust graph and its cache + +The value of a `p` tag in a contact list is any string, while an event's author is always lowercase hex. The Web of Trust graph kept a followed pubkey as it was written, so an uppercase spelling could sit in the graph and in the `wot:follows:` cache next to the lowercase form of the same account, and an account the graph had reached only through an uppercase tag did not trigger a rebuild when its own contact list changed. The graph and its cache keys now lowercase every pubkey they take in, including entries an earlier version left in the cache, and a `p` tag without a value is ignored. A `wot.seedPubkey` written in uppercase, which its pattern allows, is read as the same lowercase pubkey, so the seed is the account whose events are lowercase. diff --git a/src/services/wot-graph-service.ts b/src/services/wot-graph-service.ts index 791f1ddb2..af7700758 100644 --- a/src/services/wot-graph-service.ts +++ b/src/services/wot-graph-service.ts @@ -20,7 +20,11 @@ export const WOT_FOLLOW_LIST_BATCH_SIZE = 100 // The longest delay a Node.js timer accepts; a longer wait is re-armed. const MAX_TIMER_DELAY_MS = 2 ** 31 - 1 -const followSetKey = (pubkey: Pubkey): string => `wot:follows:${pubkey}` +// An event author is always lowercase hex, but the value of a `p` tag is any string. One form for every key keeps an +// uppercase spelling from sitting in the graph and the cache next to the lowercase form of the same pubkey. +const normalizePubkey = (pubkey: string): Pubkey => pubkey.toLowerCase() + +const followSetKey = (pubkey: Pubkey): string => `wot:follows:${normalizePubkey(pubkey)}` type WotSettings = NonNullable @@ -47,7 +51,7 @@ const graphKey = (wot: WotSettings | undefined): string => { const { maxPubkeys, maxFollowListReads, maxEdges } = rebuildLimits(wot) return JSON.stringify([ Boolean(wot?.enabled), - wot?.seedPubkey ?? null, + wot?.seedPubkey ? normalizePubkey(wot.seedPubkey) : null, wot?.maxDepth ?? 2, wot?.minimumFollowers ?? 1, maxPubkeys, @@ -59,7 +63,7 @@ const graphKey = (wot: WotSettings | undefined): string => { const extractFollowedPubkeys = (tags: Tag[]): Pubkey[] => tags .filter((tag) => tag[0] === EventTags.Pubkey && typeof tag[1] === 'string' && tag[1].length > 0) - .map((tag) => tag[1]) + .map((tag) => normalizePubkey(tag[1])) export class WotGraphService implements IWotGraphService { private distances: Map = new Map() @@ -107,7 +111,7 @@ export class WotGraphService implements IWotGraphService { return undefined } - if (pubkey === wot.seedPubkey) { + if (pubkey === normalizePubkey(wot.seedPubkey)) { return 0 } @@ -132,16 +136,21 @@ export class WotGraphService implements IWotGraphService { return } + // A `p` tag without a value passes the tag schema, so the list can hold something that is not a pubkey. + const followed = follows.filter((entry) => typeof entry === 'string' && entry.length > 0).map(normalizePubkey) await this.cache.deleteKey(followSetKey(pubkey)) - if (follows.length) { - await this.cache.addToSet(followSetKey(pubkey), follows) + if (followed.length) { + await this.cache.addToSet(followSetKey(pubkey), followed) } // A pubkey outside the current trust graph publishing a new follow list // can't change anyone's distance from the seed, so only rebuild when the // change could actually matter. The event path never waits for the // rebuild, and requestRebuild() spaces these rebuilds out. - if (pubkey === wot.seedPubkey || this.distances.has(pubkey)) { + if ( + normalizePubkey(pubkey) === normalizePubkey(wot.seedPubkey ?? '') || + this.distances.has(normalizePubkey(pubkey)) + ) { this.requestRebuild() } } @@ -259,7 +268,9 @@ export class WotGraphService implements IWotGraphService { // not just the immediately preceding frontier -- matches minimumFollowers' // documented meaning at any maxDepth, not only maxDepth=2. const followerCounts = new Map() - let frontier = [wot.seedPubkey] + // The seed is spelled as the operator wrote it, which the pattern allows in either case. + const seed = normalizePubkey(wot.seedPubkey) + let frontier = [seed] let reads = 0 let edges = 0 // Set when a limit stops the rebuild. Every layer before the current one @@ -295,12 +306,12 @@ export class WotGraphService implements IWotGraphService { for (const follows of await this.getFollowLists(batch)) { // One account is one vote: a contact list that repeats a `p` tag must // not count more than once towards minimumFollowers. - for (const followed of new Set(follows)) { + for (const followed of new Set(follows.map(normalizePubkey))) { if (edges === limits.maxEdges) { stoppedAt = 'wot.maxEdges' break } - if (followed !== wot.seedPubkey && !distances.has(followed)) { + if (followed !== seed && !distances.has(followed)) { const followers = followerCounts.get(followed) if (followers === undefined && followerCounts.size === limits.maxPubkeys) { stoppedAt = 'wot.maxPubkeys' diff --git a/test/unit/services/wot-graph-service.spec.ts b/test/unit/services/wot-graph-service.spec.ts index 90dfb520e..09b24b5f1 100644 --- a/test/unit/services/wot-graph-service.spec.ts +++ b/test/unit/services/wot-graph-service.spec.ts @@ -321,6 +321,72 @@ describe('WotGraphService', () => { expect(await wot.getDistance('a')).to.be.undefined }) + it('reads an uppercase p tag from the database as the lowercase pubkey', async () => { + settings.wot!.maxDepth = 1 + const followed = 'ab'.repeat(32) + findByFiltersStub.resolves([dbEventWithFollows([['p', followed.toUpperCase()]])]) + + const wot = service() + + expect(await wot.getDistance(followed)).to.equal(1) + expect(addToSetStub).to.have.been.calledOnceWithExactly(`wot:follows:${seedPubkey}`, [followed]) + }) + + it('reads an uppercase pubkey left in the cache by an earlier version as the lowercase pubkey', async () => { + const followed = 'cd'.repeat(32) + followsOf[seedPubkey] = [followed.toUpperCase()] + + expect(await service().getDistance(followed)).to.equal(1) + }) + + it('follows an account whose pubkey was written in uppercase in the contact list that named it', async () => { + const first = 'ab'.repeat(32) + const second = 'cd'.repeat(32) + followsOf[seedPubkey] = [first.toUpperCase()] + followsOf[first] = [second] + + expect(await service().getDistance(second)).to.equal(2) + }) + + describe('a seedPubkey the operator wrote in uppercase', () => { + const lowercaseSeed = 'a'.repeat(64) + const uppercaseSeed = lowercaseSeed.toUpperCase() + + beforeEach(() => { + settings.wot!.seedPubkey = uppercaseSeed + }) + + it('is the seed under its lowercase form, at distance 0', async () => { + followsOf[lowercaseSeed] = ['b'.repeat(64)] + + const wot = service() + + expect(await wot.getDistance(lowercaseSeed)).to.equal(0) + expect(await wot.getDistance('b'.repeat(64))).to.equal(1) + }) + + it('does not enter the graph at distance 1 by following itself in uppercase', async () => { + followsOf[lowercaseSeed] = [uppercaseSeed, 'b'.repeat(64)] + + const wot = service() + await wot.getDistance('b'.repeat(64)) + + expect((wot as any).distances.has(lowercaseSeed)).to.equal(false) + expect((wot as any).distances.get('b'.repeat(64))).to.equal(1) + }) + + it('rebuilds when the seed publishes a new contact list', async () => { + const wot = service() + await wot.getDistance('b'.repeat(64)) + getSetMembersStub.resetHistory() + + await wot.updateFollowList(lowercaseSeed, ['c'.repeat(64)]) + await (wot as any).building + + expect(await wot.getDistance('c'.repeat(64))).to.equal(1) + }) + }) + it('treats an invalid seedPubkey as an empty graph instead of throwing', async () => { settings.wot!.seedPubkey = 'not-a-valid-hex-pubkey' @@ -354,6 +420,37 @@ describe('WotGraphService', () => { expect(addToSetStub).to.have.been.calledOnceWithExactly(`wot:follows:${seedPubkey}`, ['a', 'b']) }) + it('stores the follows in lowercase and skips a p tag without a value', async () => { + const followed = 'ab'.repeat(32) + + await service().updateFollowList(seedPubkey, [followed.toUpperCase(), undefined as any, '']) + + expect(addToSetStub).to.have.been.calledOnceWithExactly(`wot:follows:${seedPubkey}`, [followed]) + }) + + it('keys the follow set of an uppercase pubkey by its lowercase form', async () => { + const owner = 'ef'.repeat(32) + + await service().updateFollowList(owner.toUpperCase(), ['ab'.repeat(32)]) + + expect(deleteKeyStub).to.have.been.calledOnceWithExactly(`wot:follows:${owner}`) + expect(addToSetStub.firstCall.args[0]).to.equal(`wot:follows:${owner}`) + }) + + it('rebuilds when a contact list changes for an account the graph reached through an uppercase tag', async () => { + const owner = 'ab'.repeat(32) + followsOf[seedPubkey] = [owner.toUpperCase()] + const wot = service() + expect(await wot.getDistance(owner)).to.equal(1) + getSetMembersStub.resetHistory() + + await wot.updateFollowList(owner, ['cd'.repeat(32)]) + await (wot as any).building + + expect(getSetMembersStub).to.have.been.called // the graph was rebuilt + expect(await wot.getDistance('cd'.repeat(32))).to.equal(2) + }) + it('does not call addToSet for an empty follow list', async () => { await service().updateFollowList(seedPubkey, []) expect(deleteKeyStub).to.have.been.calledOnce