Basic

Reading a body safely

The body is not a size, it is a stream. Reading without a limit is reading a number someone else chose, and the number can be larger than memory.

What you will see

One line with a reason. A limit with a reason is a limit that survives review.

About 1 min of reading

Try It

Enforce a body limit

1

Write the route limit

printf '/api/v1/ping 4096\n' > /tmp/l.txt; awk '{print $2}' /tmp/l.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
4096. The limit lives with the route, not in a global.
2

Attempt a larger body

printf 'bytes 90000\n' | awk '{print ($1>4096) ? "refuse" : "read"}'

Before you run it, say out loud what it will print. Then open the answer.

What it prints
refuse. The refusal is a 413, and the 413 is the limit speaking.
3

Write the reason for the limit

printf 'limit 4096 reason=one config object\n' > /tmp/r.txt; cat /tmp/r.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
One line with a reason. A limit with a reason is a limit that survives review.

Data Flow

A stream to a bounded read
1 The route declares its limit
↓
2 The stream is read up to the limit
↓
3 One extra byte is checked for
↓
4 The read is refused or accepted
Why this works
Why the limit is per route

Different routes accept different things. One global limit is a limit that is wrong for most of them.

The symptom of no limit

A process that grows and is killed. The symptom names the input, and the input names the route.

Examples

Content length is a claim

It is a header written by the client. Trusting it is trusting the input, and the input is the thing being defended.

The limit is read before the body

The count is checked while reading. A check after the read is a check that already spent the memory.

The limit is a policy

It has a number and a reason. A limit with no reason is a limit that will be raised in review.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← A handler is a function
Previous Lesson
A handler is a function
Next Lesson
Decoding into a shape
Decoding into a shape →