Skip to content

Commit c4d0cd4

Browse files
juliusmarmingecodex
andcommitted
fix(vcs): redact git command diagnostics
Co-authored-by: codex <[email protected]>
1 parent f9485b2 commit c4d0cd4

3 files changed

Lines changed: 127 additions & 93 deletions

File tree

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
9797
assert.deepInclude(error, {
9898
_tag: "GitCommandError",
9999
operation: "GitVcsDriver.test.missingCwd",
100-
command: "git status --short",
100+
command: "git",
101+
argumentCount: 2,
101102
cwd,
102103
detail: "Failed to spawn Git process.",
103104
});
@@ -109,6 +110,37 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
109110
}),
110111
);
111112

113+
it.effect("does not retain git arguments or stderr in command failures", () =>
114+
Effect.gen(function* () {
115+
const cwd = yield* makeTmpDir();
116+
const driver = yield* GitVcsDriver.GitVcsDriver;
117+
yield* driver.initRepo({ cwd });
118+
119+
const secret = "secret-token-value";
120+
const error = yield* driver
121+
.execute({
122+
operation: "GitVcsDriver.test.redactedFailure",
123+
cwd,
124+
args: ["status", `--unknown-option=${secret}`],
125+
})
126+
.pipe(Effect.flip);
127+
128+
assert.deepInclude(error, {
129+
_tag: "GitCommandError",
130+
operation: "GitVcsDriver.test.redactedFailure",
131+
command: "git",
132+
argumentCount: 2,
133+
cwd,
134+
});
135+
assert.isNumber(error.exitCode);
136+
assert.isAbove(error.stderrLength ?? 0, 0);
137+
assert.notInclude(error.detail, secret);
138+
assert.notInclude(error.message, secret);
139+
assert.notProperty(error, "args");
140+
assert.notProperty(error, "stderr");
141+
}),
142+
);
143+
112144
it.effect("recovers a structurally identified missing cwd as a non-repository", () =>
113145
Effect.gen(function* () {
114146
const parent = yield* makeTmpDir();
@@ -144,7 +176,8 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
144176
assert.deepInclude(error, {
145177
_tag: "GitCommandError",
146178
operation: "GitVcsDriver.removeWorktree",
147-
command: `git worktree remove ${missingWorktree}`,
179+
command: "git",
180+
argumentCount: 3,
148181
cwd,
149182
});
150183
assert.notProperty(error, "cause");

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 86 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,15 @@ function deriveLocalBranchNameFromRemoteRef(branchName: string): string | null {
325325
return localBranch.length > 0 ? localBranch : null;
326326
}
327327

328-
function commandLabel(args: readonly string[]): string {
329-
return `git ${args.join(" ")}`;
328+
function gitCommandContext(
329+
input: Pick<GitVcsDriver.ExecuteGitInput, "operation" | "cwd" | "args">,
330+
) {
331+
return {
332+
operation: input.operation,
333+
command: "git",
334+
cwd: input.cwd,
335+
argumentCount: input.args.length,
336+
} as const;
330337
}
331338

332339
function parseDefaultBranchFromRemoteHeadRef(value: string, remoteName: string): string | null {
@@ -428,7 +435,7 @@ const createTrace2Monitor = Effect.fn("createTrace2Monitor")(function* (
428435
const traceRecord = decodeJsonResult(Trace2Record)(trimmedLine);
429436
if (Result.isFailure(traceRecord)) {
430437
yield* Effect.logDebug(
431-
`GitVcsDriver.trace2: failed to parse trace line for ${commandLabel(input.args)} in ${input.cwd}`,
438+
`GitVcsDriver.trace2: failed to parse trace line for ${input.operation} in ${input.cwd} (${input.args.length} arguments)`,
432439
traceRecord.failure,
433440
);
434441
return;
@@ -591,10 +598,9 @@ const collectOutput = Effect.fnUntraced(function* (
591598
const nextBytes = bytes + chunk.byteLength;
592599
if (!appendTruncationMarker && nextBytes > maxOutputBytes) {
593600
return yield* new GitCommandError({
594-
operation: input.operation,
595-
command: commandLabel(input.args),
596-
cwd: input.cwd,
601+
...gitCommandContext(input),
597602
detail: `Git output exceeded ${maxOutputBytes} bytes and was truncated.`,
603+
outputLength: nextBytes,
598604
});
599605
}
600606

@@ -615,9 +621,7 @@ const collectOutput = Effect.fnUntraced(function* (
615621
Effect.catchTags({
616622
PlatformError: (cause) =>
617623
new GitCommandError({
618-
operation: input.operation,
619-
command: commandLabel(input.args),
620-
cwd: input.cwd,
624+
...gitCommandContext(input),
621625
detail: "Failed to read Git process output.",
622626
cause,
623627
}),
@@ -658,9 +662,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
658662
Effect.mapError(
659663
(cause) =>
660664
new GitCommandError({
661-
operation: commandInput.operation,
662-
command: commandLabel(commandInput.args),
663-
cwd: commandInput.cwd,
665+
...gitCommandContext(commandInput),
664666
detail: "Failed to create Git trace monitor.",
665667
cause,
666668
}),
@@ -681,9 +683,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
681683
Effect.mapError(
682684
(cause) =>
683685
new GitCommandError({
684-
operation: commandInput.operation,
685-
command: commandLabel(commandInput.args),
686-
cwd: commandInput.cwd,
686+
...gitCommandContext(commandInput),
687687
detail: "Failed to spawn Git process.",
688688
cause,
689689
}),
@@ -710,9 +710,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
710710
Effect.mapError(
711711
(cause) =>
712712
new GitCommandError({
713-
operation: commandInput.operation,
714-
command: commandLabel(commandInput.args),
715-
cwd: commandInput.cwd,
713+
...gitCommandContext(commandInput),
716714
detail: "Failed to read Git process exit code.",
717715
cause,
718716
}),
@@ -724,9 +722,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
724722
Effect.mapError(
725723
(cause) =>
726724
new GitCommandError({
727-
operation: commandInput.operation,
728-
command: commandLabel(commandInput.args),
729-
cwd: commandInput.cwd,
725+
...gitCommandContext(commandInput),
730726
detail: "Failed to write Git process input.",
731727
cause,
732728
}),
@@ -738,15 +734,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
738734
yield* trace2Monitor.flush;
739735

740736
if (!input.allowNonZeroExit && exitCode !== 0) {
741-
const trimmedStderr = stderr.text.trim();
742737
return yield* new GitCommandError({
743-
operation: commandInput.operation,
744-
command: commandLabel(commandInput.args),
745-
cwd: commandInput.cwd,
746-
detail:
747-
trimmedStderr.length > 0
748-
? `${commandLabel(commandInput.args)} failed: ${trimmedStderr}`
749-
: `${commandLabel(commandInput.args)} failed with code ${exitCode}.`,
738+
...gitCommandContext(commandInput),
739+
detail: "Git command exited with a non-zero status.",
740+
exitCode,
741+
stdoutLength: stdout.text.length,
742+
stderrLength: stderr.text.length,
750743
});
751744
}
752745

@@ -767,10 +760,8 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
767760
onNone: () =>
768761
Effect.fail(
769762
new GitCommandError({
770-
operation: commandInput.operation,
771-
command: commandLabel(commandInput.args),
772-
cwd: commandInput.cwd,
773-
detail: `${commandLabel(commandInput.args)} timed out.`,
763+
...gitCommandContext(commandInput),
764+
detail: "Git command timed out.",
774765
}),
775766
),
776767
onSome: Effect.succeed,
@@ -823,33 +814,13 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
823814
if (options.allowNonZeroExit || result.exitCode === 0) {
824815
return Effect.succeed(result);
825816
}
826-
const stderr = result.stderr.trim();
827-
if (stderr.length > 0) {
828-
return Effect.fail(
829-
new GitCommandError({
830-
operation,
831-
command: commandLabel(args),
832-
cwd,
833-
detail: stderr,
834-
}),
835-
);
836-
}
837-
if (options.fallbackErrorDetail) {
838-
return Effect.fail(
839-
new GitCommandError({
840-
operation,
841-
command: commandLabel(args),
842-
cwd,
843-
detail: options.fallbackErrorDetail,
844-
}),
845-
);
846-
}
847817
return Effect.fail(
848818
new GitCommandError({
849-
operation,
850-
command: commandLabel(args),
851-
cwd,
852-
detail: `${commandLabel(args)} failed: code=${result.exitCode ?? "null"}`,
819+
...gitCommandContext({ operation, cwd, args }),
820+
detail: options.fallbackErrorDetail ?? "Git command exited with a non-zero status.",
821+
...(result.exitCode === null ? {} : { exitCode: result.exitCode }),
822+
stdoutLength: result.stdout.length,
823+
stderrLength: result.stderr.length,
853824
}),
854825
);
855826
}),
@@ -914,9 +885,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
914885
}
915886

916887
return yield* new GitCommandError({
917-
operation: "GitVcsDriver.renameBranch",
918-
command: commandLabel(["branch", "-m", "--", desiredBranch]),
919-
cwd,
888+
...gitCommandContext({
889+
operation: "GitVcsDriver.renameBranch",
890+
cwd,
891+
args: ["branch", "-m", "--", desiredBranch],
892+
}),
920893
detail: `Could not find an available branch name for '${desiredBranch}'.`,
921894
});
922895
});
@@ -1061,9 +1034,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
10611034
return firstRemote;
10621035
}
10631036
return yield* new GitCommandError({
1064-
operation: "GitVcsDriver.resolvePrimaryRemoteName",
1065-
command: commandLabel(["remote"]),
1066-
cwd,
1037+
...gitCommandContext({
1038+
operation: "GitVcsDriver.resolvePrimaryRemoteName",
1039+
cwd,
1040+
args: ["remote"],
1041+
}),
10671042
detail: "No git remote is configured for this repository.",
10681043
});
10691044
});
@@ -1221,12 +1196,16 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
12211196
return NON_REPOSITORY_REMOTE_STATUS_DETAILS;
12221197
}
12231198
if (branchResult.exitCode !== 0) {
1224-
const stderr = branchResult.stderr.trim();
12251199
return yield* new GitCommandError({
1226-
operation: "GitVcsDriver.statusDetailsRemote.branch",
1227-
command: commandLabel(["rev-parse", "--abbrev-ref", "HEAD"]),
1228-
cwd,
1229-
detail: stderr || "git branch lookup failed",
1200+
...gitCommandContext({
1201+
operation: "GitVcsDriver.statusDetailsRemote.branch",
1202+
cwd,
1203+
args: ["rev-parse", "--abbrev-ref", "HEAD"],
1204+
}),
1205+
detail: "Git branch lookup failed.",
1206+
exitCode: branchResult.exitCode,
1207+
stdoutLength: branchResult.stdout.length,
1208+
stderrLength: branchResult.stderr.length,
12301209
});
12311210
}
12321211

@@ -1337,12 +1316,16 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
13371316
}
13381317

13391318
if (statusResult.exitCode !== 0) {
1340-
const stderr = statusResult.stderr.trim();
13411319
return yield* new GitCommandError({
1342-
operation: "GitVcsDriver.statusDetails.status",
1343-
command: commandLabel(["status", "--porcelain=2", "--branch"]),
1344-
cwd,
1345-
detail: stderr || "git status failed",
1320+
...gitCommandContext({
1321+
operation: "GitVcsDriver.statusDetails.status",
1322+
cwd,
1323+
args: ["status", "--porcelain=2", "--branch"],
1324+
}),
1325+
detail: "Git status failed.",
1326+
exitCode: statusResult.exitCode,
1327+
stdoutLength: statusResult.stdout.length,
1328+
stderrLength: statusResult.stderr.length,
13461329
});
13471330
}
13481331

@@ -1605,9 +1588,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
16051588
const branch = details.branch ?? fallbackBranch;
16061589
if (!branch) {
16071590
return yield* new GitCommandError({
1608-
operation: "GitVcsDriver.pushCurrentBranch",
1609-
command: commandLabel(["push"]),
1610-
cwd,
1591+
...gitCommandContext({
1592+
operation: "GitVcsDriver.pushCurrentBranch",
1593+
cwd,
1594+
args: ["push"],
1595+
}),
16111596
detail: "Cannot push from detached HEAD.",
16121597
});
16131598
}
@@ -1669,9 +1654,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
16691654
const publishRemoteName = yield* resolvePushRemoteName(cwd, branch);
16701655
if (!publishRemoteName) {
16711656
return yield* new GitCommandError({
1672-
operation: "GitVcsDriver.pushCurrentBranch",
1673-
command: commandLabel(["push"]),
1674-
cwd,
1657+
...gitCommandContext({
1658+
operation: "GitVcsDriver.pushCurrentBranch",
1659+
cwd,
1660+
args: ["push"],
1661+
}),
16751662
detail: "Cannot push because no git remote is configured for this repository.",
16761663
});
16771664
}
@@ -1723,17 +1710,21 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
17231710
const refName = details.branch;
17241711
if (!refName) {
17251712
return yield* new GitCommandError({
1726-
operation: "GitVcsDriver.pullCurrentBranch",
1727-
command: commandLabel(["pull", "--ff-only"]),
1728-
cwd,
1713+
...gitCommandContext({
1714+
operation: "GitVcsDriver.pullCurrentBranch",
1715+
cwd,
1716+
args: ["pull", "--ff-only"],
1717+
}),
17291718
detail: "Cannot pull from detached HEAD.",
17301719
});
17311720
}
17321721
if (!details.hasUpstream) {
17331722
return yield* new GitCommandError({
1734-
operation: "GitVcsDriver.pullCurrentBranch",
1735-
command: commandLabel(["pull", "--ff-only"]),
1736-
cwd,
1723+
...gitCommandContext({
1724+
operation: "GitVcsDriver.pullCurrentBranch",
1725+
cwd,
1726+
args: ["pull", "--ff-only"],
1727+
}),
17371728
detail: "Current branch has no upstream configured. Push with upstream first.",
17381729
});
17391730
}
@@ -2019,10 +2010,15 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
20192010
};
20202011
}
20212012
return yield* new GitCommandError({
2022-
operation: "GitVcsDriver.listRefs",
2023-
command: commandLabel(["branch", "--no-color", "--no-column"]),
2024-
cwd: input.cwd,
2025-
detail: stderr || "git branch failed",
2013+
...gitCommandContext({
2014+
operation: "GitVcsDriver.listRefs",
2015+
cwd: input.cwd,
2016+
args: ["branch", "--no-color", "--no-column"],
2017+
}),
2018+
detail: "Git branch listing failed.",
2019+
exitCode: localBranchResult.exitCode,
2020+
stdoutLength: localBranchResult.stdout.length,
2021+
stderrLength: localBranchResult.stderr.length,
20262022
});
20272023
}
20282024

‎packages/contracts/src/git.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,16 @@ export class GitCommandError extends Schema.TaggedErrorClass<GitCommandError>()(
324324
operation: Schema.String,
325325
command: Schema.String,
326326
cwd: Schema.String,
327+
argumentCount: Schema.optional(Schema.Number),
328+
exitCode: Schema.optional(Schema.Number),
329+
stdoutLength: Schema.optional(Schema.Number),
330+
stderrLength: Schema.optional(Schema.Number),
331+
outputLength: Schema.optional(Schema.Number),
327332
detail: Schema.String,
328333
cause: Schema.optional(Schema.Defect()),
329334
}) {
330335
override get message(): string {
331-
return `Git command failed in ${this.operation}: ${this.command} (${this.cwd}) - ${this.detail}`;
336+
return `Git command failed in ${this.operation} (${this.cwd}): ${this.detail}`;
332337
}
333338
}
334339

0 commit comments

Comments
 (0)