-
Notifications
You must be signed in to change notification settings - Fork 234
feat(nip42): restrict reads of encrypted kinds to authenticated recipients #702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cameri
merged 10 commits into
cameri:main
from
Anshumancanrock:feat/nip42-restricted-reads
Jul 20, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9823afe
feat(nip42): add restrictedReads settings and defaults
Anshumancanrock a3fcde7
feat(nip42): add read authorization helpers
Anshumancanrock 82a5bb2
feat(nip42): filter restricted kinds in REQ and close unauth-only subs
Anshumancanrock fa855b4
feat(nip42): require auth for restricted-kind COUNT
Anshumancanrock 6875441
feat(nip42): gate live broadcasts on auth
Anshumancanrock 444d85e
feat(nip42): advertise NIP-42 in supported_nips
Anshumancanrock e21fc9e
docs(nip42): document restrictedReads settings
Anshumancanrock 584c5c2
chore(nip42): add changeset
Anshumancanrock 740f172
refactor(nip42): rename read check to isClientAuthorizedToReadMention
Anshumancanrock 1ec6ba4
Merge remote-tracking branch 'upstream/main' into feat/nip42-restrict…
Anshumancanrock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "nostream": minor | ||
| --- | ||
|
|
||
| feat(nip42): enforce authentication on reads for restricted event kinds (encrypted DMs, gift wraps) across REQ, live broadcasts and COUNT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| 28, | ||
| 33, | ||
| 40, | ||
| 42, | ||
| 43, | ||
| 44, | ||
| 45, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import { EventKinds, EventTags } from '../constants/base' | ||
| import { EventKindsRange, Settings } from '../@types/settings' | ||
| import { Event } from '../@types/event' | ||
| import { isEventKindOrRangeMatch } from './event' | ||
| import { Pubkey } from '../@types/base' | ||
| import { SubscriptionFilter } from '../@types/subscription' | ||
|
|
||
| // NIP-42: restricted kinds are only readable by the author or a p-tagged recipient. | ||
|
|
||
| export const DEFAULT_RESTRICTED_READ_KINDS: (EventKinds | EventKindsRange)[] = [ | ||
| EventKinds.ENCRYPTED_DIRECT_MESSAGE, | ||
| EventKinds.GIFT_WRAP, | ||
| ] | ||
|
|
||
| export const getRestrictedReadKinds = (settings: Settings | undefined): (EventKinds | EventKindsRange)[] => { | ||
| const restrictedReads = settings?.nip42?.restrictedReads | ||
| if (!restrictedReads?.enabled) { | ||
| return [] | ||
| } | ||
|
|
||
| return Array.isArray(restrictedReads.kinds) ? restrictedReads.kinds : DEFAULT_RESTRICTED_READ_KINDS | ||
| } | ||
|
|
||
| const isKindRestricted = (restrictedKinds: (EventKinds | EventKindsRange)[], kind: number): boolean => | ||
| restrictedKinds.some(isEventKindOrRangeMatch({ kind } as Event)) | ||
|
|
||
| export const isClientAuthorizedToReadMention = (event: Event, authenticatedPubkeys: ReadonlySet<Pubkey>): boolean => { | ||
| if (!authenticatedPubkeys.size) { | ||
| return false | ||
| } | ||
|
|
||
| if (authenticatedPubkeys.has(event.pubkey)) { | ||
| return true | ||
| } | ||
|
|
||
| return event.tags.some( | ||
| (tag) => tag.length >= 2 && tag[0] === EventTags.Pubkey && authenticatedPubkeys.has(tag[1]), | ||
| ) | ||
| } | ||
|
|
||
| // getAuthenticatedPubkeys is only read for restricted events, so the guard is free when disabled. | ||
| export const createReadAuthorizationGuard = ( | ||
| settings: Settings | undefined, | ||
| getAuthenticatedPubkeys: () => ReadonlySet<Pubkey>, | ||
| ): ((event: Event) => boolean) => { | ||
| const restrictedKinds = getRestrictedReadKinds(settings) | ||
| if (!restrictedKinds.length) { | ||
| return () => true | ||
| } | ||
|
|
||
| return (event: Event) => { | ||
| if (!isKindRestricted(restrictedKinds, event.kind)) { | ||
| return true | ||
| } | ||
|
|
||
| return isClientAuthorizedToReadMention(event, getAuthenticatedPubkeys()) | ||
| } | ||
| } | ||
|
|
||
| const isFullyRestrictedFilter = | ||
| (restrictedKinds: (EventKinds | EventKindsRange)[]) => | ||
| (filter: SubscriptionFilter): boolean => | ||
| Array.isArray(filter.kinds) && | ||
| filter.kinds.length > 0 && | ||
| filter.kinds.every((kind) => isKindRestricted(restrictedKinds, kind)) | ||
|
|
||
| // A sub that only asks for restricted kinds can never return anything to an | ||
| // unauthenticated client, so we close it instead of serving an empty stream. | ||
| export const isSubscriptionAuthRequired = ( | ||
| settings: Settings | undefined, | ||
| filters: SubscriptionFilter[], | ||
| getAuthenticatedPubkeys: () => ReadonlySet<Pubkey>, | ||
| ): boolean => { | ||
| const restrictedKinds = getRestrictedReadKinds(settings) | ||
| if (!restrictedKinds.length) { | ||
| return false | ||
| } | ||
|
|
||
| if (!filters.length || !filters.every(isFullyRestrictedFilter(restrictedKinds))) { | ||
| return false | ||
| } | ||
|
|
||
| // An authenticated client is allowed through: createReadAuthorizationGuard | ||
| // filters restricted events per-event, so they still only receive the ones | ||
| // they authored or are p-tagged in. Only an unauthenticated client, whose | ||
| // stream would always be empty, needs to be closed with auth-required. | ||
| return getAuthenticatedPubkeys().size === 0 | ||
| } | ||
|
|
||
| // COUNT can't be filtered per event, so a restricted-kind filter must be | ||
| // scoped to the client's own pubkeys via authors/#p. | ||
| export const isCountAuthorized = ( | ||
| settings: Settings | undefined, | ||
| filters: SubscriptionFilter[], | ||
| getAuthenticatedPubkeys: () => ReadonlySet<Pubkey>, | ||
| ): boolean => { | ||
| const restrictedKinds = getRestrictedReadKinds(settings) | ||
| if (!restrictedKinds.length) { | ||
| return true | ||
| } | ||
|
|
||
| const restrictedFilters = filters.filter( | ||
| (filter) => Array.isArray(filter.kinds) && filter.kinds.some((kind) => isKindRestricted(restrictedKinds, kind)), | ||
| ) | ||
| if (!restrictedFilters.length) { | ||
| return true | ||
| } | ||
|
|
||
| const authenticatedPubkeys = getAuthenticatedPubkeys() | ||
| const isScopedToClient = (values?: Pubkey[]) => | ||
| Array.isArray(values) && values.length > 0 && values.every((value) => authenticatedPubkeys.has(value)) | ||
|
|
||
| return restrictedFilters.every( | ||
| (filter) => isScopedToClient(filter.authors) || isScopedToClient(filter['#p']), | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.