Skip to content

Commit dda5a20

Browse files
t3dotggclaude
andcommitted
fix(web): ../ file links outside the workspace open as host files
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 066c8fc commit dda5a20

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎packages/shared/src/path.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ describe("workspaceRelativeFilePath", () => {
4343
["/Repo/Project/src/main.ts", "/repo/project", null],
4444
["C:/USERS/mike/t3code/main.ts", "c:/users/MIKE/t3code", "main.ts"],
4545
["/repo/project-two/a.ts", "/repo/project", null],
46+
// regression: #16354 a `..` that leaves the root is a host path, not a workspace path
47+
["/repo/project/../other/notes.md", "/repo/project", null],
48+
["C:\\repo\\project\\src\\..\\..\\notes.md", "C:\\repo\\project", null],
49+
["/repo/project/docs/a/../b.md", "/repo/project", "docs/a/../b.md"],
4650
])("relates %s to %s", (path, workspaceRoot, relativePath) => {
4751
expect(workspaceRelativeFilePath(path, workspaceRoot)).toBe(relativePath);
4852
});

‎packages/shared/src/path.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,15 @@ export function workspaceRelativeFilePath(
9898
const rootForCompare = caseInsensitive ? normalizedRoot.toLowerCase() : normalizedRoot;
9999
if (pathForCompare.replace(/\/+$/, "") === rootForCompare) return ".";
100100
if (!pathForCompare.startsWith(`${rootForCompare}/`)) return null;
101-
return normalizedPath.slice(normalizedRoot.length + 1);
101+
const relativePath = normalizedPath.slice(normalizedRoot.length + 1);
102+
// `/repo/../x` starts with the root but escapes it; the server rejects that as a workspace path.
103+
let depth = 0;
104+
for (const segment of relativePath.split("/")) {
105+
if (segment === "..") depth -= 1;
106+
else if (segment.length > 0 && segment !== ".") depth += 1;
107+
if (depth < 0) return null;
108+
}
109+
return relativePath;
102110
}
103111

104112
export function isAbsolutePath(value: string): boolean {

0 commit comments

Comments
 (0)