feat(nip42): restrict reads of encrypted kinds to authenticated recipients#702
Merged
cameri merged 10 commits intoJul 20, 2026
Merged
Conversation
🦋 Changeset detectedLatest commit: 1ec6ba4 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Collaborator
Anshumancanrock
force-pushed
the
feat/nip42-restricted-reads
branch
from
July 20, 2026 09:36
655f295 to
584c5c2
Compare
cameri
reviewed
Jul 20, 2026
Address review feedback: the check is about who the event is directed to (author or p-tagged recipient), not the author alone. Also clarify why authenticated clients fall through isSubscriptionAuthRequired.
…ed-reads # Conflicts: # package.json # resources/default-settings.yaml
cameri
approved these changes
Jul 20, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
We already support NIP-42 client authentication (the AUTH handshake , but nothing ever used that authentication to gate reads. Any client that is authenticated or not could
REQfor kind4/1059and pull down everyone's encrypted DMs and gift wraps, ciphertext plus who-talks-to-who metadata. COUNT and live broadcasts leaked the same way.This PR adds opt-in auth-based read filtering for a configurable set of "restricted" kinds. When enabled, events of those kinds are only delivered to clients that have authenticated as the event's author or as a pubkey listed in the event's
ptags. It applies consistently across all three read paths:WebSocketAdapter.onSendEvent.authors/#p, otherwise the count is refused.When an unauthenticated client opens a subscription that exclusively targets restricted kinds (it could never return anything), the relay closes it with
auth-required:reason so the client knows to authenticate and retry, instead of silently returning an empty stream. Mixed filters (e.g.kinds: [1, 4]) are still served, the unrestricted events come back and the restricted ones are filtered out per event.Related Issue
Closes #699
Motivation and Context
A relay that supports NIP-42 but never enforces it on reads offers no real read privacy so a passive observer can harvest every user's encrypted messages and social graph. Enforcing auth on the restricted kinds is what makes NIP-42 useful for private/paid relays and for protecting DM metadata.
How Has This Been Tested?
New unit suite
test/unit/utils/nip42.spec.tscovering the authorization primitives: author match, p-tag recipient match, stranger rejection, kind ranges, mixed vs. fully-restricted filters, COUNT scoping (authors/#p), and that the auth lookup is not consulted when the feature is disabled or the kind is unrestricted (lazy evaluation).Added cases to
subscribe-message-handler.spec.ts(REQ filtering +auth-requiredCLOSED),count-message-handler.spec.ts(COUNT scoping / refusal), andweb-socket-adapter.spec.ts(live broadcast delivered only to the authenticated author/recipient).Full relevant suite: 111 passing.
biome lintclean on all changed files.Screenshots (if appropriate):
N/A
Types of changes
Checklist: