fix(server): accept canonical Windows path casing
This commit is contained in:
2 files changed
+45
-2
No files matched your search
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -52,13 +52,14 @@ async function openLinuxPath(
|
||||
async function verifyFallbackHandle(
|
||||
absolutePath: string,
|
||||
handle: NodeFSP.FileHandle,
|
||||
platform: NodeJS.Platform,
|
||||
): Promise<NodeFSP.FileHandle> {
|
||||
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(
|
||||
|
||||
Reference in new issue
Block a user