|
1 | 1 | import { afterEach, describe, expect, spyOn, test } from "bun:test"; |
2 | | -import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; |
3 | 3 | import { tmpdir } from "node:os"; |
4 | 4 | import { isAbsolute, join, posix, win32 } from "node:path"; |
5 | 5 | import * as serviceModule from "../src/service"; |
6 | 6 | import { saveConfig } from "../src/config"; |
7 | 7 | import { windowsEnvIndirectBatchValue } from "../src/lib/win-paths"; |
8 | 8 | import { assertServiceAuthEnvironment, assertServiceEnvironmentMatchesInstall, bakedServicePathsDiagnostic, confirmServiceServing, launchdListenPort, systemdListenPort, buildPlist, buildUnit, buildWindowsLauncherVbs, buildWindowsSchtasksCreateArgs, buildWindowsSchtasksCreateArgsForXml, buildWindowsServiceScript, buildWindowsTaskXml, deriveWindowsServiceDiagnostic, installFreshWindowsSchedulerSafely, installServiceSafely, launchctlLoadFailed, launchdJobMatchesPlist, normalizeServiceSubcommand, parseServiceInstallState, prepareServiceInstall, readWindowsSchedulerXmlState, registerFreshWindowsSchedulerTask, removeNativeWindowsServiceForScheduler, repairService, resolveServiceListenPort, runLaunchctl, serviceLogPath, serviceStartableFromTray, serviceStatusReport, serviceRetryCommand, serviceStatusSummary, systemdNeedsDaemonReload, windowsListenPort, winswListenPort, startLaunchd, windowsTaskRegistrationHealthy } from "../src/service"; |
9 | 9 | import type { ServiceDiagnostic } from "../src/service"; |
10 | | -import { resolvedProxyEnv } from "../src/service"; |
| 10 | +import { resolvedProxyEnv, writeServiceDefinitionFile } from "../src/service"; |
11 | 11 | import { buildWinswXml } from "../src/lib/winsw"; |
12 | 12 | import { CONFIG_OWNER_FILE, CONFIG_UNINSTALL_MANIFEST, recordOwnedConfigPath, removeOwnedConfigState } from "../src/lib/config-ownership"; |
13 | 13 | import { serviceApiTokenFilePath } from "../src/lib/service-secrets"; |
@@ -157,6 +157,23 @@ describe("systemd service unit", () => { |
157 | 157 | expect(unit).not.toContain("http_proxy="); |
158 | 158 | }); |
159 | 159 |
|
| 160 | + test("the Windows wrapper bakes proxy env the same way the unit and plist do (#2107)", () => { |
| 161 | + // This builder was the only one of the three with no proxy assertion, because the only way |
| 162 | + // to reach it was to assign process.env — the pattern that leaked HTTP_PROXY across files. |
| 163 | + const script = buildWindowsServiceScript( |
| 164 | + { bun: "C:\\OpenCodex\\bun.exe", bunRuntimeSource: "bundled", cli: "C:\\OpenCodex\\cli.ts" }, |
| 165 | + 10100, |
| 166 | + resolvedProxyEnv({ HTTP_PROXY: "http://127.0.0.1:7890", no_proxy: "localhost" }), |
| 167 | + ); |
| 168 | + |
| 169 | + expect(script).toContain("HTTP_PROXY=http://127.0.0.1:7890"); |
| 170 | + // Lower-case spellings are baked under the canonical name, never both. |
| 171 | + expect(script).toContain("NO_PROXY=localhost"); |
| 172 | + expect(script).not.toContain("no_proxy="); |
| 173 | + expect(script).not.toContain("HTTPS_PROXY="); |
| 174 | + }); |
| 175 | + |
| 176 | + |
160 | 177 | test("preserves custom Codex and OpenCodex homes", () => { |
161 | 178 | const oldCodexHome = process.env.CODEX_HOME; |
162 | 179 | const oldCodexSqliteHome = process.env.CODEX_SQLITE_HOME; |
@@ -199,7 +216,9 @@ describe("systemd service unit", () => { |
199 | 216 | expect(startSystemd).toContain("ocx service install"); |
200 | 217 | expect(startSystemd).toContain("process.exit(1)"); |
201 | 218 |
|
202 | | - const writeAt = installSystemd.indexOf('writeFileSync(unitPath(), buildUnit(), "utf8")'); |
| 219 | + // The write goes through writeServiceDefinitionFile so the unit lands 0600: it can carry a |
| 220 | + // proxy credential (#2107). What this test pins is the ORDER — write, then reload. |
| 221 | + const writeAt = installSystemd.indexOf('writeServiceDefinitionFile(unitPath(), buildUnit(), "utf8")'); |
203 | 222 | const reloadAt = installSystemd.indexOf("systemctl --user daemon-reload"); |
204 | 223 | const enableAt = installSystemd.indexOf("systemctl --user enable"); |
205 | 224 | const restartAt = installSystemd.indexOf("systemctl --user restart"); |
@@ -2152,3 +2171,52 @@ describe("service serving confirmation", () => { |
2152 | 2171 | }); |
2153 | 2172 | }); |
2154 | 2173 | }); |
| 2174 | + |
| 2175 | +// #2107 baked the outbound proxy environment into the installed service definition, and a |
| 2176 | +// proxy URL routinely carries user:password. That made these files credential-bearing, so |
| 2177 | +// they must not be written at the umask default. |
| 2178 | +describe("service definitions are not world-readable", () => { |
| 2179 | + const modeOf = (path: string): string => (statSync(path).mode & 0o777).toString(8); |
| 2180 | + |
| 2181 | + test("a freshly written definition is owner-only", () => { |
| 2182 | + const dir = mkdtempSync(join(tmpdir(), "ocx-service-mode-")); |
| 2183 | + try { |
| 2184 | + const path = join(dir, "unit"); |
| 2185 | + writeServiceDefinitionFile(path, buildUnit(resolvedProxyEnv({ HTTP_PROXY: "http://u:[email protected]:7890" })), "utf8"); |
| 2186 | + |
| 2187 | + expect(modeOf(path)).toBe("600"); |
| 2188 | + // The credential is still written — this test pins who can read it, not that it is absent. |
| 2189 | + expect(readFileSync(path, "utf8")).toContain("u:[email protected]"); |
| 2190 | + } finally { |
| 2191 | + rmSync(dir, { recursive: true, force: true }); |
| 2192 | + } |
| 2193 | + }); |
| 2194 | + |
| 2195 | + test("an install over a loose definition from an older version tightens it", () => { |
| 2196 | + // `mode` applies only on creation, so a reinstall would otherwise leave 0644 standing. |
| 2197 | + const dir = mkdtempSync(join(tmpdir(), "ocx-service-mode-")); |
| 2198 | + try { |
| 2199 | + const path = join(dir, "plist"); |
| 2200 | + writeFileSync(path, "stale", { encoding: "utf8", mode: 0o644 }); |
| 2201 | + expect(modeOf(path)).toBe("644"); |
| 2202 | + |
| 2203 | + writeServiceDefinitionFile(path, buildPlist(resolvedProxyEnv({})), "utf8"); |
| 2204 | + |
| 2205 | + expect(modeOf(path)).toBe("600"); |
| 2206 | + } finally { |
| 2207 | + rmSync(dir, { recursive: true, force: true }); |
| 2208 | + } |
| 2209 | + }); |
| 2210 | + |
| 2211 | + test("utf16le scheduler assets take the same mode", () => { |
| 2212 | + const dir = mkdtempSync(join(tmpdir(), "ocx-service-mode-")); |
| 2213 | + try { |
| 2214 | + const path = join(dir, "task.xml"); |
| 2215 | + writeServiceDefinitionFile(path, "\uFEFF<Task />", "utf16le"); |
| 2216 | + |
| 2217 | + expect(modeOf(path)).toBe("600"); |
| 2218 | + } finally { |
| 2219 | + rmSync(dir, { recursive: true, force: true }); |
| 2220 | + } |
| 2221 | + }); |
| 2222 | +}); |
0 commit comments