|
7 | 7 | ensureTailscaleEndpoint, |
8 | 8 | resetGmailSetupUtilsCachesForTest, |
9 | 9 | resolvePythonExecutablePath, |
| 10 | + runGcloud, |
10 | 11 | } from "./gmail-setup-utils.js"; |
11 | 12 |
|
12 | 13 | const itUnix = process.platform === "win32" ? it.skip : it; |
@@ -63,6 +64,89 @@ describe("resolvePythonExecutablePath", () => { |
63 | 64 | ); |
64 | 65 | }); |
65 | 66 |
|
| 67 | +describe("runGcloud", () => { |
| 68 | + itUnix( |
| 69 | + "overrides an inherited CLOUDSDK_PYTHON value with a resolved interpreter", |
| 70 | + async () => { |
| 71 | + const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-gcloud-python-")); |
| 72 | + try { |
| 73 | + const realPython = path.join(tmp, "python-real"); |
| 74 | + await fs.writeFile(realPython, "#!/bin/sh\nexit 0\n", "utf-8"); |
| 75 | + await fs.chmod(realPython, 0o755); |
| 76 | + |
| 77 | + const shimDir = path.join(tmp, "shims"); |
| 78 | + await fs.mkdir(shimDir, { recursive: true }); |
| 79 | + const shim = path.join(shimDir, "python3"); |
| 80 | + await fs.writeFile(shim, "#!/bin/sh\nexit 0\n", "utf-8"); |
| 81 | + await fs.chmod(shim, 0o755); |
| 82 | + |
| 83 | + await withEnvAsync( |
| 84 | + { |
| 85 | + CLOUDSDK_PYTHON: path.join(tmp, "evil", "python"), |
| 86 | + PATH: `${shimDir}${path.delimiter}/usr/bin`, |
| 87 | + }, |
| 88 | + async () => { |
| 89 | + runCommandWithTimeoutMock |
| 90 | + .mockResolvedValueOnce({ |
| 91 | + stdout: `${realPython}\n`, |
| 92 | + stderr: "", |
| 93 | + code: 0, |
| 94 | + signal: null, |
| 95 | + killed: false, |
| 96 | + }) |
| 97 | + .mockResolvedValueOnce({ |
| 98 | + stdout: "", |
| 99 | + stderr: "", |
| 100 | + code: 0, |
| 101 | + signal: null, |
| 102 | + killed: false, |
| 103 | + }); |
| 104 | + |
| 105 | + await runGcloud(["config", "list"]); |
| 106 | + |
| 107 | + expect(runCommandWithTimeoutMock).toHaveBeenLastCalledWith( |
| 108 | + ["gcloud", "config", "list"], |
| 109 | + { |
| 110 | + timeoutMs: 120_000, |
| 111 | + env: { CLOUDSDK_PYTHON: realPython }, |
| 112 | + }, |
| 113 | + ); |
| 114 | + }, |
| 115 | + ); |
| 116 | + } finally { |
| 117 | + await fs.rm(tmp, { recursive: true, force: true }); |
| 118 | + } |
| 119 | + }, |
| 120 | + 60_000, |
| 121 | + ); |
| 122 | + |
| 123 | + itUnix("unsets inherited CLOUDSDK_PYTHON when no trusted interpreter is found", async () => { |
| 124 | + await withEnvAsync( |
| 125 | + { |
| 126 | + CLOUDSDK_PYTHON: "/tmp/attacker-python", |
| 127 | + PATH: "", |
| 128 | + }, |
| 129 | + async () => { |
| 130 | + runCommandWithTimeoutMock.mockResolvedValueOnce({ |
| 131 | + stdout: "", |
| 132 | + stderr: "", |
| 133 | + code: 0, |
| 134 | + signal: null, |
| 135 | + killed: false, |
| 136 | + }); |
| 137 | + |
| 138 | + await runGcloud(["config", "list"]); |
| 139 | + |
| 140 | + expect(runCommandWithTimeoutMock).toHaveBeenCalledTimes(1); |
| 141 | + expect(runCommandWithTimeoutMock).toHaveBeenCalledWith(["gcloud", "config", "list"], { |
| 142 | + timeoutMs: 120_000, |
| 143 | + env: { CLOUDSDK_PYTHON: undefined }, |
| 144 | + }); |
| 145 | + }, |
| 146 | + ); |
| 147 | + }); |
| 148 | +}); |
| 149 | + |
66 | 150 | describe("ensureTailscaleEndpoint", () => { |
67 | 151 | it("includes stdout and exit code when tailscale serve fails", async () => { |
68 | 152 | runCommandWithTimeoutMock |
|
0 commit comments