Bound FinanceKit uploads before parsing, and redact the Jev endpoint
Summary
Fixes the two security findings in #234. Both are in upstream-authored code that arrived with the #232 sync, and both were verified against a52e804 before any change was made. One commit per finding.
-
FinanceKit batch uploads are bounded before Rails reads them (cubic #17 and #18).
POST /api/v1/financekit/publishers/:publisher_id/batchesauthenticates and enforcesFinancekit::MAX_BYTESin its action. By then Rails has already read and JSON-parsed the whole body:ActionController::Instrumentation#process_actionlogsrequest.filtered_parametersbefore any action code.ParamsWrapperparses the body again.- For a chunked request,
request.content_lengthis the fully readraw_post.
The result was an unauthenticated memory and CPU denial-of-service. The new
FinancekitBodyLimitmiddleware:- refuses a declared Content-Length over the limit with a 413 before Rails sees the request;
- reads a chunked body no further than one byte past the limit, which is enough for the controller's own check to answer 413.
The controller also stops wrapping parameters, and it now reads
publisher_idfrom the route instead ofparams, which merged in the parsed body. -
The Jev endpoint is shown without its credentials (cubic #31). A gateway endpoint can carry a key in its userinfo or query string. The settings panel rendered it raw to any member who had turned on preview features, because
showhas no admin gate. The field now showsProvider::Jev.redacted_endpoint. The field auto-submits on blur, so the redacted value comes straight back; submitting it now leaves the stored endpoint and its credentials untouched.
Scope and dependencies
- Issue: #234 (from the cubic review of #232)
- Parent PR/branch, if stacked: none; base is
mainata52e804 - Intentionally out of scope:
- #235 (cubic #1 and #2, unshared accounts shown to family members), which needs a design decision first.
- The same unbounded pre-auth JSON parse on the login and password-reset endpoints: Rack::Attack reads those bodies to throttle by email.
- Other endpoint-style settings on the hosting page that may carry credentials (
openai_uri_base,anthropic_base_url,external_assistant_url).
Blast radius
- The middleware only acts on
POSTto/api/v1/financekit/publishers/:id/batches(with or without a format suffix). Every other request, including aGETon the batch status path, passes through untouched, and unit tests cover both negatives. Rack::Attack's body-reading throttles are all gated to login paths, so they never read a FinanceKit upload. - The Jev change affects only the Jev endpoint field and its update branch. A submitted value is kept back only when it equals the redacted form of the stored endpoint. Any other value, including blank, still replaces or clears the endpoint as before, and the existing tests for those paths still pass.
- Known trade-off: an operator cannot remove credentials from the stored URL by resubmitting it without them. Clearing the field and re-entering the URL does it.
Upstream
Both fixes are upstream:candidate. The code is upstream's: FinanceKit from we-promise/sure#3633, #3678 and #3684, and Jev from #3660. Once this is proven here, it should go to we-promise/sure as two PRs, one per finding.
Evidence checklist
- Each issue in this PR has its own acceptance criteria and no unrelated issue work is included.
- Contract rows touched by this change name existing tests. Not applicable.
- Any gate test has an observed failing mutation transcript attached (below).
- Fixtures used as evidence assert internal consistency. The tests set their own state: the stored endpoint, the member's preview flag, the family's provider.
- Financial calculations use
BigDecimaland documented rounding points. Not applicable. - De-identification uses token-shape allowlists; no personal data is included.
- Files changed, decisions, tests/runtime, risks, and next unblocked issue are documented below.
Validation
Fail-first, against unmodified a52e804:
batches_body_limit_test.rb: the Content-Length case fails ("no part of an oversized body should reach the JSON parser"), and the chunked case fails with "parsed 2097166 bytes of a 2097166-byte body". Both tests measure how much of the body reachesActiveSupport::JSON.decode, not just the status code, because the controller already answered 413 while having parsed everything first.hostings_controller_test.rb: both new Jev tests fail. A probe confirmed the first fails for the right reason: the panel renderedhttps://gw-user:gw-secret@…?api_key=qk-secretto a non-admin member.
Mutations (each change broken on its own):
- Removing the chunked-body cap fails the chunked integration test and the cap unit test.
- Removing the Content-Length refusal fails the Content-Length test.
- Reverting the view to the raw endpoint fails the member-panel test.
- Removing the echo-back guard fails the round-trip test.
Runs:
- FinanceKit tests: 105 runs, 0 failures.
hostings_controller_test.rbandjev_test.rb: 104 runs, 3 failures. All 3 are the known Ruby 3.3.6 environment failures on the Anthropic base URL, which also fail on unmergedmainand pass in CI on 3.4.9.bin/rubocopon the changed files,erb_lint,bin/brakeman: clean, 0 warnings.- Full
bin/rails test: 10526 runs, 46229 assertions, 4 failures, 3 errors, 33 skips. The 7 are the three Anthropic tests above plusProvider::RegistryTest(Anthropic env),Provider::OpenaiTest(hash inspect),ActiveStorageAuthorizationTestandAdmin::UsersControllerTest(image variants). All of them also fail on unmergedmainhere and pass in CI on 3.4.9. Local runs use Ruby 3.3.6 because the environment cannot download 3.4.9.