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

Improve auth error messages #244

Merged
merged 2 commits into from
Dec 19, 2023
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
18 changes: 16 additions & 2 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sort"
"strings"

log "github.com/sirupsen/logrus"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/gqlerror"
)
Expand Down Expand Up @@ -33,7 +34,7 @@ func (a AllowedFields) IsAllowed(fieldName string) (bool, AllowedFields) {
return false, AllowedFields{}
}

// OperationPermissions represents the user permissions for all operation types
// OperationPermissions represents the top level permissions for all operation types
type OperationPermissions struct {
AllowedRootQueryFields AllowedFields `json:"query"`
AllowedRootMutationFields AllowedFields `json:"mutation"`
Expand All @@ -54,6 +55,14 @@ func (f fieldList) Swap(i, j int) {
f[i], f[j] = f[j], f[i]
}

func (a AllowedFields) String() string {
bytes, err := json.Marshal(a)
if err != nil {
return err.Error()
}
return string(bytes)
}

// MarshalJSON marshals to a JSON representation.
func (a AllowedFields) MarshalJSON() ([]byte, error) {
if a.AllowAll {
Expand Down Expand Up @@ -270,7 +279,12 @@ func filterFields(path []string, ss ast.SelectionSet, allowedFields AllowedField
res = append(res, s)
errs = append(errs, ferrs...)
} else {
errs = append(errs, gqlerror.Errorf("user do not have permission to access field %s.%s", strings.Join(path, "."), s.Name))
fieldPath := strings.Join(append(path, s.Name), ".")
log.WithFields(log.Fields{
"field": fieldPath,
"permissions": allowedFields,
}).Debug("field access disallowed")
errs = append(errs, gqlerror.Errorf("%s access disallowed", fieldPath))
}
case *ast.FragmentSpread:
var ferrs gqlerror.List
Expand Down
4 changes: 2 additions & 2 deletions auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestFilterAuthorizedFields(t *testing.T) {
}
errs := perms.FilterAuthorizedFields(query.Operations[0])
require.Len(t, errs, 1)
assert.Equal(t, errs[0].Message, "user do not have permission to access field query.movies.compTitles")
assert.Equal(t, errs[0].Message, "query.movies.compTitles access disallowed")

assertSelectionSetsEqual(t, schema, strToSelectionSet(schema, `{
movies {
Expand Down Expand Up @@ -219,7 +219,7 @@ func TestFilterAuthorizedFields(t *testing.T) {
}
errs := perms.FilterAuthorizedFields(query.Operations[0])
require.Len(t, errs, 1)
assert.Equal(t, errs[0].Message, "user do not have permission to access field query.movies.compTitles")
assert.Equal(t, errs[0].Message, "query.movies.compTitles access disallowed")

assertSelectionSetsEqual(t, schema, strToSelectionSet(schema, `{
movies {
Expand Down
2 changes: 1 addition & 1 deletion execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestHonorsPermissions(t *testing.T) {
resp := es.ExecuteQuery(ctx)

permissionsError := &gqlerror.Error{
Message: "user do not have permission to access field query.cinema",
Message: "query.cinema access disallowed",
}

require.Contains(t, resp.Errors, permissionsError)
Expand Down
Loading