diff --git a/apps/server/src/noFollowFile.test.ts b/apps/server/src/noFollowFile.test.ts new file mode 100644 index 0000000000..011f98ae6c --- /dev/null +++ b/apps/server/src/noFollowFile.test.ts @@ -0,0 +1,31 @@ +import { describe, expect, it } from "@effect/vitest"; + +import { canonicalPathsMatch } from "./noFollowFile.ts"; + +describe("canonicalPathsMatch", () => { + it("accepts canonical Windows paths whose casing differs", () => { + expect( + canonicalPathsMatch( + String.raw`C:\Users\Example\capture.png`, + String.raw`C:\Users\example\CAPTURE.png`, + "win32", + ), + ).toBe(true); + }); + + it("keeps canonical path comparison case-sensitive on other platforms", () => { + expect( + canonicalPathsMatch("/Users/Example/capture.png", "/users/example/capture.png", "darwin"), + ).toBe(false); + }); + + it("rejects Windows paths that differ by more than casing", () => { + expect( + canonicalPathsMatch( + String.raw`C:\Users\Example\capture.png`, + String.raw`C:\Users\Example\outside.png`, + "win32", + ), + ).toBe(false); + }); +}); diff --git a/apps/server/src/noFollowFile.ts b/apps/server/src/noFollowFile.ts index b86f13a533..bc1f26fe7a 100644 --- a/apps/server/src/noFollowFile.ts +++ b/apps/server/src/noFollowFile.ts @@ -52,13 +52,14 @@ async function openLinuxPath( async function verifyFallbackHandle( absolutePath: string, handle: NodeFSP.FileHandle, + platform: NodeJS.Platform, ): Promise { try { const handleInfo = await handle.stat(); const canonicalPath = await NodeFS.promises.realpath(absolutePath); const pathInfo = await NodeFS.promises.stat(absolutePath); if ( - canonicalPath !== absolutePath || + !canonicalPathsMatch(absolutePath, canonicalPath, platform) || pathInfo.dev !== handleInfo.dev || pathInfo.ino !== handleInfo.ino ) { @@ -71,6 +72,17 @@ async function verifyFallbackHandle( } } +export function canonicalPathsMatch( + absolutePath: string, + canonicalPath: string, + platform: NodeJS.Platform, +): boolean { + if (canonicalPath === absolutePath) { + return true; + } + return platform === "win32" && canonicalPath.toLowerCase() === absolutePath.toLowerCase(); +} + async function openPathNoFollow( absolutePath: string, flags: number, @@ -92,7 +104,7 @@ async function openPathNoFollow( // only when its identity still matches the canonical pathname. const noFollow = NodeFS.constants.O_NOFOLLOW ?? 0; const handle = await NodeFS.promises.open(absolutePath, flags | noFollow, mode); - return verifyFallbackHandle(absolutePath, handle); + return verifyFallbackHandle(absolutePath, handle, platform); } export async function openReadableFileNoFollow(