Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

Pin the 400 for invalid UTF-8 in a JSON comment and say where Rails raises it

Guido Boykeead8b26a961e
diff --git a/CLAUDE.md b/CLAUDE.md
index 7f3e75e..1ddd480 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -795,8 +795,9 @@ feature by feature):
`spec/architecture/api_v1_error_body_guard_spec.rb`. Answers outside the controllers carry
no `code`: the Rack::Attack 429 (plain text), a 500 from the exceptions app, a 400 for a
query string Rack cannot decode (`?x=%`; raised in middleware before any controller), and a
- 400 for a parameter that is no valid UTF-8 (Rails raises `ActionController::BadRequest` while
- it parses the parameters, which `BaseController` does not rescue). The 401 of `/api/mcp` (same concern,
+ 400 for a body parameter that is no valid UTF-8, form or JSON (Rails raises
+ `ActionController::BadRequest` when it first reads the parameters, in its params wrapper or
+ its request log, before any callback or `rescue_from` of `BaseController` runs). The 401 of `/api/mcp` (same concern,
`render_unauthorized`) keeps its old body.
* **`Idempotency-Key`** on every write (`idempotent :create` in the controller,
`Api::V1::Idempotent`): the same key and request replays the first 2xx answer (status and
diff --git a/spec/requests/api/v1/post_comment_format_spec.rb b/spec/requests/api/v1/post_comment_format_spec.rb
index 7cfff0b..65af592 100644
--- a/spec/requests/api/v1/post_comment_format_spec.rb
+++ b/spec/requests/api/v1/post_comment_format_spec.rb
@@ -229,6 +229,13 @@ RSpec.describe 'API V1 comment format', :ci_guard, type: :request do
expect(response).to have_http_status(:bad_request)
end
+ it 'never lets invalid UTF-8 in a JSON body reach the rule either' do
+ raw = "{\"post\":{\"format\":\"plain\",\"body\":\"a#{255.chr}b\"}}".b
+ expect { post "/api/v1/tickets/#{ticket.id}/posts", params: raw, headers: headers(json: true) }
+ .not_to(change { posts_count })
+ expect(response).to have_http_status(:bad_request)
+ end
+
it 'answers a post that is no object like a missing one, never a crash' do
post "/api/v1/tickets/#{ticket.id}/posts", params: {}, headers: headers
missing = [response.status, response.body]