Files
pad/internal/server/middleware_security_test.go
xarmian 157ca4e88f chore: bump Go toolchain to 1.26 (TASK-763) (#247)
* chore: bump Go toolchain to 1.26 (TASK-763)

Bump Go from 1.25 to 1.26 across all toolchain pins:

- go.mod — go 1.25.0 → go 1.26.0
- Dockerfile — golang:1.25-alpine → golang:1.26-alpine
- .github/workflows/ci.yml — three setup-go steps (Go, Go-Postgres, E2E jobs)
- .github/workflows/release.yml — release pipeline

No `toolchain` directive: the repo is pre-launch with no external
contributors yet, so we set the floor where we want it (hard requirement).

Verified locally before commit:
- golangci-lint v2.11.4 builds and runs under Go 1.26.2 (pinned in CI)
- golang:1.26-alpine and 1.26.2-alpine images present on Docker Hub
- go build ./... clean
- go vet ./... clean
- go test ./... all pass

Parent: PLAN-644 (OSS Repo Hygiene and Launch Polish).

* chore: gofmt -w under Go 1.26 (TASK-763)

Apply Go 1.26's gofmt to the codebase. ~41 files reformatted, all
struct-tag whitespace realignment — no semantic changes. Verified:

- gofmt -l ./cmd ./internal returns empty after
- go build ./... still clean
- go test ./... still passes (run before commit)

Bundling the gofmt diff with the toolchain bump in the same PR because
the formatting drift is a direct consequence of moving from 1.25 to
1.26; splitting them creates a mandatory two-PR ordering for no value.

Parent: PLAN-644.

* docs: bump documented Go floor to 1.26 (TASK-763)

Match go.mod's hard 1.26.0 requirement in the source-build instructions.
Caught by Codex review round 1 on PR #247.

- README.md:158 — "Go 1.25+" → "Go 1.26+"
- CONTRIBUTING.md:9 — "Go 1.25+" → "Go 1.26+"
2026-04-25 11:35:19 -04:00

95 lines
2.8 KiB
Go

package server
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestSecurityHeaders(t *testing.T) {
srv := testServer(t)
req := httptest.NewRequest(http.MethodGet, "/api/v1/health", nil)
req.RemoteAddr = "192.0.2.1:1234"
w := httptest.NewRecorder()
srv.ServeHTTP(w, req)
headers := map[string]string{
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "DENY",
"Referrer-Policy": "strict-origin-when-cross-origin",
"Permissions-Policy": "camera=(), microphone=(), geolocation=()",
}
for name, expected := range headers {
got := w.Header().Get(name)
if got != expected {
t.Errorf("%s = %q, want %q", name, got, expected)
}
}
// CSP should be set
csp := w.Header().Get("Content-Security-Policy")
if csp == "" {
t.Error("Content-Security-Policy header not set")
}
// HSTS should NOT be set when secureCookies is false (default)
if hsts := w.Header().Get("Strict-Transport-Security"); hsts != "" {
t.Errorf("HSTS should not be set when secureCookies is off, got %q", hsts)
}
}
func TestParseCORSOrigins(t *testing.T) {
tests := []struct {
input string
want []string
}{
{"", []string{"http://localhost:*", "http://127.0.0.1:*"}},
{"https://app.pad.dev", []string{"https://app.pad.dev"}},
{"https://app.pad.dev, https://admin.pad.dev", []string{"https://app.pad.dev", "https://admin.pad.dev"}},
{" , ", []string{"http://localhost:*", "http://127.0.0.1:*"}}, // empty after trim
// TASK-664: '*' is incompatible with AllowCredentials=true and must
// be dropped. When it was the ONLY configured origin, parseCORSOrigins
// falls back to localhost defaults rather than producing an empty list.
{"*", []string{"http://localhost:*", "http://127.0.0.1:*"}},
{"https://app.pad.dev, *", []string{"https://app.pad.dev"}},
{"*, https://admin.pad.dev", []string{"https://admin.pad.dev"}},
}
for _, tt := range tests {
got := parseCORSOrigins(tt.input)
if len(got) != len(tt.want) {
t.Errorf("parseCORSOrigins(%q) = %v, want %v", tt.input, got, tt.want)
continue
}
for i := range got {
if got[i] != tt.want[i] {
t.Errorf("parseCORSOrigins(%q)[%d] = %q, want %q", tt.input, i, got[i], tt.want[i])
}
}
}
}
func TestCorsAllowCredentials(t *testing.T) {
// When PAD_CORS_ORIGINS is empty (or whitespace-only), AllowCredentials
// defaults to false. When an operator sets any non-empty value they
// opt into credential sharing across the listed origins. We don't
// second-guess a typo'd comma-only value — a stricter check should
// happen at parseCORSOrigins time.
tests := []struct {
in string
want bool
}{
{"", false},
{" ", false},
{"\t", false},
{"https://app.pad.dev", true},
}
for _, tt := range tests {
if got := corsAllowCredentials(tt.in); got != tt.want {
t.Errorf("corsAllowCredentials(%q) = %v, want %v", tt.in, got, tt.want)
}
}
}