mirror of
https://github.com/coder/coder.git
synced 2026-06-03 13:08:25 +00:00
bddb808b25
Fixes all our Go file imports to match the preferred spec that we've _mostly_ been using. For example: ``` import ( "context" "time" "github.com/prometheus/client_golang/prometheus" "golang.org/x/xerrors" "gopkg.in/natefinch/lumberjack.v2" "cdr.dev/slog/v3" "github.com/coder/coder/v2/codersdk/agentsdk" "github.com/coder/serpent" ) ``` 3 groups: standard library, 3rd partly libs, Coder libs. This PR makes the change across the codebase. The PR in the stack above modifies our formatting to maintain this state of affairs, and is a separate PR so it's possible to review that one in detail.
49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package coderd
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"cdr.dev/slog/v3"
|
|
"github.com/coder/coder/v2/coderd/httpapi"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
type cspViolation struct {
|
|
Report map[string]interface{} `json:"csp-report"`
|
|
}
|
|
|
|
// logReportCSPViolations will log all reported csp violations.
|
|
//
|
|
// @Summary Report CSP violations
|
|
// @ID report-csp-violations
|
|
// @Security CoderSessionToken
|
|
// @Accept json
|
|
// @Tags General
|
|
// @Param request body cspViolation true "Violation report"
|
|
// @Success 200
|
|
// @Router /csp/reports [post]
|
|
func (api *API) logReportCSPViolations(rw http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
var v cspViolation
|
|
|
|
dec := json.NewDecoder(r.Body)
|
|
err := dec.Decode(&v)
|
|
if err != nil {
|
|
api.Logger.Warn(ctx, "CSP violation reported", slog.Error(err))
|
|
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
|
|
Message: "Failed to read body, invalid json.",
|
|
Detail: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
fields := make([]slog.Field, 0, len(v.Report))
|
|
for k, v := range v.Report {
|
|
fields = append(fields, slog.F(k, v))
|
|
}
|
|
api.Logger.Debug(ctx, "CSP violation reported", fields...)
|
|
|
|
httpapi.Write(ctx, rw, http.StatusOK, "ok")
|
|
}
|