Some checks failed
Go Benchmark / Performance Regression Check (push) Has been cancelled
Cerberus Integration / Cerberus Security Stack Integration (push) Has been cancelled
Upload Coverage to Codecov / Backend Codecov Upload (push) Has been cancelled
Upload Coverage to Codecov / Frontend Codecov Upload (push) Has been cancelled
CodeQL - Analyze / CodeQL analysis (go) (push) Has been cancelled
CodeQL - Analyze / CodeQL analysis (javascript-typescript) (push) Has been cancelled
CrowdSec Integration / CrowdSec Bouncer Integration (push) Has been cancelled
Docker Build, Publish & Test / build-and-push (push) Has been cancelled
Quality Checks / Auth Route Protection Contract (push) Has been cancelled
Quality Checks / Codecov Trigger/Comment Parity Guard (push) Has been cancelled
Quality Checks / Backend (Go) (push) Has been cancelled
Quality Checks / Frontend (React) (push) Has been cancelled
Rate Limit integration / Rate Limiting Integration (push) Has been cancelled
Security Scan (PR) / Trivy Binary Scan (push) Has been cancelled
Supply Chain Verification (PR) / Verify Supply Chain (push) Has been cancelled
WAF integration / Coraza WAF Integration (push) Has been cancelled
Docker Build, Publish & Test / Security Scan PR Image (push) Has been cancelled
Repo Health Check / Repo health (push) Has been cancelled
History Rewrite Dry-Run / Dry-run preview for history rewrite (push) Has been cancelled
Prune Renovate Branches / prune (push) Has been cancelled
Renovate / renovate (push) Has been cancelled
Nightly Build & Package / sync-development-to-nightly (push) Has been cancelled
Nightly Build & Package / Trigger Nightly Validation Workflows (push) Has been cancelled
Nightly Build & Package / build-and-push-nightly (push) Has been cancelled
Nightly Build & Package / test-nightly-image (push) Has been cancelled
Nightly Build & Package / verify-nightly-supply-chain (push) Has been cancelled
56 lines
1.5 KiB
Go
Executable File
56 lines
1.5 KiB
Go
Executable File
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestSanitizeHeaders(t *testing.T) {
|
|
t.Run("nil headers", func(t *testing.T) {
|
|
require.Nil(t, SanitizeHeaders(nil))
|
|
})
|
|
|
|
t.Run("redacts sensitive headers", func(t *testing.T) {
|
|
headers := http.Header{}
|
|
headers.Set("Authorization", "secret")
|
|
headers.Set("X-Api-Key", "token")
|
|
headers.Set("Cookie", "sessionid=abc")
|
|
|
|
sanitized := SanitizeHeaders(headers)
|
|
|
|
require.Equal(t, []string{"<redacted>"}, sanitized["Authorization"])
|
|
require.Equal(t, []string{"<redacted>"}, sanitized["X-Api-Key"])
|
|
require.Equal(t, []string{"<redacted>"}, sanitized["Cookie"])
|
|
})
|
|
|
|
t.Run("sanitizes and truncates values", func(t *testing.T) {
|
|
headers := http.Header{}
|
|
headers.Add("X-Trace", "line1\nline2\r\t")
|
|
headers.Add("X-Custom", strings.Repeat("a", 210))
|
|
|
|
sanitized := SanitizeHeaders(headers)
|
|
|
|
traceValue := sanitized["X-Trace"][0]
|
|
require.NotContains(t, traceValue, "\n")
|
|
require.NotContains(t, traceValue, "\r")
|
|
require.NotContains(t, traceValue, "\t")
|
|
|
|
customValue := sanitized["X-Custom"][0]
|
|
require.Equal(t, 200, len(customValue))
|
|
require.True(t, strings.HasPrefix(customValue, strings.Repeat("a", 200)))
|
|
})
|
|
}
|
|
|
|
func TestSanitizePath(t *testing.T) {
|
|
paddedPath := "/api/v1/resource/" + strings.Repeat("x", 210) + "?token=secret"
|
|
|
|
sanitized := SanitizePath(paddedPath)
|
|
|
|
require.NotContains(t, sanitized, "?")
|
|
require.False(t, strings.ContainsAny(sanitized, "\n\r\t"))
|
|
require.Equal(t, 200, len(sanitized))
|
|
}
|