Skip to content

Agent Mesh

bridge: harden file handling against hostile task ids and odd local files

Tomas Tormo524e5f57b58c
diff --git a/cmd/sam-a2a-bridge/a2a.go b/cmd/sam-a2a-bridge/a2a.go
index 43fa1e65..e066208c 100644
--- a/cmd/sam-a2a-bridge/a2a.go
+++ b/cmd/sam-a2a-bridge/a2a.go
@@ -222,6 +222,10 @@ func fileToPart(path, nameOverride string) (*a2a.Part, error) {
if err != nil {
return nil, err
}
+ // A FIFO or device would block or misbehave in ReadFile.
+ if !info.Mode().IsRegular() {
+ return nil, fmt.Errorf("file %s is not a regular file", path)
+ }
if info.Size() > maxAttachmentBytes {
return nil, fmt.Errorf("file %s is %d bytes; attachment cap is %d", path, info.Size(), maxAttachmentBytes)
}
@@ -328,13 +332,19 @@ func saveFilePart(dir, taskID, name string, data []byte) (string, error) {
if name == "" {
name = "file"
}
- name = filepath.Base(filepath.Clean("/" + name)) // remote input names a local path: keep only a leaf
+ // Both components are remote input naming a local path: keep only leaves.
+ taskID = filepath.Base(filepath.Clean("/" + taskID))
+ name = filepath.Base(filepath.Clean("/" + name))
base := taskID + "-" + name
path := filepath.Join(dir, base)
for n := 1; ; n++ {
- if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
+ _, err := os.Stat(path)
+ if errors.Is(err, os.ErrNotExist) {
break
}
+ if err != nil {
+ return "", err
+ }
path = filepath.Join(dir, fmt.Sprintf("%s.%d", base, n))
}
if err := os.WriteFile(path, data, 0o644); err != nil {
diff --git a/cmd/sam-a2a-bridge/a2a_test.go b/cmd/sam-a2a-bridge/a2a_test.go
index ddd0a36e..461ec3df 100644
--- a/cmd/sam-a2a-bridge/a2a_test.go
+++ b/cmd/sam-a2a-bridge/a2a_test.go
@@ -372,3 +372,20 @@ func TestResultErrorIncludesTaskID(t *testing.T) {
t.Errorf("error must include task ID; got: %v", err)
}
}
+
+func TestSaveFilePartSanitizesTaskID(t *testing.T) {
+ dir := t.TempDir()
+ path, err := saveFilePart(dir, "../../evil", "a.txt", []byte("x"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if filepath.Dir(path) != dir {
+ t.Fatalf("task id escaped the download dir: %s", path)
+ }
+}
+
+func TestFileToPartRejectsNonRegular(t *testing.T) {
+ if _, err := fileToPart(t.TempDir(), ""); err == nil || !strings.Contains(err.Error(), "not a regular file") {
+ t.Fatalf("directory must be rejected, got: %v", err)
+ }
+}
diff --git a/cmd/sam-a2a-bridge/main.go b/cmd/sam-a2a-bridge/main.go
index 7cd1f21a..44486429 100644
--- a/cmd/sam-a2a-bridge/main.go
+++ b/cmd/sam-a2a-bridge/main.go
@@ -33,7 +33,8 @@ func main() {
flag.Parse()
// A user-supplied dir is used as-is (the caller creates it); only the
- // built-in default is auto-created.
+ // built-in default is auto-created. Fail fast so a missing dir surfaces
+ // at startup, not on the first file-bearing result.
dir := *downloadDir
if dir == "" {
home, err := os.UserHomeDir()
@@ -44,6 +45,8 @@ func main() {
if err := os.MkdirAll(dir, 0o755); err != nil {
log.Fatal(err)
}
+ } else if info, err := os.Stat(dir); err != nil || !info.IsDir() {
+ log.Fatalf("-download-dir %s is not a usable directory (create it first): %v", dir, err)
}
cfg := bridgeConfig{sidecarURL: *sidecarURL, token: *token, downloadDir: dir}
if err := newBridgeServer(cfg).Run(context.Background(), &mcp.StdioTransport{}); err != nil {
diff --git a/development/examples/chat-a2a/agent.py b/development/examples/chat-a2a/agent.py
index eaead9e1..95d0f9db 100644
--- a/development/examples/chat-a2a/agent.py
+++ b/development/examples/chat-a2a/agent.py
@@ -92,8 +92,9 @@ class ChatExecutor(AgentExecutor):
continue
media = part.media_type or "application/octet-stream"
if media.startswith("text/"):
+ filename = part.filename or "unnamed"
# Text attachments read best as prompt text, not opaque blobs.
- gemini_parts[0] += f"\n[attached file {part.filename}]:\n" + part.raw.decode("utf-8", "replace")
+ gemini_parts[0] += f"\n[attached file {filename}]:\n" + part.raw.decode("utf-8", "replace")
else:
gemini_parts.append(types.Part.from_bytes(data=part.raw, mime_type=media))
# A dangling ask_user call must be answered in-history; the user's