Skip to content

Commit 568ffc1

Browse files
committed
fix(provider): withhold multiline credentials in ACP exit diagnostics
1 parent 1288e33 commit 568ffc1

7 files changed

Lines changed: 57 additions & 23 deletions

File tree

‎apps/server/src/provider/acp/AcpJsonRpcConnection.test.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,13 +364,20 @@ describe("AcpSessionRuntime", () => {
364364
it.effect.each([
365365
{
366366
name: "a final diagnostic",
367+
prefix: "api_key=",
367368
suffix: "\nFinal synthetic crash diagnostic.",
368-
expected: "Final synthetic crash diagnostic.",
369+
expected: undefined,
370+
},
371+
{ name: "no complete line", prefix: "api_key=", suffix: "", expected: undefined },
372+
{
373+
name: "a clipped private-key header",
374+
prefix: "-----BEGIN PRIVATE KEY-----\n",
375+
suffix: "\n-----END PRIVATE KEY-----",
376+
expected: undefined,
369377
},
370-
{ name: "no complete line", suffix: "", expected: undefined },
371378
])("omits a credential cut by the stderr tail boundary with $name", (testCase) =>
372379
Effect.gen(function* () {
373-
const stderr = `api_key=${"fixturecredential".repeat(600)}${testCase.suffix}`;
380+
const stderr = `${testCase.prefix}${"fixturecredential".repeat(600)}${testCase.suffix}`;
374381
const runtime = yield* AcpSessionRuntime.make({
375382
...mockRuntimeOptions,
376383
spawn: {

‎docs/internals/effect-services.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ optional-service layer. Don't route around the layer with an imperative runtime.
135135
lives only in `cause`. Expose a category, length, count, or a URL's protocol and host instead.
136136
`DeviceBootError.diagnostics` is a deliberate exception: authorized device users and agents
137137
need the device hub's launch command and failure text to diagnose host setup. It is capped at
138-
2,000 characters and included in the error message; other service errors keep the rule above.
138+
2,000 characters and included in the error message. ACP process-exit failures may include a
139+
sanitized stderr excerpt within the 4,096-character failure-message cap: credential-labelled
140+
lines and private-key bodies are withheld, and capped raw tails are omitted because a
141+
credential header may have been clipped. Other service errors keep the rule above.
139142
- **Translation.** Construct the error where the failure happens, and map it to a transport error
140143
only in the transport. A translation boundary passes through domain errors already in the target
141144
channel and wraps only unknown or lower-level failures. Map each failure where its context is

‎packages/provider-acp/src/server/AcpSessionRuntime.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,14 +1298,11 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
12981298
Effect.ignore,
12991299
Effect.andThen(Ref.get(stderrTailRef)),
13001300
Effect.map((tail) => {
1301-
// A clipped first line can contain a credential whose identifying prefix was discarded.
1302-
const completeTail =
1301+
// A capped tail can start inside a multiline credential whose header was discarded.
1302+
const stderr =
13031303
tail.length < ACP_STDERR_TAIL_MAX_CHARS
1304-
? tail
1305-
: tail.includes("\n")
1306-
? tail.slice(tail.indexOf("\n") + 1)
1307-
: "";
1308-
const stderr = sanitizeAcpStderrExcerpt(completeTail, homeDirectory);
1304+
? sanitizeAcpStderrExcerpt(tail, homeDirectory)
1305+
: "";
13091306
return stderr.length === 0
13101307
? error
13111308
: new EffectAcpErrors.AcpProcessExitedError({

‎packages/provider-acp/src/server/stderr.test.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@ describe("AcpStderr", () => {
2727
expect(excerpt).not.toContain("sk-abcdefghijklmnopqrstuv");
2828
});
2929

30-
it("redacts complete API keys whose synthetic bodies contain underscores", () => {
31-
const excerpt = sanitizeAcpStderrExcerpt(
32-
"openai=sk-proj-fixture_abc_def_0123456789\nanthropic=sk-ant-api03-fixture_abc_def_0123456789-AA",
33-
"fixture-home",
34-
{},
35-
);
36-
expect(excerpt).toBe("openai=[redacted]\nanthropic=[redacted]");
30+
it("withholds multiline private-key bodies, including unterminated keys", () => {
31+
for (const end of ["-----END PRIVATE KEY-----", ""]) {
32+
const excerpt = sanitizeAcpStderrExcerpt(
33+
`Configuration failed.\n-----BEGIN PRIVATE KEY-----\nZml4dHVyZS1wcml2YXRlLWJvZHk=\n${end}`,
34+
"/home/server",
35+
{},
36+
);
37+
expect(excerpt).toContain("Configuration failed.");
38+
expect(excerpt).not.toContain("Zml4dHVyZS1wcml2YXRlLWJvZHk=");
39+
}
3740
});
3841

3942
it("redacts hyphenated OpenAI project keys and header credentials", () => {
@@ -44,6 +47,7 @@ describe("AcpStderr", () => {
4447
"anthropic=sk-ant-api03-abcdefghijklmnopqrstuvwxyz012345",
4548
"Authorization: Basic dXNlcjpwYXNz",
4649
"x-api-key: ant-api-key-value",
50+
"github_pat_fixture_123 gho_fixture123 ghu_fixture123 ghs_fixture123 ghr_fixture123",
4751
].join("\n"),
4852
"/Users/ada",
4953
{},
@@ -58,6 +62,8 @@ describe("AcpStderr", () => {
5862
expect(excerpt).not.toContain("abcdefghijklmnopqrstuvwxyz012345");
5963
expect(excerpt).not.toContain("dXNlcjpwYXNz");
6064
expect(excerpt).not.toContain("ant-api-key-value");
65+
expect(excerpt).not.toContain("fixture123");
66+
expect(excerpt).not.toContain("fixture_123");
6167
});
6268

6369
it("redacts the whole key when its body contains underscores", () => {

‎packages/provider-acp/src/server/stderr.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const BEARER_TOKEN_PATTERN = /\bBearer\s+[A-Za-z0-9._\-+=/]+/gi;
66
const BASIC_AUTH_PATTERN = /\bAuthorization:\s*Basic\s+\S+/gi;
77
const API_KEY_HEADER_PATTERN = /\bx-api-key:\s*\S+/gi;
88
const SECRET_TOKEN_PATTERN =
9-
/\b(?:sk-[A-Za-z0-9][A-Za-z0-9_-]{7,}|ghp_[A-Za-z0-9]+|xox[a-zA-Z]-[A-Za-z0-9-]+)\b/g;
9+
/\b(?:sk-[A-Za-z0-9][A-Za-z0-9_-]{7,}|github_pat_[A-Za-z0-9_]+|gh[pousr]_[A-Za-z0-9]+|xox[a-zA-Z]-[A-Za-z0-9-]+)\b/g;
1010

1111
export function appendAcpStderrTail(current: string, chunk: string): string {
1212
const next = `${current}${chunk}`;
@@ -18,7 +18,9 @@ export function sanitizeAcpStderrExcerpt(
1818
homeDirectory: string,
1919
environment: NodeJS.ProcessEnv = process.env,
2020
): string {
21-
let result = text.replaceAll("\0", "");
21+
let result = text
22+
.replaceAll("\0", "")
23+
.replace(/-----BEGIN [^-\r\n]*PRIVATE KEY-----[\s\S]*/giu, "[redacted private key]");
2224
const homes = [environment.HOME, environment.USERPROFILE, homeDirectory].filter(
2325
(value): value is string => typeof value === "string" && value.length > 1,
2426
);

‎packages/provider-core/src/server/failure.test.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,32 @@ it("withholds whole ACP stderr lines containing credential markers", () => {
136136
}
137137
});
138138

139+
it("withholds multiline private-key bodies, including unterminated keys", () => {
140+
for (const end of ["-----END PRIVATE KEY-----", ""]) {
141+
const failure = makeProviderFailure({
142+
cause: {
143+
_tag: "AcpProcessExitedError",
144+
code: 23,
145+
stderr: `Synthetic configuration failed.\n-----BEGIN PRIVATE KEY-----\nZml4dHVyZS1wcml2YXRlLWJvZHk=\n${end}`,
146+
},
147+
});
148+
assert.include(failure.message, "ACP process exited with code 23");
149+
assert.notInclude(failure.message, "Zml4dHVyZS1wcml2YXRlLWJvZHk=");
150+
}
151+
});
152+
139153
it("redacts standalone service tokens in ACP exit diagnostics", () => {
140154
const failure = makeProviderFailure({
141155
cause: {
142156
_tag: "AcpProcessExitedError",
143157
code: 23,
144-
stderr: "Synthetic failure: ghp_fixture0123456789 xoxb-fixture-0123456789",
158+
stderr:
159+
"Synthetic failure: ghp_fixture0123456789 xoxb-fixture-0123456789 github_pat_fixture_123 gho_fixture123 ghu_fixture123 ghs_fixture123 ghr_fixture123",
145160
},
146161
});
147162
assert.equal(
148163
failure.message,
149-
"ACP process exited with code 23\nSynthetic failure: [REDACTED] [REDACTED]",
164+
"ACP process exited with code 23\nSynthetic failure: [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED] [REDACTED]",
150165
);
151166
});
152167

‎packages/provider-core/src/server/failure.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ function replaceUnsafeControlCharacters(value: string): string {
196196

197197
function redactProviderFailureText(value: string): string {
198198
return replaceUnsafeControlCharacters(value)
199+
.replace(/-----BEGIN [^-\r\n]*PRIVATE KEY-----[\s\S]*/giu, "[REDACTED private key]")
199200
.replace(/\bhttps?:\/\/[^\s<>"']+/giu, redactUrl)
200201
.replace(/\b(Bearer|Basic)\s+[^\s,;]+/giu, "$1 [REDACTED]")
201202
.replace(
@@ -207,7 +208,10 @@ function redactProviderFailureText(value: string): string {
207208
"$1[REDACTED]",
208209
)
209210
.replace(/\bsk-[A-Za-z0-9_-]{16,}\b/gu, "[REDACTED]")
210-
.replace(/\b(?:ghp_[A-Za-z0-9]+|xox[a-zA-Z]-[A-Za-z0-9-]+)\b/gu, "[REDACTED]")
211+
.replace(
212+
/\b(?:github_pat_[A-Za-z0-9_]+|gh[pousr]_[A-Za-z0-9]+|xox[a-zA-Z]-[A-Za-z0-9-]+)\b/gu,
213+
"[REDACTED]",
214+
)
211215
.trim();
212216
}
213217

0 commit comments

Comments
 (0)