Skip to content
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

Reject filters implicitly rather that failing a whole request #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions filter_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,8 @@ func (s *State) RequireKindAndSingleGroupIDOrSpecificEventReference(
}
}
case isReference:
if refE, ok := filter.Tags["e"]; ok && len(refE) > 0 {
// "#e" tags specified -- pablo's magic discovery trick
// we'll handle this while fetching the events so that we don't reveal private content
return false, ""
} else if refA, ok := filter.Tags["a"]; ok && len(refA) > 0 {
// "#a" tags specified -- idem
return false, ""
} else if len(filter.IDs) > 0 {
// "ids" specified -- idem
return false, ""
} else {
// other tags are not supported (unless they come together with "h")
return true, "invalid query, must have 'h', 'e' or 'a' tag"
}
// don't reject wholesale, since a single req may have multiple filters
// filters that we don't want to respond to are dropped in NormalEventQuery
case isMeta:
// should be fine
}
Expand Down
41 changes: 24 additions & 17 deletions queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,39 +225,46 @@ func (s *State) NormalEventQuery(ctx context.Context, filter nostr.Filter) (chan
ch := make(chan *nostr.Event)
authed := s.GetAuthed(ctx)
go func() {
// now here in refE/refA/ids we have to check for each result if it is allowed
var results chan *nostr.Event
var err error
if refE, ok := filter.Tags["e"]; ok && len(refE) > 0 {
results, err = s.DB.QueryEvents(ctx, filter)
} else if refA, ok := filter.Tags["a"]; ok && len(refA) > 0 {
results, err = s.DB.QueryEvents(ctx, filter)
} else if len(filter.IDs) > 0 {
results, err = s.DB.QueryEvents(ctx, filter)
}
results, err := s.DB.QueryEvents(ctx, filter)

if err != nil || results == nil {
// if the previous if didn't catch anything or if we got an error we must end here
close(ch)
return
}

allowed := set.NewSliceSet[string]()
disallowed := set.NewSliceSet[string]()
for evt := range results {
if group := s.GetGroupFromEvent(evt); !group.Private || allowed.Has(group.Address.ID) {
if evt.Kind != 39000 && s.GetGroupIDFromEvent(evt) == nil {
// if it has no `h` tag and isn't a metadata event, it's not protected
ch <- evt
} else if authed != "" && !disallowed.Has(group.Address.ID) {
continue
}

group := s.GetGroupFromEvent(evt)

if !group.Private {
// If the group is public, we're good to go
ch <- evt
continue
}

if authed != "" && !allowed.Has(group.Address.ID) {
group.mu.RLock()

// figure out whether the current user has access to this group
if _, isMember := group.Members[authed]; isMember {
allowed.Add(group.Address.ID)
ch <- evt
} else {
disallowed.Add(group.Address.ID)
}

group.mu.RUnlock()
}

if allowed.Has(group.Address.ID) {
// If they're allowed into the private group, we're good
ch <- evt
}
}

close(ch)
}()

Expand Down