Skip to content

Commit ab2d2b8

Browse files
authored
fix(trigger-e2e): preserve mirror push diagnostics (#1601)
Remove ambient Git tracing instead of setting non-empty false-like values, and retain the redacted error tail when output is truncated. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 0fe13dc9-ae18-4e54-b587-a53f5afb28f2
1 parent f32f86f commit ab2d2b8

2 files changed

Lines changed: 61 additions & 9 deletions

File tree

scripts/ado-script/src/trigger-e2e/__tests__/mirror.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, it, vi } from "vitest";
22

33
import {
44
loadMirrorSyncConfig,
5+
mirrorGitEnv,
56
mirrorRepoUrl,
67
runMirrorSyncPreflight,
78
type MirrorGitRequest,
@@ -41,6 +42,25 @@ function successRunner(calls: MirrorGitRequest[]): MirrorGitRunner {
4142
}
4243

4344
describe("trigger mirror sync", () => {
45+
it("removes ambient git tracing while preserving bearer auth", () => {
46+
expect(
47+
mirrorGitEnv(
48+
{ GIT_CONFIG_COUNT: "1", GIT_CONFIG_VALUE_0: "Authorization: bearer secret" },
49+
{
50+
PATH: "/bin",
51+
GIT_TRACE: "1",
52+
GIT_TRACE_CURL: "true",
53+
GIT_CURL_VERBOSE: "1",
54+
},
55+
),
56+
).toEqual({
57+
PATH: "/bin",
58+
GIT_CONFIG_COUNT: "1",
59+
GIT_CONFIG_VALUE_0: "Authorization: bearer secret",
60+
GIT_TERMINAL_PROMPT: "0",
61+
});
62+
});
63+
4464
it("is disabled unless explicitly opted in", async () => {
4565
const runner = vi.fn<MirrorGitRunner>();
4666
const result = await runMirrorSyncPreflight({}, () => {}, runner);
@@ -123,6 +143,27 @@ describe("trigger mirror sync", () => {
123143
expect(calls.some((call) => call.args[0] === "ls-remote")).toBe(false);
124144
});
125145

146+
it("reports the actionable error tail and redacts the bearer", async () => {
147+
const runner: MirrorGitRunner = async (request) => {
148+
if (request.args[0] === "rev-parse" && request.args[1] === "--is-shallow-repository") {
149+
return { status: 0, stdout: "false\n", stderr: "" };
150+
}
151+
if (request.args[0] === "rev-parse") {
152+
return { status: 0, stdout: `${HEAD}\n`, stderr: "" };
153+
}
154+
return {
155+
status: 1,
156+
stdout: "",
157+
stderr: `${"trace noise\n".repeat(300)}Authorization: bearer secret-token\nfatal: non-fast-forward`,
158+
};
159+
};
160+
161+
const result = await runMirrorSyncPreflight(baseEnv(), () => {}, runner);
162+
expect(result?.ok).toBe(false);
163+
expect(result?.message).toContain("fatal: non-fast-forward");
164+
expect(result?.message).not.toContain("secret-token");
165+
});
166+
126167
it("fails when the verified remote SHA differs", async () => {
127168
const runner: MirrorGitRunner = async (request) => {
128169
if (request.args[0] === "rev-parse" && request.args[1] === "--is-shallow-repository") {

scripts/ado-script/src/trigger-e2e/mirror.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ export interface MirrorGitResult {
3333

3434
export type MirrorGitRunner = (request: MirrorGitRequest) => Promise<MirrorGitResult>;
3535

36+
export function mirrorGitEnv(
37+
requestEnv: Record<string, string>,
38+
baseEnv: NodeJS.ProcessEnv = process.env,
39+
): NodeJS.ProcessEnv {
40+
const env: NodeJS.ProcessEnv = {
41+
...baseEnv,
42+
...requestEnv,
43+
GIT_TERMINAL_PROMPT: "0",
44+
};
45+
// Git treats any non-empty trace value — including "0" — as enabled.
46+
// Remove ambient tracing completely so bearer-bearing HTTP headers cannot
47+
// enter captured stderr or displace the actionable error tail.
48+
delete env.GIT_TRACE;
49+
delete env.GIT_TRACE_CURL;
50+
delete env.GIT_CURL_VERBOSE;
51+
return env;
52+
}
53+
3654
function cleanVar(raw: string | undefined): string | undefined {
3755
const value = raw?.trim();
3856
if (!value || /^\$\(.*\)$/.test(value)) return undefined;
@@ -84,14 +102,7 @@ function runGit(request: MirrorGitRequest): Promise<MirrorGitResult> {
84102
return new Promise((resolve, reject) => {
85103
const child = spawn("git", request.args, {
86104
cwd: request.cwd,
87-
env: {
88-
...process.env,
89-
...request.env,
90-
GIT_TERMINAL_PROMPT: "0",
91-
GIT_TRACE: "0",
92-
GIT_TRACE_CURL: "0",
93-
GIT_CURL_VERBOSE: "0",
94-
},
105+
env: mirrorGitEnv(request.env),
95106
});
96107
let stdout = "";
97108
let stderr = "";
@@ -142,7 +153,7 @@ function diagnostic(result: MirrorGitResult, secret: string): string {
142153
if (!text) return "(no output)";
143154
return text.length <= MAX_DIAGNOSTIC_CHARS
144155
? text
145-
: `${text.slice(0, MAX_DIAGNOSTIC_CHARS)}`;
156+
: `${text.slice(-MAX_DIAGNOSTIC_CHARS)}`;
146157
}
147158

148159
async function git(

0 commit comments

Comments
 (0)