fix: allow cold managed service discovery only at stage admission
This commit is contained in:
@@ -8,11 +8,13 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExecutionServiceReferencePinsIdentityAndAuth(t *testing.T) {
|
||||
t.Setenv("HOME", t.TempDir())
|
||||
c, calls := exportAPI(t, `{"data":{"info":{"id":"ses_ok"},"messages":[{"type":"user","text":"execution A"}]}}`, 200)
|
||||
chatCalls := 0
|
||||
chat := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -50,6 +52,9 @@ func TestExecutionServiceReferencePinsIdentityAndAuth(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if runtime.GOOS == "darwin" && !strings.HasPrefix(ref, filepath.Join(os.Getenv("HOME"), "Library", "Caches")+string(filepath.Separator)) {
|
||||
t.Fatal("reference escaped scratch macOS HOME cache")
|
||||
}
|
||||
data, _ := os.ReadFile(ref)
|
||||
if strings.Contains(string(data), c.password) || strings.Contains(string(data), `"password"`) {
|
||||
t.Fatal("credential copied into reference")
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package opencode
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// OpenStage is the admission-only connection path. Unlike retained-session
|
||||
// reads and interruption, a new stage may discover/start the CLI-managed V2
|
||||
// service before creating any session. Chat endpoint overrides are not involved.
|
||||
func OpenStage(ctx context.Context, executable, directory, backend string) (*Connection, error) {
|
||||
c, err := newConnection(ctx, executable, directory, backend)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if c.Backend != "v2" {
|
||||
return Open(ctx, c.Executable, c.Directory, c.Backend)
|
||||
}
|
||||
return ensureStageService(ctx, c.Executable, c.Directory)
|
||||
}
|
||||
|
||||
// ensureStageService delegates cold-start discovery to the selected CLI's
|
||||
// authoritative `api get /api/info` transport. Only a missing registration
|
||||
// permits discovery: invalid/private-file failures must not be healed by
|
||||
// overwriting security-sensitive state. Existing registrations stay pinned.
|
||||
// Never call this from export, repair, evidence, or restart interruption.
|
||||
func ensureStageService(ctx context.Context, executable, directory string) (*Connection, error) {
|
||||
path, err := ServiceCredentialPath()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := os.Lstat(path); os.IsNotExist(err) {
|
||||
cmd := CommandContext(ctx, executable, "api", "get", "/api/info")
|
||||
cmd.Dir = directory
|
||||
stdout := &boundedOutput{limit: MaxResponse}
|
||||
stderr := &boundedOutput{limit: 8 << 10}
|
||||
cmd.Stdout, cmd.Stderr = stdout, stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
// No CLI output, auth, environment, or endpoint is copied to evidence.
|
||||
return nil, fmt.Errorf("OpenCode stage service discovery failed")
|
||||
}
|
||||
if stdout.overflow {
|
||||
return nil, fmt.Errorf("OpenCode stage service discovery response exceeds limit")
|
||||
}
|
||||
}
|
||||
// Success alone is not evidence of which service the CLI selected. Require
|
||||
// the private registration, then pin its endpoint/auth for all session work.
|
||||
c, err := serviceConnection(path, directory)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.Executable = executable
|
||||
return c, nil
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package opencode
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func coldServiceCLI(t *testing.T, script string) (string, string) {
|
||||
t.Helper()
|
||||
t.Setenv("HOME", t.TempDir())
|
||||
root := t.TempDir()
|
||||
t.Setenv(OpenCodeServiceStateEnvKey, root)
|
||||
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
||||
marker := filepath.Join(root, "discovery-called")
|
||||
t.Setenv("TEST_DISCOVERY_MARKER", marker)
|
||||
cli := filepath.Join(root, "fixture-cli")
|
||||
if err := os.WriteFile(cli, []byte("#!/bin/sh\n"+script), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return cli, marker
|
||||
}
|
||||
|
||||
func TestStageAdmissionDiscoversColdServiceThenPinsHTTPExport(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
calls := 0
|
||||
service := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
calls++
|
||||
user, password, ok := r.BasicAuth()
|
||||
if !ok || user != "opencode" || password != "execution-password" || r.URL.Query().Get("directory") != directory {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
if r.Method != "GET" || r.URL.Path != "/api/experimental/session/ses_ok/export" {
|
||||
t.Error("unexpected session work")
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"data":{"info":{"id":"ses_ok"},"messages":[{"type":"user","text":"from execution A"}]}}`))
|
||||
}))
|
||||
defer service.Close()
|
||||
chatCalls := 0
|
||||
chat := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
chatCalls++
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}))
|
||||
defer chat.Close()
|
||||
t.Setenv(EndpointEnvKey, chat.URL)
|
||||
t.Setenv(EndpointPasswordFileEnvKey, "/must-not-read-chat-credential")
|
||||
t.Setenv(EndpointUsernameEnvKey, "foreign-chat-user")
|
||||
cli, marker := coldServiceCLI(t, `
|
||||
[ "$*" = 'api get /api/info' ] || exit 20
|
||||
[ "$(pwd -P)" = "$(cd "$TEST_EXECUTION_DIRECTORY" && pwd -P)" ] || exit 21
|
||||
echo called > "$TEST_DISCOVERY_MARKER"
|
||||
umask 077
|
||||
mkdir -p "$XDG_STATE_HOME/opencode"
|
||||
cat "$TEST_SERVICE_REGISTRATION" > "$XDG_STATE_HOME/opencode/service.json"
|
||||
echo '{"data":{"version":"fixture"}}'
|
||||
`)
|
||||
registration, _ := json.Marshal(map[string]string{"url": service.URL, "password": "execution-password"})
|
||||
source := filepath.Join(t.TempDir(), "service-registration")
|
||||
if err := os.WriteFile(source, registration, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("TEST_SERVICE_REGISTRATION", source)
|
||||
t.Setenv("TEST_EXECUTION_DIRECTORY", directory)
|
||||
stage, err := OpenStage(context.Background(), cli, directory, "v2")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(marker); err != nil {
|
||||
t.Fatal("admission never allowed the CLI to discover the cold service")
|
||||
}
|
||||
if stage.URL != service.URL || stage.Directory != directory || stage.password != "execution-password" {
|
||||
t.Fatal("execution context did not match discovered registration")
|
||||
}
|
||||
ref, err := stage.Reference()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
recovered, err := FromReference(ref)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err := ExportSession(context.Background(), "/must-not-run-export-cli", directory, "v2", "ses_ok", recovered)
|
||||
if err != nil || len(result.Messages) != 1 || calls != 1 || chatCalls != 0 {
|
||||
t.Fatalf("discovered execution A was not pinned: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStageDiscoveryRequiresPrivateRegistrationBeforeSessionWork(t *testing.T) {
|
||||
for _, mode := range []string{"missing", "invalid", "public", "cli-failed"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
script := `
|
||||
[ "$*" = 'api get /api/info' ] || exit 20
|
||||
echo called > "$TEST_DISCOVERY_MARKER"
|
||||
echo '{"data":{}}'
|
||||
`
|
||||
if mode == "cli-failed" {
|
||||
script += "echo 'private-output' >&2\nexit 9\n"
|
||||
}
|
||||
cli, marker := coldServiceCLI(t, script)
|
||||
if mode == "invalid" || mode == "public" {
|
||||
path, _ := ServiceCredentialPath()
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body, permission := []byte(`not-json`), os.FileMode(0600)
|
||||
if mode == "public" {
|
||||
body, permission = []byte(`{"url":"http://127.0.0.1:1","password":"private-password"}`), 0644
|
||||
}
|
||||
if err := os.WriteFile(path, body, permission); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := OpenStage(context.Background(), cli, t.TempDir(), "v2"); err == nil {
|
||||
t.Fatal("admission accepted unavailable or insecure service context")
|
||||
}
|
||||
_, err := os.Stat(marker)
|
||||
if (mode == "missing" || mode == "cli-failed") != (err == nil) {
|
||||
t.Fatal("discovery was not confined to missing registration")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRetainedSessionPathsNeverDiscoverColdService(t *testing.T) {
|
||||
cli, marker := coldServiceCLI(t, `echo called > "$TEST_DISCOVERY_MARKER"; exit 99`)
|
||||
t.Setenv(EndpointEnvKey, "http://127.0.0.1:1")
|
||||
if _, err := Open(context.Background(), cli, t.TempDir(), "v2"); err == nil {
|
||||
t.Fatal("read-only open accepted missing registration")
|
||||
}
|
||||
if _, err := ExportSession(context.Background(), cli, t.TempDir(), "v2", "ses_ok"); err == nil {
|
||||
t.Fatal("export accepted missing service context")
|
||||
}
|
||||
if _, err := os.Stat(marker); !os.IsNotExist(err) {
|
||||
t.Fatal("retained session path invoked CLI discovery")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user