[PR #84] Admin Requests Accept Only JSON Bodies
- What changed: Requests with a body under the admin path (any method other than GET) are now accepted only when the body is JSON. Other formats are rejected with 415 (the server’s answer meaning “unsupported body format”).
- Decision: The check was added to the existing origin check in one place. Admin features added later go through the same check automatically.
- Not verified: This environment has no Docker, so the full test run was confirmed through CI (the automated check server).
What and why
I requested this work, and Claude implemented it.
A web page on another site can send requests to an admin address through the user’s browser. This kind of attack, which borrows the permissions of a logged-in user without their knowledge, is called cross-site request forgery (CSRF). The gap was found during the review of the earlier PR #82.
An origin check (Origin, which checks the address of the site that sent the request) was already in place. But when the origin matched, even a body that was not JSON went through to the controller. Browsers do not ask for permission first (a CORS preflight) for ordinary form submissions, so a form on another site could come in this way. A text/plain body even caused an internal server error, 500.
So a body-format check was added after the origin check. Whether a body exists is judged in two ways: if Content-Length (a header that states the body length) is greater than 0, or if Transfer-Encoding (a header saying the body is sent in pieces) is present, a body exists. A logout with no body (204, success with nothing to return) still works. The Content-Type (the header that states the body format) must be application/json, and options after it such as charset are allowed.
Chosen approach and rejected alternatives
| Choice | Approach | Reason |
|---|---|---|
| Chosen | Add the check to the existing guard in one place | Later admin controllers are covered automatically |
| Rejected | Add the check to each controller | Easy to forget on a new controller |
| Rejected | Change the body parser’s settings | Depends on the order the parser runs in, so it breaks easily later |
How it was checked
- Before the change, 2 of 8 tests failed: a form request got 200 and a
text/plainrequest got 500. The failures came from the missing feature, not from a broken test. - After the change,
npm run verifypassed (407 server tests and 30 admin web tests). - Tests also confirmed that a JSON
charsetoption still passes, that theSameSite=Strictcookie (a cookie not sent along from other sites) is unchanged, and that GET requests are not affected. - The review tried several bypass routes: path case, double slashes, encoded paths, method override, and the order of the body parser. The only issues found were cases the server already rejects first.
- Not verified: requests whose body is sent in pieces, and the PUT, PATCH, and DELETE methods, were not given extra tests in this change.
Lessons
- A shared check has to live in one place, or new features will miss it.
- Seeing a test fail before the fix is what makes the fix trustworthy.