From 77a64493bbdc664609f4b55d1f041ff441550d03 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Mar 2026 12:05:40 -0700 Subject: [PATCH] Harden git commit extra flags - Move extra commit args after message flags to avoid swallowing `-m` - Drop non-flag tokens when tokenizing user-supplied commit flags - Add tests and clarify the settings helper text --- apps/server/src/git/Layers/GitCore.test.ts | 87 ++++++++++++++++++++++ apps/server/src/git/Layers/GitCore.ts | 5 +- apps/server/src/git/Layers/GitManager.ts | 14 +++- apps/web/src/routes/_chat.settings.tsx | 4 +- 4 files changed, 105 insertions(+), 5 deletions(-) diff --git a/apps/server/src/git/Layers/GitCore.test.ts b/apps/server/src/git/Layers/GitCore.test.ts index 6b5bc0232f..c1a6aa9984 100644 --- a/apps/server/src/git/Layers/GitCore.test.ts +++ b/apps/server/src/git/Layers/GitCore.test.ts @@ -12,6 +12,7 @@ import { GitCoreLive } from "./GitCore.ts"; import { GitCore, type GitCoreShape } from "../Services/GitCore.ts"; import { GitCommandError } from "../Errors.ts"; import { type ProcessRunResult, runProcess } from "../../processRunner.ts"; +import { tokenizeCommitFlags } from "./GitManager.ts"; // ── Helpers ── @@ -1903,4 +1904,90 @@ it.layer(TestLayer)("git integration", (it) => { }), ); }); + + describe("tokenizeCommitFlags", () => { + it("returns empty array for undefined", () => { + expect(tokenizeCommitFlags(undefined)).toEqual([]); + }); + + it("returns empty array for empty string", () => { + expect(tokenizeCommitFlags("")).toEqual([]); + }); + + it("returns empty array for whitespace-only string", () => { + expect(tokenizeCommitFlags(" ")).toEqual([]); + }); + + it("tokenizes a single flag", () => { + expect(tokenizeCommitFlags("--no-gpg-sign")).toEqual(["--no-gpg-sign"]); + }); + + it("tokenizes multiple flags separated by spaces", () => { + expect(tokenizeCommitFlags("--no-gpg-sign --signoff")).toEqual([ + "--no-gpg-sign", + "--signoff", + ]); + }); + + it("handles extra whitespace between flags", () => { + expect(tokenizeCommitFlags(" --no-gpg-sign --signoff ")).toEqual([ + "--no-gpg-sign", + "--signoff", + ]); + }); + + it("keeps flags with = values intact", () => { + expect(tokenizeCommitFlags("--cleanup=verbatim")).toEqual(["--cleanup=verbatim"]); + }); + + it("drops tokens that do not start with a dash", () => { + expect(tokenizeCommitFlags("--author Foo")).toEqual(["--author"]); + }); + + it("drops all non-flag tokens from a mixed input", () => { + expect(tokenizeCommitFlags("HEAD --no-verify somefile.txt -S")).toEqual([ + "--no-verify", + "-S", + ]); + }); + }); + + describe("commit with extraArgs", () => { + it.effect("passes extra flags through to git commit", () => + Effect.gen(function* () { + const tmp = yield* makeTmpDir(); + yield* initRepoWithCommit(tmp); + const core = yield* GitCore; + + yield* writeTextFile(path.join(tmp, "extra.txt"), "extra args test\n"); + yield* git(tmp, ["add", "extra.txt"]); + + const { commitSha } = yield* core.commit(tmp, "test extra args", "", [ + "--no-gpg-sign", + ]); + expect(commitSha).toBeTruthy(); + + // Verify the commit was created with the right message + const logOutput = yield* git(tmp, ["log", "-1", "--pretty=%s"]); + expect(logOutput).toBe("test extra args"); + }), + ); + + it.effect("works without extra args (backward compat)", () => + Effect.gen(function* () { + const tmp = yield* makeTmpDir(); + yield* initRepoWithCommit(tmp); + const core = yield* GitCore; + + yield* writeTextFile(path.join(tmp, "compat.txt"), "compat test\n"); + yield* git(tmp, ["add", "compat.txt"]); + + const { commitSha } = yield* core.commit(tmp, "no extra args", ""); + expect(commitSha).toBeTruthy(); + + const logOutput = yield* git(tmp, ["log", "-1", "--pretty=%s"]); + expect(logOutput).toBe("no extra args"); + }), + ); + }); }); diff --git a/apps/server/src/git/Layers/GitCore.ts b/apps/server/src/git/Layers/GitCore.ts index 9070d295f3..2f86e58d8a 100644 --- a/apps/server/src/git/Layers/GitCore.ts +++ b/apps/server/src/git/Layers/GitCore.ts @@ -806,11 +806,14 @@ const makeGitCore = Effect.gen(function* () { const commit: GitCoreShape["commit"] = (cwd, subject, body, extraArgs = []) => Effect.gen(function* () { - const args = ["commit", ...extraArgs, "-m", subject]; + const args = ["commit", "-m", subject]; const trimmedBody = body.trim(); if (trimmedBody.length > 0) { args.push("-m", trimmedBody); } + // Append extra flags after the message args so an argument-consuming + // flag (e.g. `-C`) cannot accidentally swallow `-m` or the subject. + args.push(...extraArgs); yield* runGit("GitCore.commit.commit", cwd, args); const commitSha = yield* runGitStdout("GitCore.commit.revParseHead", cwd, [ "rev-parse", diff --git a/apps/server/src/git/Layers/GitManager.ts b/apps/server/src/git/Layers/GitManager.ts index 2b248d3c64..9ed2add0f1 100644 --- a/apps/server/src/git/Layers/GitManager.ts +++ b/apps/server/src/git/Layers/GitManager.ts @@ -207,11 +207,21 @@ interface CommitAndBranchSuggestion { commitMessage: string; } -function tokenizeCommitFlags(rawFlags?: string): string[] { - return (rawFlags ?? "") +/** + * Tokenize and sanitise user-provided extra flags for `git commit`. + * + * Each token must look like a CLI flag (start with `-`). Tokens that don't + * start with a dash are silently dropped so that a malformed value like + * `--author "Foo"` (which the naive split turns into `["--author", "Foo"]`) + * cannot inject a positional argument into the git invocation. + */ +export function tokenizeCommitFlags(rawFlags?: string): string[] { + const tokens = (rawFlags ?? "") .trim() .split(/\s+/g) .filter((value) => value.length > 0); + + return tokens.filter((token) => token.startsWith("-")); } function formatCommitMessage(subject: string, body: string): string { diff --git a/apps/web/src/routes/_chat.settings.tsx b/apps/web/src/routes/_chat.settings.tsx index e2a43e59ff..50537c94f7 100644 --- a/apps/web/src/routes/_chat.settings.tsx +++ b/apps/web/src/routes/_chat.settings.tsx @@ -567,8 +567,8 @@ function SettingsRouteView() { spellCheck={false} /> - Applied to app-run git commit commands only. Example: --no-gpg-sign - . Quoted arguments are not supported yet. + Applied to app-run git commit commands only. Example:{" "} + --no-gpg-sign. Quoted arguments are not supported yet.