Skip to content

Commit c189c0e

Browse files
myk1ytclaude
andcommitted
fix(api): tolerate a corrupted stored mimoBaseUrl and truth the write-path comment
What: isStoredMimoBaseUrlOffList now reads the stored value as `unknown` and treats any non-string as off-list (purge) instead of passing it to stripTrailingSlashes, which threw TypeError on `.endsWith` and rejected the whole setConfiguration write. Adds two spec rows: a stored allowlisted URL carrying a trailing slash survives a partial update, and non-string stored values (42, an object, true) resolve to a purge rather than a rejection. The setConfiguration leading comment no longer claims this is the only write path. Why: the helper exists precisely because storage can hold values no schema ever saw, so a corrupted value must not be a new failure mode of the fix. The trailing-slash row pins the stripTrailingSlashes guard: only the slash-less literal is on the allowlist, so dropping the strip would purge every such cluster and silently drop the user to the default SGP endpoint. The comment was stale after review confirmed ClineProvider.createTask persists a caller-supplied configuration through setValues with no allowlist, which makes the consumers' fail-closed checks load-bearing rather than redundant. Impact: a corrupted mimoBaseUrl in storage is cleared on the next setConfiguration instead of breaking it; behavior for valid, off-list, empty and unset stored values is unchanged. No production behavior change from the two spec rows or the comment. Co-Authored-By: Claude <[email protected]>
1 parent e0a73f1 commit c189c0e

2 files changed

Lines changed: 64 additions & 11 deletions

File tree

‎src/extension/__tests__/api-configuration.spec.ts‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,12 @@ describe("API - configuration", () => {
9696

9797
describe("mimoBaseUrl allowlist at the public API boundary", () => {
9898
// setConfiguration writes straight to ContextProxy, which fails open on a
99-
// schema rejection, so this was the one write path where an off-list
99+
// schema rejection, so this is one of the write paths where an off-list
100100
// mimoBaseUrl could newly enter storage. Both persistence calls are
101101
// asserted: the proxy write and the saved profile.
102-
const makeApi = (storedBaseUrl?: string) => {
102+
// `storedBaseUrl` is deliberately `unknown`: storage is not schema-validated
103+
// on read, so the corrupted-value cases below need to inject a non-string.
104+
const makeApi = (storedBaseUrl?: unknown) => {
103105
const setValues = vi.fn().mockResolvedValue(undefined)
104106
const saveConfig = vi.fn().mockResolvedValue("default-id")
105107
const setModeConfig = vi.fn().mockResolvedValue(undefined)
@@ -222,5 +224,41 @@ describe("API - configuration", () => {
222224

223225
expect(updateGlobalState).not.toHaveBeenCalledWith("mimoBaseUrl", undefined)
224226
})
227+
228+
it("does not purge a stored allowlisted mimoBaseUrl that carries a trailing slash", async () => {
229+
// setConfiguration persists a trailing-slash variant verbatim (see the
230+
// incoming-value case above), so this exact stored shape is reachable
231+
// through this very API. Only the slash-less literal is on the allowlist,
232+
// so a stored-value check that skipped stripTrailingSlashes would purge
233+
// every such cluster and silently drop the user to the default SGP one.
234+
const { api, updateGlobalState } = makeApi("https://token-plan-ams.xiaomimimo.com/v1/")
235+
236+
await api.setConfiguration({ currentApiConfigName: "default", apiModelId: "mimo-v2.6-pro" })
237+
238+
expect(updateGlobalState).not.toHaveBeenCalledWith("mimoBaseUrl", undefined)
239+
})
240+
241+
it.each([[42], [{ nested: "https://attacker.example/v1" }], [true]])(
242+
"treats a non-string stored mimoBaseUrl (%s) as off-list instead of throwing",
243+
async (storedBaseUrl) => {
244+
// The stored read is typed `string | undefined`, but that is compile-time
245+
// only: ContextProxy hands back whatever the cache holds, and this helper
246+
// exists precisely because storage can carry values no schema ever saw.
247+
// stripTrailingSlashes calls .endsWith, so without a typeof guard a
248+
// corrupted value throws TypeError and rejects the whole configuration
249+
// write - a new failure mode in the scenario the helper targets.
250+
const { api, setValues, updateGlobalState } = makeApi(storedBaseUrl)
251+
252+
await expect(
253+
api.setConfiguration({ currentApiConfigName: "default", apiModelId: "mimo-v2.6-pro" }),
254+
).resolves.toBeUndefined()
255+
256+
expect(updateGlobalState).toHaveBeenCalledWith("mimoBaseUrl", undefined)
257+
expect(setValues).toHaveBeenCalledWith({
258+
currentApiConfigName: "default",
259+
apiModelId: "mimo-v2.6-pro",
260+
})
261+
},
262+
)
225263
})
226264
})

‎src/extension/api.ts‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -630,14 +630,20 @@ export class API extends EventEmitter<RooCodeEvents> implements RooCodeAPI {
630630
}
631631

632632
public async setConfiguration(values: RooCodeSettings) {
633-
// The allowlist normally lives at the two boundaries that consume this
634-
// value (the updateSettings persistence gate and MimoHandler's
635-
// constructor), but this public API writes straight to ContextProxy and
636-
// ContextProxy fails open on a schema rejection, so a caller could park
637-
// an off-list mimoBaseUrl in storage and every consumer would then have to
638-
// defend against it. Drop it here instead of persisting it. The value is
639-
// removed rather than overwritten so "unset" keeps its existing meaning
640-
// (fall back to the default cluster) instead of pinning a cluster.
633+
// The allowlist lives at the two boundaries that consume this value (the
634+
// updateSettings persistence gate and MimoHandler's constructor), but this
635+
// public API writes straight to ContextProxy and ContextProxy fails open on
636+
// a schema rejection, so a caller could park an off-list mimoBaseUrl in
637+
// storage. Drop it here instead of persisting it. The value is removed
638+
// rather than overwritten so "unset" keeps its existing meaning (fall back
639+
// to the default cluster) instead of pinning a cluster.
640+
//
641+
// This is not the only write path: ClineProvider.createTask persists a
642+
// caller-supplied configuration through setValues without consulting the
643+
// allowlist, so an off-list value can still reach storage that way. Every
644+
// consumer therefore fails closed on its own (MimoHandler's constructor
645+
// throws, the model fetcher and the router-models handlers refuse the
646+
// value) rather than trusting that storage was sanitized on the way in.
641647
const sanitizedValues = this.withoutOffListMimoBaseUrl(values)
642648

643649
// Dropping the incoming value is not enough on its own: a partial update
@@ -670,12 +676,21 @@ export class API extends EventEmitter<RooCodeEvents> implements RooCodeAPI {
670676
* counts as on-list, matching the sanitizer's convention.
671677
*/
672678
private isStoredMimoBaseUrlOffList(): boolean {
673-
const storedBaseUrl: string | undefined = this.sidebarProvider.getValue("mimoBaseUrl")
679+
// Typed as the four-literal union, but the type is compile-time only: the
680+
// accessor returns whatever the state cache holds, and this helper exists
681+
// because storage can carry values no schema ever saw. A non-string is
682+
// corrupted storage, not a usable endpoint, so it counts as off-list and
683+
// gets purged instead of throwing out of stripTrailingSlashes.
684+
const storedBaseUrl: unknown = this.sidebarProvider.getValue("mimoBaseUrl")
674685

675686
if (storedBaseUrl === undefined || storedBaseUrl === "") {
676687
return false
677688
}
678689

690+
if (typeof storedBaseUrl !== "string") {
691+
return true
692+
}
693+
679694
return !ALLOWED_BASE_URLS.has(stripTrailingSlashes(storedBaseUrl))
680695
}
681696

0 commit comments

Comments
 (0)