Files
Charon/backend/internal/api/middleware/sanitize.go
GitHub Actions 3169b05156 fix: skip incomplete system log viewer tests
- Marked 12 tests as skip pending feature implementation
- Features tracked in GitHub issue #686 (system log viewer feature completion)
- Tests cover sorting by timestamp/level/method/URI/status, pagination controls, filtering by text/level, download functionality
- Unblocks Phase 2 at 91.7% pass rate to proceed to Phase 3 security enforcement validation
- TODO comments in code reference GitHub #686 for feature completion tracking
- Tests skipped: Pagination (3), Search/Filter (2), Download (2), Sorting (1), Log Display (4)
2026-02-09 21:55:55 +00:00

63 lines
1.5 KiB
Go

package middleware
import (
"net/http"
"strings"
"github.com/Wikid82/charon/backend/internal/util"
)
// SanitizeHeaders returns a map of header keys to redacted/sanitized values
// for safe logging. Sensitive headers are redacted; other values are
// sanitized using util.SanitizeForLog and truncated.
func SanitizeHeaders(h http.Header) map[string][]string {
if h == nil {
return nil
}
sensitive := map[string]struct{}{
"authorization": {},
"cookie": {},
"set-cookie": {},
"proxy-authorization": {},
"x-api-key": {},
"x-api-token": {},
"x-access-token": {},
"x-auth-token": {},
"x-api-secret": {},
"x-forwarded-for": {},
}
out := make(map[string][]string, len(h))
for k, vals := range h {
keyLower := strings.ToLower(k)
if _, ok := sensitive[keyLower]; ok {
out[k] = []string{"<redacted>"}
continue
}
sanitizedVals := make([]string, 0, len(vals))
for _, v := range vals {
v2 := util.SanitizeForLog(v)
if len(v2) > 200 {
v2 = v2[:200]
}
sanitizedVals = append(sanitizedVals, v2)
}
out[k] = sanitizedVals
}
return out
}
// SanitizePath prepares a request path for safe logging by removing
// control characters and truncating long values. It does not include
// query parameters.
func SanitizePath(p string) string {
// remove query string
if i := strings.Index(p, "?"); i != -1 {
p = p[:i]
}
p = util.SanitizeForLog(p)
if len(p) > 200 {
p = p[:200]
}
return p
}