| 1 | package gates |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/FelineStateMachine/tinyrelay/internal/event" |
| 9 | "github.com/FelineStateMachine/tinyrelay/internal/policy" |
| 10 | "github.com/FelineStateMachine/tinyrelay/internal/relay" |
| 11 | ) |
| 12 | |
| 13 | func TestSignerRepliesReachUnauthenticatedRecipientSubscription(t *testing.T) { |
| 14 | sender, recipient := strings.Repeat("a" , 64), strings.Repeat("b" , 64) |
| 15 | message := event.Event{Kind: event.KIND_NOSTR_CONNECT, PubKey: sender, Tags: [][]string{{"p" , recipient}}} |
| 16 | for _, tc := range []struct { |
| 17 | name string |
| 18 | enabled bool |
| 19 | filter *event.Filter |
| 20 | session relay.Session |
| 21 | want bool |
| 22 | }{ |
| 23 | {"encrypted reply" , true, &event.Filter{Kinds: []int{24133}, Tags: map[string][]string{"p" : {recipient}}}, relay.Session{}, true}, |
| 24 | {"unrelated recipient" , true, &event.Filter{Kinds: []int{24133}, Tags: map[string][]string{"p" : {sender}}}, relay.Session{}, false}, |
| 25 | {"unscoped subscription" , true, &event.Filter{Kinds: []int{24133}}, relay.Session{}, false}, |
| 26 | {"authenticated recipient" , true, nil, relay.Session{PubKeys: []string{recipient}}, true}, |
| 27 | {"signer disabled" , false, &event.Filter{Kinds: []int{24133}, Tags: map[string][]string{"p" : {recipient}}}, relay.Session{PubKeys: []string{recipient}}, false}, |
| 28 | } { |
| 29 | t.Run(tc.name, func(t *testing.T) { |
| 30 | p := policy.Defaults(sender) |
| 31 | p.Reads, p.Writes = "members" , "owner" |
| 32 | p.Features.Signer = tc.enabled |
| 33 | g, err := New(Config{Policy: func() policy.Policy { return p }}) |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | if got := g.CanSee(context.Background(), message, tc.session, tc.filter); got != tc.want { |
| 38 | t.Fatalf("can see signer reply: got %v, want %v" , got, tc.want) |
| 39 | } |
| 40 | }) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestArtifactVisibilityTracksNestedSourcesAndCycles(t *testing.T) { |
| 45 | p := policy.Defaults(strings.Repeat("a" , 64)) |
| 46 | nodes := map[string]event.Event{ |
| 47 | "a" : {ID: "a" , Kind: event.KIND_VIEW}, |
| 48 | "b" : {ID: "b" , Kind: event.KIND_VIEW}, |
| 49 | } |
| 50 | refs := map[string][]string{"a" : {"b" }, "b" : {"hidden" }} |
| 51 | var gate *Gate |
| 52 | gate, _ = New(Config{Policy: func() policy.Policy { return p }, ArtifactVisible: func(ctx context.Context, e event.Event, s relay.Session) bool { |
| 53 | for _, id := range refs[e.ID] { |
| 54 | if id == "hidden" { |
| 55 | return false |
| 56 | } |
| 57 | if !gate.CanSee(ctx, nodes[id], s, nil) { |
| 58 | return false |
| 59 | } |
| 60 | } |
| 61 | return true |
| 62 | }}) |
| 63 | if gate.CanSee(context.Background(), nodes["a" ], relay.Session{}, nil) { |
| 64 | t.Fatal("nested artifact with hidden source became visible" ) |
| 65 | } |
| 66 | refs["b" ] = []string{"a" } |
| 67 | if gate.CanSee(context.Background(), nodes["a" ], relay.Session{}, nil) { |
| 68 | t.Fatal("cyclic artifact source became visible" ) |
| 69 | } |
| 70 | } |