Files
coder/coderd/csp.go
T
Spike Curtis 49b34a716a fix: fix slog to always use array of Fields (#21426)
Upgrades to slog v3 which includes a small, but backward incompatible API change to the acceptible call arguments when logging. This change allows us to verify via compile time type checking that arguments are correct and won't cause a panic, as was possible in slog v1, which this replaces (v2 was tagged but never used in coder/coder).

It also updates dependencies that also use slog and were updated.

I've left the `aibridge` dependency as a commit SHA, under the assumption that the team there (cc @pawbana @dannykopping ) will tag and update the dependency soon and on their own schedule.

Other dependencies, I pushed new tags.
2026-01-08 10:29:41 +04:00

50 lines
1.2 KiB
Go

package coderd
import (
"encoding/json"
"net/http"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/codersdk"
"cdr.dev/slog/v3"
)
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")
}