Skip to content

Commit 8367f1f

Browse files
fix(editor): check the authorized root before creating directories in saveDirectly
saveDirectly ran createDirectoriesForFile before safeWriteText's authorizedRoot check, and createDirectoriesForFile follows symlinks: an approved inside path whose intermediate link points outside the workspace mutated the outside filesystem before the publish was rejected. Export the canonical ancestor check from safeWriteText and run it first, so the allowlist decision has no filesystem side effects (Security Boundaries row).
1 parent 9601933 commit 8367f1f

3 files changed

Lines changed: 33 additions & 3 deletions

File tree

‎src/integrations/editor/DiffViewProvider.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { arePathsEqual, getReadablePath } from "../../utils/path"
1919
import { formatResponse } from "../../core/prompts/responses"
2020
import { diagnosticsToProblemsString, getNewDiagnostics } from "../diagnostics"
2121
import { Task } from "../../core/task/Task"
22-
import { safeWriteText } from "../../services/file-safety/safeWriteText"
22+
import { assertPublishWithinAuthorizedRoot, safeWriteText } from "../../services/file-safety/safeWriteText"
2323

2424
import { DecorationController } from "./DecorationController"
2525

@@ -1156,6 +1156,15 @@ export class DiffViewProvider {
11561156
// Get diagnostics before editing the file
11571157
this.preDiagnostics = vscode.languages.getDiagnostics()
11581158

1159+
// Reject an escape through an intermediate symlink before anything is
1160+
// created on disk: createDirectoriesForFile follows symlinks, so an
1161+
// approved inside path whose intermediate link points outside the
1162+
// workspace would otherwise create the outside directory before
1163+
// safeWriteText's authorizedRoot check rejected the publish. The same
1164+
// canonical check runs inside safeWriteText; running it here first keeps
1165+
// the allowlist decision free of filesystem side effects.
1166+
await assertPublishWithinAuthorizedRoot(this.cwd, absolutePath, absolutePath)
1167+
11591168
// Write the content directly to the file
11601169
await createDirectoriesForFile(absolutePath)
11611170
// safeWriteText publishes with rename, which only needs write permission on the

‎src/integrations/editor/__tests__/DiffViewProvider.spec.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ vi.mock("../../../services/file-safety/safeWriteText", async (importOriginal) =>
2828
return {
2929
...actual,
3030
safeWriteText: vi.fn().mockResolvedValue(undefined),
31+
assertPublishWithinAuthorizedRoot: vi.fn().mockResolvedValue(undefined),
3132
}
3233
})
3334

@@ -795,6 +796,26 @@ describe("DiffViewProvider", () => {
795796
})
796797

797798
describe("saveDirectly method", () => {
799+
it("rejects an escaped target before creating directories for it", async () => {
800+
const { assertPublishWithinAuthorizedRoot, safeWriteText } =
801+
await import("../../../services/file-safety/safeWriteText")
802+
const { createDirectoriesForFile } = await import("../../../utils/fs")
803+
vi.mocked(createDirectoriesForFile).mockClear()
804+
vi.mocked(safeWriteText).mockClear()
805+
const escape = new Error("safeWriteText: refusing to publish outside the authorized root")
806+
vi.mocked(assertPublishWithinAuthorizedRoot).mockRejectedValueOnce(escape)
807+
808+
await expect(diffViewProvider.saveDirectly("linkdir/new/secret.txt", "x", false, false, 0)).rejects.toThrow(
809+
"refusing to publish",
810+
)
811+
812+
// The allow-list decision must precede the filesystem mutation:
813+
// createDirectoriesForFile follows symlinks, so running it first would
814+
// already have created the outside directory the check exists to protect.
815+
expect(createDirectoriesForFile).not.toHaveBeenCalled()
816+
expect(safeWriteText).not.toHaveBeenCalled()
817+
vi.mocked(assertPublishWithinAuthorizedRoot).mockResolvedValue(undefined)
818+
})
798819
it("rejects a save over a read-only target instead of renaming onto it", async () => {
799820
const fs = await import("fs/promises")
800821
const { safeWriteText } = await import("../../../services/file-safety/safeWriteText")

‎src/services/file-safety/safeWriteText.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ async function _realpathNearestExisting(targetPath: string): Promise<string> {
420420
* caller's own approval flow covers outside-root writes, and this primitive has
421421
* no opinion about a root it was not asked to enforce.
422422
*/
423-
async function _assertPublishWithinAuthorizedRoot(
423+
export async function assertPublishWithinAuthorizedRoot(
424424
authorizedRoot: string,
425425
requestedPath: string,
426426
targetPath: string,
@@ -466,7 +466,7 @@ export async function safeWriteText(filePath: string, content: string, options?:
466466
// Enforce the caller's authorized root before anything is created or written
467467
// (the mkdir below can otherwise materialize an outside parent first).
468468
if (options?.authorizedRoot !== undefined) {
469-
await _assertPublishWithinAuthorizedRoot(options.authorizedRoot, absoluteFilePath, targetPath)
469+
await assertPublishWithinAuthorizedRoot(options.authorizedRoot, absoluteFilePath, targetPath)
470470
}
471471

472472
const dirPath = path.dirname(targetPath)

0 commit comments

Comments
 (0)