@@ -180,9 +180,32 @@ func (a *Alias) IterResourcesImpl(ctx *Context, subject ObjectAndRelation, filte
180180 return nil , err
181181 }
182182
183- // If the relation on the alias iterator is the same as the subject relation,
184- // we have a self-edge and should yield it
185- shouldAddSelfEdge := a .relation == subject .Relation
183+ // Check if we should add a self-edge (identity check: permission always grants access to itself)
184+ // This matches LookupResources3's logic where subject type+relation must match resource type+relation
185+ shouldAddSelfEdge := false
186+ if a .relation == subject .Relation {
187+ // Get the resource types from the iterator
188+ resourceTypes , err := a .ResourceType ()
189+ if err != nil {
190+ return nil , err
191+ }
192+
193+ // Add self-edge if:
194+ // - No resource types defined (empty/unconstrained iterator), OR
195+ // - Subject type matches one of the possible resource types
196+ // This allows self-edges for empty iterators while preventing them in nested contexts
197+ // where the types don't match
198+ if len (resourceTypes ) == 0 {
199+ shouldAddSelfEdge = true
200+ } else {
201+ for _ , rt := range resourceTypes {
202+ if rt .Type == subject .ObjectType {
203+ shouldAddSelfEdge = true
204+ break
205+ }
206+ }
207+ }
208+ }
186209
187210 return a .maybePrependSelfEdge (GetObject (subject ), subSeq , shouldAddSelfEdge ), nil
188211}
@@ -215,7 +238,7 @@ func (a *Alias) ID() string {
215238 return a .id
216239}
217240
218- func (a * Alias ) ResourceType () (ObjectType , error ) {
241+ func (a * Alias ) ResourceType () ([] ObjectType , error ) {
219242 return a .subIt .ResourceType ()
220243}
221244
0 commit comments