116 lines
3.9 KiB
Go
116 lines
3.9 KiB
Go
package opencode
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"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) {
|
|
chatCalls++
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}))
|
|
defer chat.Close()
|
|
state := t.TempDir()
|
|
t.Setenv(OpenCodeServiceStateEnvKey, state)
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
t.Setenv(EndpointEnvKey, chat.URL)
|
|
t.Setenv(EndpointUsernameEnvKey, "wrong-chat-user")
|
|
chatPassword := filepath.Join(t.TempDir(), "chat-password")
|
|
if err := os.WriteFile(chatPassword, []byte("chat-password"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv(EndpointPasswordFileEnvKey, chatPassword)
|
|
if err := os.Mkdir(filepath.Join(state, "opencode"), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := filepath.Join(state, "opencode", "service.json")
|
|
writeService := func(endpoint, password string) {
|
|
t.Helper()
|
|
data, _ := json.Marshal(map[string]string{"url": endpoint, "password": password})
|
|
if err := os.WriteFile(path, data, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
writeService(c.URL, c.password)
|
|
owned, err := Open(context.Background(), "/must-not-run-cli", "/workspace", "v2")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ref, err := owned.Reference()
|
|
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")
|
|
}
|
|
// Restart recovery remains on A, even when the chat endpoint and the state
|
|
// discovery directory point elsewhere. Only the pinned private file is read.
|
|
t.Setenv(OpenCodeServiceStateEnvKey, t.TempDir())
|
|
recovered, err := FromReference(ref)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s, err := ExportSession(context.Background(), "/must-not-run-cli", "/workspace", "v2", "ses_ok", recovered)
|
|
if err != nil || len(s.Messages) != 1 || *calls != 1 || chatCalls != 0 {
|
|
t.Fatalf("export did not stay on execution A: %v", err)
|
|
}
|
|
t.Setenv("OPENCODE_PASSWORD", "foreign-password")
|
|
t.Setenv("OPENCODE_SERVER_PASSWORD", "foreign-server-password")
|
|
cmd := exec.Command("/must-not-run-cli", "api", "get", "/api/info")
|
|
recovered.PinCLI(cmd)
|
|
if strings.Join(cmd.Args[len(cmd.Args)-2:], " ") != "--server "+c.URL {
|
|
t.Fatal("CLI discovery was not bypassed")
|
|
}
|
|
passwords := 0
|
|
for _, entry := range cmd.Env {
|
|
if strings.HasPrefix(entry, "OPENCODE_SERVER_PASSWORD=") {
|
|
t.Fatal("foreign server auth survived")
|
|
}
|
|
if strings.HasPrefix(entry, "OPENCODE_PASSWORD=") {
|
|
passwords++
|
|
if entry != "OPENCODE_PASSWORD="+c.password {
|
|
t.Fatal("CLI authentication does not match export")
|
|
}
|
|
}
|
|
}
|
|
if passwords != 1 {
|
|
t.Fatal("CLI credential missing or duplicated")
|
|
}
|
|
writeService(c.URL, "wrong-password")
|
|
wrong, err := FromReference(ref)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := ExportSession(context.Background(), "/must-not-run-cli", "/workspace", "v2", "ses_ok", wrong); err == nil {
|
|
t.Fatal("wrong execution credential accepted")
|
|
}
|
|
writeService(chat.URL, "chat-password")
|
|
if _, err := FromReference(ref); err == nil || chatCalls != 0 {
|
|
t.Fatal("changed service identity followed instead of failing closed")
|
|
}
|
|
}
|
|
|
|
func TestExecutionServiceMissingDoesNotUseChatOrStartCLI(t *testing.T) {
|
|
t.Setenv(OpenCodeServiceStateEnvKey, t.TempDir())
|
|
t.Setenv(EndpointEnvKey, "http://127.0.0.1:1")
|
|
if _, err := Open(context.Background(), "/must-not-run-cli", "/workspace", "v2"); err == nil {
|
|
t.Fatal("missing execution service accepted")
|
|
}
|
|
}
|