Skip to content

Commit 0720c1f

Browse files
committed
fix: sanitize restart handoff diagnostics
1 parent 6d485a9 commit 0720c1f

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/infra/restart-handoff.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,26 @@ describe("gateway restart handoff", () => {
271271
"Recent restart handoff: full-process via launchd; source=plugin-change; reason=plugin source changed; pid=12345; age=2s; expiresIn=57s",
272272
);
273273
});
274+
275+
it("formats restart reasons as a single diagnostic line", () => {
276+
expect(
277+
formatGatewayRestartHandoffDiagnostic(
278+
{
279+
kind: GATEWAY_SUPERVISOR_RESTART_HANDOFF_KIND,
280+
version: 1,
281+
intentId: "intent-1",
282+
pid: 12_345,
283+
createdAt: 10_000,
284+
expiresAt: 70_000,
285+
reason: "ok\nFake: bad",
286+
source: "operator-restart",
287+
restartKind: "full-process",
288+
supervisorMode: "external",
289+
},
290+
12_500,
291+
),
292+
).toBe(
293+
"Recent restart handoff: full-process via external; source=operator-restart; reason=ok Fake: bad; pid=12345; age=2s; expiresIn=57s",
294+
);
295+
});
274296
});

src/infra/restart-handoff.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,22 @@ function formatShortDuration(ms: number): string {
5353
return remainingSeconds === 0 ? `${minutes}m` : `${minutes}m ${remainingSeconds}s`;
5454
}
5555

56+
function formatDiagnosticValue(value: string): string {
57+
return value
58+
.replace(/[\u0000-\u001f\u007f]+/gu, " ")
59+
.replace(/\s+/gu, " ")
60+
.trim();
61+
}
62+
5663
export function formatGatewayRestartHandoffDiagnostic(
5764
handoff: GatewayRestartHandoff,
5865
now = Date.now(),
5966
): string {
67+
const reason = handoff.reason ? formatDiagnosticValue(handoff.reason) : undefined;
6068
const detail = [
6169
`${handoff.restartKind} via ${handoff.supervisorMode}`,
6270
`source=${handoff.source}`,
63-
handoff.reason ? `reason=${handoff.reason}` : undefined,
71+
reason ? `reason=${reason}` : undefined,
6472
`pid=${handoff.pid}`,
6573
`age=${formatShortDuration(now - handoff.createdAt)}`,
6674
`expiresIn=${formatShortDuration(handoff.expiresAt - now)}`,

0 commit comments

Comments
 (0)