-
Notifications
You must be signed in to change notification settings - Fork 230
feat(security): defense-in-depth security analyzers #2472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
csmith49
merged 21 commits into
OpenHands:main
from
Fieldnote-Echo:feat/defense-in-depth-security-analyzer
Apr 3, 2026
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
a9c4800
feat(examples): add defense-in-depth security analyzer with adversari…
Fieldnote-Echo e18fc80
docs: surface _EXTRACT_HARD_CAP tradeoff for downstream integrators
Fieldnote-Echo 3433b1e
Merge branch 'main' into feat/defense-in-depth-security-analyzer
enyst 1e50a1a
refactor(tests): extract importlib hack into conftest.py
Fieldnote-Echo c398cf2
fix: split scanning into exec and text corpora, fix dd operand order
Fieldnote-Echo 90bc325
style: apply pre-commit ruff-format fixes
Fieldnote-Echo 86ce55f
style: fix ruff-format in conftest.py
Fieldnote-Echo 5ea6c32
Merge branch 'main' into feat/defense-in-depth-security-analyzer
Fieldnote-Echo 327ea5d
feat(security): add SecurityRisk.__lt__ for natural max() ordering
Fieldnote-Echo bb2d9c7
refactor(security): promote defense-in-depth analyzers to SDK proper
Fieldnote-Echo a991b50
docs: update defense-in-depth guide for SDK promotion
Fieldnote-Echo a518e28
fix(security): restore exec/os.system patterns, normalize rail helper…
Fieldnote-Echo 3e0e260
chore: renumber defense-in-depth example to 47 (45 taken by parallel_…
Fieldnote-Echo 915bf9e
docs: rewrite docstrings and guide for adult learning theory
Fieldnote-Echo 007c820
Merge remote-tracking branch 'upstream/main' into refactor/defense-in…
Fieldnote-Echo c7b0391
test: add missing DET_EXEC_CODE_SUBPROCESS stable ID assertion
Fieldnote-Echo fd5af05
Merge branch 'main' into feat/defense-in-depth-security-analyzer
Fieldnote-Echo a885b66
fix: use HIGH as default confirmation threshold, show MEDIUM as stric…
Fieldnote-Echo ea6c8ee
fix(security): address code review findings from Codex and Gemini
Fieldnote-Echo 65b15cf
fix(security): address csmith49 review round 2
Fieldnote-Echo f70ee3f
Merge branch 'main' into feat/defense-in-depth-security-analyzer
csmith49 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
examples/01_standalone_sdk/47_defense_in_depth_security.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| """Defense-in-Depth Security: composing local analyzers with ConfirmRisky. | ||
|
|
||
| This example demonstrates how to wire the defense-in-depth analyzer family | ||
| into a conversation. The analyzers classify agent actions at the action | ||
| boundary; the confirmation policy decides whether to prompt the user. | ||
|
|
||
| Analyzer selection does not automatically change confirmation policy -- | ||
| you must configure both explicitly. | ||
| """ | ||
|
|
||
| from openhands.sdk.security import ( | ||
| ConfirmRisky, | ||
| EnsembleSecurityAnalyzer, | ||
| PatternSecurityAnalyzer, | ||
| PolicyRailSecurityAnalyzer, | ||
| SecurityRisk, | ||
| ) | ||
|
|
||
|
|
||
| # Create the analyzer ensemble | ||
| security_analyzer = EnsembleSecurityAnalyzer( | ||
| analyzers=[ | ||
| PolicyRailSecurityAnalyzer(), | ||
| PatternSecurityAnalyzer(), | ||
| ] | ||
| ) | ||
|
|
||
| # Confirmation policy: prompt the user for HIGH-risk actions | ||
| confirmation_policy = ConfirmRisky(threshold=SecurityRisk.HIGH) | ||
|
|
||
| # Wire into a conversation: | ||
| # | ||
| # conversation = Conversation(agent=agent, workspace=".") | ||
| # conversation.set_security_analyzer(security_analyzer) | ||
| # conversation.set_confirmation_policy(confirmation_policy) | ||
| # | ||
| # Every agent action now passes through the analyzer. | ||
| # HIGH -> confirmation prompt. MEDIUM/LOW -> allowed. | ||
| # UNKNOWN -> confirmed by default (confirm_unknown=True). | ||
| # | ||
| # For stricter environments, lower the threshold: | ||
| # confirmation_policy = ConfirmRisky(threshold=SecurityRisk.MEDIUM) | ||
|
|
||
| print("Defense-in-depth security analyzer configured.") | ||
| print(f"Analyzer: {security_analyzer}") | ||
| print(f"Confirmation policy: {confirmation_policy}") | ||
| print("EXAMPLE_COST: 0") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
openhands-sdk/openhands/sdk/security/defense_in_depth/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| """Deterministic, local security analyzers for agent action boundaries. | ||
|
|
||
| Two analyzers, each owning one job: | ||
|
|
||
| - ``PatternSecurityAnalyzer`` -- regex signatures with two-corpus scanning | ||
| - ``PolicyRailSecurityAnalyzer`` -- composed-condition rules (fetch-to-exec, etc.) | ||
|
|
||
| Wire them into a conversation alongside ``EnsembleSecurityAnalyzer`` and | ||
| ``ConfirmRisky`` to classify agent actions before execution. No network | ||
| calls, no model inference, no dependencies beyond the SDK runtime. | ||
| """ | ||
|
|
||
| from openhands.sdk.security.defense_in_depth.pattern import PatternSecurityAnalyzer | ||
| from openhands.sdk.security.defense_in_depth.policy_rails import ( | ||
| PolicyRailSecurityAnalyzer, | ||
| ) | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "PatternSecurityAnalyzer", | ||
| "PolicyRailSecurityAnalyzer", | ||
| ] |
244 changes: 244 additions & 0 deletions
244
openhands-sdk/openhands/sdk/security/defense_in_depth/pattern.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,244 @@ | ||
| """Classify agent actions by matching content against known threat signatures. | ||
|
|
||
| When an agent is about to run ``rm -rf /``, you want to catch it. When | ||
| the agent merely *thinks about* ``rm -rf /`` while running ``ls /tmp``, | ||
| you do not. This module solves that with two scanning corpora: | ||
|
|
||
| - **Executable corpus** (tool_name, tool_call arguments): scanned for | ||
| shell-destructive, code-execution, and network-to-exec patterns. | ||
| - **All-field corpus** (executable + thought/reasoning/summary): scanned | ||
| for injection and social-engineering patterns that are dangerous | ||
| wherever they appear. | ||
|
|
||
| Each pattern carries a stable detector ID for telemetry readiness. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| from typing import Any | ||
|
|
||
| from pydantic import Field, PrivateAttr | ||
|
|
||
| from openhands.sdk.event import ActionEvent | ||
| from openhands.sdk.logger import get_logger | ||
| from openhands.sdk.security.analyzer import SecurityAnalyzerBase | ||
| from openhands.sdk.security.defense_in_depth.utils import ( | ||
| _extract_content, | ||
| _extract_exec_content, | ||
| _normalize, | ||
| ) | ||
| from openhands.sdk.security.risk import SecurityRisk | ||
|
|
||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Stable detector IDs -- do not change between releases without documentation. | ||
| # Format: DET_{CORPUS}_{FAMILY}_{SPECIFIC} | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| DET_EXEC_DESTRUCT_RM_RF = "exec.destruct.rm_rf" | ||
| DET_EXEC_DESTRUCT_SUDO_RM = "exec.destruct.sudo_rm" | ||
| DET_EXEC_DESTRUCT_MKFS = "exec.destruct.mkfs" | ||
| DET_EXEC_DESTRUCT_DD = "exec.destruct.dd_raw_disk" | ||
| DET_EXEC_CODE_EVAL = "exec.code.eval_call" | ||
| DET_EXEC_CODE_EXEC = "exec.code.exec_call" | ||
| DET_EXEC_CODE_OS_SYSTEM = "exec.code.os_system" | ||
| DET_EXEC_CODE_SUBPROCESS = "exec.code.subprocess" | ||
| DET_EXEC_NET_CURL_EXEC = "exec.net.curl_pipe_exec" | ||
| DET_EXEC_NET_WGET_EXEC = "exec.net.wget_pipe_exec" | ||
| DET_EXEC_NET_CURL = "exec.net.curl" | ||
| DET_EXEC_NET_WGET = "exec.net.wget" | ||
| DET_INJECT_OVERRIDE = "inject.override" | ||
| DET_INJECT_MODE_SWITCH = "inject.mode_switch" | ||
| DET_INJECT_IDENTITY = "inject.identity" | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Pattern definitions | ||
| # | ||
| # Format: (regex_pattern, description, detector_id) | ||
| # | ||
| # Pattern design constraints: | ||
| # - No unbounded .* or .+ around alternations (catastrophic backtracking) | ||
| # - Risky spans are bounded ({0,N}) to prevent ReDoS | ||
| # - \s* and \w+ are acceptable in non-alternation positions | ||
| # - \b-anchored to avoid substring matches | ||
| # - IGNORECASE compiled in | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| DEFAULT_HIGH_PATTERNS: list[tuple[str, str, str]] = [ | ||
| # Destructive filesystem operations | ||
| ( | ||
| r"\brm\s+(?:-[frR]{2,}|-[rR]\s+-f|-f\s+-[rR]" | ||
| r"|--recursive\s+--force|--force\s+--recursive)\b", | ||
| "Recursive force-delete (rm -rf variants)", | ||
| DET_EXEC_DESTRUCT_RM_RF, | ||
| ), | ||
| (r"\bsudo\s+rm\b", "Privileged file deletion", DET_EXEC_DESTRUCT_SUDO_RM), | ||
| (r"\bmkfs\.\w+", "Filesystem format command", DET_EXEC_DESTRUCT_MKFS), | ||
| (r"\bdd\b.{0,100}of=/dev/", "Raw disk write", DET_EXEC_DESTRUCT_DD), | ||
| # Code invocation via dynamic interpreters | ||
| (r"\beval\s*\(", "Dynamic code evaluation", DET_EXEC_CODE_EVAL), | ||
| (r"\bexec\s*\(", "Dynamic code execution", DET_EXEC_CODE_EXEC), | ||
| (r"\bos\.system\s*\(", "OS-level command execution", DET_EXEC_CODE_OS_SYSTEM), | ||
| ( | ||
| r"\bsubprocess\.(?:call|run|Popen|check_output|check_call)\s*\(", | ||
| "Subprocess invocation", | ||
| DET_EXEC_CODE_SUBPROCESS, | ||
| ), | ||
| # Download-and-run | ||
| ( | ||
| r"\bcurl\b[^|]{0,200}\|\s*(?:ba)?sh\b", | ||
| "Download and run (curl | sh)", | ||
| DET_EXEC_NET_CURL_EXEC, | ||
| ), | ||
| ( | ||
| r"\bwget\b[^|]{0,200}\|\s*(?:ba)?sh\b", | ||
| "Download and run (wget | sh)", | ||
| DET_EXEC_NET_WGET_EXEC, | ||
| ), | ||
| ] | ||
|
|
||
| DEFAULT_MEDIUM_PATTERNS: list[tuple[str, str, str]] = [ | ||
| # Network access without invocation pipe | ||
| (r"\bcurl\b.{0,100}https?://", "HTTP request via curl", DET_EXEC_NET_CURL), | ||
| (r"\bwget\b.{0,100}https?://", "Download via wget", DET_EXEC_NET_WGET), | ||
| ] | ||
|
|
||
| # Injection patterns: scanned against ALL fields (invocation + reasoning). | ||
| # These are textual attacks targeting instruction-following, not the OS. | ||
|
|
||
| DEFAULT_INJECTION_HIGH_PATTERNS: list[tuple[str, str, str]] = [ | ||
| ( | ||
| r"\b(?:ignore|disregard|forget|override|bypass)\s+(?:all\s+)?" | ||
| r"(?:previous|prior|above)\s+(?:instructions?|prompts?|rules?|directives?)\b", | ||
| "Instruction override attempt", | ||
| DET_INJECT_OVERRIDE, | ||
| ), | ||
| ] | ||
|
|
||
| DEFAULT_INJECTION_MEDIUM_PATTERNS: list[tuple[str, str, str]] = [ | ||
| ( | ||
| r"\byou\s+are\s+now\s+(?:in\s+)?(?:\w+\s+)?mode\b", | ||
| "Mode switching attempt", | ||
| DET_INJECT_MODE_SWITCH, | ||
| ), | ||
| ( | ||
| r"\bpretend\s+(?:you\s+are|to\s+be)\s+(?:a\s+)?different\b", | ||
| "Identity manipulation", | ||
| DET_INJECT_IDENTITY, | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # PatternSecurityAnalyzer | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| class PatternSecurityAnalyzer(SecurityAnalyzerBase): | ||
| """Catch dangerous agent actions through deterministic signature scanning. | ||
|
|
||
| Use this when you want fast, local, no-network threat detection at the | ||
| action boundary. It returns ``SecurityRisk.HIGH``, ``MEDIUM``, or ``LOW`` | ||
| -- pair it with ``ConfirmRisky`` to decide what gets confirmed. | ||
|
|
||
| The key design choice: shell-destructive patterns only scan what the | ||
| agent will *execute* (tool arguments), never what it *thought about* | ||
| (reasoning text). Injection patterns scan everything, because | ||
| "ignore all previous instructions" is dangerous wherever it appears. | ||
|
|
||
| Normalization is always on -- invisible characters and fullwidth | ||
| substitutions are collapsed before matching. | ||
|
|
||
| Example:: | ||
|
|
||
| from openhands.sdk.security import PatternSecurityAnalyzer, ConfirmRisky | ||
|
|
||
| analyzer = PatternSecurityAnalyzer() | ||
| policy = ConfirmRisky(threshold=SecurityRisk.MEDIUM) | ||
| """ | ||
|
|
||
| high_patterns: list[tuple[str, str, str]] = Field( | ||
| default_factory=lambda: list(DEFAULT_HIGH_PATTERNS), | ||
| description="HIGH patterns scanned against executable fields only", | ||
| ) | ||
| medium_patterns: list[tuple[str, str, str]] = Field( | ||
| default_factory=lambda: list(DEFAULT_MEDIUM_PATTERNS), | ||
| description="MEDIUM patterns scanned against executable fields only", | ||
| ) | ||
| injection_high_patterns: list[tuple[str, str, str]] = Field( | ||
| default_factory=lambda: list(DEFAULT_INJECTION_HIGH_PATTERNS), | ||
| description="HIGH patterns scanned against all fields", | ||
| ) | ||
| injection_medium_patterns: list[tuple[str, str, str]] = Field( | ||
| default_factory=lambda: list(DEFAULT_INJECTION_MEDIUM_PATTERNS), | ||
| description="MEDIUM patterns scanned against all fields", | ||
| ) | ||
|
|
||
| _compiled_high: list[tuple[re.Pattern[str], str, str]] = PrivateAttr( | ||
| default_factory=list, | ||
| ) | ||
| _compiled_medium: list[tuple[re.Pattern[str], str, str]] = PrivateAttr( | ||
| default_factory=list, | ||
| ) | ||
| _compiled_injection_high: list[tuple[re.Pattern[str], str, str]] = PrivateAttr( | ||
| default_factory=list, | ||
| ) | ||
| _compiled_injection_medium: list[tuple[re.Pattern[str], str, str]] = PrivateAttr( | ||
| default_factory=list, | ||
| ) | ||
|
|
||
| def model_post_init(self, __context: Any) -> None: | ||
| """Compile regex patterns after model initialization.""" | ||
| self._compiled_high = [ | ||
| (re.compile(p, re.IGNORECASE), d, det_id) | ||
| for p, d, det_id in self.high_patterns | ||
| ] | ||
| self._compiled_medium = [ | ||
| (re.compile(p, re.IGNORECASE), d, det_id) | ||
| for p, d, det_id in self.medium_patterns | ||
| ] | ||
| self._compiled_injection_high = [ | ||
| (re.compile(p, re.IGNORECASE), d, det_id) | ||
| for p, d, det_id in self.injection_high_patterns | ||
| ] | ||
| self._compiled_injection_medium = [ | ||
| (re.compile(p, re.IGNORECASE), d, det_id) | ||
| for p, d, det_id in self.injection_medium_patterns | ||
| ] | ||
|
|
||
| def security_risk(self, action: ActionEvent) -> SecurityRisk: | ||
| """Evaluate security risk via two-corpus pattern matching.""" | ||
| exec_content = _normalize(_extract_exec_content(action)) | ||
| all_content = _normalize(_extract_content(action)) | ||
|
|
||
| if not exec_content and not all_content: | ||
| return SecurityRisk.LOW | ||
|
|
||
| # HIGH: patterns on executable fields only | ||
| for pattern, _desc, det_id in self._compiled_high: | ||
| if pattern.search(exec_content): | ||
| logger.debug("Pattern matched: %s -> HIGH", det_id) | ||
| return SecurityRisk.HIGH | ||
|
|
||
| # HIGH: injection patterns on all fields | ||
| for pattern, _desc, det_id in self._compiled_injection_high: | ||
| if pattern.search(all_content): | ||
| logger.debug("Pattern matched: %s -> HIGH", det_id) | ||
| return SecurityRisk.HIGH | ||
|
|
||
| # MEDIUM: patterns on executable fields only | ||
| for pattern, _desc, det_id in self._compiled_medium: | ||
| if pattern.search(exec_content): | ||
| logger.debug("Pattern matched: %s -> MEDIUM", det_id) | ||
| return SecurityRisk.MEDIUM | ||
|
|
||
| # MEDIUM: injection patterns on all fields | ||
| for pattern, _desc, det_id in self._compiled_injection_medium: | ||
| if pattern.search(all_content): | ||
| logger.debug("Pattern matched: %s -> MEDIUM", det_id) | ||
| return SecurityRisk.MEDIUM | ||
|
|
||
| return SecurityRisk.LOW |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.