Skip to content

Commit

Permalink
fix semgrep dgryski.semgrep-go issues
Browse files Browse the repository at this point in the history
Fix most of the semgrep issues with the
http://semgrep.dev/r/dgryski.semgrep-go ruleset
(`semgrep --config http://semgrep.dev/r/dgryski.semgrep-go`).
Left the issue with Content-Type text/plain on json.Encode
in endpoints/openrtb2/amp_auction.go since changing to
application/json breaks the AMP unit tests, and issues
with the pointer receiver for MarshalJSON in usersync/cookie.go.

Fix prebid#3509.

Signed-off-by: Dmitry S <[email protected]>
  • Loading branch information
dmitris committed Mar 5, 2024
1 parent 05a1293 commit f09b01b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
8 changes: 4 additions & 4 deletions endpoints/events/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (e *eventEndpoint) Handle(w http.ResponseWriter, r *http.Request, _ httprou
w.WriteHeader(http.StatusBadRequest)

for _, err := range errs {
w.Write([]byte(fmt.Sprintf("invalid request: %s\n", err.Error())))
fmt.Fprintf(w, "invalid request: %s\n", err.Error())
}

return
Expand All @@ -81,7 +81,7 @@ func (e *eventEndpoint) Handle(w http.ResponseWriter, r *http.Request, _ httprou

if err != nil {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(fmt.Sprintf("Account '%s' is required query parameter and can't be empty", AccountIdParameter)))
fmt.Fprintf(w, "Account '%s' is required query parameter and can't be empty", AccountIdParameter)
return
}
eventRequest.AccountID = accountId
Expand All @@ -105,15 +105,15 @@ func (e *eventEndpoint) Handle(w http.ResponseWriter, r *http.Request, _ httprou
w.WriteHeader(status)

for _, message := range messages {
w.Write([]byte(fmt.Sprintf("Invalid request: %s\n", message)))
fmt.Fprintf(w, "Invalid request: %s\n", message)
}
return
}

// Check if events are enabled for the account
if !account.Events.Enabled {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(fmt.Sprintf("Account '%s' doesn't support events", eventRequest.AccountID)))
fmt.Fprintf(w, "Account '%s' doesn't support events", eventRequest.AccountID)
return
}

Expand Down
12 changes: 6 additions & 6 deletions endpoints/events/vtrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ func (v *vtrackEndpoint) Handle(w http.ResponseWriter, r *http.Request, _ httpro
// account id is required
if accountId == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("Account '%s' is required query parameter and can't be empty", AccountParameter)))
fmt.Fprintf(w, "Account '%s' is required query parameter and can't be empty", AccountParameter)
return
}

// get integration value from request parameter
integrationType, err := getIntegrationType(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("Invalid integration type: %s\n", err.Error())))
fmt.Fprintf(w, "Invalid integration type: %s\n", err.Error())
return
}

Expand All @@ -92,7 +92,7 @@ func (v *vtrackEndpoint) Handle(w http.ResponseWriter, r *http.Request, _ httpro
// check if there was any error while parsing puts request
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("Invalid request: %s\n", err.Error())))
fmt.Fprintf(w, "Invalid request: %s\n", err.Error())
return
}

Expand All @@ -106,7 +106,7 @@ func (v *vtrackEndpoint) Handle(w http.ResponseWriter, r *http.Request, _ httpro
w.WriteHeader(status)

for _, message := range messages {
w.Write([]byte(fmt.Sprintf("Invalid request: %s\n", message)))
fmt.Fprintf(w, "Invalid request: %s\n", message)
}
return
}
Expand All @@ -118,7 +118,7 @@ func (v *vtrackEndpoint) Handle(w http.ResponseWriter, r *http.Request, _ httpro
if len(errs) > 0 {
w.WriteHeader(http.StatusInternalServerError)
for _, err := range errs {
w.Write([]byte(fmt.Sprintf("Error(s) updating vast: %s\n", err.Error())))
fmt.Fprintf(w, "Error(s) updating vast: %s\n", err.Error())

return
}
Expand All @@ -128,7 +128,7 @@ func (v *vtrackEndpoint) Handle(w http.ResponseWriter, r *http.Request, _ httpro

if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("Error serializing pbs cache response: %s\n", err.Error())))
fmt.Fprintf(w, "Error serializing pbs cache response: %s\n", err.Error())

return
}
Expand Down
5 changes: 3 additions & 2 deletions endpoints/openrtb2/amp_auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (deps *endpointDeps) AmpAuction(w http.ResponseWriter, r *http.Request, _ h
if errortypes.ContainsFatalError(errL) {
w.WriteHeader(http.StatusBadRequest)
for _, err := range errortypes.FatalOnly(errL) {
w.Write([]byte(fmt.Sprintf("Invalid request: %s\n", err.Error())))
fmt.Fprintf(w, "Invalid request: %s\n", err.Error())
}
labels.RequestStatus = metrics.RequestStatusBadInput
return
Expand Down Expand Up @@ -224,7 +224,7 @@ func (deps *endpointDeps) AmpAuction(w http.ResponseWriter, r *http.Request, _ h
w.WriteHeader(httpStatus)
labels.RequestStatus = metricsStatus
for _, err := range errortypes.FatalOnly(errL) {
w.Write([]byte(fmt.Sprintf("Invalid request: %s\n", err.Error())))
fmt.Fprintf(w, "Invalid request: %s\n", err.Error())
}
ao.Errors = append(ao.Errors, acctIDErrs...)
return
Expand Down Expand Up @@ -387,6 +387,7 @@ func sendAmpResponse(
// Fixes #231
enc := json.NewEncoder(w)
enc.SetEscapeHTML(false)
w.Header().Set("Content-Type", "text/plain; charset=utf-8")

// If an error happens when encoding the response, there isn't much we can do.
// If we've sent _any_ bytes, then Go would have sent the 200 status code first.
Expand Down
2 changes: 1 addition & 1 deletion endpoints/openrtb2/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2375,7 +2375,7 @@ func writeError(errs []error, w http.ResponseWriter, labels *metrics.Labels) boo
w.WriteHeader(httpStatus)
labels.RequestStatus = metricsStatus
for _, err := range errs {
w.Write([]byte(fmt.Sprintf("Invalid request: %s\n", err.Error())))
fmt.Fprintf(w, "Invalid request: %s\n", err.Error())
}
rc = true
}
Expand Down

0 comments on commit f09b01b

Please sign in to comment.