Skip to content
Merged
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
2 changes: 2 additions & 0 deletions rustsystem-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub enum APIErrorCode {
InvalidMetaData,
InvalidVoteMethod,
InvalidVoteLength,
InvalidCandidateId,
VotingInactive,

SignatureInvalid,
Expand Down Expand Up @@ -105,6 +106,7 @@ impl APIErrorCode {
409,
),
Self::InvalidVoteLength => ("Too many candidates were provided.", 409),
Self::InvalidCandidateId => ("Vote contains an out-of-bounds candidate index.", 422),
Self::VotingInactive => ("Vote has already been closed, or it was never opened.", 410),

Self::SignatureInvalid => (
Expand Down
11 changes: 11 additions & 0 deletions rustsystem-server/src/api/voter/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl APIHandler for Submit {

validate_metadata(metadata.clone(), round)?;
validate_num_choices(choice.clone(), round)?;
validate_candidate_indices(choice.clone(), round)?;
validate_signature(validation, round)?;

let is_blank = choice.is_none();
Expand Down Expand Up @@ -82,6 +83,16 @@ fn validate_num_choices(choice: Option<Choice>, round: &VoteRound) -> Result<(),
Ok(())
}

fn validate_candidate_indices(choice: Option<Choice>, round: &VoteRound) -> Result<(), APIError> {
if let Some(choices) = choice {
let num_candidates = round.metadata().get_candidates().len();
if choices.iter().any(|&id| id >= num_candidates) {
return Err(APIError::from_error_code(APIErrorCode::InvalidCandidateId));
}
}
Ok(())
}

fn validate_signature(
validation: &BallotValidation,
round: &mut VoteRound,
Expand Down
Loading