tool-security.test.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import {
  2. chmod,
  3. copyFile,
  4. mkdir,
  5. mkdtemp,
  6. readFile,
  7. realpath,
  8. rm,
  9. symlink,
  10. writeFile,
  11. } from "node:fs/promises";
  12. import os from "node:os";
  13. import path from "node:path";
  14. import { afterEach, expect, test, vi } from "vitest";
  15. import { run } from "../src/lib/run.js";
  16. import { androidEnv, checkTool, findTool, javaHome, sdkRoot } from "../src/providers/sdk.js";
  17. import { preflight } from "../src/providers/preflight.js";
  18. const windows = process.platform === "win32";
  19. const originalCwd = process.cwd();
  20. let fixtureRoot: string | undefined;
  21. afterEach(async () => {
  22. process.chdir(originalCwd);
  23. vi.unstubAllEnvs();
  24. vi.restoreAllMocks();
  25. if (fixtureRoot) await rm(fixtureRoot, { recursive: true, force: true });
  26. fixtureRoot = undefined;
  27. });
  28. async function fixture() {
  29. const root = await mkdtemp(path.join(os.tmpdir(), "android-tool-security-"));
  30. fixtureRoot = root;
  31. const workspace = path.join(root, "workspace");
  32. const sdk = path.join(root, "sdk");
  33. const bin = path.join(root, "bin");
  34. const marker = path.join(root, "executed.txt");
  35. await Promise.all([workspace, sdk, bin].map((dir) => mkdir(dir)));
  36. process.chdir(workspace);
  37. vi.spyOn(os, "homedir").mockReturnValue(root);
  38. for (const key of [
  39. "ANDROID_HOME",
  40. "ANDROID_SDK_ROOT",
  41. "JAVA_HOME",
  42. "LOCALAPPDATA",
  43. "ProgramFiles",
  44. ]) {
  45. vi.stubEnv(key, "");
  46. }
  47. vi.stubEnv("ANDROID_PLUGIN_SDK_PATH", sdk);
  48. vi.stubEnv("ANDROID_PLUGIN_JDK_MAJOR", "99999");
  49. vi.stubEnv("PATH", windows ? process.env.SystemRoot + "\\System32" : "/usr/bin:/bin");
  50. vi.stubEnv("ANDROID_TEST_MARKER", marker);
  51. if (windows) {
  52. const payload = path.join(root, "marker.cjs");
  53. await writeFile(
  54. payload,
  55. 'require("node:fs").writeFileSync(process.env.ANDROID_TEST_MARKER, "executed"); process.exit(0);',
  56. );
  57. vi.stubEnv("NODE_OPTIONS", `--require "${payload}"`);
  58. }
  59. return { root, workspace, sdk, bin, marker };
  60. }
  61. async function fakeExecutable(stem: string) {
  62. const file = windows ? `${stem}.exe` : stem;
  63. await mkdir(path.dirname(file), { recursive: true });
  64. if (windows) await copyFile(process.execPath, file);
  65. else {
  66. await writeFile(
  67. file,
  68. '#!/bin/sh\nprintf executed > "$ANDROID_TEST_MARKER"\nprintf "fake version\\n"\n',
  69. );
  70. await chmod(file, 0o755);
  71. }
  72. return file;
  73. }
  74. async function trustedExecutable(stem: string) {
  75. const file = windows ? `${stem}.cmd` : stem;
  76. await mkdir(path.dirname(file), { recursive: true });
  77. await writeFile(
  78. file,
  79. windows ? "@echo trusted-tool\r\n" : "#!/bin/sh\nprintf 'trusted-tool\\n'\n",
  80. );
  81. await chmod(file, 0o755);
  82. return file;
  83. }
  84. async function expectNotExecuted(marker: string) {
  85. await expect(readFile(marker, "utf8")).rejects.toMatchObject({ code: "ENOENT" });
  86. }
  87. test("SDK tools take priority over workspace and external PATH executables", async () => {
  88. const { workspace, sdk, bin, marker } = await fixture();
  89. await fakeExecutable(path.join(workspace, "adb"));
  90. await fakeExecutable(path.join(bin, "adb"));
  91. const trusted = await trustedExecutable(path.join(sdk, "platform-tools", "adb"));
  92. vi.stubEnv("PATH", [".", bin, process.env.PATH].join(path.delimiter));
  93. const result = await checkTool("adb", ["version"]);
  94. await expectNotExecuted(marker);
  95. expect(result).toMatchObject({ ok: true, path: await realpath(trusted), detail: "trusted-tool" });
  96. });
  97. test("PATH lookup skips empty, relative and workspace entries without invoking a locator", async () => {
  98. const { workspace, bin, marker } = await fixture();
  99. await fakeExecutable(path.join(workspace, "where"));
  100. await fakeExecutable(path.join(workspace, "which"));
  101. await fakeExecutable(path.join(workspace, "audit-tool"));
  102. const trusted = await trustedExecutable(path.join(bin, "audit-tool"));
  103. vi.stubEnv("PATH", ["", ".", "subdir", workspace, bin, process.env.PATH].join(path.delimiter));
  104. const result = await checkTool("audit-tool");
  105. await expectNotExecuted(marker);
  106. expect(result.path).toBe(await realpath(trusted));
  107. const env = await androidEnv();
  108. expect(env.PATH?.split(path.delimiter)).not.toEqual(
  109. expect.arrayContaining(["", ".", "subdir", workspace]),
  110. );
  111. });
  112. test("an external PATH link into the project is never executed", async () => {
  113. const { root, workspace, marker } = await fixture();
  114. const fake = await fakeExecutable(path.join(workspace, "audit-tool"));
  115. const link = path.join(root, "linked-bin");
  116. if (windows) await symlink(workspace, link, "junction");
  117. else {
  118. await mkdir(link);
  119. await symlink(fake, path.join(link, "audit-tool"));
  120. }
  121. vi.stubEnv("PATH", [link, process.env.PATH].join(path.delimiter));
  122. await checkTool("audit-tool");
  123. await expectNotExecuted(marker);
  124. expect(await findTool("audit-tool")).toBeUndefined();
  125. });
  126. test("configured SDK and JDK inside the project are rejected", async () => {
  127. const { workspace, marker } = await fixture();
  128. await fakeExecutable(path.join(workspace, "platform-tools", "adb"));
  129. await fakeExecutable(path.join(workspace, "bin", "java"));
  130. vi.stubEnv("ANDROID_PLUGIN_SDK_PATH", workspace);
  131. vi.stubEnv("JAVA_HOME", workspace);
  132. const resolvedSdk = await sdkRoot();
  133. expect(resolvedSdk ? await realpath(resolvedSdk) : undefined).not.toBe(await realpath(workspace));
  134. expect(await javaHome()).toBeUndefined();
  135. await expectNotExecuted(marker);
  136. });
  137. test("preflight does not execute PATH Java when no valid JDK is available", async () => {
  138. const { workspace, sdk, marker } = await fixture();
  139. for (const tool of [
  140. "platform-tools/adb",
  141. "emulator/emulator",
  142. "cmdline-tools/latest/bin/sdkmanager",
  143. "cmdline-tools/latest/bin/avdmanager",
  144. ]) {
  145. await trustedExecutable(path.join(sdk, tool));
  146. }
  147. await fakeExecutable(path.join(workspace, "java"));
  148. vi.stubEnv("PATH", [".", process.env.PATH].join(path.delimiter));
  149. const result = await preflight();
  150. await expectNotExecuted(marker);
  151. expect(result.checks.find((check) => check.name === "Java")).toMatchObject({ ok: false });
  152. });
  153. test("explicit project scripts can still execute", async () => {
  154. const { workspace } = await fixture();
  155. const wrapper = await trustedExecutable(path.join(workspace, "gradlew"));
  156. expect(await run([wrapper], { cwd: workspace })).toMatchObject({
  157. code: 0,
  158. stdout: windows ? "trusted-tool\r\n" : "trusted-tool\n",
  159. });
  160. });
  161. test("Java SDK scripts require a validated JDK instead of running their Java fallback", async () => {
  162. const { root, workspace, sdk, marker } = await fixture();
  163. const tool = await trustedExecutable(
  164. path.join(sdk, "cmdline-tools", "latest", "bin", "sdkmanager"),
  165. );
  166. await fakeExecutable(path.join(workspace, "java"));
  167. vi.stubEnv("PATH", [".", process.env.PATH].join(path.delimiter));
  168. expect(await findTool("sdkmanager")).toBeUndefined();
  169. const jdk = path.join(root, "jdk");
  170. const java = path.join(jdk, "bin", windows ? "java.exe" : "java");
  171. await mkdir(path.dirname(java), { recursive: true });
  172. if (windows) await copyFile(process.execPath, java);
  173. else await trustedExecutable(java);
  174. vi.stubEnv("JAVA_HOME", jdk);
  175. expect(await javaHome()).toBe(await realpath(jdk));
  176. expect(await findTool("sdkmanager")).toBe(await realpath(tool));
  177. await expectNotExecuted(marker);
  178. });
  179. test.runIf(windows)("batch execution never launches workspace cmd.exe", async () => {
  180. const { workspace, bin, marker } = await fixture();
  181. await fakeExecutable(path.join(workspace, "cmd"));
  182. const script = await trustedExecutable(path.join(bin, "tool"));
  183. expect(await run([script], { cwd: workspace })).toMatchObject({
  184. code: 0,
  185. stdout: "trusted-tool\r\n",
  186. });
  187. await expectNotExecuted(marker);
  188. });