Description
The WebSocket constructor converts http:// to ws://. Both schemes send messages without TLS.
Potential impact
Someone on the network path may read credentials or session tokens or change messages in transit.
Remediation
In production, use wss:// or an https:// URL, which the constructor converts to wss://. Install and renew a certificate valid for the server name. Limit cleartext connections to development on loopback addresses such as localhost, 127.0.0.1 or [::1].
Examples
Before
javascript
// Cleartext WebSocket connection
var ws = new WebSocket('ws://myapp.example.com/socket');
ws.onmessage = function(msg) {
console.log(msg.data);
}
After
javascript
// Use WebSocket over TLS (wss)
var ws = new WebSocket('wss://myapp.example.com/socket');
ws.onmessage = function(msg) {
console.log(msg.data);
}
The example connects to /socket on the same host using wss://, which protects messages in transit with TLS.