Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

Fix path traversal cleanup and nested cancel reason in Cline event mapper

Cursor Agentfeaa1f537c0f
diff --git a/cli/beacon-hooks/cmd/cline_event.go b/cli/beacon-hooks/cmd/cline_event.go
index fe6ec198..1d134be0 100644
--- a/cli/beacon-hooks/cmd/cline_event.go
+++ b/cli/beacon-hooks/cmd/cline_event.go
@@ -135,6 +135,22 @@ func clineEndpointEvents(input map[string]interface{}, sessionID string) []norma
fields["gen_ai"] = mergeNested(fields["gen_ai"], map[string]interface{}{"usage": usage})
}
reason := getFirstStr(input, "completionStatus", "completion_status", "reason")
+ if reason == "" {
+ for _, key := range []string{"taskCancel", "task_cancel", "taskMetadata", "task_metadata", "result", "metadata"} {
+ if nested := firstMap(input, key); nested != nil {
+ reason = getFirstStr(nested, "completionStatus", "completion_status", "reason")
+ if reason != "" {
+ break
+ }
+ if inner := firstMap(nested, "taskMetadata", "task_metadata", "metadata"); inner != nil {
+ reason = getFirstStr(inner, "completionStatus", "completion_status", "reason")
+ if reason != "" {
+ break
+ }
+ }
+ }
+ }
+ }
if reason == "" {
reason = "cancelled"
}
@@ -473,10 +489,11 @@ func isRootedPath(path string) bool {
}
}
-// joinWorkspacePath joins a relative path under a root using the root's own separator.
+// joinWorkspacePath joins a relative path under a root using the root's own separator, then
+// collapses . and .. segments so the result is canonical.
//
-// filepath.Join would use the host's, which mangles the result whenever the two disagree: a POSIX
-// workspace root on a Windows host produced "\\tmp\\project\\src\\app.ts", a path that
+// filepath.Join would use the host's separator, which mangles the result whenever the two disagree:
+// a POSIX workspace root on a Windows host produced "\\tmp\\project\\src\\app.ts", a path that
// matches nothing and belongs to no filesystem. Taking the separator from the root keeps the
// recorded path in the same shape as the workspace it came from.
func joinWorkspacePath(root, rel string) string {
@@ -486,7 +503,28 @@ func joinWorkspacePath(root, rel string) string {
}
rel = strings.TrimPrefix(strings.TrimPrefix(rel, "./"), ".\\")
rel = strings.ReplaceAll(strings.ReplaceAll(rel, "\\", separator), "/", separator)
- return strings.TrimRight(root, "/\\") + separator + strings.TrimLeft(rel, "/\\")
+ joined := strings.TrimRight(root, "/\\") + separator + strings.TrimLeft(rel, "/\\")
+ return cleanPathSegments(joined, separator)
+}
+
+// cleanPathSegments collapses . and .. segments in a path without using filepath.Clean (which
+// would impose the host's separator). The separator must be "/" or "\\".
+func cleanPathSegments(path, separator string) string {
+ parts := strings.Split(path, separator)
+ var cleaned []string
+ for _, part := range parts {
+ switch part {
+ case ".":
+ continue
+ case "..":
+ if len(cleaned) > 0 && cleaned[len(cleaned)-1] != "" && cleaned[len(cleaned)-1] != ".." {
+ cleaned = cleaned[:len(cleaned)-1]
+ }
+ default:
+ cleaned = append(cleaned, part)
+ }
+ }
+ return strings.Join(cleaned, separator)
}
func clineToolPath(toolInput map[string]interface{}, root string) string {
diff --git a/cli/beacon-hooks/cmd/cline_event_test.go b/cli/beacon-hooks/cmd/cline_event_test.go
index 476794ef..50574d11 100644
--- a/cli/beacon-hooks/cmd/cline_event_test.go
+++ b/cli/beacon-hooks/cmd/cline_event_test.go
@@ -565,6 +565,10 @@ func TestClineWorkspacePathIsHostIndependent(t *testing.T) {
{name: "no root leaves the path relative", path: "src/app.ts", root: "", want: "src/app.ts"},
{name: "empty path stays empty", path: "", root: "/repo", want: ""},
{name: "quoted path is unquoted first", path: `"src/app.ts"`, root: "/repo", want: "/repo/src/app.ts"},
+ {name: "dotdot segments are collapsed", path: "../../.ssh/id_rsa", root: "/home/u/proj", want: "/home/.ssh/id_rsa"},
+ {name: "single dot segments are collapsed", path: "./src/./app.ts", root: "/repo", want: "/repo/src/app.ts"},
+ {name: "mixed traversal is collapsed", path: "src/../lib/app.ts", root: "/repo", want: "/repo/lib/app.ts"},
+ {name: "windows dotdot is collapsed", path: `..\..\secret.txt`, root: `C:\Users\dev\project`, want: `C:\Users\secret.txt`},
} {
t.Run(tc.name, func(t *testing.T) {
if got := clineWorkspacePath(tc.path, tc.root); got != tc.want {
@@ -638,6 +642,27 @@ func TestClineEventTaskCancelIsAnEndNotAnError(t *testing.T) {
}
}
+// Cline's file-based hook puts completionStatus inside taskCancel.taskMetadata. When only
+// that nested field says "abandoned", the cancel reason must reflect it rather than defaulting.
+func TestClineEventTaskCancelReadsNestedCompletionStatus(t *testing.T) {
+ logPath := clineTestLog(t)
+
+ runHookWithInput(t, runClineEvent, map[string]interface{}{
+ "hookName": "TaskCancel",
+ "taskId": "task-abandoned",
+ "taskCancel": map[string]interface{}{
+ "taskMetadata": map[string]interface{}{
+ "completionStatus": "abandoned",
+ },
+ },
+ })
+
+ event := clineEventWithAction(t, logPath, "session.ended")
+ if got := nested(t, event, "session")["cancel_reason"]; got != "abandoned" {
+ t.Errorf("session.cancel_reason = %v, want abandoned", got)
+ }
+}
+
func TestClineEventTaskErrorStaysAnError(t *testing.T) {
logPath := clineTestLog(t)