Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Downstream agents should note **agent-core** upgrades in their own changelogs.
### Security

- **execution**: validate `fluid_log_path` before `os.Stat` / `os.ReadFile` (CodeQL `go/path-injection`); only absolute paths under `/tmp/fluid/`.
- **execution**: expand `safeFluidLogPath` test coverage (valid/invalid paths, prefix attacks, `startFileLogForwarder` rejection).

## [0.1.0] - 2026-05-26

Expand Down
92 changes: 75 additions & 17 deletions execution/logpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,89 @@ package execution

import (
"path/filepath"
"strings"
"testing"
)

func TestSafeFluidLogPath(t *testing.T) {
func TestSafeFluidLogPath_valid(t *testing.T) {
runID := "550e8400-e29b-41d4-a716-446655440000"
valid := filepath.Join(fluidLogPathRoot, runID, "logs", "use-case.log")
want := filepath.Join(fluidLogPathRoot, runID, "logs", "use-case.log")

got, err := safeFluidLogPath(valid)
if err != nil {
t.Fatalf("expected valid path: %v", err)
tests := []struct {
name string
in string
}{
{"canonical", want},
{"trimmed", " " + want + " "},
{"extra_slash", filepath.Join(fluidLogPathRoot, runID, "logs", "", "use-case.log")},
{"root_child", filepath.Join(fluidLogPathRoot, "child", "log.txt")},
}
if got != filepath.Clean(valid) {
t.Fatalf("got %q want %q", got, filepath.Clean(valid))
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := safeFluidLogPath(tt.in)
if err != nil {
t.Fatalf("safeFluidLogPath: %v", err)
}
if got != filepath.Clean(strings.TrimSpace(tt.in)) {
t.Fatalf("got %q want %q", got, filepath.Clean(strings.TrimSpace(tt.in)))
}
if !strings.HasPrefix(got, fluidLogPathRoot+string(filepath.Separator)) {
t.Fatalf("result not under root: %q", got)
}
})
}
}

func TestSafeFluidLogPath_rejects(t *testing.T) {
tests := []struct {
name string
path string
}{
{"empty", ""},
{"whitespace", " "},
{"relative", "tmp/fluid/run/log"},
{"relative_with_dot", "./tmp/fluid/run/log"},
{"outside_etc", "/etc/passwd"},
{"outside_var", "/var/log/syslog"},
{"prefix_sibling", "/tmp/fluid-evil/run/log"},
{"prefix_sibling_no_slash", "/tmp/fluid2/run/log"},
{"dotdot_after_root", filepath.Join(fluidLogPathRoot, "..", "etc", "passwd")},
{"dotdot_mid", filepath.Join(fluidLogPathRoot, "run", "..", "..", "etc", "passwd")},
{"encoded_traversal", "/tmp/fluid/../etc/passwd"},
{"double_encoded", "/tmp/fluid/run/../../etc/passwd"},
{"null_byte", "/tmp/fluid/run\x00/log"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := safeFluidLogPath(tt.path)
if err == nil {
t.Fatalf("expected error for path %q", tt.path)
}
})
}
}

cases := []string{
"",
"relative/log",
"/etc/passwd",
"/tmp/fluid/../etc/passwd",
"/tmp/fluid/../../etc/passwd",
func TestSafeFluidLogPath_rejectsPathOutsideRootMessage(t *testing.T) {
_, err := safeFluidLogPath("/etc/passwd")
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), fluidLogPathRoot) {
t.Fatalf("error should mention allowed root, got: %v", err)
}
for _, p := range cases {
if _, err := safeFluidLogPath(p); err == nil {
t.Fatalf("expected error for %q", p)
}
}

func TestStartFileLogForwarder_rejectsUnsafeLogPath(t *testing.T) {
a := &Agent{
cfg: Config{
LogEventsEnabled: true,
LogVerbosity: "normal",
},
}
stop := a.startFileLogForwarder(
map[string]interface{}{"run_id": "550e8400-e29b-41d4-a716-446655440000"},
map[string]interface{}{"fluid_log_path": "/etc/passwd"},
)
stop()
// No panic and immediate no-op: unsafe path must not start the tail goroutine.
}
Loading