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
Public Class Sockets
Public Shared Sub Run()
Dim socket As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
' TcpClient And UdpClient simply abstract the details of creating a Socket
Dim client As TcpClient = New TcpClient("example.com", 80) ' Sensitive
Dim listener As UdpClient = New UdpClient(80) ' Sensitive
This rule is deprecated, and will eventually be removed.