Backup-bdg/OpenHands
0
1# Security2 3Given the impressive capabilities of OpenHands and similar coding agents, ensuring robust security measures is essential to prevent unintended actions or security breaches. The SecurityAnalyzer framework provides a structured approach to monitor and analyze agent actions for potential security risks.4 5To enable this feature:6* From the web interface7 * Open Configuration (by clicking the gear icon in the bottom right)8 * Select a Security Analyzer from the dropdown9 * Save settings10 * (to disable) repeat the same steps, but click the X in the Security Analyzer dropdown11* From config.toml12```toml13[security]14# Enable confirmation mode15confirmation_mode = true16# The security analyzer to use17security_analyzer = "your-security-analyzer"18```19(to disable) remove the lines from config.toml20 21## SecurityAnalyzer Base Class22 23The `SecurityAnalyzer` class (analyzer.py) is an abstract base class designed to listen to an event stream and analyze actions for security risks and eventually act before the action is executed. Below is a detailed explanation of its components and methods:24 25### Initialization26 27- **event_stream**: An instance of `EventStream` that the analyzer will listen to for events.28 29### Event Handling30 31- **on_event(event: Event)**: Handles incoming events. If the event is an `Action`, it evaluates its security risk and acts upon it.32 33### Abstract Methods34 35- **handle_api_request(request: Request)**: Abstract method to handle API requests.36- **log_event(event: Event)**: Logs events.37- **act(event: Event)**: Defines actions to take based on the analyzed event.38- **security_risk(event: Action)**: Evaluates the security risk of an action and returns the risk level.39- **close()**: Cleanups resources used by the security analyzer.40 41In conclusion, a concrete security analyzer should evaluate the risk of each event and act accordingly (e.g. auto-confirm, send Slack message, etc).42 43For customization and decoupling from the OpenHands core logic, the security analyzer can define its own API endpoints that can then be accessed from the frontend. These API endpoints need to be secured (do not allow more capabilities than the core logic44provides).45 46## How to implement your own Security Analyzer47 481. Create a submodule in [security](/openhands/security/) with your analyzer's desired name49 * Have your main class inherit from [SecurityAnalyzer](/openhands/security/analyzer.py)50 * Optional: define API endpoints for `/api/security/{path:path}` to manage settings,512. Add your analyzer class to the [options](/openhands/security/options.py) to have it be visible from the frontend combobox523. Optional: implement your modal frontend (for when you click on the lock) in [security](/frontend/src/components/modals/security/) and add your component to [Security.tsx](/frontend/src/components/modals/security/Security.tsx)53 54## Implemented Security Analyzers55 56### Invariant57 58It uses the [Invariant Analyzer](https://github.com/invariantlabs-ai/invariant) to analyze traces and detect potential issues with OpenHands's workflow. It uses confirmation mode to ask for user confirmation on potentially risky actions.59 60This allows the agent to run autonomously without fear that it will inadvertently compromise security or perform unintended actions that could be harmful.61 62Features:63 64* Detects:65 * potential secret leaks by the agent66 * security issues in Python code67 * malicious bash commands68 * dangerous user tasks (browsing agent setting)69 * harmful content generation (browsing agent setting)70* Logs:71 * actions and their associated risk72 * OpenHands traces in JSON format73* Run-time settings:74 * the [invariant policy](https://github.com/invariantlabs-ai/invariant?tab=readme-ov-file#policy-language)75 * acceptable risk threshold76 * (Optional) check_browsing_alignment flag77 * (Optional) guardrail_llm that assesses if the agent behaviour is safe78 79Browsing Agent Safety:80 81* Guardrail feature that uses the underlying LLM of the agent to:82 * Examine the user's request and check if it is harmful.83 * Examine the content entered by the agent in a textbox (argument of the “fill” browser action) and check if it is harmful.84 85* If the guardrail evaluates either of the 2 conditions to be true, it emits a change_agent_state action and transforms the AgentState to ERROR. This stops the agent from proceeding further.86 87* To enable this feature: In the InvariantAnalyzer object, set the check_browsing_alignment attribute to True and initialize the guardrail_llm attribute with an LLM object.88 