Block Direct Edits to CI/CD Pipeline Config

A pipeline config file is a privilege-escalation path in disguise — it runs with CI's own credentials, often broader than the repo's. Blocking direct edits forces a PR review path for the one file type in a repo that can grant itself more access.

Production Guardrails Event: PreToolUse Claude Code Open source · Free

1. Save this as the hook script

#!/usr/bin/env python3
"""PreToolUse hook — blocks direct edits to CI/CD pipeline config files.
Wire this to the Write|Edit matcher."""
import json, re, sys

data = json.load(sys.stdin)
path = (data.get("tool_input") or {}).get("file_path", "")

CI_PATHS = [r"\.github/workflows/", r"\.gitlab-ci\.yml$", r"Jenkinsfile$"]

if any(re.search(p, path) for p in CI_PATHS):
    print(f"BLOCKED: {path} is a CI/CD pipeline config file — these can grant "
          f"themselves broader credentials than the repo itself. Route through a PR.", file=sys.stderr)
    sys.exit(2)

sys.exit(0)

Save to a path inside your project, e.g. .claude/hooks/block-edits-to-cicd-config.sh, then chmod +x it.

2. Wire it into .claude/settings.json

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Write|Edit",
        "hooks": [
          {"type": "command", "command": "python3 .claude/hooks/block-edits-to-cicd-config.py"}
        ]
      }
    ]
  }
}

Before you use this

A hook runs with your local shell's permissions — review the script above before adding it, the same way you'd review any script you didn't write yourself. See our guide on hook security risks for the specific CVEs this category of feature has already produced.

Related hooks

← All hooks
FEEDBACK