| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "path/filepath" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | // Lineage walks build paths from session identifiers — the caller-provided |
| 10 | // parent id and every parent id read back from branch metadata. All of them |
| 11 | // must be validated as bare filename stems so a "../"-shaped id can never |
| 12 | // escape the session directory (#5551). |
| 13 | func TestSessionLineageRejectsTraversalIdentifiers(t *testing.T) { |
| 14 | sessionDir := t.TempDir() |
| 15 | store := NewSubagentStore(filepath.Join(sessionDir, "subagents")) |
| 16 | |
| 17 | for _, id := range []string{"../evil", "..", ".", "nested/evil", "/abs"} { |
| 18 | if _, err := store.sessionAncestors(id); err == nil || !strings.Contains(err.Error(), "invalid session identifier") { |
| 19 | t.Fatalf("sessionAncestors(%q) error = %v, want invalid-identifier rejection", id, err) |
| 20 | } |
| 21 | if _, err := store.isAncestorSession("root", id); err == nil || !strings.Contains(err.Error(), "invalid session identifier") { |
| 22 | t.Fatalf("isAncestorSession(root, %q) error = %v, want invalid-identifier rejection", id, err) |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func TestSessionLineageRejectsTraversalParentFromMetadata(t *testing.T) { |
| 28 | sessionDir := t.TempDir() |
| 29 | store := NewSubagentStore(filepath.Join(sessionDir, "subagents")) |
| 30 | |
| 31 | // A well-formed child whose stored metadata declares a traversal parent: |
| 32 | // the walk must stop with a validation error instead of joining the path. |
| 33 | saveTestBranchMeta(t, sessionDir, "child", "../evil") |
| 34 | if _, err := store.sessionAncestors("child"); err == nil || !strings.Contains(err.Error(), "invalid session identifier") { |
| 35 | t.Fatalf("sessionAncestors(child) error = %v, want invalid-identifier rejection for stored parent", err) |
| 36 | } |
| 37 | } |
| 38 |