Compare commits

...

1 Commits

Author SHA1 Message Date
Hona bd5857d573 fix(desktop): contain renderer stores 2026-08-25 17:02:24 +00:00
3 changed files with 40 additions and 0 deletions
@@ -0,0 +1,31 @@
import { describe, expect, test } from "bun:test"
import { isStoreName } from "./name"
describe("store names", () => {
test.each([
"default.dat",
"opencode.global.dat",
"opencode.window.browser.dat",
"opencode.workspace.-home-user.abc123.dat",
"opencode.draft.session_123.0.dat",
])("allows %s", (name) => {
expect(isStoreName(name)).toBe(true)
})
test.each([
"../outside.dat",
"/tmp/outside.dat",
"C:\\outside.dat",
"C:outside.dat",
"opencode.window/../../outside.dat",
".",
"..",
"",
])("rejects %s", (name) => {
expect(isStoreName(name)).toBe(false)
})
test("rejects oversized names", () => {
expect(isStoreName("a".repeat(256))).toBe(false)
})
})
@@ -0,0 +1,7 @@
const storeName = /^[a-zA-Z0-9._-]+$/
export function isStoreName(value: string) {
if (value.length === 0 || value.length > 255) return false
if (value === "." || value === "..") return false
return storeName.test(value)
}
@@ -4,6 +4,7 @@ import { Effect } from "effect"
import { deleteStoreFileIfEmpty } from "./cleanup"
import { SETTINGS_STORE } from "./keys"
import { isStoreName } from "./name"
const cache = new Map<string, Store>()
@@ -12,6 +13,7 @@ const cache = new Map<string, Store>()
// in index.ts has executed, which would result in files being written to the default directory
// (e.g. bad: %APPDATA%\@opencode-ai\desktop\opencode.settings vs good: %APPDATA%\ai.opencode.desktop.dev\opencode.settings).
export function getStore(name = SETTINGS_STORE) {
if (!isStoreName(name)) throw new Error("Invalid store name")
const cached = cache.get(name)
if (cached) return cached
const next = new Store({