Skip to content

Commit 54eadc2

Browse files
authored
fix(security): unify secret redaction in public comment outputs (#1693)
Ensure all public issue, PR, and inline comments apply redactSecrets() in addition to sanitizeContent() before submitting payloads to the GitHub API. This aligns public comment output with error log and step-summary redaction policies, preventing potential leakage of Anthropic API keys, AWS credentials, Slack tokens, JWTs, and GitHub tokens.
1 parent d40ddef commit 54eadc2

5 files changed

Lines changed: 96 additions & 6 deletions

File tree

‎src/entrypoints/post-buffered-inline-comments.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
import { readFileSync } from "fs";
1313
import { createOctokit } from "../github/api/client";
14+
import { redactSecrets } from "../github/utils/sanitizer";
1415

1516
const BUFFER_PATH = "/tmp/inline-comments-buffer.jsonl";
1617

@@ -120,7 +121,7 @@ async function postComment(
120121
owner,
121122
repo,
122123
pull_number,
123-
body: c.body,
124+
body: redactSecrets(c.body),
124125
path: c.path,
125126
side: c.side || "RIGHT",
126127
commit_id: c.commit_id || headSha,

‎src/mcp/github-comment-server.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { z } from "zod";
66
import { GITHUB_API_URL } from "../github/api/config";
77
import { Octokit } from "@octokit/rest";
88
import { updateClaudeComment } from "../github/operations/comments/update-claude-comment";
9-
import { sanitizeContent } from "../github/utils/sanitizer";
9+
import { redactSecrets, sanitizeContent } from "../github/utils/sanitizer";
1010

1111
// Get repository information from environment variables
1212
const REPO_OWNER = process.env.REPO_OWNER;
@@ -55,7 +55,7 @@ server.tool(
5555
const isPullRequestReviewComment =
5656
eventName === "pull_request_review_comment";
5757

58-
const sanitizedBody = sanitizeContent(body);
58+
const sanitizedBody = redactSecrets(sanitizeContent(body));
5959

6060
const result = await updateClaudeComment(octokit, {
6161
owner,

‎src/mcp/github-inline-comment-server.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"
44
import { appendFileSync } from "fs";
55
import { z } from "zod";
66
import { createOctokit } from "../github/api/client";
7-
import { sanitizeContent } from "../github/utils/sanitizer";
7+
import { redactSecrets, sanitizeContent } from "../github/utils/sanitizer";
88
import { removeBufferedComment } from "./inline-comment-buffer";
99

1010
// Get repository and PR information from environment variables
@@ -98,8 +98,8 @@ server.tool(
9898
const repo = REPO_NAME;
9999
const pull_number = parseInt(PR_NUMBER, 10);
100100

101-
// Sanitize the comment body to remove any potential GitHub tokens
102-
const sanitizedBody = sanitizeContent(body);
101+
// Sanitize the comment body to remove potential prompt injections and redact secrets
102+
const sanitizedBody = redactSecrets(sanitizeContent(body));
103103

104104
// Validate that either line or both startLine and line are provided
105105
if (!line && !startLine) {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { describe, expect, it } from "bun:test";
2+
import { redactSecrets, sanitizeContent } from "../src/github/utils/sanitizer";
3+
4+
describe("Public Comment Output Sanitization & Redaction", () => {
5+
it("redacts all credential types from public comment output", () => {
6+
const rawComment = [
7+
"Here is the summary of the work done:",
8+
"- GitHub Token: ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
9+
"- Anthropic Key: sk-ant-api03-abcdefghijklmnopqrstuvwxyz1234567890",
10+
"- AWS Access Key: AKIAIOSFODNN7EXAMPLE",
11+
"- Slack Bot Token: xoxb-1234567890-abcdefghijkl-mnopqrstuvwx",
12+
"- JWT Bearer: eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U",
13+
"<!-- Hidden instruction injection -->",
14+
"Invisible\u200Bzero-width chars",
15+
"![Image Alt Injection](https://example.com/pic.png)",
16+
].join("\n");
17+
18+
const sanitizedOutput = redactSecrets(sanitizeContent(rawComment));
19+
20+
// Ensure all secret types are redacted
21+
expect(sanitizedOutput).not.toContain(
22+
"ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
23+
);
24+
expect(sanitizedOutput).not.toContain(
25+
"sk-ant-api03-abcdefghijklmnopqrstuvwxyz1234567890",
26+
);
27+
expect(sanitizedOutput).not.toContain("AKIAIOSFODNN7EXAMPLE");
28+
expect(sanitizedOutput).not.toContain(
29+
"xoxb-1234567890-abcdefghijkl-mnopqrstuvwx",
30+
);
31+
expect(sanitizedOutput).not.toContain(
32+
"eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U",
33+
);
34+
35+
expect(sanitizedOutput).toContain("[REDACTED_GITHUB_TOKEN]");
36+
expect(sanitizedOutput).toContain("[REDACTED_ANTHROPIC_KEY]");
37+
expect(sanitizedOutput).toContain("[REDACTED_AWS_KEY_ID]");
38+
expect(sanitizedOutput).toContain("[REDACTED_SLACK_TOKEN]");
39+
expect(sanitizedOutput).toContain("[REDACTED_JWT]");
40+
41+
// Ensure prompt injection / invisible chars / hidden tags are also sanitized
42+
expect(sanitizedOutput).not.toContain(
43+
"<!-- Hidden instruction injection -->",
44+
);
45+
expect(sanitizedOutput).not.toContain("\u200B");
46+
expect(sanitizedOutput).not.toContain("Image Alt Injection");
47+
expect(sanitizedOutput).toContain("![](https://example.com/pic.png)");
48+
});
49+
50+
it("ensures public comments have the same secret redaction coverage as logs/errors", () => {
51+
const errorDetails =
52+
"Error: failed to connect with sk-ant-abcdefghijklmnopqrstuvwxyz123456 and AKIAIOSFODNN7EXAMPLE";
53+
const commentBody =
54+
"Report: encountered sk-ant-abcdefghijklmnopqrstuvwxyz123456 and AKIAIOSFODNN7EXAMPLE";
55+
56+
const redactedError = redactSecrets(errorDetails);
57+
const redactedComment = redactSecrets(sanitizeContent(commentBody));
58+
59+
expect(redactedError).toContain("[REDACTED_ANTHROPIC_KEY]");
60+
expect(redactedError).toContain("[REDACTED_AWS_KEY_ID]");
61+
expect(redactedComment).toContain("[REDACTED_ANTHROPIC_KEY]");
62+
expect(redactedComment).toContain("[REDACTED_AWS_KEY_ID]");
63+
});
64+
});

‎test/sanitizer.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,3 +518,28 @@ describe("stripHtmlComments (legacy)", () => {
518518
);
519519
});
520520
});
521+
522+
describe("outbound comment sanitization and redaction", () => {
523+
it("should sanitize content and redact all credential types for public comments", () => {
524+
const rawComment =
525+
"Done! Configured AWS AKIAIOSFODNN7EXAMPLE, Anthropic sk-ant-api03-abcdefghijklmnopqrstuvwxyz1234567890, Slack xoxb-1234567890-abcdefghijkl-mnopqrstuvwx, and GitHub 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 <!-- secret note -->";
526+
const sanitizedAndRedacted = redactSecrets(sanitizeContent(rawComment));
527+
528+
expect(sanitizedAndRedacted).not.toContain("AKIAIOSFODNN7EXAMPLE");
529+
expect(sanitizedAndRedacted).not.toContain(
530+
"sk-ant-api03-abcdefghijklmnopqrstuvwxyz1234567890",
531+
);
532+
expect(sanitizedAndRedacted).not.toContain(
533+
"xoxb-1234567890-abcdefghijkl-mnopqrstuvwx",
534+
);
535+
expect(sanitizedAndRedacted).not.toContain(
536+
"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",
537+
);
538+
expect(sanitizedAndRedacted).not.toContain("secret note");
539+
540+
expect(sanitizedAndRedacted).toContain("[REDACTED_AWS_KEY_ID]");
541+
expect(sanitizedAndRedacted).toContain("[REDACTED_ANTHROPIC_KEY]");
542+
expect(sanitizedAndRedacted).toContain("[REDACTED_SLACK_TOKEN]");
543+
expect(sanitizedAndRedacted).toContain("[REDACTED_GITHUB_TOKEN]");
544+
});
545+
});

0 commit comments

Comments
 (0)