Skip to content

Commit 455e03b

Browse files
committed
fix(webview): only serve originalContent for approved file-edit messages
readOriginalContent could return the pre-edit file for a pending or denied approval, bypassing the isAnswered filter the webview applies. Restrict lookup to answered, non-partial file-edit tool messages and return null otherwise.
1 parent 59cd5c2 commit 455e03b

3 files changed

Lines changed: 69 additions & 11 deletions

File tree

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

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const toolAsk = (payload: unknown, extra: Partial<ClineMessage> = {}): ClineMess
1515
...extra,
1616
})
1717

18+
const approvedAsk = (payload: unknown, extra: Partial<ClineMessage> = {}): ClineMessage =>
19+
toolAsk(payload, { isAnswered: true, ...extra })
20+
1821
const bigOriginal = "line of the original file\n".repeat(2000)
1922

2023
describe("omitOriginalContent", () => {
@@ -140,17 +143,20 @@ describe("omitOriginalContent", () => {
140143
describe("findOriginalContent", () => {
141144
it("returns the originalContent of the tool message with that ts", () => {
142145
const messages = [
143-
toolAsk({ tool: "appliedDiff", path: "a.ts", originalContent: bigOriginal }),
144-
toolAsk({ tool: "appliedDiff", path: "b.ts", originalContent: "other" }),
146+
approvedAsk({ tool: "appliedDiff", path: "a.ts", originalContent: bigOriginal }),
147+
approvedAsk({ tool: "appliedDiff", path: "b.ts", originalContent: "other" }),
145148
]
146149

147150
expect(findOriginalContent(messages, { ts: messages[0]!.ts })).toBe(bigOriginal)
148151
expect(findOriginalContent(messages, { ts: messages[1]!.ts })).toBe("other")
149152
})
150153

151154
it("tells messages created in the same millisecond apart by messageId", () => {
152-
const first = toolAsk({ tool: "appliedDiff", path: "a.ts", originalContent: "first" }, { messageId: "id-1" })
153-
const second = toolAsk(
155+
const first = approvedAsk(
156+
{ tool: "appliedDiff", path: "a.ts", originalContent: "first" },
157+
{ messageId: "id-1" },
158+
)
159+
const second = approvedAsk(
154160
{ tool: "appliedDiff", path: "b.ts", originalContent: "second" },
155161
{ messageId: "id-2", ts: first.ts },
156162
)
@@ -195,8 +201,38 @@ describe("findOriginalContent", () => {
195201
expect(findOriginalContent(undefined, { ts: 1 })).toBeNull()
196202
})
197203

204+
it("does not disclose the original of a pending or denied approval", () => {
205+
const payload = { tool: "appliedDiff", path: "a.ts", originalContent: bigOriginal }
206+
const pending = toolAsk(payload)
207+
const denied = toolAsk(payload, { isAnswered: false })
208+
const all = [pending, denied]
209+
210+
for (const message of all) {
211+
expect(findOriginalContent(all, { ts: message.ts })).toBeNull()
212+
}
213+
})
214+
215+
it("does not disclose the original of a partial message", () => {
216+
const message = approvedAsk(
217+
{ tool: "appliedDiff", path: "a.ts", originalContent: bigOriginal },
218+
{ partial: true },
219+
)
220+
221+
expect(findOriginalContent([message], { ts: message.ts })).toBeNull()
222+
})
223+
224+
it("only discloses the original of file-edit tools", () => {
225+
const message = approvedAsk({ tool: "readFile", path: ".env", originalContent: "SECRET=1" })
226+
const missingTool = approvedAsk({ path: "a.ts", originalContent: "x" })
227+
const all = [message, missingTool]
228+
229+
for (const m of all) {
230+
expect(findOriginalContent(all, { ts: m.ts })).toBeNull()
231+
}
232+
})
233+
198234
it("returns an empty original as an empty string", () => {
199-
const message = toolAsk({ tool: "newFileCreated", content: "x", originalContent: "" })
235+
const message = approvedAsk({ tool: "newFileCreated", content: "x", originalContent: "" })
200236

201237
expect(findOriginalContent([message], { ts: message.ts })).toBe("")
202238
})

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,15 @@ import type { ClineMessage } from "@roo-code/types"
6565

6666
const originalFile = "const a = 1\nconst b = 2\n"
6767

68-
const toolMessage = (ts: number, payload: unknown, type: "ask" | "say" = "ask", messageId?: string): ClineMessage =>
68+
const toolMessage = (
69+
ts: number,
70+
payload: unknown,
71+
type: "ask" | "say" = "ask",
72+
messageId?: string,
73+
isAnswered = true,
74+
): ClineMessage =>
6975
type === "ask"
70-
? { ts, type: "ask", ask: "tool", text: JSON.stringify(payload), ...(messageId && { messageId }) }
76+
? { ts, type: "ask", ask: "tool", text: JSON.stringify(payload), isAnswered, ...(messageId && { messageId }) }
7177
: { ts, type: "say", say: "tool", text: JSON.stringify(payload), ...(messageId && { messageId }) }
7278

7379
function createProvider(clineMessages: ClineMessage[] | undefined) {
@@ -158,6 +164,19 @@ describe("webviewMessageHandler - readOriginalContent", () => {
158164
})
159165
})
160166

167+
it("answers null for an unanswered or denied edit approval", async () => {
168+
const { provider, postMessageToWebview } = createProvider([
169+
toolMessage(10, { tool: "appliedDiff", originalContent: originalFile }, "ask", "id-1", false),
170+
])
171+
172+
await webviewMessageHandler(provider, { type: "readOriginalContent", messageTs: 10, messageId: "id-1" })
173+
174+
expect(postMessageToWebview).toHaveBeenCalledWith({
175+
type: "originalContent",
176+
originalContentInfo: { ts: 10, messageId: "id-1", taskId: undefined, content: null },
177+
})
178+
})
179+
161180
it("ignores a request without a ts", async () => {
162181
const { provider, postMessageToWebview } = createProvider([
163182
toolMessage(10, { tool: "appliedDiff", originalContent: "x" }),

‎src/core/webview/stripOriginalContent.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import type { ClineMessage, ExtensionMessage } from "@roo-code/types"
44
// metadata such as `isAnswered` and `partial` is always taken from the current message.
55
const cache = new WeakMap<ClineMessage, { source: string; strippedText: string | undefined }>()
66

7+
const FILE_EDIT_TOOLS = new Set<unknown>(["editedExistingFile", "appliedDiff", "newFileCreated"])
8+
79
function isToolMessage(message: ClineMessage): boolean {
810
return (message.type === "ask" && message.ask === "tool") || (message.type === "say" && message.say === "tool")
911
}
@@ -42,7 +44,8 @@ function stripOriginalContentFromText(text: string): string | undefined {
4244
}
4345

4446
/**
45-
* The `originalContent` of a tool message, or null when there is none. `ts` is not unique (two messages can be
47+
* The `originalContent` of an approved file-edit tool message, or null when there is none. A pending, denied or
48+
* partial approval never discloses it, since the file has not been authorized for the webview yet. `ts` is not unique (two messages can be
4649
* created in the same millisecond), so `messageId` is preferred; `ts` only serves messages persisted without one.
4750
*/
4851
export function findOriginalContent(
@@ -53,13 +56,13 @@ export function findOriginalContent(
5356
(m) => isToolMessage(m) && (id.messageId !== undefined ? m.messageId === id.messageId : m.ts === id.ts),
5457
)
5558

56-
if (!message?.text) {
59+
if (!message?.text || message.partial || (message.type === "ask" && message.isAnswered !== true)) {
5760
return null
5861
}
5962

6063
try {
61-
const { originalContent } = JSON.parse(message.text) as { originalContent?: unknown }
62-
return typeof originalContent === "string" ? originalContent : null
64+
const { tool, originalContent } = JSON.parse(message.text) as { tool?: unknown; originalContent?: unknown }
65+
return FILE_EDIT_TOOLS.has(tool) && typeof originalContent === "string" ? originalContent : null
6366
} catch {
6467
return null
6568
}

0 commit comments

Comments
 (0)