Skip to content

Commit d09d679

Browse files
fix(GraphQL): Nested Auth Rules not working properly. (#7915) (#8084) (#8571)
Improves nested auth rule implementation in graphql. (cherry picked from commit e7a1931) Co-authored-by: minhaj-shakeel <minhaj@dgraph.io> (cherry picked from commit 26845c4) Co-authored-by: Naman Jain <naman@dgraph.io>
1 parent 412cfc3 commit d09d679

3 files changed

Lines changed: 44 additions & 4 deletions

File tree

graphql/e2e/auth/auth_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,44 @@ func TestAuthOnInterfaces(t *testing.T) {
513513
}
514514
}
515515

516+
func TestNestedAndAuthRulesWithMissingJWT(t *testing.T) {
517+
addParams := &common.GraphQLParams{
518+
Query: `
519+
mutation($user1: String!, $user2: String!){
520+
addGroup(input: [{users: {username: $user1}, createdBy: {username: $user2}}, {users: {username: $user2}, createdBy: {username: $user1}}]){
521+
numUids
522+
}
523+
}
524+
`,
525+
Variables: map[string]interface{}{"user1": "user1", "user2": "user2"},
526+
}
527+
gqlResponse := addParams.ExecuteAsPost(t, common.GraphqlURL)
528+
common.RequireNoGQLErrors(t, gqlResponse)
529+
require.JSONEq(t, `{"addGroup": {"numUids": 2}}`, string(gqlResponse.Data))
530+
531+
queryParams := &common.GraphQLParams{
532+
Query: `
533+
query{
534+
queryGroup{
535+
users{
536+
username
537+
}
538+
}
539+
}
540+
`,
541+
Headers: common.GetJWT(t, "user1", nil, metaInfo),
542+
}
543+
544+
expectedJSON := `{"queryGroup": [{"users": [{"username": "user1"}]}]}`
545+
546+
gqlResponse = queryParams.ExecuteAsPost(t, common.GraphqlURL)
547+
common.RequireNoGQLErrors(t, gqlResponse)
548+
require.JSONEq(t, expectedJSON, string(gqlResponse.Data))
549+
550+
deleteFilter := map[string]interface{}{"has": "users"}
551+
common.DeleteGqlType(t, "Group", deleteFilter, 2, nil)
552+
}
553+
516554
func TestAuthRulesWithNullValuesInJWT(t *testing.T) {
517555
testCases := []TestCase{
518556
{

graphql/resolve/auth_query_test.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -644,14 +644,11 @@
644644
queryGroup(func: uid(GroupRoot)) {
645645
Group.id : uid
646646
}
647-
GroupRoot as var(func: uid(Group_1)) @filter((uid(Group_Auth2) OR uid(Group_Auth3)))
647+
GroupRoot as var(func: uid(Group_1)) @filter(uid(Group_Auth2))
648648
Group_1 as var(func: type(Group))
649649
Group_Auth2 as var(func: uid(Group_1)) @cascade {
650650
Group.users : Group.users @filter(eq(User.username, "user1"))
651651
}
652-
Group_Auth3 as var(func: uid(Group_1)) @cascade {
653-
Group.createdBy : Group.createdBy @filter(eq(User.username, "user1"))
654-
}
655652
}
656653
657654
- name: "Auth with top level OR rbac false"

graphql/resolve/query_rewriter.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,11 @@ func (authRw *authRewriter) rewriteRuleNode(
978978

979979
switch {
980980
case len(rn.And) > 0:
981+
// if there is atleast one RBAC rule which is false, then this
982+
// whole And block needs to be ignored.
983+
if rn.EvaluateStatic(authRw.authVariables) == schema.Negative {
984+
return nil, nil
985+
}
981986
qrys, filts := nodeList(typ, rn.And)
982987
if len(filts) == 0 {
983988
return qrys, nil

0 commit comments

Comments
 (0)