Skip to content

Commit 1267cec

Browse files
fix(nip42): require auth to COUNT a filter that names no kinds
Signed-off-by: Priyanshubhartistm <[email protected]>
1 parent 026bd00 commit 1267cec

4 files changed

Lines changed: 86 additions & 10 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'nostream': patch
3+
---
4+
5+
fix(nip42): require auth to COUNT a filter that names no kinds when restricted reads are on
6+
7+
With `nip42.restrictedReads` enabled, a COUNT filter that named a restricted kind (4, 1059, ...) had to be scoped to the client's own pubkeys, but a filter that named no kinds was not checked, and the count still included the restricted events. An unauthenticated client could count, for example, `{"#p": [pubkey]}` and learn how many private messages a pubkey had received. A COUNT filter without `kinds` is now treated like one that names a restricted kind, the same rule NIP-77 reconciliation already follows: it must be scoped with `authors` or `#p` to the authenticated client's own pubkeys. Relays that do not enable `nip42.restrictedReads` are not affected.

‎src/utils/nip42.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ export const isSubscriptionAuthRequired = (
8989
return getAuthenticatedPubkeys().size === 0
9090
}
9191

92+
// A filter that could match a restricted event: one naming a restricted kind, or naming no kinds at all.
93+
const mayMatchRestrictedKind = (
94+
restrictedKinds: (EventKinds | EventKindsRange)[],
95+
filter: SubscriptionFilter,
96+
): boolean => !Array.isArray(filter.kinds) || filter.kinds.some((kind) => isKindRestricted(restrictedKinds, kind))
97+
9298
// NIP-77 reconciliation, like COUNT, can't be filtered per event: it reveals the id of every event the
9399
// filter matches. REQ hides restricted events per event, so a filter that *could* match one -- one
94100
// naming a restricted kind or naming no kinds at all -- must be scoped to the client's own pubkeys.
@@ -102,9 +108,7 @@ export const isReconciliationAuthorized = (
102108
return true
103109
}
104110

105-
const mayMatchRestricted =
106-
!Array.isArray(filter.kinds) || filter.kinds.some((kind) => isKindRestricted(restrictedKinds, kind))
107-
if (!mayMatchRestricted) {
111+
if (!mayMatchRestrictedKind(restrictedKinds, filter)) {
108112
return true
109113
}
110114

@@ -115,8 +119,8 @@ export const isReconciliationAuthorized = (
115119
return isScopedToClient(filter.authors) || isScopedToClient(filter['#p'])
116120
}
117121

118-
// COUNT can't be filtered per event, so a restricted-kind filter must be
119-
// scoped to the client's own pubkeys via authors/#p.
122+
// COUNT can't be filtered per event, so a filter that may match a restricted kind -- one naming a restricted
123+
// kind or naming no kinds at all -- must be scoped to the client's own pubkeys via authors/#p.
120124
export const isCountAuthorized = (
121125
settings: Settings | undefined,
122126
filters: SubscriptionFilter[],
@@ -127,9 +131,7 @@ export const isCountAuthorized = (
127131
return true
128132
}
129133

130-
const restrictedFilters = filters.filter(
131-
(filter) => Array.isArray(filter.kinds) && filter.kinds.some((kind) => isKindRestricted(restrictedKinds, kind)),
132-
)
134+
const restrictedFilters = filters.filter((filter) => mayMatchRestrictedKind(restrictedKinds, filter))
133135
if (!restrictedFilters.length) {
134136
return true
135137
}

‎test/unit/handlers/count-message-handler.spec.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,49 @@ describe('CountMessageHandler', () => {
307307
])
308308
})
309309

310+
it('returns CLOSED with auth-required when a filter names no kinds and could match restricted events', async () => {
311+
webSocket.getAuthenticatedPubkeys = sandbox.stub().returns(new Set())
312+
handler = new CountMessageHandler(
313+
webSocket,
314+
eventRepository,
315+
() =>
316+
({
317+
nip42: { restrictedReads: { enabled: true } },
318+
}) as Settings,
319+
)
320+
321+
const message = [MessageType.COUNT, 'q1', { '#p': ['f'.repeat(64)] }] as any
322+
323+
await handler.handleMessage(message)
324+
325+
expect(eventRepository.countByFilters).to.not.have.been.called
326+
expect(webSocketOnMessageStub).to.have.been.calledOnceWithExactly([
327+
MessageType.CLOSED,
328+
'q1',
329+
'auth-required: authentication is required to count these event kinds',
330+
])
331+
})
332+
333+
it('returns COUNT for a filter without kinds scoped to the authenticated client', async () => {
334+
const pubkey = 'f'.repeat(64)
335+
webSocket.getAuthenticatedPubkeys = sandbox.stub().returns(new Set([pubkey]))
336+
handler = new CountMessageHandler(
337+
webSocket,
338+
eventRepository,
339+
() =>
340+
({
341+
nip42: { restrictedReads: { enabled: true } },
342+
}) as Settings,
343+
)
344+
;(eventRepository.countByFilters as Sinon.SinonStub).resolves(3)
345+
346+
const filter = { authors: [pubkey] }
347+
348+
await handler.handleMessage([MessageType.COUNT, 'q1', filter] as any)
349+
350+
expect(eventRepository.countByFilters).to.have.been.calledOnceWithExactly([filter])
351+
})
352+
310353
it('returns CLOSED when counting restricted kinds scoped to another pubkey', async () => {
311354
webSocket.getAuthenticatedPubkeys = sandbox.stub().returns(new Set(['a'.repeat(64)]))
312355
handler = new CountMessageHandler(

‎test/unit/utils/nip42.spec.ts‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,32 @@ describe('nip42', () => {
163163
expect(isCountAuthorized(undefined, [{ kinds: [4] }], () => new Set())).to.be.true
164164
})
165165

166-
it('returns true when no filter targets restricted kinds', () => {
167-
expect(isCountAuthorized(enabledSettings(), [{ kinds: [1] }, {}], () => new Set())).to.be.true
166+
it('returns true when every filter names only ordinary kinds', () => {
167+
expect(isCountAuthorized(enabledSettings(), [{ kinds: [1] }, { kinds: [7], authors: [author] }], () => new Set())).to
168+
.be.true
169+
})
170+
171+
it('returns true for a filter that matches no kinds', () => {
172+
expect(isCountAuthorized(enabledSettings(), [{ kinds: [] }], () => new Set())).to.be.true
173+
})
174+
175+
it('returns false for a filter without kinds, which could match a restricted event', () => {
176+
expect(isCountAuthorized(enabledSettings(), [{}], () => new Set())).to.be.false
177+
expect(isCountAuthorized(enabledSettings(), [{ '#p': [recipient] }], () => new Set())).to.be.false
178+
expect(isCountAuthorized(enabledSettings(), [{ kinds: [1] }, {}], () => new Set([author]))).to.be.false
179+
})
180+
181+
it('returns false for a filter without kinds that is scoped to somebody else', () => {
182+
expect(isCountAuthorized(enabledSettings(), [{ '#p': [recipient] }], () => new Set([stranger]))).to.be.false
183+
})
184+
185+
it('returns true for a filter without kinds that is scoped to the authenticated client', () => {
186+
expect(isCountAuthorized(enabledSettings(), [{ authors: [author] }], () => new Set([author]))).to.be.true
187+
expect(isCountAuthorized(enabledSettings(), [{ '#p': [recipient] }], () => new Set([recipient]))).to.be.true
188+
})
189+
190+
it('returns true for a filter without kinds when restricted reads are off', () => {
191+
expect(isCountAuthorized(undefined, [{}], () => new Set())).to.be.true
168192
})
169193

170194
it('returns false for unauthenticated restricted-kind counts', () => {

0 commit comments

Comments
 (0)