git-snapshot-hardening.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { execFile as execFileCallback } from "node:child_process";
  2. import { existsSync } from "node:fs";
  3. import { chmod, mkdir, mkdtemp, realpath, rm, writeFile } from "node:fs/promises";
  4. import { tmpdir } from "node:os";
  5. import { join } from "node:path";
  6. import { promisify } from "node:util";
  7. import { afterAll, beforeAll, describe, expect, it } from "vitest";
  8. import { resolveGitSnapshot } from "../src/context/git-snapshot.js";
  9. const execFile = promisify(execFileCallback);
  10. // 用真实 git 验证加固确实生效:仓库配置里的 fsmonitor 不再被 ZCode 自己执行,
  11. // 继承来的 GIT_DIR 不再把快照指向另一个仓库。
  12. async function gitAvailable(): Promise<boolean> {
  13. try {
  14. await execFile("git", ["--version"]);
  15. return true;
  16. } catch {
  17. return false;
  18. }
  19. }
  20. async function git(cwd: string, args: string[], env?: NodeJS.ProcessEnv): Promise<string> {
  21. const { stdout } = await execFile("git", args, { cwd, env: env ?? process.env });
  22. return stdout.trim();
  23. }
  24. const runnable = process.platform !== "win32" && (await gitAvailable());
  25. describe.skipIf(!runnable)("automatic git hardening with a real git", () => {
  26. let root: string;
  27. let repo: string;
  28. let otherRepo: string;
  29. let marker: string;
  30. beforeAll(async () => {
  31. root = await realpath(await mkdtemp(join(tmpdir(), "zcode-git-hardening-")));
  32. repo = join(root, "repo");
  33. otherRepo = join(root, "other");
  34. marker = join(root, "fsmonitor-ran");
  35. for (const [directory, branch] of [
  36. [repo, "alpha"],
  37. [otherRepo, "beta"],
  38. ] as const) {
  39. await mkdir(directory, { recursive: true });
  40. await git(directory, ["init", "-q"]);
  41. await git(directory, ["symbolic-ref", "HEAD", `refs/heads/${branch}`]);
  42. }
  43. const script = join(root, "fsmonitor.sh");
  44. await writeFile(script, `#!/bin/sh\ntouch "${marker}"\nexit 1\n`);
  45. await chmod(script, 0o755);
  46. await writeFile(join(repo, "a.txt"), "a\n");
  47. await git(repo, ["add", "a.txt"]);
  48. await git(repo, ["config", "core.fsmonitor", script]);
  49. });
  50. afterAll(async () => {
  51. await rm(root, { recursive: true, force: true });
  52. });
  53. it("does not run the repository's fsmonitor program", async (context) => {
  54. await rm(marker, { force: true });
  55. // 对照:不加固的 git status 会执行仓库配置的 fsmonitor。不执行时这个 git 测不了,跳过。
  56. await git(repo, ["status", "--short"]).catch(() => "");
  57. if (!existsSync(marker)) context.skip();
  58. await rm(marker, { force: true });
  59. const snapshot = await resolveGitSnapshot(repo);
  60. expect(snapshot).toMatchObject({ isGitRepository: true, gitBranch: "alpha" });
  61. expect(snapshot.gitStatusLines).toEqual(["A a.txt"]);
  62. expect(existsSync(marker)).toBe(false);
  63. });
  64. it("reads the working directory's repository even when GIT_DIR is inherited", async () => {
  65. const inherited = { ...process.env, GIT_DIR: join(otherRepo, ".git") };
  66. // 对照:带着继承的 GIT_DIR,裸 git 读的是另一个仓库。
  67. expect(await git(repo, ["symbolic-ref", "--short", "-q", "HEAD"], inherited)).toBe("beta");
  68. const saved = process.env.GIT_DIR;
  69. process.env.GIT_DIR = inherited.GIT_DIR;
  70. try {
  71. expect((await resolveGitSnapshot(repo)).gitBranch).toBe("alpha");
  72. } finally {
  73. if (saved === undefined) delete process.env.GIT_DIR;
  74. else process.env.GIT_DIR = saved;
  75. }
  76. });
  77. });