Skip to content

Commit d031ee7

Browse files
author
Tit Petric
committed
Fix expected behaviouor for merging allowed types
1 parent 071d178 commit d031ee7

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

internal/policy/apply.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,10 @@ func (t *Service) applyPartitions(policy user.Policy, session *user.SessionState
375375

376376
r.AllowedURLs = MergeAllowedURLs(r.AllowedURLs, v.AllowedURLs)
377377

378+
// When two or more non-empty policies are applied, only the
379+
// fields restricted by all policies are in the resulting policy.
380+
// A merge of `[a b]` and `[b c]` becomes `[b]`, as `b` is
381+
// restricted by both of the policies.
378382
if len(r.RestrictedTypes) == 0 {
379383
r.RestrictedTypes = v.RestrictedTypes
380384
} else {
@@ -387,13 +391,16 @@ func (t *Service) applyPartitions(policy user.Policy, session *user.SessionState
387391
}
388392
}
389393

394+
// When two or more non-empty policies are applied, the fields allowed
395+
// are merged in the resulting policy. For an example, `[a b]` and `[b c]`,
396+
// results in a polict that allows `[a b c]`.
390397
if len(r.AllowedTypes) == 0 {
391398
r.AllowedTypes = v.AllowedTypes
392399
} else {
393400
for _, t := range v.AllowedTypes {
394401
for ri, rt := range r.AllowedTypes {
395402
if t.Name == rt.Name {
396-
r.AllowedTypes[ri].Fields = intersection(rt.Fields, t.Fields)
403+
r.AllowedTypes[ri].Fields = appendIfMissing(rt.Fields, t.Fields...)
397404
}
398405
}
399406
}

internal/policy/apply_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ func testPrepareApplyPolicies(tb testing.TB) (*policy.Service, []testApplyPolici
759759
t.Helper()
760760

761761
want := map[string]user.AccessDefinition{
762-
"a": { // It should get intersection of restricted types.
762+
"a": {
763763
RestrictedTypes: []graphql.Type{
764764
{Name: "Country", Fields: []string{"code"}},
765765
{Name: "Person", Fields: []string{"name"}},
@@ -777,12 +777,11 @@ func testPrepareApplyPolicies(tb testing.TB) (*policy.Service, []testApplyPolici
777777
sessMatch: func(t *testing.T, s *user.SessionState) {
778778
t.Helper()
779779

780-
// It should get intersection of allowed/restricted types.
781780
want := map[string]user.AccessDefinition{
782781
"a": {
783782
AllowedTypes: []graphql.Type{
784-
{Name: "Country", Fields: []string{"code"}},
785-
{Name: "Person", Fields: []string{"name"}},
783+
{Name: "Country", Fields: []string{"code", "name", "phone"}},
784+
{Name: "Person", Fields: []string{"name", "height", "mass"}},
786785
},
787786
RestrictedTypes: []graphql.Type{
788787
{Name: "Dog", Fields: []string{"name", "breed"}},

0 commit comments

Comments
 (0)