Skip to content

Commit 22d8331

Browse files
author
Yash
committed
fix(tui): prevent path type error crash when plugins return objects
Fixes anomalyco#12589 - adds coerceFsPath utility that safely handles various path-like input shapes (objects, URLs, arrays) before calling Node.js path functions.
1 parent 4abf804 commit 22d8331

4 files changed

Lines changed: 235 additions & 8 deletions

File tree

‎packages/opencode/src/cli/cmd/tui/routes/session/index.tsx‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from "solid-js"
1414
import { Dynamic } from "solid-js/web"
1515
import path from "path"
16+
import { coerceFsPath } from "@/util/coerce-path"
1617
import { useRoute, useRouteData } from "@tui/context/route"
1718
import { useSync } from "@tui/context/sync"
1819
import { SplitBorder } from "@tui/component/border"
@@ -2100,12 +2101,13 @@ function Skill(props: ToolProps<typeof SkillTool>) {
21002101
)
21012102
}
21022103

2103-
function normalizePath(input?: string) {
2104-
if (!input) return ""
2105-
if (path.isAbsolute(input)) {
2106-
return path.relative(process.cwd(), input) || "."
2104+
function normalizePath(input?: unknown): string {
2105+
const p = coerceFsPath(input, "session route normalizePath")
2106+
if (!p) return ""
2107+
if (path.isAbsolute(p)) {
2108+
return path.relative(process.cwd(), p) || "."
21072109
}
2108-
return input
2110+
return p
21092111
}
21102112

21112113
function input(input: Record<string, any>, omit?: string[]): string {

‎packages/opencode/src/cli/cmd/tui/routes/session/permission.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ import { Keybind } from "@/util/keybind"
1515
import { Locale } from "@/util/locale"
1616
import { Global } from "@/global"
1717
import { useDialog } from "../../ui/dialog"
18+
import { coerceFsPath } from "@/util/coerce-path"
1819

1920
type PermissionStage = "permission" | "always" | "reject"
2021

21-
function normalizePath(input?: string) {
22-
if (!input) return ""
22+
function normalizePath(input?: unknown): string {
23+
const p = coerceFsPath(input, "permission route normalizePath")
24+
if (!p) return ""
2325

2426
const cwd = process.cwd()
2527
const home = Global.Path.home
26-
const absolute = path.isAbsolute(input) ? input : path.resolve(cwd, input)
28+
const absolute = path.isAbsolute(p) ? p : path.resolve(cwd, p)
2729
const relative = path.relative(cwd, absolute)
2830

2931
if (!relative) return "."
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import path from "path"
2+
3+
/**
4+
* Coerces an unknown value to a filesystem path string.
5+
*
6+
* Accepts:
7+
* - string: returned as-is
8+
* - undefined/null: returns ""
9+
* - URL instance: returns pathname
10+
* - Object with path/cwd/value/filePath property that is a string
11+
* - Array of strings: joined with platform path separator
12+
*
13+
* Throws for any other type with a descriptive error.
14+
*
15+
* @param input - The value to coerce
16+
* @param context - Optional context for error messages (e.g., "session route normalizePath")
17+
* @returns A string path, or empty string for null/undefined
18+
*/
19+
export function coerceFsPath(input: unknown, context?: string): string {
20+
// Handle null/undefined
21+
if (input == null) {
22+
return ""
23+
}
24+
25+
// Handle string (most common case)
26+
if (typeof input === "string") {
27+
return input
28+
}
29+
30+
// Handle URL instance
31+
if (input instanceof URL) {
32+
return input.pathname
33+
}
34+
35+
// Handle arrays (join string segments)
36+
if (Array.isArray(input)) {
37+
if (input.length === 0) {
38+
return ""
39+
}
40+
// Validate all elements are strings
41+
for (let i = 0; i < input.length; i++) {
42+
if (typeof input[i] !== "string") {
43+
throw new TypeError(
44+
`coerceFsPath: array element at index ${i} is not a string (got ${typeof input[i]})${context ? ` in ${context}` : ""}`,
45+
)
46+
}
47+
}
48+
return path.join(...(input as string[]))
49+
}
50+
51+
// Handle objects with known path-like properties
52+
if (typeof input === "object") {
53+
const obj = input as Record<string, unknown>
54+
55+
// Check for common path-like properties in order of priority
56+
const pathKeys = ["path", "filePath", "cwd", "value"] as const
57+
for (const key of pathKeys) {
58+
if (key in obj) {
59+
const value = obj[key]
60+
if (typeof value === "string") {
61+
return value
62+
}
63+
if (value == null) {
64+
return ""
65+
}
66+
// Property exists but is not a string or null
67+
throw new TypeError(
68+
`coerceFsPath: object.${key} is not a string (got ${typeof value})${context ? ` in ${context}` : ""}`,
69+
)
70+
}
71+
}
72+
73+
// Object has no recognized path properties
74+
const keys = Object.keys(obj).slice(0, 5).join(", ")
75+
throw new TypeError(
76+
`coerceFsPath: object has no recognized path property (keys: ${keys || "none"})${context ? ` in ${context}` : ""}`,
77+
)
78+
}
79+
80+
// Reject all other types
81+
throw new TypeError(
82+
`coerceFsPath: expected string or path-like object, got ${typeof input}${context ? ` in ${context}` : ""}`,
83+
)
84+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import { describe, expect, test } from "bun:test"
2+
import { coerceFsPath } from "../../src/util/coerce-path"
3+
import path from "path"
4+
5+
describe("coerceFsPath", () => {
6+
describe("accepts valid inputs", () => {
7+
test("returns empty string for undefined", () => {
8+
expect(coerceFsPath(undefined)).toBe("")
9+
})
10+
11+
test("returns empty string for null", () => {
12+
expect(coerceFsPath(null)).toBe("")
13+
})
14+
15+
test("returns string as-is", () => {
16+
expect(coerceFsPath("/path/to/file")).toBe("/path/to/file")
17+
expect(coerceFsPath("relative/path")).toBe("relative/path")
18+
expect(coerceFsPath("")).toBe("")
19+
})
20+
21+
test("extracts pathname from URL instance", () => {
22+
const url = new URL("file:///path/to/file.txt")
23+
expect(coerceFsPath(url)).toBe("/path/to/file.txt")
24+
})
25+
26+
test("extracts path from object with path property", () => {
27+
expect(coerceFsPath({ path: "/some/path" })).toBe("/some/path")
28+
})
29+
30+
test("extracts filePath from object with filePath property", () => {
31+
expect(coerceFsPath({ filePath: "/some/file.ts" })).toBe("/some/file.ts")
32+
})
33+
34+
test("extracts cwd from object with cwd property", () => {
35+
expect(coerceFsPath({ cwd: "/working/dir" })).toBe("/working/dir")
36+
})
37+
38+
test("extracts value from object with value property", () => {
39+
expect(coerceFsPath({ value: "/value/path" })).toBe("/value/path")
40+
})
41+
42+
test("returns empty string for object with null path property", () => {
43+
expect(coerceFsPath({ path: null })).toBe("")
44+
expect(coerceFsPath({ filePath: undefined })).toBe("")
45+
})
46+
47+
test("prioritizes path over other properties", () => {
48+
expect(coerceFsPath({ path: "/a", cwd: "/b", value: "/c" })).toBe("/a")
49+
})
50+
51+
test("falls back to filePath when path is missing", () => {
52+
expect(coerceFsPath({ filePath: "/a", cwd: "/b" })).toBe("/a")
53+
})
54+
55+
test("joins string array with path separator", () => {
56+
const result = coerceFsPath(["path", "to", "file"])
57+
expect(result).toBe(path.join("path", "to", "file"))
58+
})
59+
60+
test("returns empty string for empty array", () => {
61+
expect(coerceFsPath([])).toBe("")
62+
})
63+
})
64+
65+
describe("rejects invalid inputs", () => {
66+
test("throws for number", () => {
67+
expect(() => coerceFsPath(123)).toThrow(TypeError)
68+
expect(() => coerceFsPath(123)).toThrow("expected string or path-like object, got number")
69+
})
70+
71+
test("throws for boolean", () => {
72+
expect(() => coerceFsPath(true)).toThrow(TypeError)
73+
expect(() => coerceFsPath(true)).toThrow("expected string or path-like object, got boolean")
74+
})
75+
76+
test("throws for function", () => {
77+
expect(() => coerceFsPath(() => {})).toThrow(TypeError)
78+
expect(() => coerceFsPath(() => {})).toThrow("expected string or path-like object, got function")
79+
})
80+
81+
test("throws for object without recognized path property", () => {
82+
expect(() => coerceFsPath({ foo: "bar" })).toThrow(TypeError)
83+
expect(() => coerceFsPath({ foo: "bar" })).toThrow("no recognized path property")
84+
})
85+
86+
test("throws for object with non-string path property", () => {
87+
expect(() => coerceFsPath({ path: 123 })).toThrow(TypeError)
88+
expect(() => coerceFsPath({ path: 123 })).toThrow("object.path is not a string")
89+
})
90+
91+
test("throws for array with non-string element", () => {
92+
expect(() => coerceFsPath(["a", 123, "b"])).toThrow(TypeError)
93+
expect(() => coerceFsPath(["a", 123, "b"])).toThrow("array element at index 1 is not a string")
94+
})
95+
96+
test("throws for object with nested object path", () => {
97+
expect(() => coerceFsPath({ path: { nested: "value" } })).toThrow(TypeError)
98+
expect(() => coerceFsPath({ path: { nested: "value" } })).toThrow("object.path is not a string")
99+
})
100+
})
101+
102+
describe("includes context in error messages", () => {
103+
test("includes context for primitive type error", () => {
104+
expect(() => coerceFsPath(123, "session route")).toThrow("in session route")
105+
})
106+
107+
test("includes context for object property error", () => {
108+
expect(() => coerceFsPath({ path: 123 }, "permission check")).toThrow("in permission check")
109+
})
110+
111+
test("includes context for unrecognized object error", () => {
112+
expect(() => coerceFsPath({ unknown: "value" }, "list tool")).toThrow("in list tool")
113+
})
114+
115+
test("includes context for array element error", () => {
116+
expect(() => coerceFsPath(["a", null], "glob tool")).toThrow("in glob tool")
117+
})
118+
})
119+
120+
describe("edge cases", () => {
121+
test("handles empty path property", () => {
122+
expect(coerceFsPath({ path: "" })).toBe("")
123+
})
124+
125+
test("handles whitespace-only path", () => {
126+
expect(coerceFsPath(" ")).toBe(" ")
127+
})
128+
129+
test("handles path with special characters", () => {
130+
expect(coerceFsPath("/path/with spaces/and-dashes")).toBe("/path/with spaces/and-dashes")
131+
})
132+
133+
test("handles object toString that returns [object Object]", () => {
134+
// This should NOT fall through to naive String() conversion
135+
const badObject = { toString: () => "[object Object]" }
136+
expect(() => coerceFsPath(badObject)).toThrow("no recognized path property")
137+
})
138+
})
139+
})

0 commit comments

Comments
 (0)