-
Notifications
You must be signed in to change notification settings - Fork 41
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
431d909
fix(security): run unicode normalization before secret redaction
ifireball 6ec05a2
fix(security): address PR review feedback on unicode coverage
ifireball 2a0be48
style: ruff-format unicode_posttool.py
ifireball 0cb6fa3
fix(security): address post-rebase review feedback
ifireball a4f164e
fix(security): address latest PR review on unicode normalization
ifireball File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
ifireball marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| #!/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_char(text: str, char: str) -> str: | ||
| """Insert invisible character between each codepoint.""" | ||
| return char.join(text) | ||
|
|
||
|
|
||
| def run_hook(script: str, tool_result: str) -> tuple[int, str, 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, proc.stderr | ||
|
|
||
|
|
||
| def run_wrong_chain(tool_result: str) -> str: | ||
| """Run secret_redact then unicode (wrong sandbox order — leaks obfuscated tokens).""" | ||
| rc, stdout, stderr = run_hook(SECRET_HOOK, tool_result) | ||
| if rc != 0: | ||
| raise RuntimeError(f"secret_redact hook failed: rc={rc}, stderr={stderr}") | ||
| if stdout.strip(): | ||
| out = json.loads(stdout) | ||
| tool_result = out["tool_result"] | ||
|
|
||
| rc, stdout, stderr = run_hook(UNICODE_HOOK, tool_result) | ||
| if rc != 0: | ||
| raise RuntimeError(f"unicode hook failed: rc={rc}, stderr={stderr}") | ||
| if stdout.strip(): | ||
| out = json.loads(stdout) | ||
| return out["tool_result"] | ||
| return tool_result | ||
|
|
||
|
|
||
| def to_fullwidth_ascii(text: str) -> str: | ||
| """Convert printable ASCII to fullwidth compatibility forms.""" | ||
| out: list[str] = [] | ||
| for c in text: | ||
| o = ord(c) | ||
| if 0x21 <= o <= 0x7E: | ||
| out.append(chr(o + 0xFF00 - 0x20)) | ||
| else: | ||
| out.append(c) | ||
| return "".join(out) | ||
|
|
||
|
|
||
| def run_chain(tool_result: str) -> str: | ||
| """Run unicode_posttool then secret_redact_posttool (correct sandbox order).""" | ||
| rc, stdout, stderr = run_hook(UNICODE_HOOK, tool_result) | ||
| if rc != 0: | ||
| raise RuntimeError(f"unicode hook failed: rc={rc}, stderr={stderr}") | ||
| if stdout.strip(): | ||
| out = json.loads(stdout) | ||
| tool_result = out["tool_result"] | ||
|
|
||
| rc, stdout, stderr = run_hook(SECRET_HOOK, tool_result) | ||
| if rc != 0: | ||
| raise RuntimeError(f"secret_redact hook failed: rc={rc}, stderr={stderr}") | ||
| 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_char(PLAIN_PAT, "\u200c") | ||
| result = run_chain(obfuscated) | ||
| self.assertNotIn("ghp_FAKEtest", result) | ||
| self.assertIn("...", result) | ||
|
|
||
| def test_ltr_mark_obfuscated_pat_redacted_by_chain(self): | ||
| obfuscated = obfuscate_with_char(PLAIN_PAT, "\u200e") | ||
| 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_char(PLAIN_PAT, "\u200c") | ||
| 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) | ||
|
|
||
| def test_wrong_order_chain_leaks_obfuscated_pat(self): | ||
| obfuscated = obfuscate_with_char(PLAIN_PAT, "\u200c") | ||
| result = run_wrong_chain(obfuscated) | ||
| self.assertIn("ghp_FAKEtest", result) | ||
|
|
||
| def test_fullwidth_obfuscated_pat_redacted_by_chain(self): | ||
| fullwidth = to_fullwidth_ascii(PLAIN_PAT) | ||
| result = run_chain(fullwidth) | ||
| self.assertNotIn("ghp_FAKEtest", result) | ||
| self.assertIn("...", result) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.