Skip to content

Don't let a held-back confirmation replace a voter's newer ballot - #570

Closed
Ceilovee wants to merge 4 commits into
benadida:masterfrom
Ceilovee:session-timestamp
Closed

Ceilovee wants to merge 4 commits into
benadida:masterfrom
Ceilovee:session-timestamp

Conversation

@Ceilovee

Copy link
Copy Markdown

Problem

A voter's current ballot is the one with the latest cast_at, and cast_at is set when the ballot is confirmed (auto_now_add). The ballot reaches the server earlier, though. The booth posts it to /cast and the server keeps it in the session. Only after the voter logs in on /cast_confirm and confirms is the CastVote created. So a ballot whose confirmation arrives late looks newer than the ballots the voter cast in the meantime.

Meyer and Smyth describe how to exploit this. The adversary has to be able to delay traffic from one terminal: for example, someone supervising a polling place who controls its network. They have the voter cast a "test" ballot on that terminal and hold back the message that completes it, which is the OAuth callback or the confirmation POST. The voter later votes privately, and that ballot is recorded. When the adversary releases the held message, the test ballot is confirmed at that moment, gets the latest cast_at, and replaces the voter's real ballot.

The check in Voter.store_vote (cast_vote.cast_at < self.cast_at) doesn't stop this, because both times are confirmation times.

Fix

Order a voter's ballots by when the server received them, using the server's clock. Meyer and Smyth suggest a timestamp from the voter's device or from the identity provider, but a device clock can be set by whoever controls the device.

  • Recording the time: one_election_cast stores the reception time in the session as encrypted_vote_received_at, next to the pending ballot. It's a UTC ISO string, since the session is JSON. It's saved with save_in_session_across_logouts, like encrypted_vote. Posting the same ballot again in the same session keeps the first time.
  • Using it: one_election_cast_confirm uses that time as the CastVote's cast_at. If this voter already cast this exact ballot, it keeps its original time. Ballots are public, so otherwise resubmitting a copy of an older ballot would make it look newer.
  • Rejecting: before saving, a ballot that wasn't received after the voter's current one (cast_vote.cast_at <= voter.cast_at) is rejected. The pending ballot is dropped and a new cast_rejected page explains why. That page logs the voter out like cast_done does.
  • Failing closed: if the reception time is missing, for example in a session from before this change, the confirmation is rejected too, with its own message.
  • CastVote.cast_at: changed from auto_now_add=True to default=datetime.datetime.utcnow, since auto_now_add overwrites the assigned value on save. Migration 0013 only changes model state. The column type is unchanged and it runs no SQL on PostgreSQL.

The time is kept in the session rather than in a new table. It has to be recorded before the voter is known, and the pending ballot already lives in the server-side session. That adds no new storage. And if a session expires or is purged, its pending ballot goes with it, so there's nothing left to confirm.

What else changes:

  • cast_at meaning: cast_at is now the reception time, stored in UTC. auto_now_add stored local TIME_ZONE time (because USE_TZ = False), while code such as the recent-votes cutoff in stats_views already uses utcnow().
  • Upgrading: an election running during the upgrade would mix both kinds of times, so deploying between elections is safest. A ballot pending at deploy time gets "Vote Rejected" and has to be cast again.
  • Re-voting: works as before. A later ballot is also received later, and re-voting for the same choice produces a new ciphertext.
  • Slow verification task: if the held confirmation arrives before the newer ballot's verification task has run, the view accepts it. But Voter.store_vote now compares reception times, so it keeps the newer ballot.

Tests

  • Manual reproduction on three VMs (a server with PostgreSQL and LDAP, and two clients). On master, the held confirmation replaces the later ballot. On this branch, it's rejected and the later ballot stays.

cielo and others added 4 commits November 23, 2026 22:17
With auto_now_add, Django overwrites the cast_at that
one_election_cast_confirm assigns when the CastVote is saved, so cast_at
was always the confirmation time. A default keeps the assigned value, so
cast_at can hold the time the ballot was received (next commit).

The migration only changes the field definition: the column type is the
same and it runs no SQL on PostgreSQL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
one_election_cast, where the server receives the encrypted ballot,
stores the time in the session next to the ballot (UTC, as an ISO
string, since the session is JSON). It uses save_in_session_across_logouts,
like encrypted_vote, so it survives a logout before confirmation.
Posting the same ballot again in the session keeps the first time.

one_election_cast_confirm uses that time as the ballot's cast_at. A
ballot this voter already cast keeps the time it was first received.
Without a reception time the confirmation is refused (fails closed) and
the voter has to cast again.

The session is already stored server-side, so this needs no new table,
and if a session is purged its pending ballot goes with it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Before saving, compare the reception time of the ballot being confirmed
with the voter's current ballot. If it isn't later, reject the
confirmation, drop the pending ballot from the session and show a page
that explains why. A confirmation held back and released late (Meyer and
Smyth's re-voting attack) no longer replaces a ballot received after it.

The rejection page logs the voter out like cast_done, and pops
CURRENT_VOTER_ID the way cast_done does since benadida#518.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A confirmation that arrives without a reception time in the session is
still refused, but instead of going back to the election page with no
explanation, the voter now sees the rejection page with its own message:
the server has no record of when it received the ballot, so vote again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Ceilovee
Ceilovee marked this pull request as ready for review November 24, 2026 02:03
@benadida

Copy link
Copy Markdown
Owner

I appreciate the PR, but I don't think this attack is one that can be reasonably expected, and I'd rather not complicate the flow.

@benadida benadida closed this Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants