Skip to content

tcp 2.0.0: no SIGPIPE, complete sends, full SSRF ranges, IPv6, socket ownership

Choose a tag to compare

@Mosaad-M Mosaad-M released this 08 Oct 02:19
· 10 commits to master since this release
e9dd7fa

Fixes from a review of the tcp package. Every problem below was reproduced on 1.1.0. Breaking change: see "Ownership".

Fixed

  • A write to a closed connection killed the process. The OS sends SIGPIPE when you write to a connection the peer has closed, and tcp 1.1.0 didn't suppress it, so the program exited with code 141. tcp now suppresses it (MSG_NOSIGNAL on Linux, SO_NOSIGPIPE on macOS), and the write raises tcp: connection closed by peer.
  • send returned short counts silently. After a send timeout, one send() call returned the bytes written so far with no error. In the test, 1.8 MB of 64 MiB, and requests and websocket ignored the count. send now sends everything or raises tcp: send timed out (N of M bytes sent). The new send_bytes sends a List[UInt8] the same way.
  • The SSRF filter (reject_private_ips) missed several address ranges: 100.64/10, 198.18/15, 192.0.0/24, 224/4, 240/4 and broadcast. It now blocks every special-purpose IPv4 range in RFC 6890, plus IPv6 equivalents. IPv6 addresses that embed an IPv4 address (mapped, NAT64, 6to4) are judged by that address.
  • The connect timeout didn't work on macOS: timeout_secs=2 waited 75 s. Connections now use a non-blocking connect with poll, and the timeout is honored on both platforms.
  • IPv4 only, first address only. tcp now supports IPv6 and tries every resolved address in turn, IPv4 first.
  • Leaked sockets. Dropped sockets and repeated connect() calls leaked file descriptors.
  • Errors without reasons. Messages now carry the OS or resolver reason and list every address tried. Ports and recv_bytes(0) are validated, and interrupted calls are retried.

Ownership (breaking)

A TcpSocket now closes its file descriptor when it is destroyed. Mojo destroys a value right after its last use, so code that hands the descriptor to something else must take it with detach():

var tls = TlsSocket(tcp.detach())   # was: TlsSocket(tcp.fd)

Releases updated alongside this one: requests 1.4.0, websocket 1.3.0, pg 1.7.0 and mojo-pkg 0.9.0. The package index caps older releases of requests, websocket and pg at tcp < 2.0.0. Tested: requests 1.3.0 with tcp 2.0.0 fails HTTPS with EBADF.

Testing

  • 20 local tests against test_peer.py, plus a std-compatibility build, in CI on Linux and macOS. They cover each defect above.
  • Live sweeps through tls: 55/55 sites, and 24 sites plus 4 bad endpoints all correct.