Skip to content

hookSecurity

Block Chmod 777

A Claude Code hook that refuses chmod calls giving everyone write access, such as 777, 666 or a+w, and suggests tighter modes like 755 and 644.

npx workspacetemplates add block-chmod-777 --tool claude-code

Writes 1 file to .claude/

.claude/settings.jsonmerge into your file16 lines · 1017 BFile
{
  "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 worldW=m=>m.split(',').some(x=>/^[0-7]{2}[2367]$/.test(x)||/^[0246][0-7]{2}[2367]$/.test(x)||/^([ugo]*o[ugo]*|a)[+=][rwxXst]*w/.test(x));for(const sg of String(i.command||'').split(/[;&|]+/)){const a=sg.trim().split(/[ ]+/);if(a[0]==='sudo')a.shift();if(a[0]!=='chmod')continue;const m=a.slice(1).find(t=>t[0]!=='-');if(m&&worldW(m)){console.error('Blocked: chmod '+m+' makes files writable by every user. Use 755 for programs and folders, 644 for plain files, or 775 and 664 to share with a group.');process.exit(2)}}})\"",
            "timeout": 10
          }
        ]
      }
    ]
  }
}

What it does

World-writable files are a common shortcut for a permission error and a common hole afterwards. This PreToolUse hook parses each chmod call and blocks any mode that lets other users write, in numeric or symbolic form, with or without -R.

Sticky shared folders such as 1777 are allowed, and so are modes like 755, 644 or g+w. When it blocks, the message names the mode and offers safer ones.

Example

you › The script will not run. Make everything executable with chmod -R 777 .

agent › Blocked with a note that 777 lets every user write. The agent switches to chmod +x on the one script that needs it.

Questions

Is chmod +x allowed?
Yes. Adding the execute bit for the owner or everyone does not make anything writable, so it passes.
What about numeric modes with four digits?
A leading digit with the sticky bit, like 1777, is allowed. Others such as 0777 or 2777 are refused because other users could write.
  • Block Curl Pipe Shell

    HookSecurity

    Free
  • Block Dangerous Commands

    HookSecurity

    Free
  • Block Env in Git Add

    HookSecurity

    Free
  • Block Force Push

    HookSecurity

    Free