Repository navigation
fix: refuse an ambiguous entry hash instead of picking a winner - #273
Conversation
`get_entry` is how every mutating endpoint finds the line it is about
to rewrite or delete - save_entry_slice, delete_entry_slice,
insert_metadata. It was:
next(e for e in self.all_entries if entry_hash == hash_entry(e))
First match wins, and the docstring above it asserted the result was
"the (unique) entry" without anything checking that. Nothing guarantees
uniqueness: two transactions identical in every field rustledger hashes
collide, and duplicates like that are ordinary in real ledgers. When
that happens the UI edits a DIFFERENT transaction than the one the user
clicked, with no indication anything went wrong.
Two of the three mutating paths have a second factor - the sha256sum of
the displayed source slice - so they fail with "changed externally"
rather than corrupting anything. `insert_metadata` has none, and it is
the endpoint most likely to be operating on entries that differ only by
metadata.
Now collects all matches and raises `AmbiguousEntryHashError`, turning
a silent wrong-target edit into a visible error. Deliberately not
short-circuiting: knowing whether a hash is ambiguous means looking at
every entry, so this is O(n) where it used to average less. `hash_entry`
is a dict lookup on rustledger entries, so that is microseconds even on
a large ledger - and the alternative is being fast at editing the wrong
line.
The message names the remedy rather than just the dead end: adding a
metadata key to one of the two makes them distinct. That is true as of
rustledger#1984, which puts metadata into the hash - an error telling
the user only that they are stuck sends them back to the same click.
Sabotage-verified twice: restoring first-match-wins fails the test, and
so does dropping the remedy from the message.
The test duplicates the SAME object rather than constructing a
lookalike, which is the strongest form of the case - no hash function
over directive content can tell those apart, so it cannot be papered
over by hashing more fields.
Two incidental fixes in the same file, both pre-existing and both
flagged by ruff only because this commit touches it: an unused
`to_string` import (verified nothing imports it from `rustfava.core`)
and an out-of-order `is_encrypted_file` import.
Self-review — one risk this change surfaces rather than createsRefusing an ambiguous hash is right, but it converts a silent wrong-target edit into a hard failure, and it's worth being explicit about where that failure will actually land.
if hasattr(entry, "__dataclass_fields__"):
content = f"{type(entry).__name__}|{entry.date}|{getattr(entry, 'account', '')}"
return hashlib.sha256(content.encode()).hexdigest()[:16]A Before this PR those collisions resolved to the first match and nobody saw anything. After it, they raise. That is the correct direction — the alternative was editing the wrong transaction — but it means the fallback's weakness stops being invisible, and it will show up as The window is narrow in practice: entries created through the add-entry flow are written to the file and picked up with real hashes on reload. But it is not empty, and extensions can hold non-persisted entries. Worth doing as a follow-up rather than in this PR: give the fallback the fields that actually distinguish entries — narration, payee, the postings — and drop the Happy to fold it in here if you'd rather it not be a separate PR. |
The consumer half of rustledger#1968. Pairs with rustledger#1984, but stands
alone — it is a fix regardless of what that hash contains.
The bug
get_entryis how every mutating endpoint finds the line it is about torewrite or delete:
save_entry_slice,delete_entry_slice,insert_metadata.It was:
First match wins — and the docstring above it asserted the result was "the
(unique) entry" without anything checking that.
Nothing guarantees uniqueness. Two transactions identical in every field
rustledger hashes collide, and duplicates like that are ordinary in real ledgers
(two identical coffees on the same day). When it happens, the UI edits a
different transaction than the one the user clicked, with no indication
anything went wrong.
Two of the three mutating paths carry a second factor — the sha256sum of the
displayed source slice — so they fail with "changed externally" rather than
corrupting anything.
insert_metadatacarries none, and it is the endpointmost likely to be operating on entries that differ only by metadata.
The fix
Collect all matches; raise
AmbiguousEntryHashErroron more than one. A silentwrong-target edit becomes a visible error.
Deliberately not short-circuiting — knowing whether a hash is ambiguous means
looking at every entry, so this is O(n) where it used to average less.
hash_entryis a dict lookup on rustledger entries, so that's microseconds evenon a large ledger, and the alternative is being fast at editing the wrong line.
The message names the remedy, not just the dead end: adding a metadata key to
one of the two makes them distinct (true as of rustledger#1984). An error that
only tells the user they're stuck sends them back to the same click.
Verification
Sabotage-verified twice — restoring first-match-wins fails the test, and so does
dropping the remedy from the message.
The test duplicates the same object rather than constructing a lookalike,
which is the strongest form of the case: no hash function over directive content
can tell those apart, so it can't be papered over by hashing more fields.
Full suite: 665 passed, 1 skipped. The two locale failures
(
test_get_translations,test_fava_options_language) reproduce identically onpristine
origin/main— no compiled.mofiles when running fromsrc/.Two incidental fixes in the same file, both pre-existing and both flagged by ruff
only because this commit touches it: an unused
to_stringimport (verifiednothing imports it from
rustfava.core) and an out-of-orderis_encrypted_fileimport.