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

fix(GraphQL): Nested Auth Rules not working properly. (#7915) #8084

Merged
merged 1 commit into from
Oct 19, 2021
Merged
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
38 changes: 38 additions & 0 deletions graphql/e2e/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,44 @@ func TestAuthOnInterfaces(t *testing.T) {
}
}

func TestNestedAndAuthRulesWithMissingJWT(t *testing.T) {
addParams := &common.GraphQLParams{
Query: `
mutation($user1: String!, $user2: String!){
addGroup(input: [{users: {username: $user1}, createdBy: {username: $user2}}, {users: {username: $user2}, createdBy: {username: $user1}}]){
numUids
}
}
`,
Variables: map[string]interface{}{"user1": "user1", "user2": "user2"},
}
gqlResponse := addParams.ExecuteAsPost(t, common.GraphqlURL)
common.RequireNoGQLErrors(t, gqlResponse)
require.JSONEq(t, `{"addGroup": {"numUids": 2}}`, string(gqlResponse.Data))

queryParams := &common.GraphQLParams{
Query: `
query{
queryGroup{
users{
username
}
}
}
`,
Headers: common.GetJWT(t, "user1", nil, metaInfo),
}

expectedJSON := `{"queryGroup": [{"users": [{"username": "user1"}]}]}`

gqlResponse = queryParams.ExecuteAsPost(t, common.GraphqlURL)
common.RequireNoGQLErrors(t, gqlResponse)
require.JSONEq(t, expectedJSON, string(gqlResponse.Data))

deleteFilter := map[string]interface{}{"has": "users"}
common.DeleteGqlType(t, "Group", deleteFilter, 2, nil)
}

func TestAuthRulesWithNullValuesInJWT(t *testing.T) {
testCases := []TestCase{
{
Expand Down
6 changes: 2 additions & 4 deletions graphql/resolve/auth_query_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -644,14 +644,11 @@
queryGroup(func: uid(GroupRoot)) {
Group.id : uid
}
GroupRoot as var(func: uid(Group_1)) @filter((uid(Group_Auth2) OR uid(Group_Auth3)))
GroupRoot as var(func: uid(Group_1)) @filter(uid(Group_Auth2))
Group_1 as var(func: type(Group))
Group_Auth2 as var(func: uid(Group_1)) @cascade {
Group.users : Group.users @filter(eq(User.username, "user1"))
}
Group_Auth3 as var(func: uid(Group_1)) @cascade {
Group.createdBy : Group.createdBy @filter(eq(User.username, "user1"))
}
}

- name: "Auth with top level OR rbac false"
Expand Down Expand Up @@ -2103,3 +2100,4 @@
}
}
}

5 changes: 5 additions & 0 deletions graphql/resolve/query_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,11 @@ func (authRw *authRewriter) rewriteRuleNode(

switch {
case len(rn.And) > 0:
// if there is atleast one RBAC rule which is false, then this
// whole And block needs to be ignored.
if rn.EvaluateStatic(authRw.authVariables) == schema.Negative {
return nil, nil
}
qrys, filts := nodeList(typ, rn.And)
if len(filts) == 0 {
return qrys, nil
Expand Down