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>
75 lines
2.3 KiB
Go
75 lines
2.3 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"io"
|
|
"log/slog"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestPublicEntryLimitsBeforeHandlers(t *testing.T) {
|
|
for _, path := range []string{"/api/v1/auth/login", "/api/v1/activation/instances/:instance_id", "/api/v1/instances/:instance_id/heartbeat", "/api/v1/customers/:customer_id/releases/:id/download"} {
|
|
t.Run(path, func(t *testing.T) {
|
|
r := NewOperationalRouter(NewOperations(slog.New(slog.NewJSONHandler(io.Discard, nil))))
|
|
calls := 0
|
|
r.POST(path, func(c *gin.Context) { calls++; c.Status(401) })
|
|
n := 60
|
|
if strings.Contains(path, "login") {
|
|
n = 10
|
|
}
|
|
for i := 0; i <= n; i++ {
|
|
q := httptest.NewRequest("POST", strings.ReplaceAll(strings.ReplaceAll(path, ":instance_id", "x"), ":customer_id", "1"), nil)
|
|
q.URL.Path = strings.ReplaceAll(q.URL.Path, ":id", "1")
|
|
q.RemoteAddr = "127.0.0.1:1234"
|
|
q.Header.Set("X-Forwarded-For", strings.Repeat("1", i)+".2.3.4")
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, q)
|
|
if i == n && w.Code != 429 {
|
|
t.Fatal(w.Code)
|
|
}
|
|
}
|
|
if calls != n {
|
|
t.Fatal(calls)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
func TestChunkedBodyCORSAndMalformedSession(t *testing.T) {
|
|
r := NewOperationalRouter(NewOperations(slog.New(slog.NewJSONHandler(io.Discard, nil))))
|
|
r.POST("/json", func(c *gin.Context) {
|
|
var data map[string]any
|
|
if c.ShouldBindJSON(&data) != nil {
|
|
apiError(c, 400, "invalid_request", "Invalid JSON")
|
|
return
|
|
}
|
|
c.Status(204)
|
|
})
|
|
q := httptest.NewRequest("POST", "/json", strings.NewReader(`{"secret":"`+strings.Repeat("x", 65537)+`"}`))
|
|
q.ContentLength = -1
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, q)
|
|
if w.Code != 400 {
|
|
t.Fatal(w.Code)
|
|
}
|
|
q = httptest.NewRequest("OPTIONS", "/json", nil)
|
|
q.Header.Set("Origin", "https://evil.example")
|
|
w = httptest.NewRecorder()
|
|
r.ServeHTTP(w, q)
|
|
if w.Header().Get("Access-Control-Allow-Origin") != "" {
|
|
t.Fatal("CORS must not allow external origin")
|
|
}
|
|
a := &BusinessAPI{}
|
|
r.GET("/session", a.authenticated, func(c *gin.Context) { t.Fatal("malformed session authenticated") })
|
|
for _, cookie := range []string{"", "lp_session=bad", "lp_session=" + strings.Repeat("z", 64)} {
|
|
q = httptest.NewRequest("GET", "/session", nil)
|
|
q.Header.Set("Cookie", cookie)
|
|
w = httptest.NewRecorder()
|
|
r.ServeHTTP(w, q)
|
|
if w.Code != 401 {
|
|
t.Fatal(w.Code)
|
|
}
|
|
}
|
|
}
|