← Back to blog

Hardening, tests and CI

This week had no big features. Instead, a handful of small pull requests made the existing code safer and harder to break.

Only listen to the peer

Until now, Recv returned any datagram that reached the port, from anyone. Anyone who could reach that port could write packets straight into my TUN device. The transport now drops datagrams from any address other than the configured peer:

func (t *Transport) Recv(buf []byte) (int, error) {
	for {
		n, sender, err := t.conn.ReadFromUDPAddrPort(buf)
		if err != nil {
			return 0, fmt.Errorf("reading from UDP socket: %w", err)
		}
		if t.peer != netip.AddrPortFrom(sender.Addr().Unmap(), sender.Port()) {
			continue
		}
		return n, nil
	}
}

The Unmap call is there because the socket can report an IPv4 sender as an IPv4-mapped IPv6 address (::ffff:172.30.0.3). Without it, the comparison with the configured peer would fail and every packet from the real peer would be dropped.

This isn’t real security, since source addresses are easy to spoof over UDP. It filters noise. Proper authentication comes with encryption.

Errors that say what went wrong

A small fix: the forwarding loops printed messages like “Error while writing into tun0” without the error itself, which made them useless for debugging. They now include the cause. A failed write to the TUN device also skips the packet, instead of falling through to the “did not write everything” check with a meaningless byte count.

The first tests

The transport got its first tests, followed by a few more. Each test builds a small rig: a plain UDP socket on 127.0.0.1 that plays the peer, and a Transport pointed at it. Ports are chosen by the kernel, so no test depends on a fixed port being free.

The cases cover receiving from the peer, ignoring a stranger, sending to the peer, and failing when the port is taken. The one I care about most checks the behavior that clean shutdown depends on, that closing the transport wakes up a blocked Recv:

// Shutdown relies on this: closing the socket must wake up a Recv that is
// blocked waiting for a datagram, with an error.
func TestRecvReturnsErrorAfterClose(t *testing.T) {

It starts Recv in a goroutine, checks it’s still blocked after 100 ms, closes the transport, and expects an error wrapping net.ErrClosed within two seconds. Every read uses a deadline, so a broken test fails instead of hanging forever.

CI

A GitHub Actions workflow now runs on every pull request and every push to main, with three jobs:

  • build: go build ./...
  • verification: go vet, a gofmt check that lists unformatted files, and go test -race
  • docker: builds the image, validates the Compose file, and runs govpn -h inside the image as a smoke test

The race detector matters here, since the program is two goroutines sharing a socket and a device. Dependabot now opens weekly PRs for Go modules, GitHub Actions, and the Docker base images.

What’s next

The TUN device still depends on the Docker entrypoint or manual commands for its address. I want govpn to configure its own interface, and to pick an MTU that leaves room for the tunnel’s headers.