Skip to content

Commit

Permalink
Only allow query operations on GET requests
Browse files Browse the repository at this point in the history
This mitigates the risk of CSRF attacks.

Closes #317.
  • Loading branch information
edsrzf committed Aug 28, 2018
1 parent 40943c6 commit 82a28b5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
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

0 comments on commit 82a28b5

Please sign in to comment.