|
8 | 8 | type Mock, |
9 | 9 | } from "vitest"; |
10 | 10 | import fs, { type FileHandle } from "fs/promises"; |
11 | | -import type { Dirent, PathLike, Stats } from "fs"; |
| 11 | +import type { BigIntStats, Dirent, PathLike, Stats } from "fs"; |
12 | 12 | import path from "path"; |
13 | 13 | import { |
14 | 14 | // Pure utility functions |
@@ -54,6 +54,15 @@ const mockReaddirNames = vi.mocked( |
54 | 54 | fs.readdir as (path: PathLike) => Promise<string[]>, |
55 | 55 | ); |
56 | 56 |
|
| 57 | +// The case respelling (#470) lstats with { bigint: true }, to compare inodes |
| 58 | +// Windows reports as 64-bit file IDs without losing precision. |
| 59 | +const mockLstatBigInt = vi.mocked( |
| 60 | + fs.lstat as ( |
| 61 | + path: PathLike, |
| 62 | + options: { bigint: true }, |
| 63 | + ) => Promise<BigIntStats>, |
| 64 | +); |
| 65 | + |
57 | 66 | interface MockFileHandle { |
58 | 67 | read: Mock; |
59 | 68 | close: Mock; |
@@ -429,6 +438,93 @@ describe("Lib Functions", () => { |
429 | 438 | process.cwd = originalCwd; |
430 | 439 | } |
431 | 440 | }); |
| 441 | + |
| 442 | + // #470: the allowed directory's own name spelled in a letter case that |
| 443 | + // matches neither the configured nor the on-disk spelling. It is |
| 444 | + // respelled only where the file system confirms, component by |
| 445 | + // component, that both spellings name one entry. |
| 446 | + describe("letter case of an allowed directory's own name (#470)", () => { |
| 447 | + const win = process.platform === "win32"; |
| 448 | + const configured = win ? "C:\\Users\\test" : "/home/user"; |
| 449 | + const requestedFile = win |
| 450 | + ? "C:\\USERS\\Test\\file.txt" |
| 451 | + : "/HOME/User/file.txt"; |
| 452 | + const respelled = path.join(configured, "file.txt"); |
| 453 | + // Each directory the walk lists, as the configured spelling names it. |
| 454 | + const listings: Record<string, string[]> = win |
| 455 | + ? { "C:\\": ["Users"], "C:\\Users": ["test"] } |
| 456 | + : { "/": ["home"], "/home": ["user"] }; |
| 457 | + |
| 458 | + // lstat on a case-insensitive file system: any spelling of a listed |
| 459 | + // entry is that entry, one inode per lowercased path. |
| 460 | + function foldingLstat() { |
| 461 | + mockLstatBigInt.mockImplementation( |
| 462 | + async (p: PathLike) => |
| 463 | + ({ |
| 464 | + dev: 1n, |
| 465 | + ino: BigInt(p.toString().toLowerCase().length), |
| 466 | + }) as BigIntStats, |
| 467 | + ); |
| 468 | + } |
| 469 | + |
| 470 | + beforeEach(() => { |
| 471 | + mockReaddirNames.mockImplementation( |
| 472 | + async (p: PathLike) => listings[p.toString()] ?? [], |
| 473 | + ); |
| 474 | + }); |
| 475 | + |
| 476 | + it("respells the request onto the allowed directory when the file system folds case", async () => { |
| 477 | + foldingLstat(); |
| 478 | + await expect(validatePath(requestedFile, allowedDirs)).resolves.toBe( |
| 479 | + respelled, |
| 480 | + ); |
| 481 | + }); |
| 482 | + |
| 483 | + it("refuses when a directory holds two entries differing only in case", async () => { |
| 484 | + foldingLstat(); |
| 485 | + const [first, second] = win ? ["Users", "test"] : ["home", "user"]; |
| 486 | + const parent = win ? "C:\\" : "/"; |
| 487 | + mockReaddirNames.mockImplementation(async (p: PathLike) => |
| 488 | + p.toString() === parent ? [first, first.toUpperCase()] : [second], |
| 489 | + ); |
| 490 | + await expect( |
| 491 | + validatePath(requestedFile, allowedDirs), |
| 492 | + ).rejects.toThrow("Access denied - path outside allowed directories"); |
| 493 | + }); |
| 494 | + |
| 495 | + it("refuses when the requested spelling does not exist (a case-sensitive directory)", async () => { |
| 496 | + // beforeEach makes every lstat ENOENT. |
| 497 | + await expect( |
| 498 | + validatePath(requestedFile, allowedDirs), |
| 499 | + ).rejects.toThrow("Access denied - path outside allowed directories"); |
| 500 | + }); |
| 501 | + |
| 502 | + it("refuses when the two spellings are different entries", async () => { |
| 503 | + let ino = 0n; |
| 504 | + mockLstatBigInt.mockImplementation( |
| 505 | + async () => ({ dev: 1n, ino: ++ino }) as BigIntStats, |
| 506 | + ); |
| 507 | + await expect( |
| 508 | + validatePath(requestedFile, allowedDirs), |
| 509 | + ).rejects.toThrow("Access denied - path outside allowed directories"); |
| 510 | + }); |
| 511 | + |
| 512 | + it("refuses when a directory cannot be listed", async () => { |
| 513 | + foldingLstat(); |
| 514 | + mockReaddirNames.mockRejectedValue(new Error("EACCES readdir")); |
| 515 | + await expect( |
| 516 | + validatePath(requestedFile, allowedDirs), |
| 517 | + ).rejects.toThrow("Access denied - path outside allowed directories"); |
| 518 | + }); |
| 519 | + |
| 520 | + it("still refuses a name that only folds onto part of an allowed directory", async () => { |
| 521 | + foldingLstat(); |
| 522 | + const parentOnly = win ? "C:\\USERS\\x.txt" : "/HOME/x.txt"; |
| 523 | + await expect(validatePath(parentOnly, allowedDirs)).rejects.toThrow( |
| 524 | + "Access denied - path outside allowed directories", |
| 525 | + ); |
| 526 | + }); |
| 527 | + }); |
432 | 528 | }); |
433 | 529 | }); |
434 | 530 |
|
|
0 commit comments