Close all 14 findings from the codebase audit (1 high, 5 medium, 8 low): - Locking: replace the global API and workspace mutexes with per-instance and per-user keyed locks (new internal/api/locks.go) so a slow restore, upgrade or transfer no longer blocks unrelated users or instances; move Docker I/O out of the build queue and invalidate concurrent image-index observations with a generation counter; serialize instance deletion behind in-flight creation. - Security: equalize login timing for unknown usernames, periodically purge expired sessions/refresh tokens/lock rows, URL-encode orphan IDs in the frontend, upgrade vitest 3.x->5.x to clear 3 npm audit vulnerabilities (2 critical RCE). - Deployment: add a -healthcheck mode probing /healthz (used by Dockerfile HEALTHCHECK and compose), make ADMIN_PASSWORD optional in compose with removal guidance after first start, exclude test scripts from the image, and cap panel resources (cpus: 2, pids_limit: 512). - Reliability: check rows.Err() on admin listing loops, surface intent polling errors without sticking, disable close during in-flight create. Verified: go build/vet, full go test -race -count=1 ./..., frontend tsc build + 32 tests, npm audit 0, compose config, real-container healthcheck.
118 lines
2.9 KiB
Go
118 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestInvalidAuthConfigFailsBeforeDataCreation(t *testing.T) {
|
|
for _, key := range []string{"PANEL_PUBLIC_URL", "TRUSTED_PROXY_CIDRS"} {
|
|
t.Run(key, func(t *testing.T) {
|
|
t.Setenv("PANEL_PUBLIC_URL", "")
|
|
t.Setenv("TRUSTED_PROXY_CIDRS", "")
|
|
t.Setenv(key, "invalid")
|
|
root := filepath.Join(t.TempDir(), "not-created")
|
|
t.Setenv("CONTAINER_DATA_ROOT", root)
|
|
if err := run(); err == nil || !strings.Contains(err.Error(), key) {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if _, err := os.Stat(root); !os.IsNotExist(err) {
|
|
t.Fatalf("data directory created: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestServeWaitsForActiveHandler(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
address := make(chan string, 1)
|
|
entered := make(chan struct{})
|
|
release := make(chan struct{})
|
|
srv := &http.Server{Addr: "127.0.0.1:0", BaseContext: func(l net.Listener) context.Context {
|
|
address <- l.Addr().String()
|
|
return context.Background()
|
|
}, Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
close(entered)
|
|
<-release
|
|
_, _ = w.Write([]byte("finished"))
|
|
})}
|
|
done := make(chan error, 1)
|
|
go func() { done <- serve(ctx, srv) }()
|
|
var addr string
|
|
select {
|
|
case addr = <-address:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("server did not listen")
|
|
}
|
|
requestDone := make(chan error, 1)
|
|
go func() {
|
|
resp, err := http.Get("http://" + addr)
|
|
if err == nil {
|
|
_, err = io.Copy(io.Discard, resp.Body)
|
|
resp.Body.Close()
|
|
}
|
|
requestDone <- err
|
|
}()
|
|
select {
|
|
case <-entered:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("handler did not start")
|
|
}
|
|
cancel()
|
|
select {
|
|
case err := <-done:
|
|
close(release)
|
|
t.Fatalf("returned before handler completed: %v", err)
|
|
case <-time.After(100 * time.Millisecond):
|
|
}
|
|
close(release)
|
|
select {
|
|
case err := <-done:
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("shutdown stalled")
|
|
}
|
|
if err := <-requestDone; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestHealthcheckProbesLocalEndpoint(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/healthz" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Write([]byte("ok"))
|
|
}))
|
|
defer srv.Close()
|
|
_, port, err := net.SplitHostPort(srv.Listener.Addr().String())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("LISTEN_ADDR", "127.0.0.1:"+port)
|
|
if err := healthcheck(); err != nil {
|
|
t.Fatalf("healthy server rejected: %v", err)
|
|
}
|
|
// The container listens on 0.0.0.0; the probe must target loopback.
|
|
t.Setenv("LISTEN_ADDR", "0.0.0.0:"+port)
|
|
if err := healthcheck(); err != nil {
|
|
t.Fatalf("wildcard address rejected: %v", err)
|
|
}
|
|
t.Setenv("LISTEN_ADDR", "127.0.0.1:1")
|
|
if err := healthcheck(); err == nil {
|
|
t.Fatal("dead endpoint accepted")
|
|
}
|
|
}
|