Static analysis (SAST) check · Go

HTTP server without timeouts (Go)

http.ListenAndServe starts a server with no read or write timeouts.

Why it matters

A client that sends its request one byte at a time holds a connection open indefinitely, and enough of them exhaust the server.

How to fix it

Construct an http.Server with ReadHeaderTimeout, ReadTimeout, WriteTimeout and IdleTimeout set, and call its ListenAndServe.

Example

The rule's own test fixtures, verbatim: the test suite requires the rule to report the first and not the second.

Vulnerable: reported (Go)
package fixture

import "net/http"

func serve(handler http.Handler) error {
	return http.ListenAndServe(":8080", handler)
}
Fixed: not reported (Go)
package fixture

import (
	"net/http"
	"time"
)

func serve(handler http.Handler) error {
	server := &http.Server{
		Addr:              ":8080",
		Handler:           handler,
		ReadHeaderTimeout: 5 * time.Second,
		ReadTimeout:       30 * time.Second,
		WriteTimeout:      30 * time.Second,
		IdleTimeout:       2 * time.Minute,
	}
	return server.ListenAndServe()
}

What TrustIvan reads

Runs on repository scans of Go source once static analysis is turned on (SAST_RULES and the separately built analyzer image); it is off by default.

Weakness classes and references

Terms: SAST (static application security testing), Taint analysis, CWE (Common Weakness Enumeration), OWASP Top 10

All static analysis (SAST) checks · The whole catalogue · How this works in TrustIvan · Documentation

See TrustIvan on your own estate.

Book thirty minutes with an engineer and bring an image, a repository or a cloud account, or start free on the hosted service.