Skip to content

Commit 7ea3f71

Browse files
author
Jon Staab
committed
Allow unauthenticated users to query if restrict user is off, prevent publishing events on behalf of others
1 parent 28193ea commit 7ea3f71

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

common/access.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ func ConsumeInvite(claim string) string {
6262

6363
func GenerateInviteEvents(ctx context.Context, filter nostr.Filter) []*nostr.Event {
6464
pubkey := khatru.GetAuthed(ctx)
65+
66+
if pubkey == "" {
67+
return []*nostr.Event{}
68+
}
69+
6570
claim := GenerateInvite(pubkey)
6671
event := nostr.Event{
6772
Kind: AUTH_INVITE,

common/handlers.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ import (
1313
// RejectFilter
1414

1515
func RejectFilter(ctx context.Context, filter nostr.Filter) (reject bool, msg string) {
16-
pubkey := khatru.GetAuthed(ctx)
16+
if RELAY_RESTRICT_USER {
17+
pubkey := khatru.GetAuthed(ctx)
1718

18-
if pubkey == "" {
19-
return true, "auth-required: authentication is required for access"
20-
}
19+
if pubkey == "" {
20+
return true, "auth-required: authentication is required for access"
21+
}
2122

22-
if RELAY_RESTRICT_USER && !HasAccess(pubkey) {
23-
return true, "restricted: you are not a member of this relay"
23+
if !HasAccess(pubkey) {
24+
return true, "restricted: you are not a member of this relay"
25+
}
2426
}
2527

2628
return false, ""
@@ -32,13 +34,13 @@ func QueryEvents(ctx context.Context, filter nostr.Filter) (chan *nostr.Event, e
3234
ch := make(chan *nostr.Event)
3335
pubkey := khatru.GetAuthed(ctx)
3436

35-
stripSignature := func (event *nostr.Event) *nostr.Event {
36-
if RELAY_STRIP_SIGNATURES && !slices.Contains(RELAY_ADMINS, pubkey) {
37-
event.Sig = ""
38-
}
37+
stripSignature := func(event *nostr.Event) *nostr.Event {
38+
if RELAY_STRIP_SIGNATURES && !slices.Contains(RELAY_ADMINS, pubkey) {
39+
event.Sig = ""
40+
}
3941

40-
return event
41-
}
42+
return event
43+
}
4244

4345
go func() {
4446
defer close(ch)
@@ -102,20 +104,25 @@ func RejectEvent(ctx context.Context, event *nostr.Event) (reject bool, msg stri
102104
}
103105
}
104106

105-
// Auth is always required
107+
// Auth is always required to publish events
106108
if pubkey == "" {
107109
return true, "auth-required: authentication is required for access"
108110
}
109111

112+
// Reject replaying of events (join, create group) by other people
113+
if pubkey != event.PubKey && event.Kind != nostr.KindZap {
114+
return true, "restricted: you cannot publish events on behalf of others"
115+
}
116+
110117
// Process relay-level join requests before anything else
111-
if event.Kind == AUTH_JOIN && event.PubKey == pubkey {
118+
if event.Kind == AUTH_JOIN {
112119
tag := event.Tags.GetFirst([]string{"claim"})
113120

114121
if tag != nil {
115122
claim := tag.Value()
116123

117124
if IsValidClaim(claim) || HasAccess(ConsumeInvite(claim)) {
118-
AddUserClaim(event.PubKey, claim)
125+
AddUserClaim(pubkey, claim)
119126
}
120127

121128
if RELAY_RESTRICT_USER && !HasAccess(pubkey) {
@@ -130,7 +137,7 @@ func RejectEvent(ctx context.Context, event *nostr.Event) (reject bool, msg stri
130137
return true, "restricted: you are not a member of this relay"
131138
}
132139

133-
if RELAY_RESTRICT_AUTHOR && !HasAccess(event.PubKey) {
140+
if RELAY_RESTRICT_AUTHOR && !HasAccess(pubkey) {
134141
return true, "restricted: event author is not a member of this relay"
135142
}
136143

@@ -171,7 +178,7 @@ func RejectEvent(ctx context.Context, event *nostr.Event) (reject bool, msg stri
171178
return true, "invalid: group events not accepted on this relay"
172179
}
173180

174-
if !slices.Contains(RELAY_ADMINS, event.PubKey) {
181+
if !slices.Contains(RELAY_ADMINS, pubkey) {
175182
return true, "restricted: only relay admins can manage groups"
176183
}
177184
}
@@ -181,7 +188,7 @@ func RejectEvent(ctx context.Context, event *nostr.Event) (reject bool, msg stri
181188
return true, "invalid: group events not accepted on this relay"
182189
}
183190

184-
if IsGroupMember(ctx, h, event.PubKey) {
191+
if IsGroupMember(ctx, h, pubkey) {
185192
return true, "duplicate: already a member"
186193
}
187194
}
@@ -191,7 +198,7 @@ func RejectEvent(ctx context.Context, event *nostr.Event) (reject bool, msg stri
191198
return true, "invalid: group events not accepted on this relay"
192199
}
193200

194-
if !IsGroupMember(ctx, h, event.PubKey) {
201+
if !IsGroupMember(ctx, h, pubkey) {
195202
return true, "duplicate: not currently a member"
196203
}
197204
}
@@ -217,7 +224,7 @@ func RejectEvent(ctx context.Context, event *nostr.Event) (reject bool, msg stri
217224
return true, "invalid: unknown group"
218225
}
219226

220-
if !slices.Contains(groupRequestKinds, event.Kind) && g.Closed && !IsGroupMember(ctx, h, event.PubKey) {
227+
if !slices.Contains(groupRequestKinds, event.Kind) && g.Closed && !IsGroupMember(ctx, h, pubkey) {
221228
return true, "restricted: you are not a member of this group"
222229
}
223230
}

0 commit comments

Comments
 (0)