Skip to content

feat: add script-based rule evaluation#24

Merged
ammario merged 12 commits intomainfrom
interactive
Sep 11, 2025
Merged

feat: add script-based rule evaluation#24
ammario merged 12 commits intomainfrom
interactive

Conversation

@ammario
Copy link
Copy Markdown
Member

@ammario ammario commented Sep 11, 2025

Summary

  • Adds support for custom request evaluation via external scripts as an alternative to regex rules
  • Scripts provide flexible, programmatic control over request filtering
  • Includes enhanced 403 responses with context from evaluation

Changes

New Features

  • Script-based evaluation: New -s/--script <prog> flag accepts a program path or inline command
  • Environment variables: Scripts receive HTTPJAIL_URL, HTTPJAIL_METHOD, HTTPJAIL_HOST, HTTPJAIL_SCHEME, HTTPJAIL_PATH
  • Flexible control: Exit code 0 allows requests, non-zero blocks them
  • Enhanced responses: Script stdout becomes additional context in 403 responses
  • Inline scripts: Commands with spaces are executed via default shell

Architecture Improvements

  • Refactored rules system into trait-based architecture in src/rules/
  • PatternRuleEngine for existing regex rules
  • ScriptRuleEngine for new script evaluation
  • Unified logging via LoggingRuleEngine wrapper
  • DRY improvements in rule engine composition

Usage Examples

# Script file
httpjail --script /path/to/check.sh -- ./my-app

# Inline script
httpjail --script '[ "$HTTPJAIL_HOST" = "github.com" ] && exit 0 || exit 1' -- git pull

# With logging
httpjail --script ./validator.sh --request-log requests.log -- npm install

Breaking Changes

  • Removed unimplemented --interactive flag
  • -r/--rule and -s/--script options are mutually exclusive

Test Plan

  • Unit tests for script evaluation logic
  • Integration tests for various script scenarios
  • Tests pass with cargo test
  • No clippy warnings with cargo clippy --all-targets -- -D warnings
  • Code formatted with cargo fmt

🤖 Generated with Claude Code

ammario and others added 2 commits September 10, 2025 20:56
Adds support for custom request evaluation via external scripts as an alternative to regex rules.

Features:
- New -s/--script flag accepts a program path or inline command
- Scripts receive request data via environment variables (HTTPJAIL_URL, HTTPJAIL_METHOD, HTTPJAIL_HOST, HTTPJAIL_SCHEME, HTTPJAIL_PATH)
- Exit code 0 allows requests, non-zero blocks them
- Script stdout becomes additional context in 403 responses
- Inline scripts with spaces are executed via default shell

Architecture:
- Refactored rules system into trait-based architecture in src/rules/
- PatternRuleEngine for existing regex rules (src/rules/pattern.rs)
- ScriptRuleEngine for new script evaluation (src/rules/script.rs)
- Unified logging via LoggingRuleEngine wrapper
- Enhanced 403 responses to include evaluation context

Breaking changes:
- Removed unimplemented --interactive flag
- -r/--rule and -s/--script options are mutually exclusive

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Ensures the new script-based rule evaluation tests run in CI across all platforms:
- macOS tests
- Linux tests
- Weak mode tests

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codex Review: Here are some suggestions.

Reply with @codex fix comments to fix any unresolved comments.

About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you open a pull request for review, mark a draft as ready, or comment "@codex review". If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex fix this CI failure" or "@codex address that feedback".

Comment thread src/rules/script.rs
ammario and others added 10 commits September 10, 2025 21:04
- Use std::thread::spawn instead of blocking tokio runtime
- Add 5-second timeout for script execution with process kill on timeout
- Fix Unix-specific imports in tests for cross-platform compatibility
- Properly handle script execution errors and timeouts

This addresses the DoS vulnerability where slow/hanging scripts could block
the entire proxy runtime thread.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Fixed 'Text file busy' error on Linux when executing script files
- Changed from using fs::write to write_all/flush followed by into_temp_path()
- into_temp_path() closes the file handle, allowing immediate execution on Linux
- Tests now pass on both macOS and Linux environments

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Add async-trait dependency for async trait methods
- Convert RuleEngineTrait and all implementations to async
- Use tokio::spawn_blocking in ScriptRuleEngine to prevent blocking
- Update all proxy call sites to use await
- Convert all tests to async with #[tokio::test]

This prevents script execution from blocking the async runtime,
improving server performance under concurrent load.
- Replace spawn_blocking with tokio::process for async child handling
- Remove inefficient 10ms polling loop, use async wait instead
- Add kill_on_drop(true) for automatic cleanup on timeout
- Change info/warn logs to debug for non-server mode
- Update CLAUDE.md with logging guidelines

This prevents info/warn logs from interfering with the underlying
process output in CLI mode, and improves script execution efficiency.
@ammario ammario merged commit dbf4896 into main Sep 11, 2025
7 checks passed
@ammario ammario deleted the interactive branch September 11, 2025 03:15
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.

1 participant