|
| 1 | +import { EventKinds, EventTags } from '../constants/base' |
| 2 | +import { EventKindsRange, Settings } from '../@types/settings' |
| 3 | +import { Event } from '../@types/event' |
| 4 | +import { isEventKindOrRangeMatch } from './event' |
| 5 | +import { Pubkey } from '../@types/base' |
| 6 | +import { SubscriptionFilter } from '../@types/subscription' |
| 7 | + |
| 8 | +// NIP-42: restricted kinds are only readable by the author or a p-tagged recipient. |
| 9 | + |
| 10 | +export const DEFAULT_RESTRICTED_READ_KINDS: (EventKinds | EventKindsRange)[] = [ |
| 11 | + EventKinds.ENCRYPTED_DIRECT_MESSAGE, |
| 12 | + EventKinds.GIFT_WRAP, |
| 13 | +] |
| 14 | + |
| 15 | +export const getRestrictedReadKinds = (settings: Settings | undefined): (EventKinds | EventKindsRange)[] => { |
| 16 | + const restrictedReads = settings?.nip42?.restrictedReads |
| 17 | + if (!restrictedReads?.enabled) { |
| 18 | + return [] |
| 19 | + } |
| 20 | + |
| 21 | + return Array.isArray(restrictedReads.kinds) ? restrictedReads.kinds : DEFAULT_RESTRICTED_READ_KINDS |
| 22 | +} |
| 23 | + |
| 24 | +const isKindRestricted = (restrictedKinds: (EventKinds | EventKindsRange)[], kind: number): boolean => |
| 25 | + restrictedKinds.some(isEventKindOrRangeMatch({ kind } as Event)) |
| 26 | + |
| 27 | +export const isClientAuthorizedToReadMention = (event: Event, authenticatedPubkeys: ReadonlySet<Pubkey>): boolean => { |
| 28 | + if (!authenticatedPubkeys.size) { |
| 29 | + return false |
| 30 | + } |
| 31 | + |
| 32 | + if (authenticatedPubkeys.has(event.pubkey)) { |
| 33 | + return true |
| 34 | + } |
| 35 | + |
| 36 | + return event.tags.some( |
| 37 | + (tag) => tag.length >= 2 && tag[0] === EventTags.Pubkey && authenticatedPubkeys.has(tag[1]), |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +// getAuthenticatedPubkeys is only read for restricted events, so the guard is free when disabled. |
| 42 | +export const createReadAuthorizationGuard = ( |
| 43 | + settings: Settings | undefined, |
| 44 | + getAuthenticatedPubkeys: () => ReadonlySet<Pubkey>, |
| 45 | +): ((event: Event) => boolean) => { |
| 46 | + const restrictedKinds = getRestrictedReadKinds(settings) |
| 47 | + if (!restrictedKinds.length) { |
| 48 | + return () => true |
| 49 | + } |
| 50 | + |
| 51 | + return (event: Event) => { |
| 52 | + if (!isKindRestricted(restrictedKinds, event.kind)) { |
| 53 | + return true |
| 54 | + } |
| 55 | + |
| 56 | + return isClientAuthorizedToReadMention(event, getAuthenticatedPubkeys()) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +const isFullyRestrictedFilter = |
| 61 | + (restrictedKinds: (EventKinds | EventKindsRange)[]) => |
| 62 | + (filter: SubscriptionFilter): boolean => |
| 63 | + Array.isArray(filter.kinds) && |
| 64 | + filter.kinds.length > 0 && |
| 65 | + filter.kinds.every((kind) => isKindRestricted(restrictedKinds, kind)) |
| 66 | + |
| 67 | +// A sub that only asks for restricted kinds can never return anything to an |
| 68 | +// unauthenticated client, so we close it instead of serving an empty stream. |
| 69 | +export const isSubscriptionAuthRequired = ( |
| 70 | + settings: Settings | undefined, |
| 71 | + filters: SubscriptionFilter[], |
| 72 | + getAuthenticatedPubkeys: () => ReadonlySet<Pubkey>, |
| 73 | +): boolean => { |
| 74 | + const restrictedKinds = getRestrictedReadKinds(settings) |
| 75 | + if (!restrictedKinds.length) { |
| 76 | + return false |
| 77 | + } |
| 78 | + |
| 79 | + if (!filters.length || !filters.every(isFullyRestrictedFilter(restrictedKinds))) { |
| 80 | + return false |
| 81 | + } |
| 82 | + |
| 83 | + // An authenticated client is allowed through: createReadAuthorizationGuard |
| 84 | + // filters restricted events per-event, so they still only receive the ones |
| 85 | + // they authored or are p-tagged in. Only an unauthenticated client, whose |
| 86 | + // stream would always be empty, needs to be closed with auth-required. |
| 87 | + return getAuthenticatedPubkeys().size === 0 |
| 88 | +} |
| 89 | + |
| 90 | +// COUNT can't be filtered per event, so a restricted-kind filter must be |
| 91 | +// scoped to the client's own pubkeys via authors/#p. |
| 92 | +export const isCountAuthorized = ( |
| 93 | + settings: Settings | undefined, |
| 94 | + filters: SubscriptionFilter[], |
| 95 | + getAuthenticatedPubkeys: () => ReadonlySet<Pubkey>, |
| 96 | +): boolean => { |
| 97 | + const restrictedKinds = getRestrictedReadKinds(settings) |
| 98 | + if (!restrictedKinds.length) { |
| 99 | + return true |
| 100 | + } |
| 101 | + |
| 102 | + const restrictedFilters = filters.filter( |
| 103 | + (filter) => Array.isArray(filter.kinds) && filter.kinds.some((kind) => isKindRestricted(restrictedKinds, kind)), |
| 104 | + ) |
| 105 | + if (!restrictedFilters.length) { |
| 106 | + return true |
| 107 | + } |
| 108 | + |
| 109 | + const authenticatedPubkeys = getAuthenticatedPubkeys() |
| 110 | + const isScopedToClient = (values?: Pubkey[]) => |
| 111 | + Array.isArray(values) && values.length > 0 && values.every((value) => authenticatedPubkeys.has(value)) |
| 112 | + |
| 113 | + return restrictedFilters.every( |
| 114 | + (filter) => isScopedToClient(filter.authors) || isScopedToClient(filter['#p']), |
| 115 | + ) |
| 116 | +} |
0 commit comments