-
Notifications
You must be signed in to change notification settings - Fork 39
fix(security): run unicode normalization before secret redaction #1178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #!/usr/bin/env python3 | ||
| """Integration tests for post-tool hook chain ordering (unicode before secret redact).""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import subprocess | ||
| import sys | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
| HOOKS_DIR = Path(__file__).parent | ||
| UNICODE_HOOK = str(HOOKS_DIR / "unicode_posttool.py") | ||
| SECRET_HOOK = str(HOOKS_DIR / "secret_redact_posttool.py") | ||
|
|
||
| PLAIN_PAT = "ghp_FAKEtesttoken000000000000000000000000" | ||
|
|
||
|
|
||
| def obfuscate_with_zwnj(text: str) -> str: | ||
| """Insert zero-width non-joiner (U+200C) between each character.""" | ||
| return "\u200c".join(text) | ||
|
|
||
|
|
||
| def run_hook(script: str, tool_result: str) -> tuple[int, str]: | ||
| proc = subprocess.run( | ||
| [sys.executable, script], | ||
| input=json.dumps({"tool_name": "Read", "tool_result": tool_result}), | ||
| capture_output=True, | ||
| text=True, | ||
| timeout=10, | ||
| ) | ||
| return proc.returncode, proc.stdout | ||
|
ifireball marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| def run_chain(tool_result: str) -> str: | ||
| """Run unicode_posttool then secret_redact_posttool (correct sandbox order).""" | ||
| rc, stdout = run_hook(UNICODE_HOOK, tool_result) | ||
| if rc != 0: | ||
| raise RuntimeError(f"unicode hook failed: rc={rc}") | ||
| if stdout.strip(): | ||
| out = json.loads(stdout) | ||
| tool_result = out["tool_result"] | ||
|
|
||
| rc, stdout = run_hook(SECRET_HOOK, tool_result) | ||
| if rc != 0: | ||
| raise RuntimeError(f"secret_redact hook failed: rc={rc}") | ||
| if stdout.strip(): | ||
| out = json.loads(stdout) | ||
| return out["tool_result"] | ||
| return tool_result | ||
|
|
||
|
|
||
| class TestPostToolChain(unittest.TestCase): | ||
| def test_plain_pat_redacted_by_chain(self): | ||
| result = run_chain(PLAIN_PAT) | ||
| self.assertNotIn("ghp_FAKEtest", result) | ||
| self.assertIn("...", result) | ||
|
|
||
| def test_zero_width_obfuscated_pat_redacted_by_chain(self): | ||
| obfuscated = obfuscate_with_zwnj(PLAIN_PAT) | ||
| result = run_chain(obfuscated) | ||
| self.assertNotIn("ghp_FAKEtest", result) | ||
| self.assertIn("...", result) | ||
|
|
||
| def test_redact_alone_misses_zero_width_obfuscated_pat(self): | ||
| obfuscated = obfuscate_with_zwnj(PLAIN_PAT) | ||
| rc, stdout = run_hook(SECRET_HOOK, obfuscated) | ||
| self.assertEqual(rc, 0) | ||
| # secret_redact alone does not modify output when regex cannot match | ||
| self.assertEqual(stdout.strip(), "") | ||
| # Obfuscated token still present in source (would leak after unicode strips ZWNJ) | ||
| self.assertIn("\u200c", obfuscated) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -228,6 +228,21 @@ func TestPipeline(t *testing.T) { | |
| assert.NotContains(t, r.Sanitized, "ghp_FAKEtest") | ||
| }) | ||
|
|
||
| t.Run("normalize then redact catches zero-width obfuscated PAT", func(t *testing.T) { | ||
| p := NewPipeline(NewUnicodeNormalizer(), NewSecretRedactor()) | ||
| plain := "ghp_FAKEtesttoken000000000000000000000000" | ||
| var obfuscated strings.Builder | ||
| for _, r := range plain { | ||
| obfuscated.WriteRune(r) | ||
| obfuscated.WriteRune('\u200c') | ||
| } | ||
| r := p.Scan(obfuscated.String()) | ||
| assert.False(t, r.Safe) | ||
| assert.True(t, hasFinding(r, "zero_width")) | ||
| assert.True(t, hasFinding(r, "github_pat")) | ||
| assert.NotContains(t, r.Sanitized, "ghp_FAKEtest") | ||
| }) | ||
|
|
||
| t.Run("clean text passes both", func(t *testing.T) { | ||
| p := InputPipeline() | ||
| r := p.Scan("Normal commit message fixing a null pointer bug.") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [MEDIUM] Missing wrong-order negative test (2/8 agents flagged) These tests prove the correct pipeline order catches obfuscated tokens, but there's no test proving the wrong order ( Suggestion: t.Run("wrong order leaks zero-width obfuscated PAT", func(t *testing.T) {
p := NewPipeline(NewSecretRedactor(), NewUnicodeNormalizer())
plain := "ghp_FAKEtesttoken000000000000000000000000"
var obfuscated strings.Builder
for _, r := range plain {
obfuscated.WriteRune(r)
obfuscated.WriteRune('')
}
r := p.Scan(obfuscated.String())
// Redactor runs first, sees obfuscated token, misses it
assert.True(t, hasFinding(r, "zero_width"))
assert.False(t, hasFinding(r, "github_pat"), "wrong order must NOT catch the obfuscated token")
}) |
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.