Skip to content

hookSecurity

Block Env in Git Add

A Claude Code hook that stops git add from staging .env files, private keys and credential files, and checks wildcard adds against .gitignore.

npx workspacetemplates add block-env-in-git-add --tool claude-code

Writes 1 file to .claude/

.claude/settings.jsonmerge into your file16 lines · 1.7 KBFile
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "node -e \"let d='';process.stdin.on('data',c=>d+=c).on('end',()=>{let j={};try{j=JSON.parse(d)||{}}catch(e){process.exit(0)}const i=j.tool_input||{};const q=String.fromCharCode(34),sq=String.fromCharCode(39),bs=String.fromCharCode(92),nl=String.fromCharCode(10);const fs=require('fs'),cp=require('child_process');const sec=n=>{n=n.toLowerCase();return(/^[.]env([.].+)?$/.test(n)&&!/[.](example|sample|template|dist|defaults?)$/.test(n))||/[.](pem|key|p12|pfx|jks|keystore|ppk)$/.test(n)||/^id_(rsa|dsa|ecdsa|ed25519)$/.test(n)||/^(credentials([.](json|ya?ml|xml))?|secrets?[.](json|ya?ml|toml)|[.]netrc|[.]pgpass|[.]htpasswd|service-account.*[.]json)$/.test(n)};for(const sg of String(i.command||'').split(/[;&|]+/)){const a=sg.trim().split(/[ ]+/).map(t=>t.split(q).join('').split(sq).join(''));const k=a.indexOf('add');if(a[0]!=='git'||k<0)continue;const args=a.slice(k+1).filter(t=>t&&t!=='--');let bad=args.filter(t=>t[0]!=='-').map(t=>t.split(bs).join('/').split('/').pop()).find(sec);if(!bad&&args.some(t=>['.','./','*','-A','--all',':/'].indexOf(t)>=0)){try{const cwd=j.cwd||process.cwd();for(const n of fs.readdirSync(cwd)){if(sec(n)&&cp.spawnSync('git',['check-ignore','-q',n],{cwd}).status===1&&cp.spawnSync('git',['ls-files','--',n],{cwd,encoding:'utf8'}).stdout.trim()==='')bad=n}}catch(e){}}if(bad){console.error('Blocked: git add would stage '+bad+', which looks like a secret. Add it to .gitignore, stage the other files by name, and keep real values out of the repository.');process.exit(2)}}})\"",
            "timeout": 15
          }
        ]
      }
    ]
  }
}

What it does

Once a secret is committed it lives in history for good. This PreToolUse hook reads each git add command and refuses names such as .env, .env.production, id_rsa, server.pem or credentials.json. Template files like .env.example are fine.

For broad commands such as git add . or git add -A, it lists the working folder and asks git check-ignore about every secret-looking file. Only files that are not ignored trigger the block.

Example

you › Stage everything and commit it as the first version.

agent › git add . is refused because .env.local is not ignored. The agent adds it to .gitignore and stages the remaining files.

Questions

Does it look inside subfolders on git add .?
Only the top level of the working folder is scanned. Files named directly on the command line are checked wherever they live.
Why is .env.example allowed?
Names ending in .example, .sample, .template or .dist are skipped, since those files normally carry dummy values and belong in the repo.
  • Block Chmod 777

    HookSecurity

    Free
  • Block Curl Pipe Shell

    HookSecurity

    Free
  • Block Dangerous Commands

    HookSecurity

    Free
  • Block Force Push

    HookSecurity

    Free