# secret_in_doc_guard.ps1 -- PreToolUse guard for Claude Code (matcher: Write|Edit). # # Why: an audit of the author's notes and documents found a live password and an access key # written in clear text next to the fact they belonged to. Git keeps history forever, and a # memory note is loaded into every conversation. This guard refuses to put a secret VALUE # into a document or a note; the parameter name and a pointer ("see .env") stay allowed. # # Scope: Write/Edit whose target is a document: *.md, *.txt, *.rst, *.html, *.json.example # (memory notes are *.md, so they are covered). # Blocks when the NEW text has a secret-looking assignment: # (password|passwd|pwd|secret|token|api_key|apikey|private_key) [:=] # Allowed values (placeholders): starts with < $ { % * ( [, contains xxx, ***, ..., changeme, # example, placeholder, redacted, your_; env-style NAME_IN_CAPS; dotted parameter keys. # Code files are out of scope on purpose (they read secrets from env; false positives hurt). # # Setup and platform notes -- as in edit_path_guard.ps1 (pwsh, ASCII-only source, register in # every project, run test_guards.ps1 after changes). # # Contract: read JSON on stdin; on block print permissionDecision=deny and exit 0. # FAIL-OPEN on any parse error. The value itself is never echoed back. $ErrorActionPreference = 'Stop' try { $raw = [Console]::In.ReadToEnd() if ([string]::IsNullOrWhiteSpace($raw)) { exit 0 } $data = $raw | ConvertFrom-Json $tool = [string]$data.tool_name $fp = [string]$data.tool_input.file_path if ([string]::IsNullOrWhiteSpace($fp)) { exit 0 } $leaf = (($fp -replace '/', '\') -split '\\')[-1].ToLower() if ($leaf -notmatch '\.(md|txt|rst|html?)$' -and $leaf -notlike '*.json.example') { exit 0 } $text = $null if ($tool -eq 'Write') { $text = [string]$data.tool_input.content } elseif ($tool -eq 'Edit') { $text = [string]$data.tool_input.new_string } else { exit 0 } if ([string]::IsNullOrEmpty($text)) { exit 0 } $rx = '(?i)(?[^\s"''`<>|,;]{8,})' $hit = $null foreach ($m in [regex]::Matches($text, $rx)) { $v = $m.Groups['val'].Value if ($v -match '^[\$\{\%\*\(\[]') { continue } # $VAR, {{x}}, %x%, ***, (see ...) if ($v -match '(?i)xxx|\*\*\*|\.\.\.|changeme|example|placeholder|redacted|your[_-]|<|>') { continue } if ($v -cmatch '^[a-z_]+(?:[.][a-z_]+)+$') { continue } # dotted identifier = a parameter key, not a value if ($v -cmatch '^[A-Z][A-Z0-9_]{5,}$') { continue } # NAME_OF_ENV_VAR if ($v -match '^(?i)(true|false|none|null|required|optional)') { continue } if ($v -notmatch '[0-9]' -and $v -notmatch '[^A-Za-z]') { continue } # a plain word, not a secret $hit = $m.Groups[1].Value break } if (-not $hit) { exit 0 } $reason = "Blocked by secret_in_doc_guard: the text assigns a value to '$hit' inside a document ($leaf). " + "Documents under git keep secrets forever. Write the parameter NAME and a pointer instead " + "(e.g. 'password -- see .env / password manager'). If this is a false positive (not a secret), " + "reword so the value is not an assignment, or use a placeholder like ." $payload = @{ hookSpecificOutput = @{ hookEventName = 'PreToolUse' permissionDecision = 'deny' permissionDecisionReason = $reason } } Write-Output ($payload | ConvertTo-Json -Depth 5 -Compress) exit 0 } catch { exit 0 # fail-open }