-
Notifications
You must be signed in to change notification settings - Fork 683
Add WebSocket generator for real-time LLM security testing #1379
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
Open
dyrtyData
wants to merge
41
commits into
NVIDIA:main
Choose a base branch
from
dyrtyData:websocket-generator-feature
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
a401113
Add WebSocket generator for real-time LLM security testing
dyrtyData b356b68
Apply suggestions from code review
dyrtyData 976869f
Merge branch 'NVIDIA:main' into websocket-generator-feature
dyrtyData e220924
Implement improved WebSocket generator with websockets library
dyrtyData 0639bb9
Remove old markdown documentation
dyrtyData 012e960
Add websockets library as dependency
dyrtyData 3efda30
Fix WebSocket generator test failures
dyrtyData 7690301
Apply suggestion from @jmartin-tech
dyrtyData e19892c
Apply suggestion from @jmartin-tech
dyrtyData 27e9130
Apply suggestion from @jmartin-tech
dyrtyData c63a409
Remove PRtests/ from .gitignore
dyrtyData 26a7924
Re-apply critical test fixes after architectural changes
dyrtyData 68d4fda
Fix documentation security: Use environment variables for passwords
dyrtyData d074a25
Address security and logging concerns from code review
dyrtyData 2c6ca51
Update garak/generators/websocket.py
dyrtyData 65bf1e8
Update garak/generators/websocket.py
dyrtyData 7c53a7f
Update garak/generators/websocket.py
dyrtyData 0f272e7
Move environment variable access to _validate_env_var
dyrtyData 79d7670
Improve test robustness with dynamic values
dyrtyData e7c0346
Move DEFAULT_PARAMS from module level to class level
dyrtyData 5f6b8de
Add ENV_VAR class constant for environment variable name
dyrtyData bc864de
Apply suggestion from @jmartin-tech
dyrtyData 0fbb0a4
Apply suggestion from @jmartin-tech
dyrtyData 0de7c07
Apply suggestion from @jmartin-tech
dyrtyData 72a576f
Fix syntax error in test configuration
dyrtyData 05c3d53
Fix environment variable access initialization order bug
dyrtyData f26358f
Fix remaining WebSocket generator test failures
dyrtyData 39cf785
Fix AsyncMock test failure in WebSocket generator
dyrtyData 06ac326
Update docs/source/garak.generators.websocket.rst
dyrtyData 365ce09
Update docs/source/garak.generators.websocket.rst
dyrtyData 7ff3f5b
Update garak/generators/websocket.py
dyrtyData 810f7d7
Refactor WebSocket generator to use framework-compliant initialization
dyrtyData e9c65cd
Add smart detection for unsupported WebSocket scenarios
dyrtyData 5065027
Fix security vulnerability in WebSocket URI handling
dyrtyData af36310
Update garak/generators/websocket.py
dyrtyData b5f62ca
Update garak/generators/websocket.py
dyrtyData b2824da
Update garak/generators/websocket.py
dyrtyData 0bb5386
fix: resolve maintainer issues for WebSocket generator
dyrtyData 35614fe
fix: remove unused imports and clarify documentation
dyrtyData aa3a2af
refactor: implement jmartin-tech's Option 2 for name handling
dyrtyData 2388455
fix: update async tests to use config_root pattern
dyrtyData 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
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,184 @@ | ||
| # WebSocket Generator for Garak | ||
|
|
||
| This adds WebSocket support to garak, enabling security testing of WebSocket-based LLM services. | ||
|
|
||
| ## Features | ||
|
|
||
| - **Full WebSocket Protocol Support** - RFC 6455 compliant WebSocket implementation | ||
| - **Flexible Authentication** - Basic Auth, Bearer tokens, custom headers | ||
| - **Response Pattern Recognition** - Configurable typing indicators and response timing | ||
| - **SSH Tunnel Compatible** - Works with secure remote access patterns | ||
| - **Production Tested** - Successfully tested with real WebSocket LLM services | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Command Line | ||
|
|
||
| ```bash | ||
| python -m garak \ | ||
| --model_type websocket.WebSocketGenerator \ | ||
| --generator_options '{"websocket": {"WebSocketGenerator": {"endpoint": "ws://localhost:3000/", "auth_type": "basic", "username": "your_user", "password": "your_pass", "api_key": "your_key", "conversation_id": "session_id"}}}' \ | ||
| --probes encoding,dan,jailbreak \ | ||
| --generations 1 | ||
| ``` | ||
|
|
||
| ### Programmatic Usage | ||
|
|
||
| ```python | ||
| from garak.generators.websocket import WebSocketGenerator | ||
| from garak.attempt import Message, Conversation | ||
|
|
||
| generator = WebSocketGenerator( | ||
| endpoint="ws://localhost:3000/", | ||
| auth_type="basic", | ||
| username="your_user", | ||
| password="your_pass", | ||
| api_key="your_key", | ||
| conversation_id="session_id" | ||
| ) | ||
|
|
||
| # Create a conversation | ||
| conversation = Conversation() | ||
| conversation.add_message(Message("Test prompt", role="user")) | ||
|
|
||
| # Generate response | ||
| responses = generator._call_model(conversation) | ||
| print(responses[0].text) | ||
| ``` | ||
|
|
||
| ## Configuration Parameters | ||
|
|
||
| | Parameter | Type | Description | Default | | ||
| |-----------|------|-------------|---------| | ||
| | `endpoint` | str | WebSocket URL (ws:// or wss://) | Required | | ||
| | `auth_type` | str | Authentication method ('basic', 'bearer', 'custom') | 'basic' | | ||
| | `username` | str | Username for basic authentication | None | | ||
| | `password` | str | Password for basic authentication | None | | ||
| | `api_key` | str | API key parameter | None | | ||
| | `conversation_id` | str | Session/conversation identifier | None | | ||
| | `custom_headers` | dict | Additional WebSocket headers | {} | | ||
| | `response_timeout` | int | Response timeout in seconds | 15 | | ||
| | `typing_indicators` | list | Frames to ignore (e.g., typing indicators) | ['typing on', 'typing off'] | | ||
| | `response_after_typing` | bool | Whether response comes after typing indicators | True | | ||
| | `max_message_length` | int | Maximum message length | 1000 | | ||
|
|
||
| ## Authentication Types | ||
|
|
||
| ### Basic Authentication | ||
| ```json | ||
| { | ||
| "auth_type": "basic", | ||
| "username": "your_username", | ||
| "password": "your_password" | ||
| } | ||
| ``` | ||
|
|
||
| ### Bearer Token | ||
| ```json | ||
| { | ||
| "auth_type": "bearer", | ||
| "api_key": "your_bearer_token" | ||
| } | ||
| ``` | ||
|
|
||
| ### Custom Headers | ||
| ```json | ||
| { | ||
| "auth_type": "custom", | ||
| "custom_headers": { | ||
| "Authorization": "Custom your_token", | ||
| "X-API-Key": "your_api_key" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## WebSocket LLM Patterns | ||
|
|
||
| The generator handles common WebSocket LLM patterns: | ||
|
|
||
| ### Typing Indicators | ||
| Many chat-based LLMs send typing indicators: | ||
| ``` | ||
| → "Hello!" | ||
| ← "typing on" | ||
| ← "typing off" | ||
| ← "Hi there! How can I help?" | ||
| ``` | ||
|
|
||
| Configure with: | ||
| ```json | ||
| { | ||
| "typing_indicators": ["typing on", "typing off"], | ||
| "response_after_typing": true | ||
| } | ||
| ``` | ||
|
|
||
| ### Direct Response | ||
| Some LLMs respond immediately: | ||
| ``` | ||
| → "Hello!" | ||
| ← "Hi there! How can I help?" | ||
| ``` | ||
|
|
||
| Configure with: | ||
| ```json | ||
| { | ||
| "response_after_typing": false | ||
| } | ||
| ``` | ||
|
|
||
| ## SSH Tunnel Support | ||
|
|
||
| For remote WebSocket services: | ||
|
|
||
| ```bash | ||
| # Set up tunnel | ||
| ssh -L 3000:remote-llm-service.com:3000 your-server | ||
|
|
||
| # Use localhost endpoint | ||
| python -m garak \ | ||
| --model_type websocket.WebSocketGenerator \ | ||
| --generator_options '{"websocket": {"WebSocketGenerator": {"endpoint": "ws://localhost:3000/"}}}' \ | ||
| --probes dan | ||
| ``` | ||
|
|
||
| ## Example: Testing a Chat LLM | ||
|
|
||
| ```bash | ||
| python -m garak \ | ||
| --model_type websocket.WebSocketGenerator \ | ||
| --generator_options '{"websocket": {"WebSocketGenerator": { | ||
| "endpoint": "ws://chat-service.example.com:8080/chat", | ||
| "auth_type": "basic", | ||
| "username": "test_user", | ||
| "password": "test_pass", | ||
| "conversation_id": "test_session", | ||
| "typing_indicators": ["typing_start", "typing_end"], | ||
| "response_after_typing": true | ||
| }}}' \ | ||
| --probes encoding,injection,jailbreak \ | ||
| --generations 2 | ||
| ``` | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| ### Connection Issues | ||
| - Verify WebSocket endpoint is reachable | ||
| - Check authentication credentials | ||
| - Ensure proper SSL/TLS configuration for wss:// endpoints | ||
|
|
||
| ### No Responses | ||
| - Adjust `response_timeout` for slow services | ||
| - Check `typing_indicators` configuration | ||
| - Verify `response_after_typing` setting matches your service | ||
|
|
||
| ### Authentication Failures | ||
| - Verify username/password for basic auth | ||
| - Check API key format for bearer auth | ||
| - Ensure custom headers are correctly formatted | ||
|
|
||
| ## Contributing | ||
|
|
||
| This WebSocket generator was developed to enable security testing of WebSocket-based LLM services. It has been tested with various WebSocket LLM implementations and follows RFC 6455 WebSocket standards. | ||
|
|
||
| For issues or improvements, please contribute to the garak project on GitHub. |
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.