Require auth on /api/v1/ and derive capability guards server-side

Findings 1 and 2 of REVIEW-2026-07-30.md, which must land together: every
workspace capability registered with enabled=false, so the only working
provider could never execute. Fixing that alone would have turned a dead
execution path into a reachable one on an unauthenticated port.

Auth: a shared bearer token (HEXIS_API_TOKEN) is now required on the whole
/api/v1/ surface, compared with crypto/subtle.ConstantTimeCompare. /health
and /ready stay open for probes. It fails closed twice over — hexisd refuses
to start with an empty token, and the middleware returns 503 rather than ever
serving unauthenticated.

Guards: `enabled` and `requires_confirmation` are no longer readable from the
request body at all. Previously the handler derived the correct §4.3 default
and then let the caller override it, which is worse than no guard because it
reads as enforced. Both are now derived from the risk tier by shared helpers
in domain, used by the HTTP and provider registration paths alike;
unrecognised tiers fail closed to requiring confirmation.

BuildCapabilities sets Enabled, RequiresConfirmation and TimeoutSeconds
explicitly, and hexisd reconciles drifted rows on startup instead of skipping
any capability whose ID already exists — without that, allowlist edits never
reach an existing database.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uea55zaiWuEByEDC4UBSdd
This commit is contained in:
kami
2026-07-30 23:39:13 +04:00
parent 945e4ba1ac
commit c7325a20d4
7 changed files with 368 additions and 90 deletions
+4 -33
View File
@@ -2,11 +2,7 @@ package api
import (
"context"
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"github.com/kami/hexis/internal/execution"
"github.com/kami/hexis/internal/storage"
@@ -15,40 +11,15 @@ import (
type Server struct {
handler *Handler
httpSrv *http.Server
socket string
}
func NewServer(store *storage.Store, engine *execution.Engine) *Server {
handler := NewHandler(store, engine)
// NewServer builds the HTTP server. authToken is the shared bearer token
// required on /api/v1/; see Handler.requireAuth.
func NewServer(store *storage.Store, engine *execution.Engine, authToken string) *Server {
handler := NewHandler(store, engine, authToken)
return &Server{handler: handler}
}
func (s *Server) ListenUnix(socketPath string) error {
dir := filepath.Dir(socketPath)
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("create socket directory: %w", err)
}
os.Remove(socketPath)
listener, err := net.Listen("unix", socketPath)
if err != nil {
return fmt.Errorf("listen unix: %w", err)
}
if err := os.Chmod(socketPath, 0660); err != nil {
listener.Close()
return fmt.Errorf("chmod socket: %w", err)
}
s.socket = socketPath
mux := http.NewServeMux()
s.handler.Register(mux)
return http.Serve(listener, mux)
}
func (s *Server) ListenHTTP(addr string) error {
mux := http.NewServeMux()
s.handler.Register(mux)