Skip to content
Open
Show file tree
Hide file tree
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 Sep 18, 2025
b356b68
Apply suggestions from code review
dyrtyData Sep 25, 2025
976869f
Merge branch 'NVIDIA:main' into websocket-generator-feature
dyrtyData Sep 25, 2025
e220924
Implement improved WebSocket generator with websockets library
dyrtyData Sep 25, 2025
0639bb9
Remove old markdown documentation
dyrtyData Sep 25, 2025
012e960
Add websockets library as dependency
dyrtyData Oct 9, 2025
3efda30
Fix WebSocket generator test failures
dyrtyData Oct 9, 2025
7690301
Apply suggestion from @jmartin-tech
dyrtyData Oct 10, 2025
e19892c
Apply suggestion from @jmartin-tech
dyrtyData Oct 10, 2025
27e9130
Apply suggestion from @jmartin-tech
dyrtyData Oct 10, 2025
c63a409
Remove PRtests/ from .gitignore
dyrtyData Oct 10, 2025
26a7924
Re-apply critical test fixes after architectural changes
dyrtyData Oct 10, 2025
68d4fda
Fix documentation security: Use environment variables for passwords
dyrtyData Oct 10, 2025
d074a25
Address security and logging concerns from code review
dyrtyData Oct 10, 2025
2c6ca51
Update garak/generators/websocket.py
dyrtyData Oct 10, 2025
65bf1e8
Update garak/generators/websocket.py
dyrtyData Oct 10, 2025
7c53a7f
Update garak/generators/websocket.py
dyrtyData Oct 10, 2025
0f272e7
Move environment variable access to _validate_env_var
dyrtyData Oct 10, 2025
79d7670
Improve test robustness with dynamic values
dyrtyData Oct 10, 2025
e7c0346
Move DEFAULT_PARAMS from module level to class level
dyrtyData Oct 10, 2025
5f6b8de
Add ENV_VAR class constant for environment variable name
dyrtyData Oct 10, 2025
bc864de
Apply suggestion from @jmartin-tech
dyrtyData Oct 10, 2025
0fbb0a4
Apply suggestion from @jmartin-tech
dyrtyData Oct 10, 2025
0de7c07
Apply suggestion from @jmartin-tech
dyrtyData Oct 10, 2025
72a576f
Fix syntax error in test configuration
dyrtyData Oct 10, 2025
05c3d53
Fix environment variable access initialization order bug
dyrtyData Oct 11, 2025
f26358f
Fix remaining WebSocket generator test failures
dyrtyData Oct 11, 2025
39cf785
Fix AsyncMock test failure in WebSocket generator
dyrtyData Oct 13, 2025
06ac326
Update docs/source/garak.generators.websocket.rst
dyrtyData Oct 13, 2025
365ce09
Update docs/source/garak.generators.websocket.rst
dyrtyData Oct 13, 2025
7ff3f5b
Update garak/generators/websocket.py
dyrtyData Oct 13, 2025
810f7d7
Refactor WebSocket generator to use framework-compliant initialization
dyrtyData Oct 13, 2025
e9c65cd
Add smart detection for unsupported WebSocket scenarios
dyrtyData Oct 13, 2025
5065027
Fix security vulnerability in WebSocket URI handling
dyrtyData Oct 14, 2025
af36310
Update garak/generators/websocket.py
dyrtyData Oct 14, 2025
b5f62ca
Update garak/generators/websocket.py
dyrtyData Oct 14, 2025
b2824da
Update garak/generators/websocket.py
dyrtyData Oct 14, 2025
0bb5386
fix: resolve maintainer issues for WebSocket generator
dyrtyData Oct 30, 2025
35614fe
fix: remove unused imports and clarify documentation
dyrtyData Oct 30, 2025
aa3a2af
refactor: implement jmartin-tech's Option 2 for name handling
dyrtyData Oct 30, 2025
2388455
fix: update async tests to use config_root pattern
dyrtyData Oct 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 184 additions & 0 deletions docs/websocket_generator.md
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.
Loading
Loading