2022-05-27 09:59:13 -05:00
|
|
|
package coderd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2026-07-14 12:29:38 -07:00
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2022-05-27 09:59:13 -05:00
|
|
|
"net/http"
|
|
|
|
|
|
2026-01-08 15:24:11 +04:00
|
|
|
"cdr.dev/slog/v3"
|
2023-08-18 13:55:43 -05:00
|
|
|
"github.com/coder/coder/v2/coderd/httpapi"
|
|
|
|
|
"github.com/coder/coder/v2/codersdk"
|
2022-05-27 09:59:13 -05:00
|
|
|
)
|
|
|
|
|
|
2026-07-14 12:29:38 -07:00
|
|
|
// cspReportMaxBytes bounds the size of a single CSP violation report. This
|
|
|
|
|
// endpoint is unauthenticated and CSRF-exempt (it's the browser's
|
|
|
|
|
// `report-uri` target), so it must not allow unbounded body sizes to reach
|
|
|
|
|
// json.Decode. Real CSP reports are small JSON objects; 64KB is generous.
|
|
|
|
|
const cspReportMaxBytes = 64 * 1024
|
|
|
|
|
|
2022-05-27 09:59:13 -05:00
|
|
|
type cspViolation struct {
|
|
|
|
|
Report map[string]interface{} `json:"csp-report"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// logReportCSPViolations will log all reported csp violations.
|
2022-12-22 15:53:14 +01:00
|
|
|
//
|
|
|
|
|
// @Summary Report CSP violations
|
|
|
|
|
// @ID report-csp-violations
|
|
|
|
|
// @Security CoderSessionToken
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Tags General
|
|
|
|
|
// @Param request body cspViolation true "Violation report"
|
|
|
|
|
// @Success 200
|
2026-08-18 12:54:45 -07:00
|
|
|
// @Failure 413 {object} codersdk.Response "Request body exceeds 64 KiB"
|
2026-05-05 02:54:03 +05:00
|
|
|
// @Router /api/v2/csp/reports [post]
|
2022-05-27 09:59:13 -05:00
|
|
|
func (api *API) logReportCSPViolations(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
var v cspViolation
|
|
|
|
|
|
2026-07-14 12:29:38 -07:00
|
|
|
r.Body = http.MaxBytesReader(rw, r.Body, cspReportMaxBytes)
|
2022-05-27 09:59:13 -05:00
|
|
|
dec := json.NewDecoder(r.Body)
|
|
|
|
|
err := dec.Decode(&v)
|
|
|
|
|
if err != nil {
|
2026-07-14 12:29:38 -07:00
|
|
|
if _, ok := errors.AsType[*http.MaxBytesError](err); ok {
|
2026-08-18 12:54:45 -07:00
|
|
|
httpapi.RecordRequestBodyLimit(ctx, cspReportMaxBytes)
|
2026-07-14 12:29:38 -07:00
|
|
|
httpapi.Write(ctx, rw, http.StatusRequestEntityTooLarge, codersdk.Response{
|
|
|
|
|
Message: "Request body too large.",
|
|
|
|
|
Detail: fmt.Sprintf("Maximum CSP report size is %d bytes.", cspReportMaxBytes),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-06-21 12:00:38 +02:00
|
|
|
api.Logger.Warn(ctx, "CSP violation reported", slog.Error(err))
|
2022-09-21 17:07:00 -05:00
|
|
|
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
|
2022-06-07 09:33:06 -05:00
|
|
|
Message: "Failed to read body, invalid json.",
|
2022-06-03 16:48:09 -05:00
|
|
|
Detail: err.Error(),
|
2022-05-27 09:59:13 -05:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-08 10:29:41 +04:00
|
|
|
fields := make([]slog.Field, 0, len(v.Report))
|
2022-05-27 09:59:13 -05:00
|
|
|
for k, v := range v.Report {
|
|
|
|
|
fields = append(fields, slog.F(k, v))
|
|
|
|
|
}
|
2023-06-21 12:00:38 +02:00
|
|
|
api.Logger.Debug(ctx, "CSP violation reported", fields...)
|
2022-05-27 09:59:13 -05:00
|
|
|
|
2022-09-21 17:07:00 -05:00
|
|
|
httpapi.Write(ctx, rw, http.StatusOK, "ok")
|
2022-05-27 09:59:13 -05:00
|
|
|
}
|