This rule is deprecated, and will eventually be removed.
Using sockets is security-sensitive. It has led in the past to the following vulnerabilities:
Sockets are vulnerable in multiple ways:
- They enable a software to interact with the outside world. As this world is full of attackers it is necessary to check that they cannot receive
sensitive information or inject dangerous input.
- The number of sockets is limited and can be exhausted. Which makes the application unresponsive to users who need additional sockets.
This rules flags code that creates sockets. It matches only the direct use of sockets, not use through frameworks or high-level APIs such as the
use of http connections.
Ask Yourself Whether
- sockets are created without any limit every time a user performs an action.
- input received from sockets is used without being sanitized.
- sensitive data is sent via sockets without being encrypted.
There is a risk if you answered yes to any of those questions.
Recommended Secure Coding Practices
- In many cases there is no need to open a socket yourself. Use instead libraries and existing protocols.
- Encrypt all data sent if it is sensitive. Usually it is better to encrypt it even if the data is not sensitive as it might change later.
- Sanitize any input read from the socket.
- Limit the number of sockets a given user can create. Close the sockets as soon as possible.
Sensitive Code Example
const net = require('net');
var socket = new net.Socket(); // Sensitive
socket.connect(80, 'google.com');
// net.createConnection creates a new net.Socket, initiates connection with socket.connect(), then returns the net.Socket that starts the connection
net.createConnection({ port: port }, () => {}); // Sensitive
// net.connect is an alias to net.createConnection
net.connect({ port: port }, () => {}); // Sensitive
See