diff --git a/internal/codex/attachments.go b/internal/codex/attachments.go new file mode 100644 index 00000000..6d62aa12 --- /dev/null +++ b/internal/codex/attachments.go @@ -0,0 +1,90 @@ +package codex + +import ( + "bytes" + "errors" + "fmt" + "image" + _ "image/gif" + _ "image/jpeg" + _ "image/png" + "io" + "net/url" + "os" + "path/filepath" + "strings" + + "git.a-wan.net/alan/pipelines/internal/opencode" +) + +const maxPromptFileBytes = 1 << 20 + +// Checked against rust-v0.163.0's v2/UserInput.ts: localImage takes a +// filesystem path; mention is only a reference, not loaded file contents. +func promptInput(text string, files []opencode.PromptFile) ([]any, error) { + if strings.TrimSpace(text) == "" && len(files) == 0 { + return nil, errors.New("empty chat prompt") + } + if len(files) > 8 { + return nil, errors.New("at most 8 attachments are allowed") + } + input := []any{} + if text != "" { + input = append(input, textInput(text)) + } + var total int64 + for _, file := range files { + u, err := url.Parse(file.URI) + if err != nil || !file.ServerPrepared() || u.Scheme != "file" || u.Host != "" || u.User != nil || u.RawQuery != "" || u.ForceQuery || u.Fragment != "" || u.Opaque != "" || !filepath.IsAbs(u.Path) || filepath.Clean(u.Path) != u.Path { + return nil, errors.New("attachment must be an unchanged server-prepared local file URI") + } + info, err := os.Lstat(u.Path) + if err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0o077 != 0 { + return nil, errors.New("attachment must be a private regular file") + } + if info.Size() <= 0 || info.Size() > maxPromptFileBytes-total { + return nil, errors.New("attachments exceed the 1 MiB limit or are empty") + } + f, err := os.Open(u.Path) + if err != nil { + return nil, fmt.Errorf("open attachment: %w", err) + } + actual, statErr := f.Stat() + data, readErr := io.ReadAll(io.LimitReader(f, maxPromptFileBytes-total+1)) + closeErr := f.Close() + if statErr != nil || !os.SameFile(info, actual) || readErr != nil || closeErr != nil || int64(len(data)) != info.Size() { + return nil, errors.New("attachment changed or could not be read") + } + total += int64(len(data)) + name := file.Name + if name == "" { + name = filepath.Base(u.Path) + } + // Verify bytes rather than trusting the browser MIME or filename. Other + // formats remain available intact through the ordinary file reference. + if isPromptImage(data) { + input = append(input, textInput(fmt.Sprintf("Uploaded image: %q", name)), map[string]any{"type": "localImage", "path": u.Path}) + } else { + // Mention metadata alone need not reach the model (Codex also uses + // mentions for app/plugin targets). Include the exact server path in + // the explanatory text so normal file tools can actually inspect it. + input = append(input, textInput(fmt.Sprintf("Uploaded server-local file reference: name=%q path=%q (contents are not loaded by this reference; inspect the file with your normal tools if needed).", name, u.Path)), map[string]any{"type": "mention", "name": name, "path": u.Path}) + } + } + return input, nil +} + +func isPromptImage(data []byte) bool { + config, _, err := image.DecodeConfig(bytes.NewReader(data)) + // Avoid allocating arbitrarily large decompressed images. Unrecognized, + // corrupt or oversized images still travel intact as local file references. + if err != nil || config.Width <= 0 || config.Height <= 0 || int64(config.Width) > (16<<20)/int64(config.Height) { + return false + } + _, _, err = image.Decode(bytes.NewReader(data)) + return err == nil +} + +func textInput(text string) map[string]any { + return map[string]any{"type": "text", "text": text, "text_elements": []any{}} +} diff --git a/internal/codex/attachments_test.go b/internal/codex/attachments_test.go new file mode 100644 index 00000000..398191c0 --- /dev/null +++ b/internal/codex/attachments_test.go @@ -0,0 +1,165 @@ +package codex + +import ( + "bytes" + "context" + "encoding/json" + "image" + "image/png" + "net/url" + "os" + "path/filepath" + "strings" + "testing" + + "git.a-wan.net/alan/pipelines/internal/opencode" + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +func preparedFixture(t *testing.T, name string, data []byte) opencode.PromptFile { + t.Helper() + path := filepath.Join(t.TempDir(), name) + if err := os.WriteFile(path, data, 0o600); err != nil { + t.Fatal(err) + } + return opencode.NewPreparedPromptFile((&url.URL{Scheme: "file", Path: path}).String(), name) +} + +func TestAttachmentTurnWire(t *testing.T) { + var imageBytes bytes.Buffer + if err := png.Encode(&imageBytes, image.NewRGBA(image.Rect(0, 0, 1, 1))); err != nil { + t.Fatal(err) + } + files := []opencode.PromptFile{ + preparedFixture(t, "actual.png", imageBytes.Bytes()), + preparedFixture(t, "ordinary.png", []byte("not an image\x00retain these bytes")), + } + chat := modelTestChat(t, func(method string, raw json.RawMessage) any { + if method != "turn/start" { + t.Errorf("unexpected method %s", method) + } + var params struct { + Thread string `json:"threadId"` + Model string `json:"model"` + ClientID string `json:"clientUserMessageId"` + Input []struct { + Type, Text, Name, Path string + Elements []any `json:"text_elements"` + } `json:"input"` + } + if err := json.Unmarshal(raw, ¶ms); err != nil { + t.Error(err) + } + if params.Thread != fixtureThread || params.Model != "fixture-model" || params.ClientID != "msg_files" { + t.Errorf("params = %s", raw) + } + if len(params.Input) != 5 { + t.Errorf("inputs = %s", raw) + return map[string]any{} + } + if params.Input[2].Type != "localImage" || params.Input[4].Type != "mention" || params.Input[4].Name != "ordinary.png" { + t.Errorf("inputs = %s", raw) + } + if !strings.Contains(params.Input[3].Text, "contents are not loaded") { + t.Errorf("missing file semantics: %s", raw) + } + if !strings.Contains(params.Input[3].Text, params.Input[4].Path) || !strings.Contains(params.Input[3].Text, "ordinary.png") { + t.Errorf("file reference is not visible in model text: %s", raw) + } + for _, index := range []int{2, 4} { + if !filepath.IsAbs(params.Input[index].Path) || strings.HasPrefix(params.Input[index].Path, "file:") { + t.Errorf("not a local path: %s", raw) + } + } + if params.Input[0].Elements == nil { + t.Errorf("missing schema text_elements: %s", raw) + } + return map[string]any{} + }) + if err := chat.PromptWithMessageIDModelFiles(context.Background(), fixtureThread, "inspect", "msg_files", "fixture-model", files); err != nil { + t.Fatal(err) + } + u, _ := url.Parse(files[1].URI) + data, err := os.ReadFile(u.Path) + if err != nil || string(data) != "not an image\x00retain these bytes" { + t.Fatal("uploaded bytes changed", err) + } +} + +func TestAttachmentInputRejectsUnsafeReferences(t *testing.T) { + good := preparedFixture(t, "file.txt", []byte("contents")) + for _, uri := range []string{"", "https://example.com/file", "file://remote/etc/passwd", "file:relative", "file:///etc/passwd?x=1", "file:///etc/passwd#x", "file:///a/../etc/passwd"} { + if _, err := promptInput("hello", []opencode.PromptFile{opencode.NewPreparedPromptFile(uri, "file")}); err == nil { + t.Errorf("accepted %q", uri) + } + } + if _, err := promptInput("hello", []opencode.PromptFile{{URI: good.URI, Name: good.Name}}); err == nil { + t.Fatal("accepted unprepared URI") + } + mutated := good + mutated.URI = "file:///etc/passwd" + if _, err := promptInput("hello", []opencode.PromptFile{mutated}); err == nil { + t.Fatal("accepted mutated URI") + } + oversize := preparedFixture(t, "large", make([]byte, maxPromptFileBytes+1)) + if _, err := promptInput("hello", []opencode.PromptFile{oversize}); err == nil { + t.Fatal("accepted oversize file") + } + u, _ := url.Parse(good.URI) + link := filepath.Join(t.TempDir(), "link") + if err := os.Symlink(u.Path, link); err != nil { + t.Fatal(err) + } + if _, err := promptInput("hello", []opencode.PromptFile{opencode.NewPreparedPromptFile((&url.URL{Scheme: "file", Path: link}).String(), "link")}); err == nil { + t.Fatal("accepted symlink") + } + if _, err := promptInput("", []opencode.PromptFile{good}); err != nil { + t.Fatal("file-only prompt rejected", err) + } + if _, err := promptInput("", nil); err == nil { + t.Fatal("empty prompt accepted") + } + if _, err := promptInput("hello", []opencode.PromptFile{good, preparedFixture(t, "combined", make([]byte, maxPromptFileBytes))}); err == nil { + t.Fatal("combined size not bounded") + } +} + +func TestUserAttachmentHistory(t *testing.T) { + i := item{Type: "userMessage", ID: "native", ClientID: "client", Content: json.RawMessage(`[{"type":"text","text":"inspect"},{"type":"localImage","path":"/private/image.png"},{"type":"mention","name":"report.txt","path":"/private/report.txt"}]`)} + msg, ok := i.chatMessage(fixtureThread) + if !ok || msg.ID != "client" || len(msg.Parts) != 3 || msg.Parts[1].Type != protocol.ChatPartFile || msg.Parts[2].Name != "report.txt" || msg.Parts[2].URI != "file:///private/report.txt" { + t.Fatalf("history = %+v", msg) + } +} + +func TestAttachmentAndPlainPromptWithoutModel(t *testing.T) { + file := preparedFixture(t, "file.txt", []byte("hello")) + calls := 0 + chat := modelTestChat(t, func(method string, raw json.RawMessage) any { + var params map[string]json.RawMessage + if err := json.Unmarshal(raw, ¶ms); err != nil { + t.Error(err) + } + if method != "turn/start" || params["model"] != nil { + t.Errorf("unexpected operation/override: %s %s", method, raw) + } + var input []map[string]any + if err := json.Unmarshal(params["input"], &input); err != nil { + t.Error(err) + } + calls++ + if calls == 1 && (len(input) != 1 || input[0]["text"] != "plain prompt") { + t.Errorf("plain prompt changed: %s", raw) + } + if calls == 2 && (len(input) != 2 || input[1]["type"] != "mention") { + t.Errorf("file-only prompt changed: %s", raw) + } + return map[string]any{} + }) + if err := chat.Prompt(context.Background(), fixtureThread, "plain prompt"); err != nil { + t.Fatal(err) + } + if err := chat.PromptWithMessageIDFiles(context.Background(), fixtureThread, "", "msg", []opencode.PromptFile{file}); err != nil { + t.Fatal(err) + } +} diff --git a/internal/codex/chat.go b/internal/codex/chat.go index 80d5e5d0..a056b0b3 100644 --- a/internal/codex/chat.go +++ b/internal/codex/chat.go @@ -14,6 +14,7 @@ import ( "fmt" "net" "net/http" + "net/url" "os" "path/filepath" "strconv" @@ -21,6 +22,7 @@ import ( "sync" "time" + "git.a-wan.net/alan/pipelines/internal/opencode" "git.a-wan.net/alan/pipelines/internal/protocol" "github.com/coder/websocket" ) @@ -332,12 +334,24 @@ func (c *Chat) PromptWithMessageID(ctx context.Context, id, text, messageID stri // PromptWithMessageIDModel applies a persisted per-chat choice at the explicit // next-turn boundary. It does not resume a thread or edit global configuration. func (c *Chat) PromptWithMessageIDModel(ctx context.Context, id, text, messageID, model string) error { - if strings.TrimSpace(text) == "" { - return errors.New("empty chat prompt") + return c.PromptWithMessageIDModelFiles(ctx, id, text, messageID, model, nil) +} + +// PromptWithMessageIDFiles submits server-prepared attachments without changing +// provider policy or configuration. +func (c *Chat) PromptWithMessageIDFiles(ctx context.Context, id, text, messageID string, files []opencode.PromptFile) error { + return c.PromptWithMessageIDModelFiles(ctx, id, text, messageID, "", files) +} + +// PromptWithMessageIDModelFiles keeps attachment and model overrides on one turn. +func (c *Chat) PromptWithMessageIDModelFiles(ctx context.Context, id, text, messageID, model string, files []opencode.PromptFile) error { + input, err := promptInput(text, files) + if err != nil { + return err } params := map[string]any{ "threadId": id, - "input": []any{map[string]any{"type": "text", "text": text}}, + "input": input, } if messageID != "" { params["clientUserMessageId"] = messageID @@ -345,7 +359,7 @@ func (c *Chat) PromptWithMessageIDModel(ctx context.Context, id, text, messageID if model != "" { params["model"] = model } - _, err := c.call(ctx, "turn/start", params) + _, err = c.call(ctx, "turn/start", params) return err } @@ -799,11 +813,22 @@ func (i item) chatMessage(sessionID string) (protocol.ChatMessage, bool) { var content []struct { Type string `json:"type"` Text string `json:"text"` + Name string `json:"name"` + Path string `json:"path"` + URL string `json:"url"` } _ = json.Unmarshal(i.Content, &content) for _, part := range content { if part.Type == "text" && part.Text != "" { msg.Parts = append(msg.Parts, protocol.ChatPart{Type: protocol.ChatPartText, Text: part.Text}) + } else if (part.Type == "mention" || part.Type == "localImage") && filepath.IsAbs(part.Path) { + name := part.Name + if name == "" { + name = filepath.Base(part.Path) + } + msg.Parts = append(msg.Parts, protocol.ChatPart{Type: protocol.ChatPartFile, Name: name, URI: (&url.URL{Scheme: "file", Path: part.Path}).String()}) + } else if part.Type == "image" && part.URL != "" { + msg.Parts = append(msg.Parts, protocol.ChatPart{Type: protocol.ChatPartFile, Name: "image", URI: part.URL}) } } case "agentMessage": diff --git a/internal/codex/testdata/README.md b/internal/codex/testdata/README.md index 43863c8b..092becb4 100644 --- a/internal/codex/testdata/README.md +++ b/internal/codex/testdata/README.md @@ -24,3 +24,21 @@ prompt. It never calls `thread/resume`, which can awaken a queued goal, nor any configuration-writing API. The acknowledgement means "saved for the next turn," not "remote settings changed." The override is supplied again after a Pipelines restart; an already-running turn is not reconfigured. + +## Attachment inputs + +Attachment wire tests also use the installed 0.163.0 tag's +`schema/typescript/v2/UserInput.ts` and `TurnStartParams.ts`. Verified PNG, +JPEG and GIF bytes use `localImage` with an absolute local path. Other files +(including unsupported/corrupt image formats) retain their bytes and use a +`mention` name/path plus an explicit statement that the reference has **not** +loaded their contents: the agent must inspect them with its normal tools. +This does not bypass the thread's sandbox or grant filesystem permissions. +Text inputs include the schema's `text_elements` array. + +Only unchanged server-prepared file URIs are admitted; private regular files, +eight-file/1 MiB combined bounds, and image decode allocation bounds are checked +before `turn/start`. Files retain the existing attachment-store retention rules. +History keeps local file/image parts; native image parts use the stored basename +while a companion text input retains the original display name. +All wire tests use fake JSON-RPC servers, never the production control socket. diff --git a/internal/opencode/chat.go b/internal/opencode/chat.go index 1c96d1f8..d99fc0bb 100644 --- a/internal/opencode/chat.go +++ b/internal/opencode/chat.go @@ -89,10 +89,21 @@ func (c *Chat) Prompt(ctx context.Context, id, text string) error { // PromptFile is one local file handed to OpenCode with a prompt. URI is the // file:// URL the server wrote; Name is the browser's display name. type PromptFile struct { - URI string - Name string + URI string + Name string + preparedURI string } +// NewPreparedPromptFile marks a file written by the server attachment store. +// Only trusted server-side upload preparation should call this constructor; +// browser-provided paths must never be marked as prepared. +func NewPreparedPromptFile(uri, name string) PromptFile { + return PromptFile{URI: uri, Name: name, preparedURI: uri} +} + +// ServerPrepared reports whether URI is the unchanged server-prepared reference. +func (f PromptFile) ServerPrepared() bool { return f.URI != "" && f.URI == f.preparedURI } + // PromptWithMessageID forwards the caller's canonical message id to OpenCode. // OpenCode persists this id in history, which is the only safe bridge between // the live user echo and an authoritative history page. diff --git a/internal/server/chat_attachments.go b/internal/server/chat_attachments.go index 89a27039..fadb22a5 100644 --- a/internal/server/chat_attachments.go +++ b/internal/server/chat_attachments.go @@ -177,7 +177,7 @@ func prepareChatAttachments(sessionID string, attachments []protocol.ChatAttachm return nil, nil, fmt.Errorf("close attachment %q: %w", name, err) } uri := (&url.URL{Scheme: "file", Path: path}).String() - files = append(files, opencode.PromptFile{URI: uri, Name: name}) + files = append(files, opencode.NewPreparedPromptFile(uri, name)) parts = append(parts, protocol.ChatPart{Type: protocol.ChatPartFile, Name: name, Mime: mime, URI: uri}) } return files, parts, nil diff --git a/internal/server/chat_codex_models_test.go b/internal/server/chat_codex_models_test.go index 55369e71..4d83e021 100644 --- a/internal/server/chat_codex_models_test.go +++ b/internal/server/chat_codex_models_test.go @@ -5,6 +5,8 @@ import ( "path/filepath" "testing" + "git.a-wan.net/alan/pipelines/internal/codex" + "git.a-wan.net/alan/pipelines/internal/opencode" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" ) @@ -13,6 +15,19 @@ type selectedModelChat struct { *fakeChat model, nativeID string overrides int + files []opencode.PromptFile +} + +func TestCodexManagerAttachmentsRejectUnpreparedURI(t *testing.T) { + // Invalid input must fail before any JSON-RPC/socket access, including when + // no model override is saved. + manager := &chatManager{codex: &codex.Chat{}, cache: map[string]string{"thread": "codex"}} + if err := manager.CheckAttachments(context.Background(), "thread"); err != nil { + t.Fatal(err) + } + if err := manager.PromptWithMessageIDFiles(context.Background(), "thread", "inspect", "msg", []opencode.PromptFile{{URI: "file:///etc/passwd"}}); err == nil { + t.Fatal("unprepared URI accepted") + } } func (c *selectedModelChat) PromptWithMessageIDModel(_ context.Context, _, _, nativeID, model string) error { @@ -21,7 +36,13 @@ func (c *selectedModelChat) PromptWithMessageIDModel(_ context.Context, _, _, na return nil } +func (c *selectedModelChat) PromptWithMessageIDModelFiles(ctx context.Context, id, text, nativeID, model string, files []opencode.PromptFile) error { + c.files = append([]opencode.PromptFile(nil), files...) + return c.PromptWithMessageIDModel(ctx, id, text, nativeID, model) +} + func TestCodexModelSelectionSurvivesServerRestart(t *testing.T) { + t.Setenv("PIPELINES_CHAT_ATTACHMENT_DIR", filepath.Join(t.TempDir(), "attachments")) ctx := context.Background() path := filepath.Join(t.TempDir(), "chat.db") store, err := pipeline.Open(path) @@ -85,11 +106,25 @@ func TestCodexModelSelectionSurvivesServerRestart(t *testing.T) { if fake.overrides != 3 || fake.nativeID != "msg_empty" { t.Fatal("empty attachments bypassed saved choice") } + files, _, err := prepareChatAttachments(id, []protocol.ChatAttachment{{Name: "report.txt", Data: "aGVsbG8="}, {Name: "image.png", Mime: "image/png", Data: "aW1hZ2UgYnl0ZXM="}}) + if err != nil { + t.Fatal(err) + } + defer releaseChatAttachments(files) + if err := manager.PromptWithMessageIDFiles(ctx, id, "with files", "msg_files", files); err != nil { + t.Fatal(err) + } + if fake.overrides != 4 || fake.model != "fixture-model" || fake.nativeID != "msg_files" || len(fake.files) != 2 || !fake.files[0].ServerPrepared() { + t.Fatalf("attachment model override lost: %+v", fake) + } fake.sessionRecord.ModelProvider = "different-provider" if err := manager.Prompt(ctx, id, "must not send"); err == nil { t.Fatal("saved model sent to a different provider") } - if fake.overrides != 3 { + if err := manager.PromptWithMessageIDFiles(ctx, id, "must not send files", "msg_bad", files); err == nil { + t.Fatal("saved model with files sent to a different provider") + } + if fake.overrides != 4 { t.Fatal("mismatched provider started a turn") } // A failed durable save must never acknowledge the next-turn selection. diff --git a/internal/server/chat_manager.go b/internal/server/chat_manager.go index 135460ae..2fc02de6 100644 --- a/internal/server/chat_manager.go +++ b/internal/server/chat_manager.go @@ -128,18 +128,11 @@ func (m *chatManager) PromptWithMessageID(ctx context.Context, sessionID, text, return err } if m.backendFor(ctx, sessionID) == codex.Backend { - provider, model, err := m.chatModelSelection(ctx, sessionID) + model, err := m.codexPromptModel(ctx, adapter, sessionID) if err != nil { return err } if model != "" { - session, found, err := adapter.Session(ctx, sessionID) - if err != nil { - return err - } - if !found || session.ModelProvider != provider { - return errors.New("saved model provider no longer matches the Codex thread; select a model again") - } withModel, ok := adapter.(interface { PromptWithMessageIDModel(context.Context, string, string, string, string) error }) @@ -155,6 +148,21 @@ func (m *chatManager) PromptWithMessageID(ctx context.Context, sessionID, text, return adapter.Prompt(ctx, sessionID, text) } +func (m *chatManager) codexPromptModel(ctx context.Context, adapter chatBroker, sessionID string) (string, error) { + provider, model, err := m.chatModelSelection(ctx, sessionID) + if err != nil || model == "" { + return model, err + } + session, found, err := adapter.Session(ctx, sessionID) + if err != nil { + return "", err + } + if !found || session.ModelProvider != provider { + return "", errors.New("saved model provider no longer matches the Codex thread; select a model again") + } + return model, nil +} + // PromptWithMessageIDFiles forwards a prompt with the private local files the // server wrote for browser attachments. Never silently discard files. func (m *chatManager) PromptWithMessageIDFiles(ctx context.Context, sessionID, text, messageID string, files []opencode.PromptFile) error { @@ -165,6 +173,21 @@ func (m *chatManager) PromptWithMessageIDFiles(ctx context.Context, sessionID, t if err != nil { return err } + if m.backendFor(ctx, sessionID) == codex.Backend { + model, err := m.codexPromptModel(ctx, adapter, sessionID) + if err != nil { + return err + } + if model != "" { + withModelFiles, ok := adapter.(interface { + PromptWithMessageIDModelFiles(context.Context, string, string, string, string, []opencode.PromptFile) error + }) + if !ok { + return errors.New("this Codex adapter cannot apply the saved next-turn model with attachments") + } + return withModelFiles.PromptWithMessageIDModelFiles(ctx, sessionID, text, messageID, model, files) + } + } if withFiles, ok := adapter.(chatPromptWithFiles); ok { return withFiles.PromptWithMessageIDFiles(ctx, sessionID, text, messageID, files) }