mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-04-01 23:02:26 +00:00
fix: check worktree for external_directory permission in subdirs (#7811)
This commit is contained in:
@@ -279,7 +279,7 @@ export namespace File {
|
||||
|
||||
// TODO: Filesystem.contains is lexical only - symlinks inside the project can escape.
|
||||
// TODO: On Windows, cross-drive paths bypass this check. Consider realpath canonicalization.
|
||||
if (!Filesystem.contains(Instance.directory, full)) {
|
||||
if (!Instance.containsPath(full)) {
|
||||
throw new Error(`Access denied: path escapes project directory`)
|
||||
}
|
||||
|
||||
@@ -339,7 +339,7 @@ export namespace File {
|
||||
|
||||
// TODO: Filesystem.contains is lexical only - symlinks inside the project can escape.
|
||||
// TODO: On Windows, cross-drive paths bypass this check. Consider realpath canonicalization.
|
||||
if (!Filesystem.contains(Instance.directory, resolved)) {
|
||||
if (!Instance.containsPath(resolved)) {
|
||||
throw new Error(`Access denied: path escapes project directory`)
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import { Project } from "./project"
|
||||
import { State } from "./state"
|
||||
import { iife } from "@/util/iife"
|
||||
import { GlobalBus } from "@/bus/global"
|
||||
import { Filesystem } from "@/util/filesystem"
|
||||
|
||||
interface Context {
|
||||
directory: string
|
||||
@@ -46,6 +47,18 @@ export const Instance = {
|
||||
get project() {
|
||||
return context.use().project
|
||||
},
|
||||
/**
|
||||
* Check if a path is within the project boundary.
|
||||
* Returns true if path is inside Instance.directory OR Instance.worktree.
|
||||
* Paths within the worktree but outside the working directory should not trigger external_directory permission.
|
||||
*/
|
||||
containsPath(filepath: string) {
|
||||
if (Filesystem.contains(Instance.directory, filepath)) return true
|
||||
// Non-git projects set worktree to "/" which would match ANY absolute path.
|
||||
// Skip worktree check in this case to preserve external_directory permissions.
|
||||
if (Instance.worktree === "/") return false
|
||||
return Filesystem.contains(Instance.worktree, filepath)
|
||||
},
|
||||
state<S>(init: () => S, dispose?: (state: Awaited<S>) => Promise<void>): () => S {
|
||||
return State.create(() => Instance.directory, init, dispose)
|
||||
},
|
||||
|
||||
@@ -85,7 +85,7 @@ export const BashTool = Tool.define("bash", async () => {
|
||||
throw new Error("Failed to parse command")
|
||||
}
|
||||
const directories = new Set<string>()
|
||||
if (!Filesystem.contains(Instance.directory, cwd)) directories.add(cwd)
|
||||
if (!Instance.containsPath(cwd)) directories.add(cwd)
|
||||
const patterns = new Set<string>()
|
||||
const always = new Set<string>()
|
||||
|
||||
@@ -124,7 +124,7 @@ export const BashTool = Tool.define("bash", async () => {
|
||||
process.platform === "win32" && resolved.match(/^\/[a-z]\//)
|
||||
? resolved.replace(/^\/([a-z])\//, (_, drive) => `${drive.toUpperCase()}:\\`).replace(/\//g, "\\")
|
||||
: resolved
|
||||
if (!Filesystem.contains(Instance.directory, normalized)) directories.add(normalized)
|
||||
if (!Instance.containsPath(normalized)) directories.add(normalized)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import path from "path"
|
||||
import type { Tool } from "./tool"
|
||||
import { Filesystem } from "../util/filesystem"
|
||||
import { Instance } from "../project/instance"
|
||||
|
||||
type Kind = "file" | "directory"
|
||||
@@ -15,7 +14,7 @@ export async function assertExternalDirectory(ctx: Tool.Context, target?: string
|
||||
|
||||
if (options?.bypass) return
|
||||
|
||||
if (Filesystem.contains(Instance.directory, target)) return
|
||||
if (Instance.containsPath(target)) return
|
||||
|
||||
const kind = options?.kind ?? "file"
|
||||
const parentDir = kind === "directory" ? target : path.dirname(target)
|
||||
|
||||
Reference in New Issue
Block a user