diff --git a/.changeset/wot-lowercase-follow-pubkeys.md b/.changeset/wot-lowercase-follow-pubkeys.md new file mode 100644 index 000000000..c1eeb78a2 --- /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. diff --git a/src/services/wot-graph-service.ts b/src/services/wot-graph-service.ts index 40675f127..071411157 100644 --- a/src/services/wot-graph-service.ts +++ b/src/services/wot-graph-service.ts @@ -11,7 +11,11 @@ const logger = createLogger('wot-graph-service') const SEED_PUBKEY_PATTERN = /^[0-9a-f]{64}$/i -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)}` // The settings a built graph depends on. A graph built for one key must not // answer for another: a hot-reloaded seed, depth or follower threshold would @@ -24,7 +28,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() @@ -91,9 +95,11 @@ 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 @@ -101,7 +107,7 @@ export class WotGraphService implements IWotGraphService { // change could actually matter. Fire-and-forget: the event path must not // block on a full graph rebuild, and scheduleRebuild() coalesces any // other triggers that land while one is already in flight. - if (pubkey === wot.seedPubkey || this.distances.has(pubkey)) { + if (pubkey === wot.seedPubkey || this.distances.has(normalizePubkey(pubkey))) { void this.scheduleRebuild() } } @@ -184,7 +190,7 @@ export class WotGraphService implements IWotGraphService { const follows = await this.getFollows(pubkey) // 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 (followed === wot.seedPubkey || distances.has(followed)) { continue } diff --git a/test/unit/services/wot-graph-service.spec.ts b/test/unit/services/wot-graph-service.spec.ts index 778256750..7dd62b267 100644 --- a/test/unit/services/wot-graph-service.spec.ts +++ b/test/unit/services/wot-graph-service.spec.ts @@ -306,6 +306,33 @@ 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) + }) + it('treats an invalid seedPubkey as an empty graph instead of throwing', async () => { settings.wot!.seedPubkey = 'not-a-valid-hex-pubkey' @@ -339,6 +366,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