Datasets:
Download scripts/test_redact.py from to0p/codex-tool-use-trajectories: direct link, hf CLI and curl.
- Browser
- Download file 5.42 kB
-
https://huggingface.co/datasets/to0p/codex-tool-use-trajectories/resolve/main/scripts/test_redact.py
- Command line
-
hf download hf://datasets/to0p/codex-tool-use-trajectories/scripts/test_redact.py
-
curl -L -o test_redact.py https://huggingface.co/datasets/to0p/codex-tool-use-trajectories/resolve/main/scripts/test_redact.py
5.42 kB
| import sys, os | |
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | |
| from redact import redact_text, phone_candidates, VALIDATION_REGEXES | |
| def r(t): return redact_text(t)[0] | |
| cases = [ | |
| # (input, expected-substring-or-exact, description) | |
| (r"C:\Users\Alice\.codex\skills\x", r"C:\Users\user\.codex\skills\x", "backslash path"), | |
| (r"C:\\Users\\Alice\\.codex", r"C:\\Users\\user\\.codex", "escaped double backslash"), | |
| (r"C:\\\\Users\\\\Alice\\\\a", r"C:\\\\Users\\\\user\\\\a", "quad backslash"), | |
| ("C:/Users/Alice/AppData", "C:/Users/user/AppData", "forward slash"), | |
| ("file:///C:/Users/Alice/x.txt", "file:///C:/Users/user/x.txt", "file URL"), | |
| (r"C:\Users\Public\Documents", r"C:\Users\Public\Documents", "system profile kept"), | |
| (r"M:\Codex\Bink2Combined\src", r"M:\Codex\Bink2Combined\src", "project path kept"), | |
| ("/mnt/c/Users/Alice/x", "/mnt/c/Users/user/x", "wsl path"), | |
| (r"D:\OneDrive - Some School's Name\Documents\x", r"D:\OneDrive - <ORG>\Documents\x", "onedrive organisation"), | |
| (r"if (D:\\OneDrive - Contoso Ltd\\Documents\\a.ps1)", r"if (D:\\OneDrive - <ORG>\\Documents\\a.ps1)", "onedrive organisation, escaped backslashes"), | |
| ("C:/Users/Alice/OneDrive - Contoso/Docs", "C:/Users/user/OneDrive - <ORG>/Docs", "onedrive org together with a user path"), | |
| (r"C:\Users\Public\OneDrive - Personal\Docs", r"C:\Users\Public\OneDrive - Personal\Docs", "onedrive personal kept"), | |
| (r"C:\Users\Public\OneDrive\Docs and OneDrive - for sync", r"C:\Users\Public\OneDrive\Docs and OneDrive - for sync", "plain OneDrive kept"), | |
| ("mail me at jane.doe+x@example.co.uk now", "mail me at <EMAIL> now", "email"), | |
| ("icon@2x.png and git@github.com:o/r.git", "icon@2x.png and <EMAIL>:o/r.git", "email guards"), | |
| ("pkg@1.2.3 and HEAD@{1}", "pkg@1.2.3 and HEAD@{1}", "not emails"), | |
| ("host 10.1.2.3 and 127.0.0.1 and 0.0.0.0 and v1.2.3.4 and 1.2.3.4.5", "host <IP> and 127.0.0.1 and 0.0.0.0 and v1.2.3.4 and 1.2.3.4.5", "ipv4"), | |
| ("see 192.168.0.12.", "see <IP>.", "ip end of sentence"), | |
| ('mscorlib, Version=4.0.0.0, Culture=neutral and Version="1.2.3.4"', 'mscorlib, Version=4.0.0.0, Culture=neutral and Version="1.2.3.4"', "assembly versions kept"), | |
| ("IPAddress : 93.184.216.34", "IPAddress : <IP>", "dns answer redacted"), | |
| ("key AKIAABCDEFGHIJKLMNOP end", "key <SECRET> end", "aws"), | |
| ("tok ghp_" + "A1b2C3d4E5f6G7h8I9j0K1l2M3n4O5p6Q7r8" + " end", "tok <SECRET> end", "gh token"), | |
| ("xghp_" + "A1b2C3d4E5f6G7h8I9j0K1l2M3n4O5p6Q7r8", "x<SECRET>", "gh glued to a word: literal spec regex still applies"), | |
| ("github_pat_" + "11AAAAAAA0abcdefghijkl_mnopqrstuvwxyz0123", "<SECRET>", "gh pat"), | |
| ("sk-" + "abcdefghijklmnopqrstuvwxyz0123", "<SECRET>", "sk"), | |
| ("disk-image-extraction-tool-for-things", "di<SECRET>", "sk- inside a word: literal spec regex applies"), | |
| ("-----BEGIN RSA PRIVATE KEY-----\nMIIEabc\ndef==\n-----END RSA PRIVATE KEY----- tail", "<SECRET> tail", "pem"), | |
| ("x -----BEGIN PRIVATE KEY-----\nMIIEabc\ndef==", "x <SECRET>", "pem unterminated"), | |
| ("Authorization: Bearer abcdef123456.xyz", "Authorization: Bearer <SECRET>", "bearer"), | |
| ("curl -H 'Authorization: Bearer $TOKEN'", "curl -H 'Authorization: Bearer $TOKEN'", "bearer var kept"), | |
| ("password=hunter22 next", "password=<SECRET> next", "kv password"), | |
| ('{"token": "abc123def456"}', '{"token": "<SECRET>"}', "kv json"), | |
| (r'\"api_key\":\"abc123def456\"', r'\"api_key\":\"<SECRET>\"', "kv escaped json"), | |
| ("export GITHUB_TOKEN=xyz9abcdef", "export GITHUB_TOKEN=<SECRET>", "kv prefixed"), | |
| ("token = next_token;", "token = next_token;", "kv identifier kept"), | |
| ("token_count=123456 tokens=abc123abc", "token_count=123456 tokens=abc123abc", "kv suffix kept"), | |
| ("if (token == other123456)", "if (token == other123456)", "comparison kept"), | |
| ("secret: $SECRET_VALUE", "secret: $SECRET_VALUE", "kv var kept"), | |
| ("Password: 'my pass phrase'", "Password: '<SECRET>'", "kv quoted"), | |
| ("\t\t// Token: 0x06000001\r\n", "\t\t// Token: 0x06000001\r\n", "decompiler hex token kept"), | |
| ("var token = this.Client.Token;", "var token = this.Client.Token;", "member access kept"), | |
| ("funnel token:\n 123:Abc line numbers", "funnel token:\n 123:Abc line numbers", "newline not consumed"), | |
| ("token=eyJhbGciOiJIUzI1NiIs.eyJzdWIiOiIxMjM0NTY3ODkw.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c", "token=<SECRET>", "jwt redacted"), | |
| ] | |
| bad = 0 | |
| for inp, exp, d in cases: | |
| got = r(inp) | |
| ok = got == exp | |
| if not ok: | |
| bad += 1; print("FAIL", d, "\n in :", inp, "\n got:", got, "\n exp:", exp) | |
| print("cases", len(cases), "failed", bad) | |
| from redact import redact_text as _rt | |
| assert _rt("disk-image-extraction-tool-for-things")[1][0][2] == "secret:sk_key:embedded_in_word" | |
| assert _rt("key sk-" + "a"*30)[1][0][2] == "secret:sk_key" | |
| # events offsets | |
| t = "a user@example.com b 10.0.0.1 c" | |
| n, ev = redact_text(t) | |
| print(n, [(s, e, k, n[s:e]) for s, e, k, h, rep in ev]) | |
| # phone candidates | |
| print(phone_candidates("call +1 (555) 123-4567 or 555-123-4567 or 1920 1080 24 30 or 0.5 0.25 0.125")) | |
| # validation regexes on redacted output must be empty | |
| for name, rx in VALIDATION_REGEXES.items(): | |
| assert not rx.search(r("AKIAABCDEFGHIJKLMNOP ghp_" + "A"*36 + " sk-" + "a"*30 + " Authorization: Bearer abcdefgh1234")), name | |
| print("validation regexes ok") | |
| # performance sanity on pathological input (long alnum run, many @) | |
| import time | |
| s = ("a"*200000) + "@" * 50 + "1."*50000 | |
| t0 = time.time(); redact_text(s); print("pathological ms", int((time.time()-t0)*1000)) | |