Skip to content

hookSecurity

Protect Env Files

A Claude Code hook that blocks the agent from reading or writing .env files and private keys such as .pem and id_rsa, while allowing .env.example.

npx workspacetemplates add protect-env-files --tool claude-code

Writes 1 file to .claude/

.claude/settings.jsonmerge into your file16 lines · 797 BFile
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Edit|Write|Read",
        "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 f=String(i.file_path||'').split(String.fromCharCode(92)).join('/');const b=f.split('/').pop();const ok=/[.](example|sample|template|dist)$/i.test(b);const bad=(/^[.]env([.].*)?$/i.test(b)&&!ok)||/[.](pem|key|p12|pfx)$/i.test(b)||/^id_(rsa|dsa|ecdsa|ed25519)$/.test(b);if(bad){console.error('Blocked: '+b+' holds secrets. Ask the user for the value you need, or use a .env.example file.');process.exit(2)}})\"",
            "timeout": 10
          }
        ]
      }
    ]
  }
}

What it does

Environment files and private keys hold the secrets that matter most, and an agent that reads them can echo them into logs, chats or commits. This hook checks the path of every Read, Edit and Write call.

It blocks .env and .env.* files, .pem, .key, .p12 and .pfx files, and SSH keys such as id_rsa and id_ed25519. Template files ending in .example, .sample or .template stay open.

Example

you › Read .env and tell me which database URL the app uses.

agent › Read blocked: .env holds secrets. The agent asks you for the variable name or checks .env.example instead.

Questions

Does it block cat .env in the shell?
No. The matcher covers the file tools only. Add a Bash rule to your permissions if you need that too.
Can it read .env.example?
Yes. Files ending in example, sample, template or dist are allowed.
  • Block Chmod 777

    HookSecurity

    Free
  • Block Curl Pipe Shell

    HookSecurity

    Free
  • Block Dangerous Commands

    HookSecurity

    Free
  • Block Env in Git Add

    HookSecurity

    Free