Add admin and customer interfaces, orders, signed activation and leases, protected package delivery, keyring verification, migrations, security controls, tests and deployment documentation. Co-Authored-By: Craft Agent <agents-noreply@craft.do>
128 lines
3.8 KiB
Go
128 lines
3.8 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"github.com/gin-gonic/gin"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestOperationsSecurity(t *testing.T) {
|
|
var logs bytes.Buffer
|
|
ops := NewOperations(slog.New(slog.NewJSONHandler(&logs, nil)))
|
|
r := NewOperationalRouter(ops)
|
|
r.POST("/api/v1/auth/login", func(c *gin.Context) { apiError(c, 401, "invalid_credentials", "Denied") })
|
|
r.GET("/panic/:secret", func(c *gin.Context) { panic("PRIVATE_PEM_password") })
|
|
request := func(method, path, body, origin string) *httptest.ResponseRecorder {
|
|
q := httptest.NewRequest(method, path, strings.NewReader(body))
|
|
q.RemoteAddr = "127.0.0.1:1234"
|
|
q.Header.Set("Origin", origin)
|
|
q.Header.Set("Cookie", "lp_session=PRIVATE_COOKIE")
|
|
q.Header.Set("X-Request-ID", "FORGED_ID")
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, q)
|
|
return w
|
|
}
|
|
for i := 0; i < 10; i++ {
|
|
if w := request("POST", "/api/v1/auth/login?password=PRIVATE_QUERY", "PRIVATE_PASSWORD", ""); w.Code != 401 {
|
|
t.Fatal(w.Code)
|
|
}
|
|
}
|
|
w := request("POST", "/api/v1/auth/login", "", "")
|
|
if w.Code != 429 || w.Header().Get("Retry-After") == "" {
|
|
t.Fatal(w.Code)
|
|
}
|
|
w = request("GET", "/panic/PRIVATE_PATH", "", "")
|
|
if w.Code != 500 || strings.Contains(w.Body.String(), "PRIVATE") {
|
|
t.Fatal(w.Body.String())
|
|
}
|
|
if strings.Contains(logs.String(), "PRIVATE") || strings.Contains(logs.String(), "FORGED_ID") {
|
|
t.Fatal("secret or forged ID in logs")
|
|
}
|
|
dec := json.NewDecoder(&logs)
|
|
n := 0
|
|
for {
|
|
var row map[string]any
|
|
err := dec.Decode(&row)
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if row["request_id"] == "" || row["trace_id"] != row["request_id"] {
|
|
t.Fatal(row)
|
|
}
|
|
n++
|
|
}
|
|
if n != 12 {
|
|
t.Fatal(n)
|
|
}
|
|
mw := httptest.NewRecorder()
|
|
ops.Metrics(mw, httptest.NewRequest("GET", "/metrics", nil))
|
|
if !strings.Contains(mw.Body.String(), "license_requests_total{operation=\"login\",outcome=\"failure\"} 11") {
|
|
t.Fatal(mw.Body.String())
|
|
}
|
|
if request("GET", "/metrics", "", "").Code != 404 {
|
|
t.Fatal("public metrics exposed")
|
|
}
|
|
}
|
|
func TestOriginAndBodyGuards(t *testing.T) {
|
|
r := NewOperationalRouter(NewOperations(slog.New(slog.NewJSONHandler(io.Discard, nil))))
|
|
r.POST("/write", func(c *gin.Context) { c.Status(204) })
|
|
for _, tc := range []struct {
|
|
origin, body string
|
|
status int
|
|
}{{"https://evil.example", "", 403}, {"http://example.com", "", 204}, {"", "", 204}, {"", strings.Repeat("x", 65537), 413}} {
|
|
q := httptest.NewRequest("POST", "http://example.com/write", strings.NewReader(tc.body))
|
|
q.Header.Set("Origin", tc.origin)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, q)
|
|
if w.Code != tc.status {
|
|
t.Fatalf("origin=%s status=%d", tc.origin, w.Code)
|
|
}
|
|
}
|
|
}
|
|
func TestLocalLimiterBoundaryAndScope(t *testing.T) {
|
|
now := time.Now()
|
|
a := NewOperations(slog.Default())
|
|
a.now = func() time.Time { return now }
|
|
if !a.allow("test", 1) || a.allow("test", 1) {
|
|
t.Fatal("limit not enforced")
|
|
}
|
|
b := NewOperations(slog.Default())
|
|
if !b.allow("test", 1) {
|
|
t.Fatal("independent process scope")
|
|
}
|
|
now = now.Add(time.Minute)
|
|
if !a.allow("test", 1) {
|
|
t.Fatal("window not reset")
|
|
}
|
|
for i := 0; i < 10000; i++ {
|
|
a.buckets[string(rune(i))] = rateBucket{start: now}
|
|
}
|
|
if a.allow("new_key", 1) {
|
|
t.Fatal("capacity must fail closed")
|
|
}
|
|
}
|
|
func TestSessionCookieAttributes(t *testing.T) {
|
|
t.Setenv("SESSION_COOKIE_SECURE", "true")
|
|
r := gin.New()
|
|
r.GET("/cookie", func(c *gin.Context) {
|
|
c.SetSameSite(http.SameSiteStrictMode)
|
|
c.SetCookie("lp_session", "test", 60, "/", "", secureSessionCookie(c), true)
|
|
})
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, httptest.NewRequest("GET", "/cookie", nil))
|
|
cookies := w.Result().Cookies()
|
|
if len(cookies) != 1 || !cookies[0].Secure || !cookies[0].HttpOnly || cookies[0].SameSite != http.SameSiteStrictMode {
|
|
t.Fatal(cookies)
|
|
}
|
|
}
|