⚠️ Important: This demo requires GitHub Copilot CLI version 0.0.396 or later (released January 27, 2026). Earlier versions do not support using pre-tool-use hooks to block tools.
This repository demonstrates how to use GitHub Copilot CLI hooks to prevent the agent from accessing certain files. This is useful for protecting sensitive files like credentials, private keys, or proprietary code that you don't want the AI to read or use.
- A
preToolUsehook intercepts all tool calls before they execute - For file-accessing tools (
view,edit,create,grep,glob), the hook extracts the file path - The path is checked against patterns in
.github/blocked-files.txt - If matched, the tool call is denied with an explanatory message
- The agent sees the denial and should respect it
.github/
├── hooks/
│ └── block-files.json # Hook configuration
├── scripts/
│ ├── check-file-access.sh # Bash implementation (macOS/Linux)
│ └── check-file-access.ps1 # PowerShell implementation (Windows)
└── blocked-files.txt # Patterns for blocked files
secrets/ # Example blocked directory
├── api-credentials.txt
└── database.key
src/
└── app.js # Example allowed file
The blocked-files.txt file supports a simplified glob syntax:
| Pattern | Meaning | Example |
|---|---|---|
* |
Any characters except / |
*.key matches secret.key but not dir/secret.key |
** |
Any characters including / |
secrets/** matches secrets/foo/bar.txt |
? |
Exactly one character (not /) |
file?.txt matches file1.txt |
literal |
Exact path match | config.yml matches only config.yml |
Lines starting with # are comments. Empty lines are ignored.
# Block all files in the secrets directory
secrets/**
# Block private key files
*.pem
*.key
# Block specific sensitive files
.env.production
config/database.yml
- Open this repository with GitHub Copilot CLI
- Ask the agent to read a blocked file:
Show me the contents of secrets/api-credentials.txt - The agent should receive a denial message and refuse to read the file
- Ask the agent to read an allowed file:
Show me the contents of src/app.js - This should work normally
- Bash commands: The hook checks paths passed to file tools, but cannot parse arbitrary bash commands like
cat secrets/file.txt - Indirect access: The agent could potentially work around this by using bash to read files directly
- Pattern matching: The simplified glob syntax doesn't support
{a,b}alternation or[abc]character classes
To add more robust protection:
- Block bash access to sensitive paths: Modify the hook to inspect bash commands for blocked paths
- Add more tools: Extend the tool list in the scripts to cover additional file-accessing tools
- Support more patterns: Enhance
glob_to_regexto support character classes and alternation
- macOS/Linux:
bash,jq - Windows: PowerShell 5.1+
MIT