Skip to content

Commit

Permalink
Fixed Issue: guacsec#2195 500 error when sending empty JSON payload t…
Browse files Browse the repository at this point in the history
…o GraphQL server
  • Loading branch information
000xs committed Oct 18, 2024
1 parent 1a04f13 commit ec04cb8
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ func (pc *prometheusCollector) MeasureGraphQLResponseDuration(next http.Handler)

// Create a copy of the request body
body, err := io.ReadAll(r.Body)

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand All @@ -292,6 +293,11 @@ func (pc *prometheusCollector) MeasureGraphQLResponseDuration(next http.Handler)
var graphqlRequest struct {
OperationName string `json:"operationName"`
}
if !json.Valid(bodyCopy) { // Check if the body is valid JSON
err_msg := fmt.Sprintf("Provided input json couldn't be parsed. likely it was empty or was wrongly structured; error message: %v", err.Error())
http.Error(w, err_msg, http.StatusBadRequest)
return
}
if err := json.Unmarshal(bodyCopy, &graphqlRequest); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down

0 comments on commit ec04cb8

Please sign in to comment.