Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /agents /guardrails.py
| """ | |
| Guardrails – Sicherheitskontrollen für alle Agenten. | |
| - Private-IP-Blocking | |
| - Timeout-Management | |
| - Resource-Limits | |
| - Read-Only Default | |
| - Domain-Filtering | |
| """ | |
| import ipaddress | |
| import os | |
| import re | |
| from typing import Any | |
| # Private/Internal IP ranges | |
| PRIVATE_RANGES = [ | |
| "10.0.0.0/8", | |
| "172.16.0.0/12", | |
| "192.168.0.0/16", | |
| "127.0.0.0/8", | |
| "169.254.0.0/16", | |
| "::1/128", | |
| "fc00::/7", | |
| "fe80::/10", | |
| ] | |
| # Blocked domains | |
| INTERNAL_DOMAINS = [ | |
| "localhost", | |
| "local", | |
| ".internal", | |
| ".local", | |
| ".lan", | |
| ".corp", | |
| ".home.arpa", | |
| ] | |
| # Risk levels | |
| RISK_SAFE = 0 # Automated – recon only | |
| RISK_NORMAL = 1 # Automated – standard scanning | |
| RISK_ELEVATED = 2 # Requires confirmation | |
| RISK_AGGRESSIVE = 3 # Requires --force flag | |
| # Defaults | |
| DEFAULT_TIMEOUT = 600 # 10 minutes max per task | |
| MAX_MEMORY_MB = 512 | |
| MAX_CPU_SHARES = 512 | |
| BLOCKED_PORTS = [135, 137, 138, 139, 445] # SMB/etc only on explicit allow | |
| class GuardrailViolation(Exception): | |
| pass | |
| def check_ip(target: str) -> None: | |
| """Block private/internal IPs unless explicitly allowed.""" | |
| try: | |
| ip = ipaddress.ip_address(target) | |
| for rng in PRIVATE_RANGES: | |
| if ip in ipaddress.ip_network(rng, strict=False): | |
| raise GuardrailViolation(f"Blocked private IP: {target} in range {rng}. Use --internal to allow.") | |
| except ValueError: | |
| pass # Not an IP, might be a hostname | |
| def check_domain(target: str) -> None: | |
| """Block internal domains.""" | |
| target_lower = target.lower() | |
| for dom in INTERNAL_DOMAINS: | |
| if target_lower == dom.lstrip(".") or target_lower.endswith(dom): | |
| raise GuardrailViolation(f"Blocked internal domain: {target}. Use --internal to allow.") | |
| def check_target(target: str, risk_level: int = RISK_NORMAL, force: bool = False) -> None: | |
| """Full target validation based on risk level.""" | |
| if risk_level >= RISK_AGGRESSIVE and not force: | |
| raise GuardrailViolation(f"Risk level {risk_level} requires --force flag") | |
| if risk_level >= RISK_ELEVATED: | |
| return # Allow internal for elevated+ with confirmation | |
| # Default: block private/internal | |
| check_ip(target) | |
| check_domain(target) | |
| def validate_timeout(timeout: int) -> int: | |
| """Enforce max timeout.""" | |
| return min(timeout, DEFAULT_TIMEOUT) | |
| def sanitize_command(cmd: str) -> str: | |
| """Remove dangerous operations from shell commands.""" | |
| dangerous = [ | |
| "rm -rf /", | |
| "mkfs.", | |
| "dd if=/dev/zero", | |
| "chmod 777 /", | |
| "> /dev/", | |
| ":(){ :|:& };:", | |
| ] | |
| for d in dangerous: | |
| if d in cmd.lower(): | |
| raise GuardrailViolation(f"Blocked dangerous command pattern: {d}") | |
| return cmd | |
Xet Storage Details
- Size:
- 2.73 kB
- Xet hash:
- bb211ae7a5cfb25fa89c31d736625bc4505197fbbb23554d063dbb0657f2b1c5
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.