Skip to content

Commit 4dcc7ad

Browse files
fix(webview): keep the host-owned viewStates map out of the renderer settings write
Two review rows on this branch: - webviewMessageHandler.ts: the updateSettings loop wrote every key the payload carried through provider.setValue, so a renderer that included viewStates would overwrite every view persisted mode and pin in one shot and bypass the ownership checks in setViewStateId. The import path and both API writes already refuse that key through isNonPortableSettingsKey; this write path now follows the same rule. - TranslationContext.locale-loading.spec.tsx: the new cases awaited loadLanguage themselves, which supplies the bundle even to a provider that deferred the load or switched language later. They now capture the provider own call (spying the module export and recording the promise it returns) and await that, so the assertion depends on the provider starting the load rather than on the test starting it. Measured first: the viewStates case fails at 06aab59 with "expected the mock to not be called with arguments: [ viewStates, Anything ]". With the provider effect changed so it no longer calls loadLanguage, the locale case fails on its recorded-call assertion with "expected 0 to be greater than 0", which the old form would have passed. webviewMessageHandler.spec.ts 177 passed, TranslationContext.locale-loading.spec.tsx 6 passed, src tsc 0 errors, eslint 0 problems, webview-ui tsc clean, prettier clean, no suppression changes.
1 parent 06aab59 commit 4dcc7ad

3 files changed

Lines changed: 59 additions & 7 deletions

File tree

‎src/core/webview/__tests__/webviewMessageHandler.spec.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2242,6 +2242,27 @@ it("persists alwaysDenyUnapprovedCommands through the generic updateSettings loo
22422242
expect(mockClineProvider.postStateToWebview).toHaveBeenCalledTimes(1)
22432243
})
22442244

2245+
// viewStates is host-owned: one map holding every view persisted mode and pin. The
2246+
// renderer payload is not trusted with it, for the same reason the import path and both
2247+
// API writes refuse it.
2248+
it("keeps the host-owned viewStates map out of the updateSettings write", async () => {
2249+
vi.clearAllMocks()
2250+
2251+
await webviewMessageHandler(mockClineProvider, {
2252+
type: "updateSettings",
2253+
updatedSettings: {
2254+
viewStates: { "view-1": { currentApiConfigName: "ghost-profile" } },
2255+
alwaysDenyUnapprovedCommands: true,
2256+
},
2257+
})
2258+
2259+
expect(mockClineProvider.setValue).not.toHaveBeenCalledWith("viewStates", expect.anything())
2260+
expect(mockClineProvider.contextProxy.setValue).not.toHaveBeenCalledWith("viewStates", expect.anything())
2261+
// A portable key in the same payload still persists through the provider path.
2262+
expect(mockClineProvider.contextProxy.setValue).toHaveBeenCalledWith("alwaysDenyUnapprovedCommands", true)
2263+
expect(mockClineProvider.postStateToWebview).toHaveBeenCalledTimes(1)
2264+
})
2265+
22452266
// Both allowlists are normalized by the same branch, so both are held to the
22462267
// same contract.
22472268
describe.each(["allowedReadFiles", "allowedWriteFiles"] as const)("webviewMessageHandler - %s", (key) => {

‎src/core/webview/webviewMessageHandler.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { TelemetryService } from "@roo-code/telemetry"
3838
import { type ApiMessage } from "../task-persistence/apiMessages"
3939
import { saveTaskMessages } from "../task-persistence"
4040
import { isInvalidDisplayValue } from "../config/displaySettings"
41+
import { isNonPortableSettingsKey } from "../config/ContextProxy"
4142
import { importRooTaskHistory } from "../task-persistence/importRooTaskHistory"
4243

4344
import { ClineProvider } from "./ClineProvider"
@@ -933,6 +934,13 @@ export const webviewMessageHandler = async (
933934

934935
// Route through provider.setValue so view-local buffer/pin sync stays
935936
// consistent with the other mutation paths.
937+
// The payload comes from the renderer, and viewStates is every view persisted mode
938+
// and profile pin in one map: written here it would replace all of them and bypass the
939+
// ownership checks in setViewStateId, so this path refuses it the way the import path
940+
// and both API writes do.
941+
if (isNonPortableSettingsKey(key)) {
942+
continue
943+
}
936944
await provider.setValue(key as keyof RooCodeSettings, newValue)
937945
}
938946
} catch (error) {

‎webview-ui/src/i18n/__tests__/TranslationContext.locale-loading.spec.tsx‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
import { act, render, screen, waitFor } from "@testing-library/react"
22
import React from "react"
33

4-
import i18next, { loadLanguage } from "../setup"
4+
import i18next, * as i18nSetup from "../setup"
55
import TranslationProvider, { useAppTranslation } from "../TranslationContext"
66

77
// The provider reads the active language from extension state. Tests drive it
88
// through a mutable holder so a rerender can simulate a settings change.
9+
// The provider must start its own locale load. A test that called loadLanguage itself would
10+
// supply the bundle even to a provider that deferred the load or skipped it and switched
11+
// language later, which is the hole these cases must not paper over. The wrapper records
12+
// every call the provider makes and hands back the real promise, so awaiting it is awaiting
13+
// the memoized in-flight load the provider is waiting on.
14+
const realLoadLanguage = i18nSetup.loadLanguage
15+
const providerLoads: { calls: Array<{ language: string; promise: Promise<void> }> } = { calls: [] }
16+
const awaitProviderLoad = async (language: string) => {
17+
const started = providerLoads.calls.filter((call) => call.language === language)
18+
expect(started.length).toBeGreaterThan(0)
19+
await Promise.all(started.map((call) => call.promise))
20+
}
21+
922
const extensionState: { language: string } = { language: "en" }
1023

1124
vi.mock("@/context/ExtensionStateContext", () => ({
@@ -38,6 +51,16 @@ describe("TranslationProvider locale loading", () => {
3851
// i18next is a module-level singleton, so a language left active by an
3952
// earlier test would mask the fallback path this suite is checking.
4053
await i18next.changeLanguage("en")
54+
providerLoads.calls = []
55+
vi.spyOn(i18nSetup, "loadLanguage").mockImplementation((language: string) => {
56+
const promise = realLoadLanguage(language)
57+
providerLoads.calls.push({ language, promise })
58+
return promise
59+
})
60+
})
61+
62+
afterEach(() => {
63+
vi.restoreAllMocks()
4164
})
4265

4366
it("renders translated strings for a non-English initial language", async () => {
@@ -54,7 +77,7 @@ describe("TranslationProvider locale loading", () => {
5477
// itself waits on - the idiom the unmount case at the end of this file uses - takes
5578
// the clock out of the assertion. What `waitFor` has left to catch is the render
5679
// that `languageChanged` triggers, which settles in the same task.
57-
await loadLanguage("ko")
80+
await awaitProviderLoad("ko")
5881

5982
await waitFor(() => {
6083
expect(screen.getByTestId("error-title")).toHaveTextContent("문제가 발생했습니다")
@@ -77,7 +100,7 @@ describe("TranslationProvider locale loading", () => {
77100
})
78101

79102
// Await the fetch instead of racing a fixed window (note in the first case).
80-
await loadLanguage("ja")
103+
await awaitProviderLoad("ja")
81104

82105
await waitFor(() => {
83106
expect(screen.getByTestId("error-title")).toHaveTextContent("問題が発生しました")
@@ -97,7 +120,7 @@ describe("TranslationProvider locale loading", () => {
97120
const { rerender } = renderProbe("de")
98121

99122
// German is loaded on demand as well: await it rather than time it.
100-
await loadLanguage("de")
123+
await awaitProviderLoad("de")
101124

102125
await waitFor(() => {
103126
expect(screen.getByTestId("error-title")).toHaveTextContent("Etwas ist schiefgelaufen")
@@ -121,7 +144,7 @@ describe("TranslationProvider locale loading", () => {
121144
const { rerender } = renderProbe("de")
122145

123146
// The cold locale is awaited here too (see the note in the first case).
124-
await loadLanguage("de")
147+
await awaitProviderLoad("de")
125148

126149
await waitFor(() => {
127150
expect(screen.getByTestId("error-title")).toHaveTextContent("Etwas ist schiefgelaufen")
@@ -143,7 +166,7 @@ describe("TranslationProvider locale loading", () => {
143166

144167
// Awaiting the fetch through the promise the provider waits on keeps the assertion
145168
// above a statement about ordering rather than about the clock.
146-
await loadLanguage("fr")
169+
await awaitProviderLoad("fr")
147170

148171
await waitFor(() => {
149172
expect(screen.getByTestId("error-title")).toHaveTextContent("Une erreur s'est produite")
@@ -163,7 +186,7 @@ describe("TranslationProvider locale loading", () => {
163186
// locale here waits for the exact resolution the guard is deciding on.
164187
// The provider attached its `.then` first, so its callback has already run
165188
// by the time this returns: the switch was suppressed, not left pending.
166-
await loadLanguage("vi")
189+
await awaitProviderLoad("vi")
167190

168191
expect(i18next.hasResourceBundle("vi", "common")).toBe(true)
169192
expect(changeLanguage).not.toHaveBeenCalledWith("vi")

0 commit comments

Comments
 (0)