audit-my-agents 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +117 -2
- package/bin/agent-audit.mjs +62 -0
- package/package.json +40 -4
- package/src/findings.mjs +241 -0
- package/src/pii.mjs +154 -0
- package/src/redact.mjs +119 -0
- package/src/report.mjs +113 -0
- package/src/scan.mjs +98 -0
- package/src/sources/claude.mjs +77 -0
- package/src/sources/codex.mjs +131 -0
- package/src/vendor/command.mjs +20 -0
- package/src/vendor/decide.mjs +750 -0
- package/src/vendor/identity.mjs +487 -0
- package/src/vendor/net.mjs +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Agentic Control Plane
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,118 @@
|
|
|
1
|
-
#
|
|
1
|
+
# audit-my-agents
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
What did your coding agents actually do?
|
|
4
|
+
|
|
5
|
+
Paste this into Claude Code or Codex:
|
|
6
|
+
|
|
7
|
+
```text
|
|
8
|
+
Run `npx audit-my-agents` and walk me through what it found.
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or run it yourself:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npx audit-my-agents
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
It reads the session logs Claude Code and Codex already keep on your machine, and reports the calls you'd want to know about:
|
|
18
|
+
|
|
19
|
+
```text
|
|
20
|
+
What your coding agents did in the last 30 days
|
|
21
|
+
Claude Code 48 sessions · Codex 6 sessions · 4,120 tool calls · 2,980 shell commands · $412 spent
|
|
22
|
+
────────────────────────────────────────────────────────────────
|
|
23
|
+
✓ Catastrophic commands none
|
|
24
|
+
✗ Destructive commands 3 in 3 sessions
|
|
25
|
+
2026-11-02 Claude Code: force-pushes over shared git history
|
|
26
|
+
git push --force origin main
|
|
27
|
+
✗ Live secrets in tool calls 1 in 1 session
|
|
28
|
+
2026-10-29 Codex: GitHub token
|
|
29
|
+
curl -H "Authorization: token [GitHub token]" https://api.github.com/…
|
|
30
|
+
! Personal data the model read 6 in 2 sessions
|
|
31
|
+
2026-10-30 Claude Code: card number in tool output
|
|
32
|
+
Visa card ending 8431
|
|
33
|
+
! Credential files read 14 in 9 sessions
|
|
34
|
+
! Your email sent to a site 2 in 1 session
|
|
35
|
+
✓ Local data sent out none
|
|
36
|
+
· Pushes to a git remote 61 in 22 sessions
|
|
37
|
+
────────────────────────────────────────────────────────────────
|
|
38
|
+
Reached 58 hosts. Most often: github.com (210), npmjs.org (96), pypi.org (40) …
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
*(Illustrative output.)*
|
|
42
|
+
|
|
43
|
+
It's read-only. It makes no network calls, needs no account and installs no hooks. Nothing leaves your machine. It has no dependencies and needs Node 18 or later.
|
|
44
|
+
|
|
45
|
+
No npm? Clone the repo and run `node bin/agent-audit.mjs`. There is no install step.
|
|
46
|
+
|
|
47
|
+
## What it looks for
|
|
48
|
+
|
|
49
|
+
| Finding | Examples |
|
|
50
|
+
|---|---|
|
|
51
|
+
| Catastrophic commands | `rm -rf` on `/`, `~` or `.`, `mkfs`, raw disk writes |
|
|
52
|
+
| Destructive commands | force-push, `DROP TABLE` / `TRUNCATE` / `DELETE` without `WHERE`, `curl … \| sh`, a recursive delete outside the project |
|
|
53
|
+
| Live secrets in tool calls | an API key or token passed in a command or tool input |
|
|
54
|
+
| Personal data the model read | a card number, a US Social Security number or someone else's email address in a prompt or in what a tool returned |
|
|
55
|
+
| Credential files read | `.env`, SSH keys, `~/.aws/credentials`, `.npmrc`, keychain reads |
|
|
56
|
+
| Local data sent out | `curl -d @file`, uploads, `scp` / `rsync` to a remote |
|
|
57
|
+
| Your email sent to a site | your git email in a User-Agent, From header or contact field |
|
|
58
|
+
| Sensitive files changed | shell startup files, SSH config and authorized_keys, git hooks, crontab, login items |
|
|
59
|
+
| Sessions with no approval prompts | Claude Code bypass mode, Codex never-ask with a full-access sandbox |
|
|
60
|
+
|
|
61
|
+
The destructive and catastrophic rules are the same open rules [Agentic Control Plane](https://agenticcontrolplane.com) enforces before a call runs. They're vendored from [acp-install](https://github.com/agentic-control-plane/acp-install). A finding here is a call that check would have held for your approval.
|
|
62
|
+
|
|
63
|
+
It skips text that's only written, searched for or described, not run. That covers a `curl | sh` inside an issue body, a `grep` for `git push --force`, a heredoc writing a test file, or a commit message. A heredoc fed to a shell or a SQL client is still checked. Placeholder keys like `sk-test-xxxx` aren't reported.
|
|
64
|
+
|
|
65
|
+
Personal data is counted once per distinct value, however many times the model read it. The rules lean toward missing something over flagging something that isn't personal:
|
|
66
|
+
|
|
67
|
+
- Not counted as someone's email: your own git email, role addresses like `support@` and `noreply@`, authors in a git log or a package manifest, ssh logins, and placeholder domains like `example.com`.
|
|
68
|
+
- A card number needs a real brand prefix, the right length and a valid checksum. Written without spaces, it also needs a word like "card" or "visa" nearby. Test cards published by payment providers aren't counted.
|
|
69
|
+
- A Social Security number is counted only when written with dashes and a nearby word says what it is.
|
|
70
|
+
- Phone numbers and street addresses aren't detected.
|
|
71
|
+
|
|
72
|
+
## Running it from an agent
|
|
73
|
+
|
|
74
|
+
When a coding agent runs the audit, the agent is the reader. Inside Claude Code the report ends with a few plain notes for it: what to lead with, that the commands shown already ran and shouldn't be run again, and that the examples are redacted on purpose. Pass `--agent` to get the same notes anywhere else.
|
|
75
|
+
|
|
76
|
+
The agent only ever sees the redacted report. Personal data is shown as `d***@gmail.com`, `Visa card ending 8431` or `***-**-****`, with or without `--raw`.
|
|
77
|
+
|
|
78
|
+
## Options
|
|
79
|
+
|
|
80
|
+
```text
|
|
81
|
+
--days N look back N days (default 30)
|
|
82
|
+
--harness NAME claude or codex (repeatable)
|
|
83
|
+
--email ADDR also flag this address in outbound headers (git user.email is automatic)
|
|
84
|
+
--html FILE write a shareable HTML report
|
|
85
|
+
--json print the summary as JSON
|
|
86
|
+
--examples N examples per finding (default 3)
|
|
87
|
+
--raw show examples unredacted (local only; don't screenshot)
|
|
88
|
+
--agent add short notes for a coding agent relaying the report
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Privacy
|
|
92
|
+
|
|
93
|
+
- Examples are redacted by default, and the redaction errs toward hiding too much:
|
|
94
|
+
- Known key shapes become labels like `[GitHub token]`.
|
|
95
|
+
- Passwords in `-u`, `-p`, `--password` and URL userinfo, `NAME_TOKEN=`-style values, and long opaque strings become `[redacted]`.
|
|
96
|
+
- Email addresses become `[email]`, usernames become `[user]`, and home paths become `~`.
|
|
97
|
+
- Intranet and `.local` hostnames are never listed.
|
|
98
|
+
- The HTML export is always redacted.
|
|
99
|
+
- `--raw` prints full commands to your own terminal only. If an agent runs it, those commands go to the model, so leave it off there.
|
|
100
|
+
- Personal data is never printed in full, even with `--raw`.
|
|
101
|
+
- Redaction is pattern-based, so glance over a report before you post it.
|
|
102
|
+
|
|
103
|
+
## Coverage
|
|
104
|
+
|
|
105
|
+
| Harness | Where it reads | Status |
|
|
106
|
+
|---|---|---|
|
|
107
|
+
| Claude Code | `~/.claude/projects/**/*.jsonl` | covered, including subagents |
|
|
108
|
+
| Codex CLI | `~/.codex/sessions/**/*.jsonl` | covered: shell, exec code mode, apply_patch |
|
|
109
|
+
| OpenCode | `~/.local/share/opencode/opencode.db` | not yet (SQLite) |
|
|
110
|
+
| Cursor, Copilot, Gemini CLI | — | not yet |
|
|
111
|
+
|
|
112
|
+
The session formats aren't documented by their vendors and can change. If a harness update breaks parsing, please open an issue.
|
|
113
|
+
|
|
114
|
+
## Next step
|
|
115
|
+
|
|
116
|
+
This shows what already happened. To be asked before the next force-push, `DROP TABLE` or `curl | sh` runs, with the same rules in every harness, see [Agentic Control Plane](https://agenticcontrolplane.com/getting-started?ref=audit).
|
|
117
|
+
|
|
118
|
+
MIT licensed.
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// audit-my-agents — what did your coding agents actually do?
|
|
3
|
+
//
|
|
4
|
+
// Reads the session logs Claude Code and Codex already keep on your
|
|
5
|
+
// machine and reports the risky calls. Read-only. Nothing leaves your
|
|
6
|
+
// machine.
|
|
7
|
+
|
|
8
|
+
import { writeFileSync } from "node:fs";
|
|
9
|
+
import { scan, SOURCES } from "../src/scan.mjs";
|
|
10
|
+
import { summarise, renderTerminal, renderHtml, renderAgentNotes } from "../src/report.mjs";
|
|
11
|
+
|
|
12
|
+
const HELP = `audit-my-agents — what did your coding agents actually do?
|
|
13
|
+
|
|
14
|
+
Usage: npx audit-my-agents [options]
|
|
15
|
+
|
|
16
|
+
--days N look back N days (default 30)
|
|
17
|
+
--harness NAME only scan one harness: ${Object.keys(SOURCES).join(", ")} (repeatable)
|
|
18
|
+
--email ADDR also flag this address in outbound headers (repeatable; git user.email is automatic)
|
|
19
|
+
--html FILE also write a shareable HTML report
|
|
20
|
+
--json print the summary as JSON
|
|
21
|
+
--examples N examples per finding (default 3)
|
|
22
|
+
--raw show examples unredacted (stays on your machine; don't screenshot)
|
|
23
|
+
--agent add short notes for a coding agent relaying the report (automatic inside Claude Code)
|
|
24
|
+
-h, --help this help
|
|
25
|
+
`;
|
|
26
|
+
|
|
27
|
+
const argv = process.argv.slice(2);
|
|
28
|
+
const opts = { days: 30, harnesses: [], emails: [], html: null, json: false, raw: false, examples: 3, agent: false };
|
|
29
|
+
for (let i = 0; i < argv.length; i++) {
|
|
30
|
+
const a = argv[i];
|
|
31
|
+
const next = () => argv[++i];
|
|
32
|
+
if (a === "-h" || a === "--help") { process.stdout.write(HELP); process.exit(0); }
|
|
33
|
+
else if (a === "--days") {
|
|
34
|
+
const v = Number(next());
|
|
35
|
+
if (!Number.isFinite(v) || v <= 0) { process.stderr.write("--days needs a positive number\n"); process.exit(2); }
|
|
36
|
+
opts.days = v;
|
|
37
|
+
}
|
|
38
|
+
else if (a === "--harness") opts.harnesses.push(next());
|
|
39
|
+
else if (a === "--email") opts.emails.push(next());
|
|
40
|
+
else if (a === "--html") opts.html = next();
|
|
41
|
+
else if (a === "--json") opts.json = true;
|
|
42
|
+
else if (a === "--raw") opts.raw = true;
|
|
43
|
+
else if (a === "--agent") opts.agent = true;
|
|
44
|
+
else if (a === "--examples") opts.examples = Math.max(0, Number(next()) || 0);
|
|
45
|
+
else { process.stderr.write(`unknown option: ${a}\n\n${HELP}`); process.exit(2); }
|
|
46
|
+
}
|
|
47
|
+
const bad = opts.harnesses.filter((h) => !SOURCES[h]);
|
|
48
|
+
if (bad.length) { process.stderr.write(`unknown harness: ${bad.join(", ")}\n`); process.exit(2); }
|
|
49
|
+
|
|
50
|
+
const result = await scan({ days: opts.days, harnesses: opts.harnesses.length ? opts.harnesses : undefined, emails: opts.emails });
|
|
51
|
+
const summary = summarise(result, { raw: opts.raw, examples: opts.examples });
|
|
52
|
+
if (opts.json) process.stdout.write(JSON.stringify(summary, null, 2) + "\n");
|
|
53
|
+
else {
|
|
54
|
+
process.stdout.write(renderTerminal(summary) + "\n");
|
|
55
|
+
// Inside a coding agent the reader is the agent; tell it how to relay this.
|
|
56
|
+
const inAgent = opts.agent || Boolean(process.env.CLAUDECODE || process.env.AI_AGENT || process.env.CODEX_SANDBOX || process.env.CURSOR_AGENT);
|
|
57
|
+
if (inAgent) process.stdout.write(renderAgentNotes(summary) + "\n");
|
|
58
|
+
}
|
|
59
|
+
if (opts.html) {
|
|
60
|
+
writeFileSync(opts.html, renderHtml(summarise(result, { raw: false, examples: opts.examples })));
|
|
61
|
+
process.stderr.write(`HTML report written to ${opts.html} (redacted)\n`);
|
|
62
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "audit-my-agents",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "What did your coding agents actually do? One command reads your local Claude Code and Codex sessions and reports force-pushes, destructive commands, credential reads, leaked secrets and more.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"audit-my-agents": "bin/agent-audit.mjs",
|
|
8
|
+
"agent-audit": "bin/agent-audit.mjs"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"bin",
|
|
12
|
+
"src",
|
|
13
|
+
"README.md",
|
|
14
|
+
"LICENSE"
|
|
15
|
+
],
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=18"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "node --test test/*.test.mjs"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"claude-code",
|
|
24
|
+
"codex",
|
|
25
|
+
"coding-agent",
|
|
26
|
+
"audit",
|
|
27
|
+
"security",
|
|
28
|
+
"ai-agents"
|
|
29
|
+
],
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/agentic-control-plane/agent-audit.git"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/agentic-control-plane/agent-audit#readme",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/agentic-control-plane/agent-audit/issues"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/findings.mjs
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
// What counts as worth showing. Every rule is the replay of something a
|
|
2
|
+
// pre-tool check could have stopped; the floors come straight from ACP's
|
|
3
|
+
// open decision engine so a finding here is exactly a call ACP holds.
|
|
4
|
+
|
|
5
|
+
import { hardlineFloor, destructiveFloor, stripDataHeredocs } from "./vendor/decide.mjs";
|
|
6
|
+
import { detectIdentityEgress, identityTokens } from "./vendor/identity.mjs";
|
|
7
|
+
import { classifyHost, isInternalHostClass, displayHost } from "./vendor/net.mjs";
|
|
8
|
+
import { secretTypes } from "./redact.mjs";
|
|
9
|
+
|
|
10
|
+
export const CATEGORIES = [
|
|
11
|
+
{ id: "catastrophic", title: "Catastrophic commands", severity: 3, blurb: "rm -rf on /, ~ or ., mkfs, raw disk writes" },
|
|
12
|
+
{ id: "destructive", title: "Destructive commands", severity: 3, blurb: "force-push, destructive SQL, curl | sh, recursive delete outside the project" },
|
|
13
|
+
{ id: "secret-in-args", title: "Live secrets in tool calls", severity: 3, blurb: "a real-looking key or token passed in a command or tool input" },
|
|
14
|
+
{ id: "pii", title: "Personal data the model read", severity: 2, blurb: "card numbers, US Social Security numbers and other people's email addresses in prompts and tool output" },
|
|
15
|
+
{ id: "credential-read", title: "Credential files read", severity: 2, blurb: ".env, SSH keys, cloud and registry credentials" },
|
|
16
|
+
{ id: "upload", title: "Local data sent out", severity: 2, blurb: "a file or body uploaded to a host outside your machine" },
|
|
17
|
+
{ id: "identity", title: "Your email sent to a site", severity: 2, blurb: "your address in a User-Agent, From header or contact field" },
|
|
18
|
+
{ id: "sensitive-write", title: "Sensitive files changed", severity: 2, blurb: "shell startup files, SSH config, CI workflows, git hooks" },
|
|
19
|
+
{ id: "unattended", title: "Sessions with no approval prompts", severity: 1, blurb: "Claude Code bypass mode, Codex never-ask + full-access sandbox" },
|
|
20
|
+
{ id: "push", title: "Pushes to a git remote", severity: 1, blurb: "every git push the agent ran" },
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
/** Commands longer than this are scanned on their first part only. */
|
|
24
|
+
const MAX_CMD = 50_000;
|
|
25
|
+
|
|
26
|
+
const SHELL = new Set(["Bash", "run_terminal_cmd", "shell"]);
|
|
27
|
+
const READ_TOOLS = new Set(["Read", "read_file", "View", "view"]);
|
|
28
|
+
const WRITE_TOOLS = new Set(["Write", "Edit", "MultiEdit", "create_file", "edit_file", "NotebookEdit"]);
|
|
29
|
+
|
|
30
|
+
const CREDENTIAL_PATH_RE = /(?:^|[\s/"'=])(?:\.env(?:\.(?!example\b|sample\b|template\b|dist\b)[\w.-]+)?|\.npmrc|\.pypirc|\.netrc|\.pgpass|\.git-credentials|\.aws\/credentials|\.aws\/config|\.ssh\/id_[a-z0-9]+(?!\.pub)|\.kube\/config|\.docker\/config\.json|\.config\/gcloud\/[\w./-]*credentials[\w.-]*|\.config\/gh\/hosts\.yml|[\w.-]*(?:key|private|id_)[\w.-]*\.pem|[\w.-]+\.p12|[\w.-]+\.pfx|[\w.-]+\.key|(?:gcp|google|firebase|aws)?[-_]?credentials\.json|service[-_]account[\w-]*\.json)(?=$|[\s"'`;|&)])/;
|
|
31
|
+
const NOT_A_SECRET_RE = /\.(?:example|sample|template|dist|tmpl|defaults?)(?=$|[\s"'`;|&)])/;
|
|
32
|
+
function credentialIn(text) {
|
|
33
|
+
const m = CREDENTIAL_PATH_RE.exec(text);
|
|
34
|
+
if (!m) return false;
|
|
35
|
+
const tail = text.slice(m.index, m.index + m[0].length + 40);
|
|
36
|
+
return !NOT_A_SECRET_RE.test(tail.split(/[\s"'`;|&)]/).filter(Boolean)[0] ?? "");
|
|
37
|
+
}
|
|
38
|
+
const DUMP_BINS = new Set(["cat", "head", "tail", "less", "more", "bat", "grep", "rg", "awk", "sed", "cp", "scp", "base64", "xxd", "strings", "source", ".", "open", "code", "vim", "nano"]);
|
|
39
|
+
const KEYCHAIN_RE = /\bsecurity\s+find-(?:generic|internet)-password\b[^|;&\n]*\s-w\b|\bgcloud\s+auth\s+print-access-token\b|\baws\s+configure\s+get\s+\S*secret/;
|
|
40
|
+
const SENSITIVE_WRITE_RE = /(?:^|\/)(?:\.(?:zshrc|bashrc|bash_profile|profile|zprofile|zshenv|bash_login)|\.ssh\/(?:config|authorized_keys|rc)|\.git\/hooks\/[^/]+|crontab|Library\/LaunchAgents\/[^/]+\.plist|\.config\/autostart\/[^/]+)$/;
|
|
41
|
+
const UPLOAD_RE = /\bcurl\b[^|;&\n]*(?:\s-(?:d|F|T)\s*@|\s--data(?:-binary|-raw)?[=\s]+@|\s--upload-file\b|\s-F\s+\S*=@)|\b(?:scp|rsync)\b[^|;&\n]*\s[\w.-]+@?[\w.-]+:\S|\bnc\s+[\w.-]+\s+\d+\s*<|\bgh\s+gist\s+create\b/;
|
|
42
|
+
const PUSH_RE = /\bgit\b(?:\s+-[cC]\s+\S+)*\s+push\b/;
|
|
43
|
+
|
|
44
|
+
/** What the shell would RUN: heredoc bodies and message-style arguments
|
|
45
|
+
* (--body, --title, -m …) are text being written or described, so a
|
|
46
|
+
* `curl | sh` inside an issue body or a test fixture is not a finding.
|
|
47
|
+
* Precision beats recall in a report people screenshot. */
|
|
48
|
+
export function executableText(cmd) {
|
|
49
|
+
// Data heredocs (cat > file <<EOF …) are content; heredocs fed to a shell
|
|
50
|
+
// or SQL client (bash <<EOF, psql <<SQL) stay, so their commands count.
|
|
51
|
+
let t = stripDataHeredocs(String(cmd ?? "").slice(0, MAX_CMD));
|
|
52
|
+
t = t.replace(/(--(?:body|title|notes|message|description|subject|comment|text|msg)|-[mb])(?:=|\s+)(?:"(?:[^"\\]|\\.){0,20000}"|'[^']{0,20000}'|\$\([^)]{0,20000}\)|\S+)/g, "$1 MSG");
|
|
53
|
+
return t;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Shell segments split at ; && || | and newlines, outside quotes (approx). */
|
|
57
|
+
function segments(t, depth = 0) {
|
|
58
|
+
const out = [];
|
|
59
|
+
for (const seg of splitOutsideQuotes(t, true)) {
|
|
60
|
+
out.push(seg);
|
|
61
|
+
// A quoted string handed to a shell runs: bash -c "…", ssh host "…",
|
|
62
|
+
// docker exec app sh -c "…".
|
|
63
|
+
if (depth < 2 && runsQuotedText(seg)) {
|
|
64
|
+
for (const q of quotedStrings(seg)) out.push(...segments(q, depth + 1));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const SHELL_RUNNERS = new Set(["sh", "bash", "zsh", "dash", "ksh", "ssh", "eval", "xargs", "watch", "timeout", "su"]);
|
|
71
|
+
const SHELL_DASH_C_RE = /(?:^|[\s/])(?:ba|z|da|k)?sh\s+(?:-[A-Za-z]+\s+)*-[A-Za-z]*c\b/;
|
|
72
|
+
const REMOTE_RUN_RE = /(?:^|\s)(?:ssh|tmux\s+send-keys|screen\s+-\S*X)\s/;
|
|
73
|
+
function runsQuotedText(seg) {
|
|
74
|
+
return SHELL_RUNNERS.has(firstWord(seg)) || SHELL_DASH_C_RE.test(seg) || REMOTE_RUN_RE.test(seg);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Split at ; && || newline (and | when asked), ignoring separators inside
|
|
78
|
+
* single or double quotes. A `|` in a grep pattern is not a pipe.
|
|
79
|
+
* Comments are dropped first, so an apostrophe in `# don't` opens nothing.
|
|
80
|
+
* If the quotes still don't balance, the text is split the plain way: a
|
|
81
|
+
* stray quote must never hide the command after it. */
|
|
82
|
+
function splitOutsideQuotes(t, pipes) {
|
|
83
|
+
const s = String(t);
|
|
84
|
+
const out = [];
|
|
85
|
+
let cur = "";
|
|
86
|
+
let quote = "";
|
|
87
|
+
let ansi = false; // inside $'…', where backslash escapes
|
|
88
|
+
for (let i = 0; i < s.length; i++) {
|
|
89
|
+
const c = s[i];
|
|
90
|
+
if (quote) {
|
|
91
|
+
if (c === "\\" && (quote === '"' || ansi) && i + 1 < s.length) { cur += c + s[++i]; continue; }
|
|
92
|
+
if (c === quote) { quote = ""; ansi = false; }
|
|
93
|
+
cur += c;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (c === "\\" && i + 1 < s.length) { cur += c + s[++i]; continue; }
|
|
97
|
+
if (c === "#" && (i === 0 || /\s/.test(s[i - 1]))) {
|
|
98
|
+
const nl = s.indexOf("\n", i);
|
|
99
|
+
if (nl === -1) break;
|
|
100
|
+
i = nl - 1;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (c === '"' || c === "'") { quote = c; ansi = c === "'" && s[i - 1] === "$"; cur += c; continue; }
|
|
104
|
+
const two = s.slice(i, i + 2);
|
|
105
|
+
if (two === "&&" || two === "||") { out.push(cur); cur = ""; i++; continue; }
|
|
106
|
+
if (c === "\n" || c === ";" || (pipes && c === "|")) { out.push(cur); cur = ""; continue; }
|
|
107
|
+
cur += c;
|
|
108
|
+
}
|
|
109
|
+
if (quote) return s.split(pipes ? /\n|;|&&|\|\||\|/ : /\n|;|&&|\|\|/).map((x) => x.trim()).filter(Boolean);
|
|
110
|
+
out.push(cur);
|
|
111
|
+
return out.map((x) => x.trim()).filter(Boolean);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function quotedStrings(seg) {
|
|
115
|
+
const out = [];
|
|
116
|
+
const re = /"((?:[^"\\]|\\.)*)"|'([^']*)'/g;
|
|
117
|
+
let m;
|
|
118
|
+
while ((m = re.exec(seg)) !== null) out.push(m[1] !== undefined ? m[1].replace(/\\(["\\$`])/g, "$1") : m[2]);
|
|
119
|
+
return out;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function firstWord(seg) {
|
|
123
|
+
const toks = seg.split(/\s+/);
|
|
124
|
+
let i = 0;
|
|
125
|
+
while (i < toks.length && (/^[A-Za-z_]\w*=/.test(toks[i]) || /^(?:sudo|env|command|exec|nohup|time)$/.test(toks[i]))) i++;
|
|
126
|
+
const w = toks[i] ?? "";
|
|
127
|
+
return w.slice(w.lastIndexOf("/") + 1);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The engine reads the whole command line; a report people screenshot
|
|
131
|
+
* needs the matching command to be the one actually run. A force-push
|
|
132
|
+
* needs a git segment that pushes with force; a pipe-to-shell needs a
|
|
133
|
+
* curl/wget segment piped into a shell. `grep 'git push --force'` and
|
|
134
|
+
* `echo "curl x | sh"` are not findings. */
|
|
135
|
+
function backedBySegment(reason, run) {
|
|
136
|
+
if (/force-push/.test(reason)) {
|
|
137
|
+
return segments(run).some((seg) => firstWord(seg) === "git" && /\bpush\b/.test(seg) && /(?:\s--force(?!-with-lease|-if-includes)\b|\s-[a-eg-zA-Z]*f[a-zA-Z]*(?=\s|$)|\s\+[^\s:]+)/.test(seg));
|
|
138
|
+
}
|
|
139
|
+
if (/pipes a remote download/.test(reason)) {
|
|
140
|
+
const parts = splitOutsideQuotes(run, false).flatMap((p) => (runsQuotedText(p) ? [p, ...quotedStrings(p)] : [p]));
|
|
141
|
+
return parts.some((p) => {
|
|
142
|
+
const piped = splitOutsideQuotes(p, true);
|
|
143
|
+
for (let i = 0; i + 1 < piped.length; i++) {
|
|
144
|
+
if (/^(?:curl|wget)$/.test(firstWord(piped[i])) && /^(?:sudo\s+)?(?:\S*\/)?(?:ba|z|da|k)?sh\b/.test(piped[i + 1])) return true;
|
|
145
|
+
}
|
|
146
|
+
return /(?:^|\s)(?:ba|z)?sh\s+(?:-\w+\s+)*(?:-c\s+["']?\$\(\s*(?:curl|wget)|<\s*<?\s*\(\s*(?:curl|wget))/.test(p);
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function commandOf(ev) {
|
|
153
|
+
const i = ev.input;
|
|
154
|
+
if (i && typeof i === "object") {
|
|
155
|
+
if (typeof i.command === "string") return i.command;
|
|
156
|
+
if (typeof i.cmd === "string") return i.cmd;
|
|
157
|
+
}
|
|
158
|
+
return "";
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function hostsIn(text) {
|
|
162
|
+
const out = [];
|
|
163
|
+
const re = /\bhttps?:\/\/(?:[^\s/?#@"'`]*@)?(\[[^\]\s]+\]|[^\s/:?#"'`)]+)/gi;
|
|
164
|
+
let m;
|
|
165
|
+
while ((m = re.exec(text)) !== null) {
|
|
166
|
+
const h = m[1].replace(/^\[|\]$/g, "").toLowerCase();
|
|
167
|
+
// A host the shell computes ($VAR, ${…}) or a placeholder is not a destination we know.
|
|
168
|
+
if (/[$`{}]/.test(h) || !h.includes(".")) continue;
|
|
169
|
+
if (!isInternalHostClass(classifyHost(h))) out.push(h);
|
|
170
|
+
}
|
|
171
|
+
return out;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Findings for one tool call. Returns [{ category, why, example }]; the
|
|
176
|
+
* example is the raw text, redacted later by the report.
|
|
177
|
+
*/
|
|
178
|
+
export function findingsFor(ev, ctx) {
|
|
179
|
+
const out = [];
|
|
180
|
+
const cmd = commandOf(ev).slice(0, MAX_CMD);
|
|
181
|
+
const shellLike = SHELL.has(ev.tool) || (cmd && !READ_TOOLS.has(ev.tool) && !WRITE_TOOLS.has(ev.tool));
|
|
182
|
+
const shellInput = { command: cmd };
|
|
183
|
+
|
|
184
|
+
const run = executableText(cmd);
|
|
185
|
+
const runInput = { command: run };
|
|
186
|
+
if (shellLike && cmd) {
|
|
187
|
+
const hard = hardlineFloor("Bash", runInput);
|
|
188
|
+
if (hard) out.push({ category: "catastrophic", why: hard, example: cmd });
|
|
189
|
+
const destr = hard ? null : destructiveFloor("Bash", runInput, { cwd: ev.cwd });
|
|
190
|
+
// ACP records a variable-named rm without asking; so does this report.
|
|
191
|
+
if (destr && !/variable-named path/.test(destr) && backedBySegment(destr, run)) out.push({ category: "destructive", why: destr.replace(/:\s.*$/, ""), example: cmd });
|
|
192
|
+
if (segments(run).some((seg) => DUMP_BINS.has(firstWord(seg)) && credentialIn(` ${seg}`))) {
|
|
193
|
+
out.push({ category: "credential-read", why: "read a credential file in the shell", example: cmd });
|
|
194
|
+
}
|
|
195
|
+
if (KEYCHAIN_RE.test(run)) out.push({ category: "credential-read", why: "pulled a secret from the keychain or a cloud CLI", example: cmd });
|
|
196
|
+
const up = segments(run).find((seg) => UPLOAD_RE.test(seg));
|
|
197
|
+
if (up) {
|
|
198
|
+
const ext = hostsIn(up);
|
|
199
|
+
const remoteCopy = /^(?:scp|rsync)$/.test(firstWord(up));
|
|
200
|
+
if (ext.length || remoteCopy || /gh\s+gist/.test(up)) out.push({ category: "upload", why: ext.length ? `uploaded to ${displayHost(ext[0])}` : "copied files to a remote machine", example: cmd });
|
|
201
|
+
}
|
|
202
|
+
if (segments(run).some((seg) => firstWord(seg) === "git" && PUSH_RE.test(seg))) out.push({ category: "push", why: "git push", example: cmd });
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (READ_TOOLS.has(ev.tool)) {
|
|
206
|
+
const p = String(ev.input?.file_path ?? ev.input?.path ?? "");
|
|
207
|
+
if (p && credentialIn(` ${p}`)) out.push({ category: "credential-read", why: "read a credential file", example: p });
|
|
208
|
+
}
|
|
209
|
+
if (WRITE_TOOLS.has(ev.tool)) {
|
|
210
|
+
const p = String(ev.input?.file_path ?? ev.input?.path ?? "");
|
|
211
|
+
if (p && SENSITIVE_WRITE_RE.test(p)) out.push({ category: "sensitive-write", why: "changed a file that runs or authenticates on its own", example: p });
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Secrets: anything the call carries. Write/Edit content is the user's
|
|
215
|
+
// own code more often than a leak, so only commands and non-file tools.
|
|
216
|
+
if (!WRITE_TOOLS.has(ev.tool)) {
|
|
217
|
+
let blob = run;
|
|
218
|
+
if (!blob) { try { blob = JSON.stringify(ev.input ?? ""); } catch { blob = ""; } }
|
|
219
|
+
const types = secretTypes(blob.slice(0, 200_000));
|
|
220
|
+
if (types.length) out.push({ category: "secret-in-args", why: types.join(", "), example: blob });
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (ctx.tokens.length || cmd) {
|
|
224
|
+
const id = detectIdentityEgress(ev.tool, ev.input, ctx.tokens);
|
|
225
|
+
if (id) out.push({ category: "identity", why: `${id.what} to ${id.where}`, example: cmd || JSON.stringify(ev.input ?? "") });
|
|
226
|
+
}
|
|
227
|
+
return out;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** Hosts this call talked to (for the "where it went" line). */
|
|
231
|
+
export function hostsFor(ev) {
|
|
232
|
+
const cmd = commandOf(ev);
|
|
233
|
+
if (cmd && /\b(?:curl|wget|http|xh|Invoke-WebRequest|iwr|git\s+clone)\b/.test(cmd)) return hostsIn(cmd);
|
|
234
|
+
const url = ev.input?.url;
|
|
235
|
+
if (typeof url === "string") return hostsIn(url);
|
|
236
|
+
return [];
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export function tokensFor(emails) {
|
|
240
|
+
return identityTokens(undefined, [], emails);
|
|
241
|
+
}
|
package/src/pii.mjs
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// Personal data in text the model read: prompts and tool output.
|
|
2
|
+
// Precision over recall. Every detector returns a masked form for the
|
|
3
|
+
// report and never the value itself.
|
|
4
|
+
|
|
5
|
+
const EMAIL_RE = /(?<![A-Za-z0-9._%+'-])([A-Za-z0-9][A-Za-z0-9._%+-]{0,63})@((?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,24})(?![A-Za-z0-9-])/g;
|
|
6
|
+
// Addresses that are a role, a bot or a tool, not a person.
|
|
7
|
+
const ROLE_LOCAL_RE = /^(?:git|hg|svn|root|admin|administrator|postmaster|webmaster|hostmaster|abuse|security|support|help|info|hello|hi|contact|sales|team|press|legal|privacy|billing|jobs|careers|feedback|notifications?|mailer-daemon|bounce|bot|ci|ops|dev|devs|eng|engineering|it|office|mail|news|newsletter|marketing|accounts?|service|system|alerts?|actions|dependabot|renovate|github-actions|ec2-user|ubuntu|deploy|www-data|user|username|name|you|me|email|someone|somebody|foo|bar|baz|test|tester|testing|example|sample|demo|jane|john|jdoe|alice|bob)$/i;
|
|
8
|
+
const NOT_PERSON_LOCAL_RE = /no-?reply|do-?not-?reply|\[bot\]|^\d+\+/i;
|
|
9
|
+
const NOT_PERSON_DOMAIN_RE = /(?:^|\.)(?:example\.(?:com|org|net)|example|test|invalid|localhost|local|internal|lan|users\.noreply\.github\.com|noreply\.github\.com|sentry\.io|iam\.gserviceaccount\.com|gserviceaccount\.com|appspot\.com|googlegroups\.com|group\.calendar\.google\.com|resource\.calendar\.google\.com|amazonaws\.com|compute\.internal|mailinator\.com|corp\.com|owner\.com|evil\.com|attacker\.com|domain\.com|email\.com|company\.com|test\.com|foo\.com|bar\.com|baz\.com|placeholder\.com|yourcompany\.com|mycompany\.com|yourdomain\.com|mydomain\.com)$/i;
|
|
10
|
+
// acme-corp.dev, x.com, b.co: fixture and placeholder domains.
|
|
11
|
+
const PLACEHOLDER_DOMAIN_RE = /^(?:[a-z0-9]\.|acme[.-]|contoso\.|fabrikam\.|northwind|initech\.|globex\.|example-)/i;
|
|
12
|
+
// user@2x.png, name@file.json: a file, not an address.
|
|
13
|
+
const FILE_TLD_RE = /\.(?:png|jpe?g|gif|svg|webp|ico|js|mjs|cjs|ts|tsx|jsx|json|md|css|scss|html?|py|rb|go|rs|sh|ya?ml|lock|txt|log|map|xml|toml|pdf|zip|gz|tgz)$/i;
|
|
14
|
+
// A real top-level domain: any two-letter country code, or a common generic one.
|
|
15
|
+
const TLD_RE = /\.(?:[a-z]{2}|com|org|net|edu|gov|mil|int|info|biz|name|pro|dev|app|xyz|tech|online|site|cloud|email|mail|page|blog|shop|store|studio|design|agency|company|group|world|life|live|news|media|team|work|works|tools|systems|software|solutions|digital|network|health|care|law|bank|finance|consulting|academy|school|university|church|club|social|community|foundation|events|travel|photo|photos|art|music|games|fun|one|top|vip|global|asia|africa|nyc|london|berlin|paris|tokyo)$/i;
|
|
16
|
+
const COMMON_MAIL = new Set(["gmail.com", "googlemail.com", "outlook.com", "hotmail.com", "live.com", "msn.com", "yahoo.com", "icloud.com", "me.com", "proton.me", "protonmail.com", "aol.com", "qq.com", "163.com"]);
|
|
17
|
+
// A git log, a commit trailer or a package manifest names its authors. That
|
|
18
|
+
// is project metadata, not personal data someone pasted in.
|
|
19
|
+
const AUTHORSHIP_RE = /(?:\bauthor|\bcommitter|\bcommit:|signed-off-by|co-authored-by|reviewed-by|acked-by|reported-by|tested-by|\bmaintainers?|\bcontributors?|\bcopyright|\(c\)|©)[^\n]{0,120}$/i;
|
|
20
|
+
// ssh host targets and URL userinfo: user@host is a login, not an address.
|
|
21
|
+
const LOGIN_BEFORE_RE = /(?::\/\/[^\s/@]{0,200}|(?:^|[\s;&|(])(?:ssh|scp|sftp|rsync|mosh|ssh-copy-id)\b[^\n]{0,200})$/;
|
|
22
|
+
|
|
23
|
+
function maskEmail(local, domain) {
|
|
24
|
+
const d = domain.toLowerCase();
|
|
25
|
+
// A one or two letter name would be given away by its first letter.
|
|
26
|
+
const l = local.length >= 3 ? `${local[0]}***` : "***";
|
|
27
|
+
if (COMMON_MAIL.has(d)) return `${l}@${d}`;
|
|
28
|
+
const tld = d.slice(d.lastIndexOf("."));
|
|
29
|
+
const first = d.slice(0, d.indexOf("."));
|
|
30
|
+
return `${l}@${first.length >= 3 ? d[0] : ""}***${tld}`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 13-19 digits, optionally in groups split by single spaces or dashes.
|
|
34
|
+
const CARD_RE = /(?<![\dA-Za-z.+-])(?:\d[ -]?){12,18}\d(?![\dA-Za-z]|[.-]\d)/g;
|
|
35
|
+
const GROUPED_CARD_RE = /^\d{4}([ -])\d{4}\1\d{4}\1\d{1,7}$|^\d{4}([ -])\d{6}\2\d{5}$/;
|
|
36
|
+
// Card numbers payment providers publish for testing.
|
|
37
|
+
const TEST_CARDS = new Set([
|
|
38
|
+
"4242424242424242", "4111111111111111", "4532015112830366", "4916338506082832", "4024007103939509", "4556737586899855", "4012888888881881", "4009348888881881", "4012000033330026", "4012000077777777",
|
|
39
|
+
"4217651111111119", "4500600000000061", "4005519200000004", "4444333322221111", "4917610000000000", "4988438843884305",
|
|
40
|
+
"5555555555554444", "5105105105105100", "5200828282828210", "5431111111111111", "5454545454545454", "5500000000000004",
|
|
41
|
+
"5577000055770004", "5136333333333335", "5585558555855583", "5399999999999999", "2223003122003222", "2223000048400011",
|
|
42
|
+
"2222400010000008", "2222400030000004", "2222410740360010", "2222410700000002", "2223520443560010",
|
|
43
|
+
"378282246310005", "371449635398431", "378734493671000", "370000000000002", "374245455400126", "340000000000009",
|
|
44
|
+
"6011111111111117", "6011000990139424", "6011000000000004", "6011016011016011", "6011981111111113", "6555900000604105",
|
|
45
|
+
"3056930009020004", "36227206271667", "3566002020360505", "6200000000000005",
|
|
46
|
+
]);
|
|
47
|
+
const CARD_WORD_RE = /\b(?:cards?|card[ _-]?(?:number|num|no)|cc|ccn|pan|visa|master ?card|amex|american express|discover|credit|debit|payment|billing)\b/i;
|
|
48
|
+
const TEST_WORD_RE = /\b(?:tests?|testing|fixtures?|stripe|braintree|adyen|sandbox|examples?|dummy|fake|sample|mock|demo|placeholder)\b/i;
|
|
49
|
+
|
|
50
|
+
export function luhn(digits) {
|
|
51
|
+
let sum = 0;
|
|
52
|
+
let alt = false;
|
|
53
|
+
for (let i = digits.length - 1; i >= 0; i--) {
|
|
54
|
+
let n = digits.charCodeAt(i) - 48;
|
|
55
|
+
if (alt) { n *= 2; if (n > 9) n -= 9; }
|
|
56
|
+
sum += n;
|
|
57
|
+
alt = !alt;
|
|
58
|
+
}
|
|
59
|
+
return sum % 10 === 0;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** A card brand prefix with that brand's length. */
|
|
63
|
+
function cardBrand(d) {
|
|
64
|
+
const n = d.length;
|
|
65
|
+
if (/^4/.test(d) && (n === 13 || n === 16 || n === 19)) return "Visa";
|
|
66
|
+
if ((/^5[1-5]/.test(d) || /^2(?:2[2-9]|[3-6]\d|7[01]|720)/.test(d)) && n === 16) return "Mastercard";
|
|
67
|
+
if (/^3[47]/.test(d) && n === 15) return "Amex";
|
|
68
|
+
if (/^(?:6011|65|64[4-9])/.test(d) && n === 16) return "Discover";
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Published test numbers and obvious placeholders. */
|
|
73
|
+
function testCard(d) {
|
|
74
|
+
return TEST_CARDS.has(d) || /^40000\d/.test(d) || /(\d)\1{6,}/.test(d) || new Set(d).size <= 4;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// US Social Security number, written with dashes.
|
|
78
|
+
const SSN_RE = /(?<![\dA-Za-z-])(\d{3})-(\d{2})-(\d{4})(?![\dA-Za-z-])/g;
|
|
79
|
+
const SSN_EXAMPLES = new Set(["123-45-6789", "078-05-1120", "219-09-9999", "111-11-1111", "222-22-2222", "333-33-3333", "444-44-4444", "555-55-5555", "777-77-7777", "888-88-8888"]);
|
|
80
|
+
const SSN_WORD_RE = /\b(?:ssn|social(?:[ _-]?security)?|soc(?:ial)?[ _-]?sec|taxpayer|tax[ _-]?id|tin)\b/i;
|
|
81
|
+
|
|
82
|
+
const LABEL = { card: "card number", ssn: "US Social Security number", email: "someone's email address" };
|
|
83
|
+
const RANK = { card: 4, ssn: 4, email: 1 };
|
|
84
|
+
|
|
85
|
+
/** Longest text scanned per message. */
|
|
86
|
+
const MAX_TEXT = 300_000;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Personal data in one text. `own` is the set of the user's own addresses
|
|
90
|
+
* (lower case), which are not counted. Returns
|
|
91
|
+
* [{ type, key, masked, label, rank }]; `key` identifies the value for
|
|
92
|
+
* de-duplication and is never shown.
|
|
93
|
+
*/
|
|
94
|
+
export function piiIn(text, own = new Set()) {
|
|
95
|
+
// Tool output often arrives JSON-escaped: a literal \n or \t before a
|
|
96
|
+
// value would otherwise read as a letter glued to it.
|
|
97
|
+
const s = String(text ?? "").slice(0, MAX_TEXT).replace(/\\[nrt]/g, " ").replace(/\\u00[0-9a-fA-F]{2}/g, " ");
|
|
98
|
+
const out = [];
|
|
99
|
+
if (s.length < 6) return out;
|
|
100
|
+
const before = (i, n) => s.slice(Math.max(0, i - n), i);
|
|
101
|
+
const around = (i, len, pre, post) => s.slice(Math.max(0, i - pre), i + len + post);
|
|
102
|
+
|
|
103
|
+
if (s.includes("@")) {
|
|
104
|
+
EMAIL_RE.lastIndex = 0;
|
|
105
|
+
for (const m of s.matchAll(EMAIL_RE)) {
|
|
106
|
+
const local = m[1];
|
|
107
|
+
const domain = m[2];
|
|
108
|
+
const full = `${local}@${domain}`.toLowerCase();
|
|
109
|
+
if (own.has(full)) continue;
|
|
110
|
+
if (ROLE_LOCAL_RE.test(local) || NOT_PERSON_LOCAL_RE.test(local)) continue;
|
|
111
|
+
if (NOT_PERSON_DOMAIN_RE.test(domain) || PLACEHOLDER_DOMAIN_RE.test(domain) || FILE_TLD_RE.test(domain) || !TLD_RE.test(domain)) continue;
|
|
112
|
+
// ssh remotes and scp targets: git@host:path, user@host:/path
|
|
113
|
+
if (s[m.index + m[0].length] === ":") continue;
|
|
114
|
+
const lead = before(m.index, 220);
|
|
115
|
+
if (LOGIN_BEFORE_RE.test(lead) || AUTHORSHIP_RE.test(lead)) continue;
|
|
116
|
+
out.push({ type: "email", key: `email:${full}`, masked: maskEmail(local, domain) });
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (/\d{4}/.test(s)) {
|
|
121
|
+
CARD_RE.lastIndex = 0;
|
|
122
|
+
for (const m of s.matchAll(CARD_RE)) {
|
|
123
|
+
const raw = m[0];
|
|
124
|
+
// One separator style, in groups, or none at all.
|
|
125
|
+
if (/ /.test(raw) && /-/.test(raw)) continue;
|
|
126
|
+
const d = raw.replace(/[ -]/g, "");
|
|
127
|
+
if (d.length < 13 || d.length > 19) continue;
|
|
128
|
+
const grouped = /[ -]/.test(raw);
|
|
129
|
+
if (grouped && !GROUPED_CARD_RE.test(raw)) continue;
|
|
130
|
+
const brand = cardBrand(d);
|
|
131
|
+
if (!brand || !luhn(d) || testCard(d)) continue;
|
|
132
|
+
const near = around(m.index, raw.length, 80, 30);
|
|
133
|
+
// One random 16-digit id in ten passes the checksum. A bare run of
|
|
134
|
+
// digits needs a word nearby that says it is a card.
|
|
135
|
+
if (!grouped && !CARD_WORD_RE.test(near)) continue;
|
|
136
|
+
if (TEST_WORD_RE.test(near)) continue;
|
|
137
|
+
out.push({ type: "card", key: `card:${d}`, masked: `${brand} card ending ${d.slice(-4)}` });
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
SSN_RE.lastIndex = 0;
|
|
141
|
+
for (const m of s.matchAll(SSN_RE)) {
|
|
142
|
+
const [raw, area, group, serial] = m;
|
|
143
|
+
if (area === "000" || area === "666" || area[0] === "9" || group === "00" || serial === "0000") continue;
|
|
144
|
+
if (SSN_EXAMPLES.has(raw)) continue;
|
|
145
|
+
// A bare number in this shape is as often an id or a date range. Ask
|
|
146
|
+
// for a nearby word that says what it is.
|
|
147
|
+
if (!SSN_WORD_RE.test(around(m.index, raw.length, 60, 20))) continue;
|
|
148
|
+
out.push({ type: "ssn", key: `ssn:${raw}`, masked: "***-**-****" });
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
for (const f of out) { f.label = LABEL[f.type]; f.rank = RANK[f.type]; }
|
|
153
|
+
return out;
|
|
154
|
+
}
|