Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions docs/architecture/tla/WorkspaceContainment.NeverInside.cfg

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions docs/architecture/tla/WorkspaceContainment.Unfixed.cfg

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions docs/architecture/tla/WorkspaceContainment.cfg

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

168 changes: 168 additions & 0 deletions docs/architecture/tla/WorkspaceContainment.tla
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
---- MODULE WorkspaceContainment ----
(*

**************************************************************************)
(* Workspace containment for write auto-approval (issue 2344). *)
(* *)
(* Functions. Source: the approval payload of a write tool. It carries *)
(* `isOutsideWorkspace` from `isPathOutsideWorkspace`, a lexical check, *)
(* and its file targets from `resolveApprovalFileTarget` and *)
(* `imageWriteTargets`, the native paths the tool writes. The realpath *)
(* oracle `isRealPathOutsideWorkspace` resolves each target. Sink: the *)
(* write branch of `checkAutoApproval` in src/core/auto-approval/index.ts.*)
(* *)
(* Failure. F1: a write through a symlink inside the workspace to a file *)
(* outside it is auto-approved while `alwaysAllowWriteOutsideWorkspace` *)
(* is off. F2: a write inside the workspace stops being auto-approved. *)
(* F3: the gate decides on a display path that `getReadablePath` rewrote *)
(* (POSIX turns a backslash into a separator), so an inside verdict *)
(* approves a write to a file outside the workspace. F4: a tool writes a *)
(* file the approval did not resolve (`generate_image` appends the *)
(* extension after the approval), and a link at that file escapes. *)
(* *)
(* Invariants. SymlinkEscapeNeedsConsent is the opposite of F1: an *)
(* approved write whose resolved target is outside has a matching *)
(* pattern or the outside-write consent. InsideWriteApproved is the *)
(* opposite of F2. DisplayPathNeedsConsent is the opposite of F3: an *)
(* approved write decided without the native target is not an outside *)
(* write, unless a pattern or the outside consent covers it. *)
(* UncheckedCandidateNeedsConsent is the opposite of F4, for a payload *)
(* with native targets that leaves a candidate out. *)
(* *)
(* Model. One write action. The inputs are the lexical flag, the real *)
(* flag of the resolved targets, whether the payload names the file the *)
(* tool writes (`nativeTarget`), whether it names every file the tool *)
(* may write (`allCandidatesChecked`), the real location of the file *)
(* written (`writtenOutside`), three settings, and two action properties.*)
(* The gate cannot tell a complete target list from a partial one, so *)
(* every candidate is the payload source's duty: `SourceCandidates` *)
(* constrains the payload a tool sends, and the fixed tools list every *)
(* file they may write. The gate is `lexicalOutside \/ realOutside \/ *)
(* ~nativeTarget`: a payload it cannot verify fails closed. The oracle *)
(* is sound for a file it resolved, and only listed targets are resolved.*)
(* *)
(* Cost. Base cfg: 768 distinct states, under one second. Each sabotage *)
(* cfg stops at its first violation. *)
(* Findings. Unfixed fails SymlinkEscapeNeedsConsent: lexical inside, *)
(* real outside, write allowed, no outside consent, no pattern (F1). *)
(* NoContainment fails SymlinkEscapeNeedsConsent. NeverInside fails *)
(* InsideWriteApproved (F2). CorruptDisplay fails DisplayPathNeedsConsent *)
(* (F3). UncheckedCandidate fails UncheckedCandidateNeedsConsent (F4): *)
(* the base cfg cannot reach F4, since the fixed source lists every *)
(* candidate, so that cfg relaxes the source. Each cfg fails only the *)
(* invariant named here. *)
(* *)
(* Review ratchet. covered 0, added 2, gap 0 (PR 2816 round 1: threads *)
(* r4792649128 and r4792649138 added F3 and F4). *)
(*

**************************************************************************)
EXTENDS Naturals

(* The containment gate of the write branch: `OutsideGate(lex, real, *)
(* native)` is TRUE when the action counts as outside every workspace *)
(* folder. The MC module gives the fixed gate and each sabotage. *)
CONSTANT OutsideGate(_, _, _)

(* The payload source: `SourceCandidates(native, all)` constrains the *)
(* payload a write tool sends. FixedSource names every candidate with the *)
(* native target. The MC module gives the source before round 1. *)
CONSTANT SourceCandidates(_, _)

VARIABLES
lexicalOutside, \* The payload flag `isOutsideWorkspace`.
realOutside, \* The realpath oracle: some resolved target is outside.
nativeTarget, \* The payload names the file the tool writes.
allCandidatesChecked, \* The payload names every file the tool may write.
writtenOutside, \* The file the tool writes is really outside.
allowWrite, \* `alwaysAllowWrite`.
allowOutsideWrite, \* `alwaysAllowWriteOutsideWorkspace`.
allowProtected, \* `alwaysAllowWriteProtected`.
protected, \* The action touches a protected file.
patternMatched, \* Every file of the action matches `allowedWriteFiles`.
decision

Vars == <<lexicalOutside, realOutside, nativeTarget, allCandidatesChecked, writtenOutside,
allowWrite, allowOutsideWrite, allowProtected, protected, patternMatched, decision>>

(* The fixed gate: a lexical inside is not final. A symlink inside the *)
(* workspace can lead outside it, so the real flag decides too. A payload *)
(* without the native target cannot be verified, so it counts as outside. *)
(* The oracle fails closed: an unresolvable target reports real outside. *)
FixedOutsideGate(lex, real, native) == lex \/ real \/ \lnot native

(* The fixed write tools: a payload that names the native target also *)
(* names every file the tool may write (`imageWriteTargets`). *)
FixedSource(native, all) == native => all

Init ==
/\ lexicalOutside \in BOOLEAN
/\ realOutside \in BOOLEAN
/\ nativeTarget \in BOOLEAN
/\ allCandidatesChecked \in BOOLEAN
/\ writtenOutside \in BOOLEAN
\* Only listed targets are resolved, so a checked candidate needs a native target.
/\ allCandidatesChecked => nativeTarget
/\ SourceCandidates(nativeTarget, allCandidatesChecked)
\* The oracle is sound for a file it resolved.
/\ allCandidatesChecked => (writtenOutside = realOutside)
/\ allowWrite \in BOOLEAN
/\ allowOutsideWrite \in BOOLEAN
/\ allowProtected \in BOOLEAN
/\ protected \in BOOLEAN
/\ patternMatched \in BOOLEAN
/\ decision = "pending"

(* The write branch of `checkAutoApproval`: a matching pattern or the *)
(* write permission with containment consent, then the protected check. *)
Decide ==
/\ decision = "pending"
/\ decision' =
IF patternMatched \/
(allowWrite /\ (\lnot OutsideGate(lexicalOutside, realOutside, nativeTarget) \/ allowOutsideWrite))
THEN IF \lnot protected \/ allowProtected THEN "approve" ELSE "ask"
ELSE "ask"
/\ UNCHANGED <<lexicalOutside, realOutside, nativeTarget, allCandidatesChecked, writtenOutside,
allowWrite, allowOutsideWrite, allowProtected, protected, patternMatched>>

Next == Decide

Spec == Init /\ [][Next]_Vars

TypeOK ==
/\ decision \in {"pending", "approve", "ask"}
/\ lexicalOutside \in BOOLEAN
/\ realOutside \in BOOLEAN
/\ nativeTarget \in BOOLEAN
/\ allCandidatesChecked \in BOOLEAN
/\ writtenOutside \in BOOLEAN
/\ allowWrite \in BOOLEAN
/\ allowOutsideWrite \in BOOLEAN
/\ allowProtected \in BOOLEAN
/\ protected \in BOOLEAN
/\ patternMatched \in BOOLEAN

(* F1. An approved write whose resolved target is outside has a pattern *)
(* match or the outside-write consent. *)
SymlinkEscapeNeedsConsent ==
decision = "approve" /\ realOutside => patternMatched \/ allowOutsideWrite

(* F2. A write whose every candidate resolved inside stays auto-approved *)
(* when writes are allowed and the protected check passes. *)
InsideWriteApproved ==
\lnot lexicalOutside /\ \lnot realOutside /\ allCandidatesChecked
/\ allowWrite /\ (\lnot protected \/ allowProtected) => decision # "ask"

(* F3. An approved write decided without the native target is not an *)
(* outside write, unless a pattern or the outside consent covers it. *)
DisplayPathNeedsConsent ==
decision = "approve" /\ \lnot nativeTarget /\ writtenOutside => patternMatched \/ allowOutsideWrite

(* F4. An approved write whose payload has native targets but leaves a *)
(* candidate out is not an outside write, unless a pattern or the outside *)
(* consent covers it. *)
UncheckedCandidateNeedsConsent ==
decision = "approve" /\ nativeTarget /\ \lnot allCandidatesChecked /\ writtenOutside
=> patternMatched \/ allowOutsideWrite

====
21 changes: 21 additions & 0 deletions docs/architecture/tla/WorkspaceContainmentMC.tla
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---- MODULE WorkspaceContainmentMC ----
\* Model-checking settings for WorkspaceContainment. Each *Gate and *Source operator is one variant.
EXTENDS WorkspaceContainment

\* Code before the fix (issue 2344): drops the realpath conjunct. The lexical flag alone decides containment.
LexicalOnly(lex, real, native) == lex \/ \lnot native

\* The write branch drops containment: a payload it can verify is approved by `alwaysAllowWrite` alone.
NoContainment(lex, real, native) == \lnot native

\* Over-reject: every write counts as outside.
NeverInside(lex, real, native) == TRUE

\* PR 2816 round 1, thread r4792649128: drops the native-target conjunct. The gate trusts a display path (F3).
CorruptDisplay(lex, real, native) == lex \/ real

\* PR 2816 round 1, thread r4792649138: the `generate_image` payload before the round, which named the bare
\* path alone. The gate cannot tell a complete target list from a partial one (F4).
IncompleteSource(native, all) == TRUE

====
21 changes: 19 additions & 2 deletions src/core/auto-approval/__tests__/allowedWriteFiles.spec.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/core/auto-approval/__tests__/blanket-deny.spec.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions src/core/auto-approval/__tests__/fileTargets.spec.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/core/auto-approval/__tests__/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// maintaining near-identical literals side by side.

import path from "path"
import * as vscode from "vscode"

import type { ClineSayTool, ExtensionState } from "@roo-code/types"

Expand Down Expand Up @@ -76,3 +77,18 @@ export function readFileAsk(
})),
}
}

/**
* Register `roots` as the workspace folders. The write branch resolves the
* real target of a file against them, so a spec that approves an inside write
* names the folder the file is inside.
*/
export function setWorkspaceFolders(...roots: string[]): void {
Object.assign(vscode.workspace, {
workspaceFolders: roots.map((fsPath, index) => ({
uri: vscode.Uri.file(fsPath),
name: path.basename(fsPath),
index,
})),
})
}
Loading
Loading