reference security-reviewerintegrator

Runbook — The ceiling for the size of a request body

Each request to OpenGov can carry data. That data is the body of the request. The platform accepts a body only up to a ceiling: the largest request body that one route accepts, in bytes. It refuses a larger body with the status 413 and one sentence. It refuses before it reads the body and before it checks the sign-in. The ceiling is on in each deployment, and an operator does not set it.

This runbook pairs with the design, docs/specs/2026-10-11-request-body-ceiling.md, and with its plan, docs/plans/2026-10-11-request-body-ceiling.md.

Words used here

  • body: the data that a browser or a system sends with a request.
  • route: one thing that the application does on request. It is a verb plus an address pattern.
  • ceiling (code: Ceiling, the answer request_too_large): the largest request body that one route accepts, in bytes.
  • the list (code: NAMED_CEILINGS): the routes that have a ceiling above the default. An entry is one line of the list.
  • declared length: the number in the Content-Length header of a request.
  • level: one list or one object of a JSON body. A list inside an object is a second level.
  • depth limit (code: MAX_JSON_DEPTH): the largest count of levels that a JSON request body can have.
  • proxy: the server of a country that receives each request first. It passes the request to the platform.
  • request identifier: a short code the platform invents for every request and sends back in a response header.
  • operator: the person who provisions and maintains the machinery.

What a person sees

A system that sends a body above the ceiling gets the status 413 and this JSON body:

{
  "detail": {
    "code": "request_too_large",
    "message": "This request is too large. The largest request accepted here is 1048576 bytes.",
    "max_bytes": 1048576
  }
}
  • max_bytes is the ceiling of the route that the request named.
  • The answer is the same with a declared length and with none.
  • An upload route answers with the code upload_too_large and the largest file size. That is the code of the file check, so a client reads one code for a large file.
  • The answer has the security headers and the request identifier, in the header X-Request-ID.

What a person reads in a browser application:

ApplicationWhat the person reads
The portal, for a form or for one answerThe sentence of the portal for the size. It says that a second try does not help.
The portal, for a fileThe sentence of the portal for a file that is too large
Each other browser applicationThe general failure sentence of that application

Only the portal has a sentence for the size in this build. That is ruling 3 of the spec.

A page of a different origin cannot read the refusal. The refusal has no CORS header. The ceiling is around the layer that adds that header. The browser of such a page reports a network error, and the page cannot show the cause. Each browser application of the product calls the platform on its own origin, so this does not change what its users see. A system that is not a browser reads the refusal with no change.

The two log lines

The layer writes one line for each refusal, at the level WARNING. The logger is opengov.platform.body_ceiling. These are two examples:

request.body_refused entry=default ceiling=1_048_576 declared=2_097_152 read=0
request.body_refused entry=default ceiling=1_048_576 declared=none read=1_114_112
  • entry is the name of the entry of the list, or default.
  • The line never holds the address of the request, because an address can hold a secret.
  • ceiling is the ceiling of that route, in bytes.
  • declared is the declared length. The value none means that the request declared no length.
  • read is the count of bytes that the application read before the refusal.
  • read=0 means that the declared length was above the ceiling. The application read nothing.
  • Each number is in groups of three digits, such as 1_048_576. The log formatter hides a run of 9 to 16 digits. A number in groups has no such run, so the line keeps each size. That matters when a deployment raises the upload limit: the ceiling then has 9 digits.
  • A declared length above 999,999,999,999 shows as 999_999_999_999+. A caller chooses that number, so the line gives it a bound.

The layer writes a second line at the level INFO. It writes this line when a body passes and is above half of its ceiling:

request.body_near_ceiling entry=default ceiling=1_048_576 read=600_000

Many such lines for one entry mean that real bodies come near the ceiling. Tell the maintainers before a real body gets the refusal.

To find one refused request:

  1. Get the request identifier from the header X-Request-ID of the 413 answer.
  2. Search the log for that identifier.
  3. Read the two lines that the search finds: the refusal line and the request line.

The request line has the event request.completed and the status 413. It has no route name when the layer refused before the router ran.

The list of ceilings

One file holds each ceiling: src/opengov/platform/body_ceiling.py. Do not copy its values into a document. This command prints each ceiling of the running code:

python -c "from opengov.platform import body_ceiling as b; [print(c.name, c.method or 'each verb', c.template or 'each other route', c.limit()) for c in (b.DEFAULT_CEILING, *b.NAMED_CEILINGS)]"

Each line gives the name of the entry, the verb, the address pattern and the ceiling in bytes. The first line is the default. A request that matches no entry gets the default.

Each ceiling is a constant, and only the upload ceiling follows a setting. To change a constant, change the list and release the platform.

The upload setting

OPENGOV_MAX_UPLOAD_BYTES sets the largest file that the platform accepts. The ceiling of each upload route follows it. That ceiling is the setting plus 1 MiB for the other parts of the request. An operator who changes the setting changes the ceiling with it. No second value exists.

The proxy

A proxy can refuse a large body before the platform gets the request. That limit is a second layer. The platform does not rely on it, because each country chooses its own proxy.

  • Set the limit of the proxy at or above the highest ceiling of the list. A lower limit refuses a real upload before the platform gets it.
  • The command of “The list of ceilings” prints the highest ceiling.
  • A proxy can replace the 413 of the platform with its own answer. The portal then cannot show its sentence for the size.

Do this check one time after a deployment gets the ceiling. Do it again after each change of the proxy.

  1. Make a file of 2 MiB:

    head -c 2097152 /dev/zero | tr '\0' 'A' > two-mib.txt
    
  2. Send the file with no token, through the proxy:

    curl -sS -X POST -H 'content-type: application/json' --data-binary @two-mib.txt \
      -w '\n%{http_code}\n' https://<the portal host>/api/v1/smartrules/requirements
    
  3. Read the answer. The correct answer is the JSON body with request_too_large, then 413.

  4. Open the portal in a browser. Run this line in the console of the browser:

    fetch('/api/v1/smartrules/requirements', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: 'A'.repeat(2 * 1048576)}).then(r => r.status)
    
  5. Read the result. The correct result is 413.

A network error in step 5 means that the proxy hides the refusal from the browser. File an issue, and tell the maintainers.

A new route with a large body

A route with no entry gets the default. To let a route accept a larger body:

  1. Add one entry to NAMED_CEILINGS in src/opengov/platform/body_ceiling.py.

  2. Give the entry a name with a hyphen, the verb, the address pattern and the ceiling.

  3. Write the address pattern as the router declares it.

  4. Correct the count in test_each_name_is_different_and_no_ceiling_is_below_the_default.

  5. Run the two test files:

    pytest tests/platform/test_body_ceiling.py tests/platform/test_body_ceiling_in_the_app.py
    
  6. Put the entry in the same pull request as the route.

A name uses a hyphen for one reason. A platform file must not hold a text that starts with the table prefix of a tool, such as smartlink_. The table gate refuses that text.

What the tests find:

  • test_each_entry_names_exactly_one_mounted_route fails when an entry names no route. So a route that changes its address fails the test.
  • test_each_upload_route_and_each_raw_route_is_in_the_list fails when an upload route has no entry. It also fails when a route that reads the raw body has no entry.
  • test_each_own_limit_is_below_its_ceiling fails when the own limit of a route is above its ceiling.

No test fails for a JSON route that has no entry. That route gets the default, and its large body gets the 413. So send the largest real body of a new route through HTTP in a test.

A JSON body with too many levels

A second check is in the layer directly inside the ceiling. It refuses a JSON body that has more levels than the depth limit. The answer is the status 400 and this JSON body:

{"detail": "request body must not be nested deeper than 100 levels"}
  • The refusal comes before the sign-in check, on each route.
  • The deepest real body of the repository has 8 levels. So a real form does not get this refusal.
  • Before this check, a small body with thousands of levels got the status 500. Each such request wrote a server fault in the log. That was issue #4165.
  • This check writes no log line of its own. The request line has the status 400.

This command prints the depth limit of the running code:

python -c "from opengov.platform.http_guards import MAX_JSON_DEPTH; print(MAX_JSON_DEPTH)"

The check reads a body when its content type can be JSON. These are the three cases:

  • The request has no content type.
  • The content type is application/json, in each letter case.
  • The content type is application/…+json, such as application/vnd.api+json.

A request can have two content types. The check then reads the body when one of the two is in this list, whatever the order.

The same three cases apply to the two older checks of this layer: a NUL character and a lone surrogate. The check does not read a body of a different kind, such as an upload, a form or plain text. It does not change a request that has no body.

Three receivers parse a body as JSON themselves, whatever its content type: the callback receiver, the inbound endpoint and the mesh holder. A deep body with the label text/plain gets no check there. Each receiver checks a credential before it parses. Issue #4193 has the measure.

The check reads each JSON body up to the ceiling of its route. On an upload route, the check reads 2 MiB at most. That bound keeps a JSON body on an upload route from using the full upload ceiling in memory.

What the ceiling does not bound

The ceiling bounds one request. It does not bound these two things:

  • Many requests at one time. Many large bodies at one time can fill the memory or the disk.
  • A slow sender. A person can hold a connection open with a body below the ceiling.

The section “What stays open after this build” of the spec gives the measures. A limit on the count of requests at one time is a separate proposal. That is ruling 5 of the spec.