Skip to content

feat: Implement ordered rules security system#5

Merged
genro merged 7 commits intomainfrom
feature/config-security
Nov 4, 2025
Merged

feat: Implement ordered rules security system#5
genro merged 7 commits intomainfrom
feature/config-security

Conversation

@genro
Copy link
Contributor

@genro genro commented Nov 3, 2025

Summary

This PR implements a comprehensive security system for gtext based on ordered rules with first-match wins evaluation logic.

Key Features:

  • 🛡️ Secure by default: Everything denied unless explicitly allowed
  • 📋 Ordered rules: First-match wins pattern (like firewall rules)
  • 🎯 Protocol-specific: Independent rules for cli:, static:, glob: protocols
  • 🌍 Global & Project: System-wide rules + project-specific configuration
  • ⚠️ Dangerous characters: Metacharacters (;, &, |, $, `, <, >) always blocked
  • 🔄 Rule management: Complete CLI for adding, removing, and reordering rules

Implementation:

  • config.py: Ordered rules system with first-match evaluation algorithm
  • cli.py: Complete CLI commands for security configuration management
  • include.py: Security checks at protocol dispatch point
  • 65 comprehensive tests (unit, CLI, integration) with 90% coverage

Documentation:

  • SECURITY.md: Full security documentation with visual examples and step-by-step explanations
  • QUICKSTART-SECURITY.md: 5-minute quick start guide
  • README.md: Updated with security section and CLI reference

Test Plan

  • All 65 tests passing (unit + CLI + integration)
  • 90% code coverage on config.py
  • Manual testing of CLI commands
  • First-match wins logic verified with integration tests
  • Dangerous metacharacters blocking verified
  • Global vs project configuration tested
  • Pattern matching with wildcards tested
  • Documentation reviewed and complete

Breaking Changes

⚠️ This introduces secure-by-default behavior

Existing gtext users will need to configure security rules after upgrading:

# Quick setup for common use cases
gtext config :cli add_rule "date" allow --global
gtext config :cli add_rule "git status" allow --global
gtext config :static add_rule "*.md" allow --global

See QUICKSTART-SECURITY.md for migration guidance.

Fixes #4

genro added 2 commits November 3, 2025 21:24
This commit adds complete documentation for the new security system:

- SECURITY.md: Full security documentation with detailed explanations
  - Rule evaluation algorithm (first-match wins)
  - Visual examples with step-by-step tables
  - Complete CLI command reference
  - Best practices and troubleshooting guide
  - FAQ section for common questions

- QUICKSTART-SECURITY.md: 5-minute quick start guide
  - Basic concepts and first steps
  - Common setup examples (docs, Python, paranoid mode)
  - Rule ordering explanation
  - Global vs project configuration

- README.md: Updated with security section
  - Quick security setup examples
  - Key security features overview
  - Rule evaluation explanation
  - Links to full documentation

Documentation highlights:
- Clear explanation of first-match wins behavior
- Visual tables showing rule evaluation step-by-step
- Examples of correct vs wrong rule ordering
- Security best practices for different use cases
- Complete CLI command reference for config management

Related to #4
This commit implements a comprehensive security system for gtext based
on ordered rules with first-match wins logic.

## Core Implementation

### config.py
- Add ordered rules system for protocols (cli, static, glob)
- Implement first-match wins evaluation algorithm
- Add rule management methods:
  - add_rule: Add new security rules
  - remove_rule: Remove by index or name
  - move_rule: Reorder rules (up, down, top, bottom)
  - clear_rules: Remove all rules for a protocol
- Global and project-level configuration support
- Dangerous metacharacter detection (;, &, |, $, `, <, >)
- Pattern matching with Unix wildcards (*, ?, [])

### cli.py
- Add 'config' command group with subcommands:
  - show: View merged configuration (global + project)
  - :cli/:static/:glob subgroups for protocol-specific management
  - add_rule: Add security rules
  - remove_rule: Remove rules by index or name
  - list_rules: List rules for a protocol
  - clear_rules: Clear all rules
  - rule: Reorder rules (up, down, top, bottom)
- Support for --global flag to manage system-wide rules
- Support for --name flag to name rules for easier management
- JSON output format with --json flag

### include.py
- Integrate security checks at protocol dispatch point
- Block commands/includes without matching allow rule
- Return descriptive error messages for security violations
- Enforce secure-by-default policy

## Test Coverage

### tests/test_config.py (39 tests)
- Unit tests for all config.py methods
- Edge case tests for wildcards, boundaries, errors
- Pattern matching tests (*, ?, [])
- Protocol independence tests
- Global/project merge logic tests
- 90% coverage on config.py

### tests/test_cli_security.py (17 tests)
- CLI command tests using subprocess
- Tests for all config CLI commands
- Global vs project configuration
- Rule management (add, remove, move, clear)
- JSON output format
- Help message tests

### tests/test_security_integration.py (9 tests)
- Integration tests with rendering
- First-match wins logic verification
- Dangerous metacharacter blocking
- Pattern matching in real documents
- Global precedence verification
- Protocol-specific rule enforcement

## Key Features

- **Secure by Default**: Everything denied unless explicitly allowed
- **First-Match Wins**: Rules evaluated in order, first match determines result
- **Protocol-Specific**: Independent rules for cli, static, glob
- **Global & Project**: System-wide + project-specific configuration
- **Rule Ordering**: Critical for security - specific before general
- **Dangerous Characters**: Always blocked (;, &, |, $, `, <, >)
- **Pattern Matching**: Unix wildcards (*, ?, [abc], [a-z])

## Test Results

All 65 tests passing:
- test_config.py: 39 tests ✓
- test_cli_security.py: 17 tests ✓
- test_security_integration.py: 9 tests ✓
- Coverage: 90% on config.py

Fixes #4
@genro genro force-pushed the feature/config-security branch from 55e75a2 to d6b571c Compare November 3, 2025 20:25
genro added 3 commits November 3, 2025 22:02
- Split long lines to respect 100 char limit
- Rename DANGEROUS constant to lowercase dangerous
- Fix import ordering
- Create conftest.py with setup_permissive_security fixture
- Automatically configure allow-all rules for non-security tests
- Exclude security-specific test modules (test_cli_security, test_config, test_security_integration)
- Fix test_cli_command_nonzero_exit to use 'false' instead of dangerous semicolon

This allows existing tests to work with the new security system
without modification while keeping security tests isolated.
@codecov
Copy link

codecov bot commented Nov 3, 2025

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

genro added 2 commits November 3, 2025 23:02
Replace ✓ -> OK:, ✗ -> ERROR:, → -> -> to fix encoding issues
on Windows where charmap codec cannot handle Unicode characters.

This fixes test failures on Windows CI runners.
Replace 'date' command with 'python -c' for cross-platform
compatibility. The 'date' command is not available or works
differently on Windows.

This fixes test_render_with_allowed_command on Windows CI.
@genro genro merged commit 80d3ab9 into main Nov 4, 2025
11 of 13 checks passed
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.

Security concerns for cli: handle

1 participant