Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions helios/election_url_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
ELECTION_PASSWORD_VOTER_LOGIN="election@password-voter-login"
ELECTION_PASSWORD_VOTER_RESEND="election@password-voter-resend"
ELECTION_CAST_DONE="election@cast-done"
ELECTION_CAST_REJECTED="election@cast-rejected"

ELECTION_POST_AUDITED_BALLOT="election@post-audited-ballot"

Expand Down
1 change: 1 addition & 0 deletions helios/election_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
path('/password_voter_login', views.password_voter_login, name=names.ELECTION_PASSWORD_VOTER_LOGIN),
path('/password_voter_resend', views.password_voter_resend, name=names.ELECTION_PASSWORD_VOTER_RESEND),
path('/cast_done', views.one_election_cast_done, name=names.ELECTION_CAST_DONE),
path('/cast_rejected', views.one_election_cast_rejected, name=names.ELECTION_CAST_REJECTED),

# post audited ballot
re_path(r'^/post-audited-ballot', views.post_audited_ballot, name=names.ELECTION_POST_AUDITED_BALLOT),
Expand Down
20 changes: 20 additions & 0 deletions helios/migrations/0013_castvote_cast_at_reception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import datetime

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('helios', '0012_site_announcement'),
]

operations = [
# cast_at now holds the time the server received the ballot, which the
# view assigns; auto_now_add would replace it with the confirmation time
migrations.AlterField(
model_name='castvote',
name='cast_at',
field=models.DateTimeField(default=datetime.datetime.utcnow),
),
]
4 changes: 3 additions & 1 deletion helios/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,9 @@ class CastVote(HeliosModel):
# a tiny version of the hash to enable short URLs
vote_tinyhash = models.CharField(max_length=50, null=True, unique=True)

cast_at = models.DateTimeField(auto_now_add=True)
# time the server received the ballot (see one_election_cast), in UTC, not when
# it was confirmed; auto_now_add would overwrite the value the view assigns
cast_at = models.DateTimeField(default=datetime.datetime.utcnow)

# some ballots can be quarantined (this is not the same thing as provisional)
quarantined_p = models.BooleanField(default=False, null=False)
Expand Down
35 changes: 35 additions & 0 deletions helios/templates/cast_rejected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{% extends TEMPLATE_BASE %}
{% block title %}Vote Rejected{% endblock %}

{% block content %}
<h1>{{election.name}} &mdash; Vote Rejected</h1>

{% if missing_reception_time %}
<p>
Your ballot was <b>not</b> recorded: the server has no record of when it received it
(for example, because it was sent before the server was updated).
</p>
<p>
Please vote again.
</p>
{% else %}
<p>
Your ballot was <b>not</b> recorded: it was received before the ballot already on file for you,
so that ballot remains the valid one.
</p>
<p>
If you want to change your vote, vote again.
</p>
{% endif %}

{% if logout %}
<p><b>For your safety, we have logged you out.</b></p>
<iframe width="0" height="0" border="0" frameborder="0" src="{% url "auth@logout" %}">
</iframe>
{% endif %}

<p style="font-size: 1.4em;">
[ <a href="{% url "election@view" election.uuid %}">return to election info</a> ]
</p>

{% endblock %}
67 changes: 64 additions & 3 deletions helios/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,23 @@ def one_election_cast(request, election):
user = get_user(request)
encrypted_vote = request.POST['encrypted_vote']

# remember when the server received this ballot: that time, not the confirmation,
# orders it against the voter's other ballots (see one_election_cast_confirm).
# Sending the same ballot again in this session keeps the first time.
if request.session.get('encrypted_vote') != encrypted_vote or not request.session.get('encrypted_vote_received_at'):
save_in_session_across_logouts(request, 'encrypted_vote_received_at', datetime.datetime.utcnow().isoformat())

save_in_session_across_logouts(request, 'encrypted_vote', encrypted_vote)

return HttpResponseRedirect(settings.SECURE_URL_HOST + reverse(one_election_cast_confirm, args=[election.uuid]))

def _forget_pending_vote(request):
"""
Drop the ballot waiting for confirmation and the time it was received.
"""
request.session.pop('encrypted_vote', None)
request.session.pop('encrypted_vote_received_at', None)

def _voter_login_options(request, election, return_url):
"""
The ways a voter can log in to this election: the password form, the login
Expand Down Expand Up @@ -856,6 +869,10 @@ def one_election_cast_confirm(request, election):
if ('encrypted_vote' not in request.session) or request.session['encrypted_vote'] is None:
return HttpResponseRedirect(settings.URL_HOST)

# when the server received the ballot (see one_election_cast)
received_at = request.session.get('encrypted_vote_received_at')
received_at = datetime.datetime.fromisoformat(received_at) if received_at else None

# election not frozen or started
if not election.voting_has_started():
return render_template(request, 'election_not_started', {'election': election})
Expand Down Expand Up @@ -886,12 +903,18 @@ def one_election_cast_confirm(request, election):
else:
cast_ip = request.META.get('REMOTE_ADDR', None)

# prepare the vote to cast
# a ballot this voter already cast keeps the time it was first received, so
# sending it again can't make it look newer than the ballots cast after it
first_cast = CastVote.objects.filter(voter=voter, vote_hash=vote_fingerprint).order_by('cast_at').first()
if first_cast and received_at and first_cast.cast_at < received_at:
received_at = first_cast.cast_at

# prepare the vote to cast, stamped with the time it was received
cast_vote_params = {
'vote' : vote,
'voter' : voter,
'vote_hash': vote_fingerprint,
'cast_at': datetime.datetime.utcnow(),
'cast_at': received_at,
'cast_ip': cast_ip
}

Expand Down Expand Up @@ -945,6 +968,20 @@ def one_election_cast_confirm(request, election):
if not voter:
return HttpResponseRedirect(settings.SECURE_URL_HOST + reverse(one_election_cast_confirm, args=[election.uuid]))

# without the time the ballot was received there is nothing to order it by, so
# it can't be confirmed (this fails closed): the voter has to cast it again
if received_at is None:
_forget_pending_vote(request)
return HttpResponseRedirect(settings.SECURE_URL_HOST + reverse(one_election_cast_rejected, args=[election.uuid]) + '?reason=no_reception_time')

# a ballot received before the one already on file for this voter doesn't replace
# it, however late its confirmation arrives.
# No try/except: if the comparison fails, the request fails and nothing is saved.
if voter.cast_at and cast_vote.cast_at <= voter.cast_at:
# discard the pending vote so it can't be retried, and tell the voter why
_forget_pending_vote(request)
return HttpResponseRedirect(settings.SECURE_URL_HOST + reverse(one_election_cast_rejected, args=[election.uuid]))

# status update?
if request.POST.get('status_update', False):
status_update_message = request.POST.get('status_update_message')
Expand All @@ -961,7 +998,7 @@ def one_election_cast_confirm(request, election):
status_update_message = status_update_message)

# remove the vote from the store
del request.session['encrypted_vote']
_forget_pending_vote(request)

return HttpResponseRedirect(settings.SECURE_URL_HOST + reverse(one_election_cast_done, args=[election.uuid]))

Expand Down Expand Up @@ -1008,6 +1045,30 @@ def one_election_cast_done(request, election):
'vote_hash': vote_hash, 'logout': logout},
include_user=(not logout))

@election_view()
def one_election_cast_rejected(request, election):
"""
Shown when a ballot is rejected because it was received before
the ballot already recorded for this voter, or because the time it
was received is missing.
"""
user = get_user(request)
voter = get_voter(request, user, election)

# same logout as one_election_cast_done: a rejection must not leave the session open
logout = False
if voter:
if voter.user == user and voter.user is not None:
logout = settings.LOGOUT_ON_CONFIRMATION
else:
request.session.pop('CURRENT_VOTER_ID', None)

missing_reception_time = (request.GET.get('reason') == 'no_reception_time')

return render_template(request, 'cast_rejected', {'election': election, 'logout': logout,
'missing_reception_time': missing_reception_time},
include_user=(not logout))

@election_view()
@return_json
def one_election_result(request, election):
Expand Down