Keep the FinanceKit downstream lock key on MD5
Summary
This is a follow-up to #243. It answers the CodeRabbit finding that was left open on that PR (thread).
#243 changed Financekit::Downstream#advisory_lock_key from MD5 to SHA256. That key is how worker processes stay out of each other's way, through pg_try_advisory_lock. During a rolling deploy, an old worker (MD5) and a new worker (SHA256) derive different keys for the same publisher. Both then acquire their lock, and both run the downstream fan-out, repeating RuleRun work. The digest is not used for secrecy, so the change bought nothing that is worth that window.
This PR:
- restores
app/models/financekit/downstream.rbto its pre-#243 content, which is byte-identical to upstream again (git diff origin/mirror/upstream-main -- app/models/financekit/downstream.rbis empty); - adds
test/models/financekit/downstream_lock_key_test.rb, which pins the key for a fixed publisher id. Any future change to the derivation then fails a test and has to be done as a deliberate, drained deploy.
Scope and dependencies
- Issue: follow-up to #243 (and #239)
- Parent PR/branch, if stacked: none. Branched from
mainatc89eb2e4. - Intentionally out of scope:
DemoFamilyRefreshJobandGenerateInsightsJobstay on SHA256. CodeRabbit's own analysis found thatGenerateInsightsJobis also protected by its:until_executedSidekiq lock. The demo refresh runs on a single daily schedule, so an overlapping deploy would have to coincide with that one run. The owner asked for the FinanceKit key only.- Codacy suppression. See the risks below.
Blast radius
advisory_lock_key is private and read only by with_publisher_claim, which is used by FinancekitInboxJob through Financekit::Downstream#perform!, both per upload and in the periodic sweep. Nothing else reads the key.
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. See Validation.
- Fixtures used as evidence assert internal consistency. Not applicable: the test uses a literal id, not a fixture.
- 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
main's SHA256 derivation, the new test fails:Expected: 2897850380234485413, Actual: 2179911876805092343. With MD5 restored, it passes. bin/rails test test/models/financekit: 85 runs, 638 assertions, 0 failures.bin/rubocopon both files: no offences.
Risks, blockers, and follow-up
- Codacy will flag the MD5 line again. These are the same two "critical" rules as #239, finding 1. #239 already assessed them as not exploitable: the digest only derives an advisory-lock integer. They need marking as a false positive in the Codacy UI, which is an owner action.
- Deploy note. If
mainwith #243 (SHA256) has already been deployed, this change switches the key back. The same overlap window then applies once more, in reverse. Stopping the worker before starting the new one (the default withdocker compose up -d) avoids it.
Review follow-up
Checked against head cc54c7cb. No code change was needed. Local re-run: test/models/financekit gives 85 runs, 638 assertions, 0 failures. Mutation check: switching line 98 back to Digest::SHA256 fails the pin test (Expected: 2897850380234485413, Actual: 2179911876805092343). With MD5 restored, it passes. Both values were recomputed independently in plain Ruby. bin/rubocop on both files: no offences. bin/brakeman: no warnings. The branch merges cleanly with current main.
| Finding (source) | Status |
|---|---|
Codacy: 2 critical, both on downstream.rb#L98. The messages are "Should not use md5 to generate hashes…" and "The MD5 hashing algorithm is considered cryptographically weak…" | Declined: false positive. The digest turns "financekit_downstream:<id>" into a 62-bit integer for pg_try_advisory_lock/pg_advisory_unlock. It is private, and with_publisher_claim is its only reader. Nothing relies on the hash for secrecy, integrity or collision resistance. A collision could only make two publishers wait on the same lock, which is harmless. Brakeman does not flag the line. No inline suppression comment was added, because any comment or nosemgrep marker would break this file's byte-identity with mirror/upstream-main, and restoring that identity is the point of the PR. The rationale already sits beside the test that pins the value. Owner action: mark both issues as false positives in the Codacy UI. Codacy (action_required) is the only non-green check, and it is what makes mergeable_state read unstable. |
| PRR (2026-09-28) MUST VERIFY 1: if #243 is live, restart workers non-rolling | OWNER DECISION: deploy step. See Deploy instruction below. |
| PRR SHOULD IMPROVE 1: mark the Codacy MD5 findings as false positives | Owner action in the Codacy UI. Same as the first row. |
| PRR non-blocking 1 (MEDIUM): deploy ordering | Same as MUST VERIFY 1. |
| PRR non-blocking 2 (LOW): the remaining SHA256 sites are unchanged | Already addressed. This is intentional scope. GenerateInsightsJob also has lock: :until_executed. DemoFamilyRefreshJob runs once a day on a schedule. |
PRR non-blocking 3 (LOW): the test calls the private method with send(:advisory_lock_key) | Declined. The reviewer marked this acceptable for a contract pin. Exposing the method only for the test would widen the model's API and change an upstream-identical file. |
| Observation made during the sweep: the upstream comment on line 97 ("Matches the keying used by the other advisory-locked jobs") is not accurate in this fork, because #243 moved the other two jobs to SHA256 | Declined. Editing it would break byte-identity with upstream. The comment is true upstream, where all three sites use MD5. |
| Gatekeeper (2026-09-27): APPROVE | No action needed. |
| CodeRabbit: draft not reviewed. cubic: monthly line limit reached (neutral) | No review content to address. The coordinator requests reviews. |
Deploy instruction (owner decision)
- If a build of
mainthat contains #243 (SHA256 lock key) is running anywhere, deploy this release without overlap. Stop every Sidekiq worker, then start the new ones. With Docker Compose,docker compose up -drecreates the worker container this way (stop, then start). Do not use a rolling or blue-green worker swap for this release. If old and new workers overlap, they compute different keys for the same publisher. Both then get the advisory lock, and the downstream fan-out (sync_later,auto_match_transfers!,rules.apply_later) runs twice. That repeats work that is safe to re-run; it does not corrupt data. - If #243 was never deployed, the lock key does not change between releases, and any deploy method is safe.
- Recommendation: treat the release as the first case unless you know #243 never shipped. A stop-then-start restart costs very little.
Summary by CodeRabbit
- Chores
- Updated internal processing coordination. No user-facing functionality was added or removed.