|
| 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 | + "", |
| 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(""); |
| 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 | +}); |
0 commit comments