Skip to content

The agent that grows with you

fix(desktop): silence trusted simple-git binary warning

Fabio Mottae7585e05ad0e
diff --git a/apps/desktop/electron/git-review-ops.test.ts b/apps/desktop/electron/git-review-ops.test.ts
index 5aa195af515..346543789c1 100644
--- a/apps/desktop/electron/git-review-ops.test.ts
+++ b/apps/desktop/electron/git-review-ops.test.ts
@@ -4,9 +4,18 @@ import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
+import simpleGit from 'simple-git'
import { afterEach, test, vi } from 'vitest'
-import { gitFor, repoStatus, resolveRenamePath, REVIEW_FILE_CAP, reviewCreatePr, reviewList } from './git-review-ops'
+import {
+ gitFor,
+ repoStatus,
+ resolveRenamePath,
+ REVIEW_FILE_CAP,
+ reviewCreatePr,
+ reviewList,
+ SIMPLE_GIT_UNSAFE_BINARY_WARN
+} from './git-review-ops'
// `runGh` shells to the `gh` CLI via execFile. Mock it so reviewCreatePr's gh
// invocation is controllable (real `gh` may be absent or slow in CI) while the
@@ -47,6 +56,54 @@ test('gitFor accepts an internally resolved git binary path containing spaces',
assert.doesNotThrow(() => gitFor(process.cwd(), 'C:\\Program Files\\Git\\cmd\\git.exe'))
})
+test('gitFor accepts internally resolved git paths with restricted non-space characters', () => {
+ // simple-git's whitelist is `/^([a-z]:)?([a-z0-9/.\_~-]+)$/i`, so parentheses
+ // (`Program Files (x86)`), `+`, and accented profile dirs (`C:\Users\João\...`)
+ // are rejected exactly like a space — a `/\s/` guess still throws on them.
+ const restrictedBinaries = [
+ String.raw`C:\Git(x86)\cmd\git.exe`,
+ String.raw`C:\tools\git+portable\cmd\git.exe`,
+ String.raw`C:\Users\João\AppData\Local\hermes\git\cmd\git.exe`
+ ]
+
+ for (const binary of restrictedBinaries) {
+ assert.doesNotThrow(() => gitFor(process.cwd(), binary), `should accept ${binary}`)
+ }
+})
+
+test('gitFor suppresses only the known custom-binary warning and restores console.warn', () => {
+ const spacedBin = String.raw`C:\Program Files\Git\cmd\git.exe`
+ // `windowsGitHost()` resolves nothing in this process (no configured roots, no
+ // HERMES_DESKTOP_PYTHON), so `gitBin` itself is what simple-git validates — the
+ // spaced `Program Files` path, which warns once per factory call.
+ const warnings: unknown[][] = []
+ const originalWarn = console.warn
+
+ const recordingWarn = (...args: unknown[]) => {
+ warnings.push(args)
+ }
+
+ console.warn = recordingWarn
+
+ try {
+ for (let i = 0; i < 5; i += 1) {
+ gitFor(process.cwd(), spacedBin)
+ }
+
+ assert.equal(console.warn, recordingWarn)
+
+ // The escape hatch used directly still warns: the message gitFor filters is a
+ // live emission of the installed simple-git, so the filter cannot go stale
+ // silently (an upgrade that rewords it fails this test, not production).
+ simpleGit({ baseDir: process.cwd(), binary: spacedBin, unsafe: { allowUnsafeCustomBinary: true } })
+ console.warn('unrelated warning')
+ } finally {
+ console.warn = originalWarn
+ }
+
+ assert.deepEqual(warnings, [[SIMPLE_GIT_UNSAFE_BINARY_WARN], ['unrelated warning']])
+})
+
test('resolveRenamePath: simple rename resolves to the new path', () => {
assert.equal(resolveRenamePath('old.ts => new.ts'), 'new.ts')
})
diff --git a/apps/desktop/electron/git-review-ops.ts b/apps/desktop/electron/git-review-ops.ts
index 73b4ca09296..af9fcf8b4d7 100644
--- a/apps/desktop/electron/git-review-ops.ts
+++ b/apps/desktop/electron/git-review-ops.ts
@@ -19,6 +19,17 @@ const REVIEW_FILE_CAP = 2_000
const UNTRACKED_LINE_COUNT_CONCURRENCY = 16
const UNTRACKED_LINE_COUNT_MAX_BYTES = 1024 * 1024
+// simple-git 3.x validates a custom binary path against an ASCII whitelist —
+// `isBadArgument` in node_modules/simple-git/dist/cjs/index.js accepts only
+// `/^([a-z]:)?([a-z0-9/.\_~-]+)$/i`. Everything outside it is "restricted": a
+// space in the default `C:\Program Files\Git\...`, the parentheses in
+// `Program Files (x86)`, the accented user name in `C:\Users\João\...`. Without
+// the escape hatch simple-git THROWS on such a path; with it, it console.warns
+// this exact message — the flag only downgrades the throw. Exported so gitFor
+// and its tests share one source of truth.
+export const SIMPLE_GIT_UNSAFE_BINARY_WARN =
+ 'Invalid value supplied for custom binary, restricted characters must be removed or supply the unsafe.allowUnsafeCustomBinary option'
+
// GUI-launched Electron apps on macOS inherit only a minimal PATH (no
// /opt/homebrew/bin or /usr/local/bin), so `gh` — and the `git` gh shells out
// to — aren't found. Augment PATH with the resolved gh dir + the common
@@ -47,33 +58,58 @@ function runGh(args, cwd, ghBin): Promise<{ ok: boolean; stdout: string; stderr:
function gitFor(cwd, gitBin) {
// `gitBin` is resolved inside the Electron main process from known install
- // locations or PATH — never renderer/user input. simple-git's custom-binary
- // validation rejects paths containing spaces (the default Windows install is
- // `C:\Program Files\Git\cmd\git.exe`), which silently broke the Review pane.
- // For spaced paths, opt into simple-git's trusted-binary escape hatch instead
- // of falling back to PATH (often absent in GUI-launched apps, and PATH lookup
- // could resolve a repo-local git.exe).
+ // locations or PATH — never renderer/user input, and the Windows tuple below is
+ // built there too: `gitBin` is the resolved install/path and the python host is
+ // this process's own interpreter. simple-git's ASCII whitelist accepts
+ // backslashes but rejects every character outside it — the space in the default
+ // `C:\Program Files\Git\cmd\git.exe`, the parentheses in `Program Files (x86)`,
+ // the accent in `C:\Users\João\...` — and without the escape hatch it THROWS
+ // inside the factory, silently breaking the Review pane. Key the hatch on "we
+ // resolved this binary ourselves", not on a `/\s/` guess, which still throws for
+ // every other restricted character. Falling back to PATH instead is not an
+ // option (often absent in GUI-launched apps, and a PATH lookup could resolve a
+ // repo-local git.exe).
// On Windows the binary tuple is [python.exe, host script]. simple-git has no
// creationFlags slot; the script spawns the real git with CREATE_NO_WINDOW and
// forwards argv unchanged.
const host = windowsGitHost()
const binary = simpleGitBinary(gitBin, host)
- const binaryParts = Array.isArray(binary) ? binary : [binary]
- const unsafe = binaryParts.some(part => /\s/.test(part)) || Boolean(gitBin && /\s/.test(gitBin))
-
- const git = simpleGit({
- baseDir: cwd,
- binary,
- maxConcurrentProcesses: 4,
- trimmed: false,
- ...(unsafe ? { unsafe: { allowUnsafeCustomBinary: true } } : {})
- })
+ const unsafe = Boolean(gitBin) || Array.isArray(binary)
+
+ const makeGit = () =>
+ simpleGit({
+ baseDir: cwd,
+ binary,
+ maxConcurrentProcesses: 4,
+ trimmed: false,
+ ...(unsafe ? { unsafe: { allowUnsafeCustomBinary: true } } : {})
+ })
+
+ if (!unsafe) {
+ return makeGit()
+ }
- if (Array.isArray(binary)) {
- return git.env(noConsoleGitEnv(process.env, gitBin || 'git'))
+ // With the escape hatch set, simple-git still console.warns that same message on
+ // every factory call — the flag only downgrades the throw to a warning. The
+ // binary was resolved by this process, never supplied by the renderer, so on
+ // Windows (where the standard git lives under a spaced "Program Files") that
+ // warning is pure console spam. Filter exactly that message, for the
+ // synchronous factory call only.
+ const originalWarn = console.warn
+
+ console.warn = (message?: unknown, ...rest: unknown[]) => {
+ if (typeof message !== 'string' || !message.startsWith(SIMPLE_GIT_UNSAFE_BINARY_WARN)) {
+ originalWarn(message, ...rest)
+ }
}
- return git
+ try {
+ const git = makeGit()
+
+ return Array.isArray(binary) ? git.env(noConsoleGitEnv(process.env, gitBin || 'git')) : git
+ } finally {
+ console.warn = originalWarn
+ }
}
// simple-git reports renames as `old => new` (and `dir/{old => new}/f`); resolve