-
Notifications
You must be signed in to change notification settings - Fork 53
fix(backend): prevent double voting #1257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
538657c
a4a7c1c
5c3ff77
a74a75e
305dbd5
38c9f29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,19 +2,80 @@ import { Ballot } from "@equal-vote/star-vote-shared/domain_model/Ballot"; | |
| import { ElectionRoll } from "@equal-vote/star-vote-shared/domain_model/ElectionRoll"; | ||
| import { ILoggingContext } from "../Services/Logging/ILogger"; | ||
| import Logger from "../Services/Logging/Logger"; | ||
| import { Kysely } from "kysely"; | ||
| import { Database } from "./Database"; | ||
| import { Uid } from "@equal-vote/star-vote-shared/domain_model/Uid"; | ||
| import ServiceLocator from "../ServiceLocator"; | ||
|
|
||
| export type CastVoteEvent = { | ||
| requestId: Uid, | ||
| inputBallot: Ballot, | ||
| roll?: ElectionRoll, | ||
| userEmail?: string, | ||
| isBallotUpdate: boolean, | ||
| } | ||
| var pgFormat = require("pg-format"); | ||
|
|
||
| export default class CastVoteStore { | ||
| _postgresClient; | ||
| _db: Kysely<Database>; | ||
| _ballotTableName: string; | ||
| _rollTableName: string; | ||
|
|
||
| constructor(postgresClient: any) { | ||
| constructor(postgresClient: any, db: Kysely<Database>) { | ||
| this._postgresClient = postgresClient; | ||
| this._db = db; | ||
| this._ballotTableName = "ballotDB"; | ||
| this._rollTableName = "electionRollDB"; | ||
| } | ||
|
|
||
| async submitBallotEvent(event: CastVoteEvent, ctx: ILoggingContext): Promise<void> { | ||
| return this._db.transaction().execute(async (trx) => { | ||
| if (event.roll) { | ||
| const currentRoll = await trx.selectFrom('electionRollDB') | ||
| .select(['submitted']) | ||
| .where('election_id', '=', event.roll.election_id) | ||
| .where('voter_id', '=', event.roll.voter_id) | ||
| .where('head', '=', true) | ||
| .forUpdate() | ||
| .executeTakeFirst(); | ||
|
|
||
| if (currentRoll && currentRoll.submitted && !event.isBallotUpdate) { | ||
| throw new Error("ALREADY_VOTED"); | ||
| } | ||
|
|
||
| if (!currentRoll) { | ||
|
||
| // Rescan to catch newly committed rows if a concurrent transaction modified our locked row while we waited. | ||
| const doubleCheckRoll = await trx.selectFrom('electionRollDB') | ||
| .select(['submitted']) | ||
| .where('election_id', '=', event.roll.election_id) | ||
| .where('voter_id', '=', event.roll.voter_id) | ||
| .where('head', '=', true) | ||
| .forUpdate() | ||
| .executeTakeFirst(); | ||
|
|
||
| if (doubleCheckRoll && doubleCheckRoll.submitted && !event.isBallotUpdate) { | ||
| throw new Error("ALREADY_VOTED"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const BallotModel = ServiceLocator.ballotsDb(); | ||
| const ElectionRollModel = ServiceLocator.electionRollDb(); | ||
|
|
||
| if (event.isBallotUpdate) { | ||
| await BallotModel.updateBallot(event.inputBallot, ctx, `User updates a ballot`, trx); | ||
| } else { | ||
| await BallotModel.submitBallot(event.inputBallot, ctx, `User submits a ballot`, trx); | ||
| } | ||
|
|
||
| if (event.roll != null) { | ||
| event.roll.submitted = true; | ||
| await ElectionRollModel.update(event.roll, ctx, `User submits a ballot`, trx); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| submitBallot( | ||
|
||
| ballot: Ballot, | ||
| roll: ElectionRoll, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,16 @@ | ||
| import { Ballot } from "@equal-vote/star-vote-shared/domain_model/Ballot"; | ||
| import { Uid } from "@equal-vote/star-vote-shared/domain_model/Uid"; | ||
| import { ILoggingContext } from "../Services/Logging/ILogger"; | ||
| import { Kysely, Transaction } from 'kysely'; | ||
| import { Database } from './Database'; | ||
|
|
||
| export interface IBallotStore { | ||
| submitBallot: (ballot: Ballot, ctx:ILoggingContext, reason:string) => Promise<Ballot>; | ||
| updateBallot: (ballot: Ballot, ctx: ILoggingContext, reason: string) => Promise<Ballot>; | ||
| bulkSubmitBallots: (ballots: Ballot[], ctx:ILoggingContext, reason:string) => Promise<Ballot[]>; | ||
| getBallotByID: (ballot_id: string, ctx:ILoggingContext) => Promise<Ballot | null>; | ||
| getBallotsByElectionID: (election_id: string, ctx:ILoggingContext) => Promise<Ballot[] | null>; | ||
| getBallotByVoterID: (voter_id: string, election_id: string, ctx:ILoggingContext) => Promise<Ballot | undefined>; | ||
| delete(ballot_id: Uid, ctx:ILoggingContext, reason:string): Promise<boolean>; | ||
| deleteAllBallotsForElectionID: (election_id: string, ctx:ILoggingContext) => Promise<boolean>; | ||
| submitBallot: (ballot: Ballot, ctx: ILoggingContext, reason: string, db?: Kysely<Database> | Transaction<Database>) => Promise<Ballot>; | ||
| updateBallot: (ballot: Ballot, ctx: ILoggingContext, reason: string, db?: Kysely<Database> | Transaction<Database>) => Promise<Ballot>; | ||
| bulkSubmitBallots: (ballots: Ballot[], ctx: ILoggingContext, reason: string, db?: Kysely<Database> | Transaction<Database>) => Promise<Ballot[]>; | ||
| getBallotByID: (ballot_id: string, ctx: ILoggingContext, db?: Kysely<Database> | Transaction<Database>) => Promise<Ballot | null>; | ||
| getBallotsByElectionID: (election_id: string, ctx: ILoggingContext, db?: Kysely<Database> | Transaction<Database>) => Promise<Ballot[] | null>; | ||
| getBallotByVoterID: (voter_id: string, election_id: string, ctx: ILoggingContext, db?: Kysely<Database> | Transaction<Database>) => Promise<Ballot | undefined>; | ||
| delete(ballot_id: Uid, ctx: ILoggingContext, reason: string, db?: Kysely<Database> | Transaction<Database>): Promise<boolean>; | ||
| deleteAllBallotsForElectionID: (election_id: string, ctx: ILoggingContext, db?: Kysely<Database> | Transaction<Database>) => Promise<boolean>; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This log isn't visible to an election admin. So, from their point of view, this will fail silently. I suppose we need some ux plan for handling this.