-
Notifications
You must be signed in to change notification settings - Fork 13
feat(runtime): reject inbound file/media parts loudly (#255 Phase 0) #527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3dcce0f
feat(runtime): reject inbound file/media parts loudly instead of sile…
initializ-mk 0e8ed5f
fix(runtime): align REST inbound body cap with the JSON-RPC transport…
initializ-mk 0c1e29b
feat(runtime): raise inbound body cap to 32 MiB on both transports fo…
initializ-mk 92c4675
fix(runtime): keep inbound body cap at 2 MiB for Phase 0 (#255 review)
initializ-mk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| package runtime | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/initializ/forge/forge-core/a2a" | ||
| "github.com/initializ/forge/forge-core/auth" | ||
| "github.com/initializ/forge/forge-core/types" | ||
| ) | ||
|
|
||
| // TestRunner_RejectsInboundFileParts is the #255 footgun regression: a client | ||
| // attaching a file (image/document) part used to get a 200 and a plausible | ||
| // answer that silently ignored the attachment, because the runtime projects | ||
| // only text/data parts into the prompt. Now every send path rejects a file | ||
| // part loudly (JSON-RPC error / HTTP 400) naming the mime type, while a | ||
| // text-only message still succeeds. | ||
| func TestRunner_RejectsInboundFileParts(t *testing.T) { | ||
| dir := t.TempDir() | ||
| cfg := &types.ForgeConfig{ | ||
| AgentID: "media-reject-test", | ||
| Version: "0.1.0", | ||
| Framework: "forge", | ||
| Entrypoint: "python main.py", | ||
| Tools: []types.ToolRef{{Name: "search"}}, | ||
| } | ||
| port, err := findFreePort() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| runner, err := NewRunner(RunnerConfig{Config: cfg, WorkDir: dir, Port: port, MockTools: true}) | ||
| if err != nil { | ||
| t.Fatalf("NewRunner: %v", err) | ||
| } | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
| go func() { _ = runner.Run(ctx) }() | ||
|
|
||
| baseURL := fmt.Sprintf("http://localhost:%d", port) | ||
| waitForServer(t, baseURL, 5*time.Second) | ||
| token, err := auth.LoadToken(dir) | ||
| if err != nil { | ||
| t.Fatalf("loading auth token: %v", err) | ||
| } | ||
|
|
||
| fileMsg := a2a.Message{ | ||
| Role: a2a.MessageRoleUser, | ||
| Parts: []a2a.Part{ | ||
| a2a.NewTextPart("describe this"), | ||
| a2a.NewFilePart(a2a.FileContent{Name: "photo.png", MimeType: "image/png", Bytes: []byte{0x89, 0x50, 0x4e, 0x47}}), | ||
| }, | ||
| } | ||
|
|
||
| t.Run("json-rpc tasks/send rejects file part", func(t *testing.T) { | ||
| rpcReq := a2a.JSONRPCRequest{ | ||
| JSONRPC: "2.0", ID: "1", Method: "tasks/send", | ||
| Params: mustMarshal(a2a.SendTaskParams{ID: "t-media-1", Message: fileMsg}), | ||
| } | ||
| body, _ := json.Marshal(rpcReq) | ||
| resp, err := authPost(baseURL+"/", token, body) | ||
| if err != nil { | ||
| t.Fatalf("send: %v", err) | ||
| } | ||
| defer func() { _ = resp.Body.Close() }() | ||
|
|
||
| var rpcResp a2a.JSONRPCResponse | ||
| if err := json.NewDecoder(resp.Body).Decode(&rpcResp); err != nil { | ||
| t.Fatalf("decode: %v", err) | ||
| } | ||
| if rpcResp.Error == nil { | ||
| t.Fatalf("expected a JSON-RPC error rejecting the file part, got result: %+v", rpcResp.Result) | ||
| } | ||
| if !strings.Contains(rpcResp.Error.Message, "image/png") { | ||
| t.Errorf("error message should name the rejected mime type; got %q", rpcResp.Error.Message) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("REST /tasks/send rejects file part with 400", func(t *testing.T) { | ||
| body, _ := json.Marshal(restBody("t-media-2", fileMsg)) | ||
| resp, err := authPost(baseURL+"/tasks/send", token, body) | ||
| if err != nil { | ||
| t.Fatalf("send: %v", err) | ||
| } | ||
| defer func() { _ = resp.Body.Close() }() | ||
| if resp.StatusCode != http.StatusBadRequest { | ||
| t.Fatalf("status = %d, want 400", resp.StatusCode) | ||
| } | ||
| var errBody map[string]string | ||
| _ = json.NewDecoder(resp.Body).Decode(&errBody) | ||
| if !strings.Contains(errBody["error"], "image/png") { | ||
| t.Errorf("400 body should name the rejected mime type; got %q", errBody["error"]) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("text-only message still succeeds", func(t *testing.T) { | ||
| textMsg := a2a.Message{Role: a2a.MessageRoleUser, Parts: []a2a.Part{a2a.NewTextPart("hello")}} | ||
| body, _ := json.Marshal(restBody("t-text-ok", textMsg)) | ||
| resp, err := authPost(baseURL+"/tasks/send", token, body) | ||
| if err != nil { | ||
| t.Fatalf("send: %v", err) | ||
| } | ||
| defer func() { _ = resp.Body.Close() }() | ||
| if resp.StatusCode != http.StatusOK { | ||
| t.Fatalf("text-only status = %d, want 200 (media gate must not affect text)", resp.StatusCode) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // restBody builds the REST {"task":{"id","message"}} envelope. | ||
| func restBody(id string, msg a2a.Message) map[string]any { | ||
| return map[string]any{"task": map[string]any{"id": id, "message": msg}} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package a2a | ||
|
|
||
| import "testing" | ||
|
|
||
| // TestMessageFileParts verifies FileParts surfaces exactly the file parts (with | ||
| // index + mime + name) and ignores text/data parts — the basis for the #255 | ||
| // ingest gate that rejects media the runtime can't forward to the model. | ||
| func TestMessageFileParts(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| parts []Part | ||
| want []MediaPartInfo | ||
| }{ | ||
| { | ||
| name: "text only", | ||
| parts: []Part{NewTextPart("hi")}, | ||
| want: nil, | ||
| }, | ||
| { | ||
| name: "data only", | ||
| parts: []Part{NewDataPart(map[string]any{"k": "v"})}, | ||
| want: nil, | ||
| }, | ||
| { | ||
| name: "single file part", | ||
| parts: []Part{ | ||
| NewTextPart("look at this"), | ||
| NewFilePart(FileContent{Name: "photo.png", MimeType: "image/png", Bytes: []byte{1, 2, 3}}), | ||
| }, | ||
| want: []MediaPartInfo{{Index: 1, MimeType: "image/png", Name: "photo.png"}}, | ||
| }, | ||
| { | ||
| name: "multiple file parts keep their indices", | ||
| parts: []Part{ | ||
| NewFilePart(FileContent{Name: "a.pdf", MimeType: "application/pdf"}), | ||
| NewTextPart("between"), | ||
| NewFilePart(FileContent{Name: "b.mp4", MimeType: "video/mp4"}), | ||
| }, | ||
| want: []MediaPartInfo{ | ||
| {Index: 0, MimeType: "application/pdf", Name: "a.pdf"}, | ||
| {Index: 2, MimeType: "video/mp4", Name: "b.mp4"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "file part with nil FileContent still reported", | ||
| parts: []Part{{Kind: PartKindFile}}, | ||
| want: []MediaPartInfo{{Index: 0}}, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got := Message{Role: MessageRoleUser, Parts: tt.parts}.FileParts() | ||
| if len(got) != len(tt.want) { | ||
| t.Fatalf("FileParts() = %+v, want %+v", got, tt.want) | ||
| } | ||
| for i := range got { | ||
| if got[i] != tt.want[i] { | ||
| t.Errorf("FileParts()[%d] = %+v, want %+v", i, got[i], tt.want[i]) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LOW (future-proofing): per-handler gating, no single choke point. This gate is invoked from 4 handlers; current coverage is complete (verified: 2
executeTaskcallers + 2ExecuteStreamhandlers, all gated). But there's no one shared point below them in the runtime — the non-streaming paths funnel throughexecuteTask, the streaming ones throughExecuteStream— so a future 5th ingest path (a new endpoint, an in-process channel→executor call, a batch/replay) would silently reintroduce the footgun unless it remembers this gate. Consider a structural backstop at the executor boundary (forge-coreExecute/ExecuteStream) in a later #255 phase so media rejection is guaranteed regardless of caller. Not needed for Phase 0 — just flagging the maintenance risk of a 4-call-site invariant.