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

Only allow query operations on GET requests #325

Merged
merged 1 commit into from
Aug 29, 2018
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
5 changes: 5 additions & 0 deletions handler/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ func GraphQL(exec graphql.ExecutableSchema, options ...Option) http.HandlerFunc
return
}

if op.Operation != ast.Query && r.Method == http.MethodGet {
sendErrorf(w, http.StatusUnprocessableEntity, "GET requests only allow query operations")
return
}

vars, err := validator.VariableValues(exec.Schema(), op, reqParams.Variables)
if err != nil {
sendError(w, http.StatusUnprocessableEntity, err)
Expand Down
6 changes: 6 additions & 0 deletions handler/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ func TestHandlerGET(t *testing.T) {
assert.Equal(t, http.StatusUnprocessableEntity, resp.Code)
assert.Equal(t, `{"data":null,"errors":[{"message":"Unexpected !","locations":[{"line":1,"column":1}]}]}`, resp.Body.String())
})

t.Run("no mutations", func(t *testing.T) {
resp := doRequest(h, "GET", "/graphql?query=mutation{me{name}}", "")
assert.Equal(t, http.StatusUnprocessableEntity, resp.Code)
assert.Equal(t, `{"data":null,"errors":[{"message":"GET requests only allow query operations"}]}`, resp.Body.String())
})
}

func TestHandlerOptions(t *testing.T) {
Expand Down