codemod/replace-insecure-websocket-connections
Replace insecure ws:// WebSocket connections with secure wss:// connections to prevent man-in-the-middle attacks and data interception
# Replace Insecure WebSocket Connections
A security-focused codemod that replaces insecure `ws://` WebSocket connections with secure `wss://` connections to prevent man-in-the-middle attacks and data interception.
## Security Issue
Insecure WebSocket connections using `ws://` protocol transmit data in plaintext, making applications vulnerable to:
- Man-in-the-middle attacks
- Data interception and eavesdropping
- Security compliance violations
This codemod addresses **CWE-319: Cleartext Transmission of Sensitive Information**.
## Installation
```bash
# Run from registry
codemod run @codemod/replace-insecure-websocket-connections
# Or run locally
codemod run -w workflow.yaml
```
## What it does
This codemod automatically transforms:
### WebSocket Constructor Calls
```javascript
// Before
new WebSocket('ws://localhost:8080')
// After
new WebSocket('wss://localhost:8080')
```
### Template Literals
```javascript
// Before
const socket = new WebSocket(`ws://${host}:${port}`)
// After
const socket = new WebSocket(`wss://${host}:${port}`)
```
### Configuration Objects
```javascript
// Before
const config = {
websocketUrl: 'ws://api.example.com/socket'
}
// After
const config = {
websocketUrl: 'wss://api.example.com/socket'
}
```
### String Concatenation
```javascript
// Before
connect('ws://' + domain + '/websocket')
// After
connect('wss://' + domain + '/websocket')
```
### URL Replace Methods
```javascript
// Before
url.replace('http:', 'ws:')
// After
url.replace('http:', 'wss:').replace('https:', 'wss:')
```
## What it preserves
The codemod intelligently avoids transforming:
- URLs already using `wss://` protocol
- HTTP/HTTPS URLs (non-WebSocket)
- Error messages and logging statements containing `ws://`
- Documentation strings and comments
- Test files with explicit insecure connection examples
## Supported File Types
- JavaScript (`.js`, `.jsx`)
- TypeScript (`.ts`, `.tsx`)
- Configuration files
- Template files
## Examples
### Before
```javascript
const config = {
development: {
websocket: 'ws://localhost:3000/ws'
},
production: {
websocket: 'ws://api.example.com:8080/socket'
}
};
const socket = new WebSocket(`ws://${host}:${port}/live`);
const fallbackUrl = baseUrl.replace('https:', 'ws:');
```
### After
```javascript
const config = {
development: {
websocket: 'wss://localhost:3000/ws'
},
production: {
websocket: 'wss://api.example.com:8080/socket'
}
};
const socket = new WebSocket(`wss://${host}:${port}/live`);
const fallbackUrl = baseUrl.replace('https:', 'wss:').replace('http:', 'wss:');
```
## Development
```bash
# Test the transformation
pnpm test
# Run on specific test case
pnpm test --filter websocket-constructor
# Update test snapshots
pnpm test -u
# Validate the workflow
codemod validate -w workflow.yaml
```
## Security References
- [CWE-319: Cleartext Transmission of Sensitive Information](https://cwe.mitre.org/data/definitions/319.html)
- [OWASP WebSocket Security Guidelines](https://owasp.org/www-community/attacks/WebSocket_security)
- [MDN WebSocket Security](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#Security)
## License
MIT