Bound FinanceKit uploads before parsing, and redact the Jev endpoint

Merged
JA
2mo ago
+228-3

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.

  1. FinanceKit batch uploads are bounded before Rails reads them (cubic #17 and #18). POST /api/v1/financekit/publishers/:publisher_id/batches authenticates and enforces Financekit::MAX_BYTES in its action. By then Rails has already read and JSON-parsed the whole body:

    • ActionController::Instrumentation#process_action logs request.filtered_parameters before any action code.
    • ParamsWrapper parses the body again.
    • For a chunked request, request.content_length is the fully read raw_post.

    The result was an unauthenticated memory and CPU denial-of-service. The new FinancekitBodyLimit middleware:

    • 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_id from the route instead of params, which merged in the parsed body.

  2. 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 show has no admin gate. The field now shows Provider::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 main at a52e804
  • 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 POST to /api/v1/financekit/publishers/:id/batches (with or without a format suffix). Every other request, including a GET on 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 BigDecimal and 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 reaches ActiveSupport::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 rendered https://gw-user:gw-secret@…?api_key=qk-secret to 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.rb and jev_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 unmerged main and pass in CI on 3.4.9.
  • bin/rubocop on 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 plus Provider::RegistryTest (Anthropic env), Provider::OpenaiTest (hash inspect), ActiveStorageAuthorizationTest and Admin::UsersControllerTest (image variants). All of them also fail on unmerged main here and pass in CI on 3.4.9. Local runs use Ruby 3.3.6 because the environment cannot download 3.4.9.

Risks, blockers, and follow-up

  • The Gatekeeper review of #234 had not landed when this was opened. The owner asked for these fixes directly, so this PR opens as a draft now and waits for that review and the light-touch one before going ready.
  • Next: #235, once the owner has chosen a design.