TypeScript
fix(web): ../ file links outside the workspace open as host files (#22853)
Theo Browneedce5f0e53e8
diff --git a/packages/shared/src/path.test.ts b/packages/shared/src/path.test.tsindex 32cdb8ab2c8..ad7f88c1bd0 100644--- a/packages/shared/src/path.test.ts+++ b/packages/shared/src/path.test.ts@@ -43,6 +43,10 @@ describe("workspaceRelativeFilePath", () => {["/Repo/Project/src/main.ts", "/repo/project", null],["C:/USERS/mike/t3code/main.ts", "c:/users/MIKE/t3code", "main.ts"],["/repo/project-two/a.ts", "/repo/project", null],+ // regression: #16354 a `..` that leaves the root is a host path, not a workspace path+ ["/repo/project/../other/notes.md", "/repo/project", null],+ ["C:\\repo\\project\\src\\..\\..\\notes.md", "C:\\repo\\project", null],+ ["/repo/project/docs/a/../b.md", "/repo/project", "docs/a/../b.md"],])("relates %s to %s", (path, workspaceRoot, relativePath) => {expect(workspaceRelativeFilePath(path, workspaceRoot)).toBe(relativePath);});diff --git a/packages/shared/src/path.ts b/packages/shared/src/path.tsindex 62dd11d9129..ba06f352cb1 100644--- a/packages/shared/src/path.ts+++ b/packages/shared/src/path.ts@@ -98,7 +98,15 @@ export function workspaceRelativeFilePath(const rootForCompare = caseInsensitive ? normalizedRoot.toLowerCase() : normalizedRoot;if (pathForCompare.replace(/\/+$/, "") === rootForCompare) return ".";if (!pathForCompare.startsWith(`${rootForCompare}/`)) return null;- return normalizedPath.slice(normalizedRoot.length + 1);+ const relativePath = normalizedPath.slice(normalizedRoot.length + 1);+ // `/repo/../x` starts with the root but escapes it; the server rejects that as a workspace path.+ let depth = 0;+ for (const segment of relativePath.split("/")) {+ if (segment === "..") depth -= 1;+ else if (segment.length > 0 && segment !== ".") depth += 1;+ if (depth < 0) return null;+ }+ return relativePath;}export function isAbsolutePath(value: string): boolean {