staticRoutePathContainment.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { afterEach, describe, expect, it } from "vitest";
  2. import { AddressInfo } from "node:net";
  3. import { mkdtemp, rm, writeFile } from "node:fs/promises";
  4. import { join } from "node:path";
  5. import { tmpdir } from "node:os";
  6. import win32 from "node:path/win32";
  7. import posix from "node:path/posix";
  8. import { ServiceCollection } from "@zcode/services";
  9. import { createHttpServer, isInsideDirectory, isPathDriveQualified } from "../src/http.js";
  10. // 背景:Windows 上 path.resolve('C:\\web', 'Z:/x') 直接切到 Z 盘,而跨盘时
  11. // path.relative 返回的是绝对路径(不以 ".." 开头),旧的越界检查因此把它误判为
  12. // 「静态根目录内」,通配静态路由又不在 token 中间件保护范围内,导致另一块盘上
  13. // 的任意文件可被未授权读取(含 %3A 编码形式与映射的网络盘)。这里固化三道防线:
  14. // 盘符路径前置拒绝、relative 结果为绝对路径一律视为越界、以及原有 ".." 检查回归。
  15. describe("isPathDriveQualified", () => {
  16. it("识别 URL 形式与反斜杠形式的盘符路径", () => {
  17. expect(isPathDriveQualified("Z:/marker.txt")).toBe(true);
  18. expect(isPathDriveQualified("z:\\marker.txt")).toBe(true);
  19. // 无分隔符的盘符相对路径在 win32 resolve 下同样会切盘(resolve 成 Z:\<file>)
  20. expect(isPathDriveQualified("Z:marker.txt")).toBe(true);
  21. });
  22. it("不误伤正常相对路径", () => {
  23. expect(isPathDriveQualified("assets/app.js")).toBe(false);
  24. expect(isPathDriveQualified("assets/Z-report.txt")).toBe(false);
  25. expect(isPathDriveQualified("index.html")).toBe(false);
  26. });
  27. });
  28. describe("isInsideDirectory", () => {
  29. it("win32 跨盘符:relative 返回绝对路径,必须判为越界(回归用例)", () => {
  30. // 修复前:path.win32.relative('C:\\zcode\\web', 'Z:\\marker.txt') === 'Z:\\marker.txt'
  31. // 不以 ".." 开头,被旧实现误判为在目录内
  32. expect(isInsideDirectory("C:\\zcode\\web", "Z:\\marker.txt", win32)).toBe(false);
  33. expect(
  34. isInsideDirectory("C:\\zcode\\web", win32.resolve("C:\\zcode\\web", "index.html"), win32),
  35. ).toBe(true);
  36. });
  37. it("posix 语义保持不变", () => {
  38. expect(isInsideDirectory("/srv/web", "/srv/web/assets/app.js", posix)).toBe(true);
  39. expect(isInsideDirectory("/srv/web", "/srv/web", posix)).toBe(true);
  40. expect(isInsideDirectory("/srv/web", "/srv/etc/passwd", posix)).toBe(false);
  41. expect(isInsideDirectory("/srv/web", "/srv/web/../secret", posix)).toBe(false);
  42. });
  43. });
  44. describe("static route path containment", () => {
  45. const servers: Array<{ close(callback?: (err?: Error) => void): void }> = [];
  46. const tempDirs: string[] = [];
  47. afterEach(async () => {
  48. await Promise.all(
  49. servers.map(
  50. (server) =>
  51. new Promise<void>((resolveClose, reject) => {
  52. server.close((error?: Error) => {
  53. if (error) {
  54. reject(error);
  55. return;
  56. }
  57. resolveClose();
  58. });
  59. }),
  60. ),
  61. );
  62. servers.length = 0;
  63. await Promise.all(tempDirs.map((dir) => rm(dir, { force: true, recursive: true })));
  64. tempDirs.length = 0;
  65. });
  66. async function startStaticServer(): Promise<number> {
  67. const staticRoot = await mkdtemp(join(tmpdir(), "zcode-static-"));
  68. tempDirs.push(staticRoot);
  69. await writeFile(join(staticRoot, "index.html"), "<html>home</html>");
  70. const server = createHttpServer(new ServiceCollection(), 0, {
  71. staticRoot,
  72. spaFallback: true,
  73. });
  74. servers.push(server);
  75. return (server.address() as AddressInfo).port;
  76. }
  77. it("盘符路径(明文与 %3A 编码)一律 404,不再解析到其他盘", async () => {
  78. const port = await startStaticServer();
  79. expect((await fetch(`http://127.0.0.1:${port}/Z:/marker.txt`)).status).toBe(404);
  80. expect((await fetch(`http://127.0.0.1:${port}/Z%3A/marker.txt`)).status).toBe(404);
  81. });
  82. it("编码形式的 .. 在 WHATWG URL 归一化后落回根内,未命中时走 SPA fallback 而非越界", async () => {
  83. const port = await startStaticServer();
  84. const response = await fetch(`http://127.0.0.1:${port}/%2e%2e/secret.txt`);
  85. // new URL() 会把 %2e%2e 折叠为 .. 并归一化路径,路由收到的是根内的 /secret.txt;
  86. // 该文件不存在,落到 SPA fallback 返回 index.html —— 证明响应来自 fallback 而非越界文件
  87. expect(response.status).toBe(200);
  88. expect(await response.text()).toBe("<html>home</html>");
  89. });
  90. });