|
| 1 | +'use strict' |
| 2 | + |
| 3 | +// Pre-commit guard: asks Claude (sonnet) to flag any specific customer names |
| 4 | +// or customer-identifying personal information in the commit message and the |
| 5 | +// staged diff. Blocks the commit when Claude reports a match. |
| 6 | +// |
| 7 | +// Usage (from .husky/commit-msg): |
| 8 | +// node ./scripts/check-commit-pii.js "$1" |
| 9 | +// |
| 10 | +// Skip with: DISABLE_PRECOMMIT_PII_CHECK=1 git commit ... |
| 11 | + |
| 12 | +const { execSync, spawnSync } = require('node:child_process') |
| 13 | +const fs = require('node:fs') |
| 14 | + |
| 15 | +const MAX_DIFF_CHARS = 200_000 |
| 16 | +const MAX_BUDGET_USD = '0.10' |
| 17 | + |
| 18 | +function detectClaude() { |
| 19 | + const result = spawnSync('claude', ['--version'], { |
| 20 | + encoding: 'utf8', |
| 21 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 22 | + }) |
| 23 | + return result.status === 0 |
| 24 | +} |
| 25 | + |
| 26 | +function readCommitMessage(msgFilePath) { |
| 27 | + if (!msgFilePath || !fs.existsSync(msgFilePath)) { |
| 28 | + return '' |
| 29 | + } |
| 30 | + // Strip git's comment lines (lines starting with #) and trailing whitespace. |
| 31 | + return fs |
| 32 | + .readFileSync(msgFilePath, 'utf8') |
| 33 | + .split(/\r?\n/) |
| 34 | + .filter(line => !line.startsWith('#')) |
| 35 | + .join('\n') |
| 36 | + .trim() |
| 37 | +} |
| 38 | + |
| 39 | +function readStagedDiff() { |
| 40 | + try { |
| 41 | + let diff = execSync('git diff --cached --no-color', { |
| 42 | + encoding: 'utf8', |
| 43 | + maxBuffer: 50 * 1024 * 1024, |
| 44 | + }) |
| 45 | + if (diff.length > MAX_DIFF_CHARS) { |
| 46 | + diff = |
| 47 | + diff.slice(0, MAX_DIFF_CHARS) + |
| 48 | + '\n\n[...diff truncated for length...]\n' |
| 49 | + } |
| 50 | + return diff |
| 51 | + } catch (e) { |
| 52 | + console.error(`[pii-check] Could not read staged diff: ${e.message}`) |
| 53 | + return '' |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function buildPrompt(commitMsg, stagedDiff) { |
| 58 | + return `You are a strict reviewer guarding a public-ish git repository against accidentally leaking information about Socket's customers. |
| 59 | +
|
| 60 | +Inspect the COMMIT MESSAGE and STAGED DIFF below and decide whether they mention any of: |
| 61 | +- A specific customer / client / end-user organization by name (a named business that uses Socket). |
| 62 | +- Personal information that identifies a specific customer end-user (real person names, customer emails, customer account IDs, internal customer references). |
| 63 | +- Any phrasing that would let an outside reader figure out which customer reported an issue or requested a feature. |
| 64 | +
|
| 65 | +DO NOT flag: |
| 66 | +- Generic third-party tool, vendor, or platform names (e.g. npm, pnpm, GitHub, Linear, Slack, Sentry, Coana, Grafana, Anthropic, Vercel, AWS). |
| 67 | +- Socket's own product names, internal team names, employee names, or the Socket organization itself. |
| 68 | +- Names of open-source libraries, dependencies, or maintainers found in package metadata. |
| 69 | +- Test fixture data that is obviously synthetic ("foo", "bar", "test-user", "example.com"). |
| 70 | +
|
| 71 | +Reply with EXACTLY ONE LINE, one of: |
| 72 | +- OK |
| 73 | +- BLOCK: <one short sentence describing what was found and where> |
| 74 | +
|
| 75 | +=== COMMIT MESSAGE === |
| 76 | +${commitMsg || '(empty)'} |
| 77 | +
|
| 78 | +=== STAGED DIFF === |
| 79 | +${stagedDiff || '(empty)'} |
| 80 | +` |
| 81 | +} |
| 82 | + |
| 83 | +function askClaude(prompt) { |
| 84 | + const result = spawnSync( |
| 85 | + 'claude', |
| 86 | + [ |
| 87 | + '--print', |
| 88 | + '--model', |
| 89 | + 'sonnet', |
| 90 | + // Disable every tool so the model can only emit text. No tools => no |
| 91 | + // permission prompts => safe to run unattended from a git hook. |
| 92 | + '--tools', |
| 93 | + '', |
| 94 | + '--disable-slash-commands', |
| 95 | + '--max-budget-usd', |
| 96 | + MAX_BUDGET_USD, |
| 97 | + '--no-session-persistence', |
| 98 | + ], |
| 99 | + { |
| 100 | + input: prompt, |
| 101 | + encoding: 'utf8', |
| 102 | + stdio: ['pipe', 'pipe', 'pipe'], |
| 103 | + maxBuffer: 10 * 1024 * 1024, |
| 104 | + }, |
| 105 | + ) |
| 106 | + if (result.error) { |
| 107 | + return { ok: false, error: result.error.message } |
| 108 | + } |
| 109 | + if (result.status !== 0) { |
| 110 | + // claude sometimes writes its error to stdout in --print mode, so include |
| 111 | + // both streams in the message for diagnosability. |
| 112 | + const tail = `${result.stderr || ''}${result.stdout || ''}`.trim() |
| 113 | + return { |
| 114 | + ok: false, |
| 115 | + error: `claude exited with status ${result.status}${tail ? `: ${tail}` : ''}`, |
| 116 | + } |
| 117 | + } |
| 118 | + return { ok: true, output: (result.stdout || '').trim() } |
| 119 | +} |
| 120 | + |
| 121 | +function main() { |
| 122 | + if (process.env['DISABLE_PRECOMMIT_PII_CHECK']) { |
| 123 | + console.log('[pii-check] Skipping (DISABLE_PRECOMMIT_PII_CHECK is set).') |
| 124 | + return 0 |
| 125 | + } |
| 126 | + if (!detectClaude()) { |
| 127 | + console.warn( |
| 128 | + '[pii-check] WARNING: `claude` CLI not found on PATH. Skipping PII check.', |
| 129 | + ) |
| 130 | + console.warn( |
| 131 | + '[pii-check] Install Claude Code (https://claude.com/claude-code) to enable this guard.', |
| 132 | + ) |
| 133 | + return 0 |
| 134 | + } |
| 135 | + const commitMsg = readCommitMessage(process.argv[2]) |
| 136 | + const stagedDiff = readStagedDiff() |
| 137 | + if (!commitMsg && !stagedDiff) { |
| 138 | + return 0 |
| 139 | + } |
| 140 | + const prompt = buildPrompt(commitMsg, stagedDiff) |
| 141 | + const result = askClaude(prompt) |
| 142 | + if (!result.ok) { |
| 143 | + console.warn( |
| 144 | + `[pii-check] WARNING: Claude check failed to run: ${result.error}`, |
| 145 | + ) |
| 146 | + console.warn('[pii-check] Allowing commit; please review manually.') |
| 147 | + return 0 |
| 148 | + } |
| 149 | + // Match the first non-empty line so wrapping or stray whitespace does not |
| 150 | + // hide a verdict. |
| 151 | + const firstLine = result.output |
| 152 | + .split(/\r?\n/) |
| 153 | + .map(line => line.trim()) |
| 154 | + .find(line => line.length > 0) |
| 155 | + if (firstLine && /^BLOCK\b/i.test(firstLine)) { |
| 156 | + console.error('') |
| 157 | + console.error('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━') |
| 158 | + console.error('[pii-check] Commit blocked: customer reference detected.') |
| 159 | + console.error('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━') |
| 160 | + console.error(result.output) |
| 161 | + console.error('') |
| 162 | + console.error( |
| 163 | + 'Revise the commit message and/or staged changes to remove the reference.', |
| 164 | + ) |
| 165 | + console.error( |
| 166 | + 'If this is a false positive, bypass once with: DISABLE_PRECOMMIT_PII_CHECK=1 git commit ...', |
| 167 | + ) |
| 168 | + console.error('') |
| 169 | + return 1 |
| 170 | + } |
| 171 | + // Treat anything that is not an explicit OK as a malformed response and |
| 172 | + // fail closed. Otherwise a Claude refusal, hallucination, or stray |
| 173 | + // explanatory text would silently let a problematic commit through. |
| 174 | + if (!firstLine || !/^OK\b/i.test(firstLine)) { |
| 175 | + console.error('') |
| 176 | + console.error('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━') |
| 177 | + console.error('[pii-check] Commit blocked: unrecognized Claude response.') |
| 178 | + console.error('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━') |
| 179 | + console.error(result.output || '(empty response)') |
| 180 | + console.error('') |
| 181 | + console.error( |
| 182 | + 'Expected the first non-empty line to start with "OK" or "BLOCK:".', |
| 183 | + ) |
| 184 | + console.error( |
| 185 | + 'If this is a transient model error, retry; otherwise bypass with: DISABLE_PRECOMMIT_PII_CHECK=1 git commit ...', |
| 186 | + ) |
| 187 | + console.error('') |
| 188 | + return 1 |
| 189 | + } |
| 190 | + console.log('[pii-check] No customer references detected.') |
| 191 | + return 0 |
| 192 | +} |
| 193 | + |
| 194 | +process.exitCode = main() |
0 commit comments