47be24c4cc
Finding 3 of REVIEW-2026-07-30.md. target_entity_id was accepted as any non-empty string; the engine only compared it against a pinned TargetEntityID, which is empty for every registered capability. Spec §4.3: "Hexis never accepts a free-text target. Ever." Targets are now checked in order: ent_ shape (free, never touches the network), pinned target, existence in Nexus, entity still active, and a match against the capability's TargetTypes. Validation runs before a confirmation is consumed, so a bad target cannot burn one, and at confirmation-mint time too, since a confirmation binds a target. Two deliberate calls: Nexus unreachable fails closed (503, ErrTargetUnverifiable). Failing open would reinstate exactly this hole the moment Nexus blips, and hand it to anyone able to degrade Nexus. Hexis holds no entity table, so "unreachable" and "I cannot tell if this target is real" are the same statement. The cost is that executes now require Nexus liveness; the lookup is bounded at 5s so a hung Nexus fails fast rather than consuming the capability timeout. An empty TargetTypes means no type constraint, not a bypass — the entity must still exist, be canonical and be active. Rejecting empty outright would disable 16 of the 19 registered capabilities, since only the docker.* entries declare a target type. The spec's stronger blessing guard is not implementable: Nexus has no blessing concept at all. This is the achievable guard, and strictly weaker. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uea55zaiWuEByEDC4UBSdd
113 lines
3.4 KiB
Go
113 lines
3.4 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/kami/hexis/internal/domain"
|
|
"github.com/kami/hexis/internal/execution"
|
|
"github.com/kami/hexis/internal/nexusclient"
|
|
"github.com/kami/hexis/internal/provider"
|
|
"github.com/kami/hexis/internal/storage"
|
|
)
|
|
|
|
type targetTestProvider struct{}
|
|
|
|
func (targetTestProvider) Name() string { return "fake" }
|
|
func (targetTestProvider) Execute(ctx context.Context, c *domain.Capability, r *domain.ExecuteRequest) (map[string]any, error) {
|
|
return map[string]any{"ok": true}, nil
|
|
}
|
|
|
|
// newTargetValidationServer wires a Handler against a stub Nexus that knows
|
|
// exactly one container entity, plus one capability scoped to containers.
|
|
func newTargetValidationServer(t *testing.T) (*http.ServeMux, *domain.Capability) {
|
|
t.Helper()
|
|
|
|
nexus := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch strings.TrimPrefix(r.URL.Path, "/api/v1/entities/") {
|
|
case "ent_container_maven":
|
|
json.NewEncoder(w).Encode(map[string]any{
|
|
"id": "ent_container_maven", "type": "container", "state": "active",
|
|
})
|
|
case "ent_host_homesrv":
|
|
json.NewEncoder(w).Encode(map[string]any{
|
|
"id": "ent_host_homesrv", "type": "host", "state": "active",
|
|
})
|
|
default:
|
|
w.WriteHeader(http.StatusNotFound)
|
|
json.NewEncoder(w).Encode(map[string]string{"error": "entity not found"})
|
|
}
|
|
}))
|
|
t.Cleanup(nexus.Close)
|
|
|
|
store, err := storage.Open(filepath.Join(t.TempDir(), "hexis.db"))
|
|
if err != nil {
|
|
t.Fatalf("open store: %v", err)
|
|
}
|
|
t.Cleanup(func() { store.Close() })
|
|
|
|
reg := provider.NewRegistry()
|
|
reg.Register(targetTestProvider{})
|
|
|
|
now := time.Now().UTC()
|
|
cap := &domain.Capability{
|
|
ID: domain.NewCapabilityID(),
|
|
Name: "workspace.docker.restart",
|
|
Provider: "fake",
|
|
Operation: "noop",
|
|
Risk: "low",
|
|
Enabled: true,
|
|
TargetTypes: []string{"container"},
|
|
Attributes: map[string]any{},
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
Version: 1,
|
|
}
|
|
if err := store.CreateCapability(cap); err != nil {
|
|
t.Fatalf("create capability: %v", err)
|
|
}
|
|
|
|
engine := execution.New(store, reg, execution.WithEntityLookup(nexusclient.New(nexus.URL)))
|
|
mux := http.NewServeMux()
|
|
NewHandler(store, engine, testToken).Register(mux)
|
|
return mux, cap
|
|
}
|
|
|
|
// TestExecuteHTTP_TargetValidation pins the HTTP status codes for each way a
|
|
// target can be refused (ECOSYSTEM-SPEC.md §4.3).
|
|
func TestExecuteHTTP_TargetValidation(t *testing.T) {
|
|
mux, cap := newTargetValidationServer(t)
|
|
|
|
cases := []struct {
|
|
name string
|
|
target string
|
|
want int
|
|
}{
|
|
{"free text", "the maven container", http.StatusBadRequest},
|
|
{"bare name", "maven", http.StatusBadRequest},
|
|
{"unknown entity", "ent_not_real", http.StatusNotFound},
|
|
{"wrong type", "ent_host_homesrv", http.StatusUnprocessableEntity},
|
|
{"valid target", "ent_container_maven", http.StatusOK},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
body := `{"capability_id":"` + cap.ID + `","target_entity_id":"` + tc.target + `"}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/execute", strings.NewReader(body))
|
|
req.Header.Set("Authorization", "Bearer "+testToken)
|
|
w := httptest.NewRecorder()
|
|
mux.ServeHTTP(w, req)
|
|
|
|
if w.Code != tc.want {
|
|
t.Fatalf("target %q: expected %d, got %d: %s", tc.target, tc.want, w.Code, w.Body.String())
|
|
}
|
|
})
|
|
}
|
|
}
|