Skip to content

Add hook safety controls for structkit - #190

Merged
httpdss merged 2 commits into
mainfrom
cursor/hook-safety-5707
Aug 23, 2026
Merged

httpdss merged 2 commits into
mainfrom
cursor/hook-safety-5707

Conversation

@httpdss

@httpdss httpdss commented Aug 23, 2026

Copy link
Copy Markdown
Owner

Issue

Fixes #100

Description

This PR implements comprehensive hook safety mechanisms to prevent arbitrary shell command execution when structures are generated via MCP or CI/CD pipelines.

Key Features

1. --no-hooks Flag

  • Allows disabling all pre/post hooks
  • Can be set via CLI flag or STRUCTKIT_NO_HOOKS environment variable
  • Recommended for CI/CD and untrusted structure definitions

2. Interactive Confirmation Prompts

  • By default, users are prompted to confirm before hooks execute
  • Shows all commands that will run
  • Skipped in --non-interactive mode
  • Prevents accidental execution of hooks

3. Allowlist Support

  • Create .struct-hooks-allowlist file to restrict which commands can run
  • Supports both exact command matches and base command (first word) matches
  • Comments (lines starting with #) and empty lines are ignored
  • Custom allowlist path via --hooks-allowlist flag or STRUCTKIT_HOOKS_ALLOWLIST env var
  • Auto-detects .struct-hooks-allowlist in current directory

4. MCP Safety

  • MCP generate_structure calls now skip hooks by default (no_hooks=true)
  • Prevents arbitrary shell execution when AI tools or automation generate structures
  • Can be explicitly enabled with no_hooks=false (not recommended)

Implementation Details

Modified Files:

  • structkit/commands/generate.py: Added safety flags, allowlist parsing, confirmation prompts
  • structkit/mcp_server.py: Added no_hooks parameter (defaults to true)
  • docs/hooks.md: Comprehensive documentation of all safety features
  • docs/mcp-integration.md: Updated to document no_hooks parameter and safety practices
  • tests/test_hooks.py: Extensive test coverage for all new features
  • tests/test_commands.py: Updated test to handle new flags
  • tests/test_commands_more.py: Fixed test to use --non-interactive

Examples:

Skip hooks in CI/CD:

structkit generate .struct.yaml --no-hooks

Use allowlist for controlled execution:

# .struct-hooks-allowlist
echo
git
npm
python
./scripts/prep.sh

Interactive confirmation:

⚠️  The following pre-hooks will be executed:
  - echo "Preparing environment..."
  - ./scripts/prep.sh

Do you want to run these pre-hooks? [y/N]:

Why This Makes MCP/CI Safer

  1. MCP Integration: Hooks are disabled by default when structures are generated through MCP, preventing AI tools from unknowingly executing arbitrary shell commands embedded in structure YAML files.

  2. CI/CD Pipelines: The --no-hooks flag allows safe execution in automated environments where hooks shouldn't run or need explicit allowlisting.

  3. Interactive Safety: When running locally, users are prompted before hooks execute, providing visibility into what will run.

  4. Allowlist Control: Teams can create allowlists for production environments to ensure only approved commands execute.

Checklist

  • I have read the contributing guidelines.
  • My code follows the code style of this project.
  • I have performed a self-review of my own code.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes (251 tests passing).
  • I have updated the documentation accordingly.

Additional Comments

This implementation follows the requirements from issue #100 while ensuring backward compatibility. Existing users won't see any breaking changes:

  • Interactive mode now prompts (can be disabled with --non-interactive)
  • All tests updated to handle the new behavior
  • MCP integration is safer by default but can be configured if needed

The change is reviewable and focused on security without introducing unrelated refactors.

Open in Web Open in Cursor 

cursoragent and others added 2 commits August 23, 2026 17:20
Implement comprehensive hook safety mechanisms to prevent arbitrary shell
command execution when structures are generated via MCP or CI/CD.

Changes:
- Add --no-hooks flag and STRUCTKIT_NO_HOOKS env var to disable all hooks
- Add --hooks-allowlist flag and STRUCTKIT_HOOKS_ALLOWLIST env var for command allowlisting
- Implement interactive confirmation prompts before running hooks (skipped with --non-interactive)
- Auto-detect .struct-hooks-allowlist in current directory
- Update MCP generate_structure to skip hooks by default (no_hooks=true)
- Add comprehensive test coverage for all safety features
- Update documentation (hooks.md, mcp-integration.md) with safety guidance

Fixes #100

Co-authored-by: Kenneth Belitzky <kenny@belitzky.com>
Co-authored-by: Kenneth Belitzky <kenny@belitzky.com>
@httpdss
httpdss marked this pull request as ready for review August 23, 2026 17:45
@httpdss
httpdss merged commit ef8bd95 into main Aug 23, 2026
3 checks passed
@httpdss
httpdss deleted the cursor/hook-safety-5707 branch August 23, 2026 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pre/Post hooks safety controls

2 participants