Skip to content

Commit f27ef59

Browse files
authored
fix(app): sanitize workspace store filenames on Windows (anomalyco#16703)
1 parent 3432882 commit f27ef59

3 files changed

Lines changed: 11 additions & 2 deletions

File tree

packages/app/e2e/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function sessionPath(directory: string, sessionID?: string) {
5757
}
5858

5959
export function workspacePersistKey(directory: string, key: string) {
60-
const head = directory.slice(0, 12) || "workspace"
60+
const head = (directory.slice(0, 12) || "workspace").replace(/[^a-zA-Z0-9._-]/g, "-")
6161
const sum = checksum(directory) ?? "0"
6262
return `opencode.workspace.${head}.${sum}.dat:workspace:${key}`
6363
}

packages/app/src/utils/persist.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,12 @@ describe("persist localStorage resilience", () => {
104104
const result = persistTesting.normalize({ value: "ok" }, '{"value":"\\x"}')
105105
expect(result).toBeUndefined()
106106
})
107+
108+
test("workspace storage sanitizes Windows filename characters", () => {
109+
const result = persistTesting.workspaceStorage("C:\\Users\\foo")
110+
111+
expect(result).toStartWith("opencode.workspace.")
112+
expect(result.endsWith(".dat")).toBeTrue()
113+
expect(/[:\\/]/.test(result)).toBeFalse()
114+
})
107115
})

packages/app/src/utils/persist.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ function normalize(defaults: unknown, raw: string, migrate?: (value: unknown) =>
204204
}
205205

206206
function workspaceStorage(dir: string) {
207-
const head = dir.slice(0, 12) || "workspace"
207+
const head = (dir.slice(0, 12) || "workspace").replace(/[^a-zA-Z0-9._-]/g, "-")
208208
const sum = checksum(dir) ?? "0"
209209
return `opencode.workspace.${head}.${sum}.dat`
210210
}
@@ -300,6 +300,7 @@ export const PersistTesting = {
300300
localStorageDirect,
301301
localStorageWithPrefix,
302302
normalize,
303+
workspaceStorage,
303304
}
304305

305306
export const Persist = {

0 commit comments

Comments
 (0)