Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 2 min read

One Laptop Sending Headers Slowly Took Your Go Server Down

The stock service was a small Go binary. Fast. Simple. One line started it: http.ListenAndServe(":8080", mux) It ran for two years. Then one afternoon it stopped answering. CPU was low. Memory was fine. The log

The stock service
was a small Go binary.

Fast.

Simple.

One line started it:

http.ListenAndServe(":8080", mux)

It ran for two years.

Then one afternoon
it stopped answering.

CPU was low.

Memory was fine.

The log said
http: Accept error,
too many open files.

Here is the mechanism.

Someone had opened
a few thousand connections.

On each one
they started an HTTP request.

And never finished the headers.

A few bytes.

Wait ten seconds.

A few more bytes.

The server was patient.

It kept every connection open,
waiting politely
for the blank line
that ends the headers.

It never came.

Each connection
held a file descriptor.

The process
had a limit on those.

When the limit was reached,
real customers
could not connect at all.

The attack has a name.

Slowloris.

Robert Hansen
published it in 2009.

It needs no botnet.

One laptop on a slow line
is enough.

Why Go?

http.ListenAndServe
builds a server
with every timeout set to zero.

And zero means
no limit.

Goroutines are cheap,
so nothing complains,
until the descriptors run out.

Your monitoring
watches CPU and memory.

Both look calm.

The server is not busy.

It is just full,
holding doors open
for visitors
who never walk in.

The fix is to build
the server yourself:

srv := &http.Server{
Addr: ":8080",
Handler: mux,
ReadHeaderTimeout: 5 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 120 * time.Second,
}
srv.ListenAndServe()

ReadHeaderTimeout
is the one that matters most.

A real browser
sends its headers
in milliseconds.

Five seconds
is generous.

The others
stop a client
that sends its body slowly,
reads your response slowly,
or opens a keep-alive connection
and simply sits there.

The linter gosec
flags this exact pattern
as rule G112.

Turn it on.

Then check
what sits in front.

A proxy that buffers headers
with its own timeout
takes the blow for you.

A load balancer
that passes raw TCP through
does not.

A server with no timeout
is not patient.

It is available
to whoever is slowest.

– Serguey Asael Shinder

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.