Skip to content
Merged
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
45 changes: 38 additions & 7 deletions src/commands/quickActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1961,11 +1961,42 @@ export function isPathInsideWorkspace(workspaceRoot: string, absolutePath: strin
return isResolvedPathInsideWorkspace(workspaceRoot, absolutePath);
}

function isEnoent(error: unknown): boolean {
return typeof error === "object"
&& error !== null
&& "code" in error
&& (error as { code?: unknown }).code === "ENOENT";
}

/** Real path of absolutePath, or of its nearest existing ancestor plus the missing suffix. */
function realPathAllowingMissingSuffix(absolutePath: string): string | undefined {
const missing: string[] = [];
let current = path.resolve(absolutePath);
while (true) {
try {
const realAncestor = realpathSync(current);
if (missing.length === 0) {
return realAncestor;
}
return path.join(realAncestor, ...missing.slice().reverse());
} catch (error) {
if (!isEnoent(error)) {
return undefined;
}
const parent = path.dirname(current);
if (parent === current) {
return undefined;
}
missing.push(path.basename(current));
current = parent;
}
}
}

/**
* True when the real path stays inside the workspace.
* Missing or dangling targets fall back to the lexical path so a typed
* in-workspace miss can reach ensureWorkspaceFileReady instead of looking
* like an escape. Existing files that realpath outside stay rejected.
* A missing leaf uses the nearest existing ancestor's real path plus the
* missing suffix. Existing files that realpath outside stay rejected.
*/
export function isRealPathInsideWorkspace(workspaceRoot: string, absolutePath: string): boolean {
let realRoot: string;
Expand All @@ -1974,11 +2005,11 @@ export function isRealPathInsideWorkspace(workspaceRoot: string, absolutePath: s
} catch {
return false;
}
try {
return isResolvedPathInsideWorkspace(realRoot, realpathSync(absolutePath));
} catch {
return isResolvedPathInsideWorkspace(workspaceRoot, absolutePath);
const resolved = realPathAllowingMissingSuffix(absolutePath);
if (resolved === undefined) {
return false;
}
return isResolvedPathInsideWorkspace(realRoot, resolved);
}

export interface StagedExternalPatch {
Expand Down
18 changes: 18 additions & 0 deletions test/unit/quickActions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,24 @@ test("isRealPathInsideWorkspace follows symlinks and stays fail-closed", async (
return;
}
assert.equal(isRealPathInsideWorkspace(workspaceRoot, linkPath), false);

const escapeDir = path.join(workspaceRoot, "escape");
try {
await fs.symlink(outsideRoot, escapeDir, "dir");
} catch (error) {
const code = typeof error === "object" && error !== null && "code" in error
? (error as { code?: unknown }).code
: undefined;
if (code === "EPERM" || code === "EACCES" || code === "ENOTSUP") {
return;
}
throw error;
}
assert.equal(
isRealPathInsideWorkspace(workspaceRoot, path.join(workspaceRoot, "escape", "missing.txt")),
false,
"missing file under a directory symlink to outside"
);
} finally {
await fs.rm(workspaceRoot, { recursive: true, force: true });
await fs.rm(outsideRoot, { recursive: true, force: true });
Expand Down
Loading