Skip to content

Commit

Permalink
Prevent request hashes from being user more than once (#1191)
Browse files Browse the repository at this point in the history
* Prevent request hashes from being user more than once

* Sort user solutions before giving to tasks

* fix typo
  • Loading branch information
forgetso authored May 2, 2024
1 parent 989b22e commit 0320c57
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
10 changes: 4 additions & 6 deletions packages/database/src/databases/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,9 +634,9 @@ export class ProsopoDatabase extends AsyncFactory implements Database {
}

/**
* @description Update a Dapp User's pending record
* @description Mark a pending request as used
*/
async updateDappUserPendingStatus(userAccount: string, requestHash: string, approve: boolean): Promise<void> {
async updateDappUserPendingStatus(requestHash: string): Promise<void> {
if (!isHex(requestHash)) {
throw new ProsopoEnvError('DATABASE.INVALID_HASH', {
context: { failedFuncName: this.updateDappUserPendingStatus.name, requestHash },
Expand All @@ -647,10 +647,7 @@ export class ProsopoDatabase extends AsyncFactory implements Database {
{ requestHash: requestHash },
{
$set: {
accountId: userAccount,
pending: false,
approved: approve,
requestHash,
},
},
{ upsert: true }
Expand Down Expand Up @@ -794,7 +791,8 @@ export class ProsopoDatabase extends AsyncFactory implements Database {
*/
async getDappUserCommitmentByAccount(userAccount: string): Promise<UserCommitmentRecord[]> {
const docs: UserCommitmentRecord[] | null | undefined = await this.tables?.commitment
?.find({ userAccount })
// sort by most recent first to avoid old solutions being used in development
?.find({ userAccount }, { _id: 0 }, { sort: { _id: -1 } })
.lean()

return docs ? (docs as UserCommitmentRecord[]) : []
Expand Down
2 changes: 2 additions & 0 deletions packages/provider/src/api/captcha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export function prosopoRouter(env: ProviderEnvironment): Router {
: tasks.getDappUserCommitmentByAccount(parsed.user))

if (!solution) {
tasks.logger.debug('Not verified - no solution found')
return res.json({
[ApiParams.status]: req.t('API.USER_NOT_VERIFIED'),
[ApiParams.verified]: false,
Expand All @@ -162,6 +163,7 @@ export function prosopoRouter(env: ProviderEnvironment): Router {
[ApiParams.verified]: false,
}
if (timeSinceCompletion > parsed.maxVerifiedTime) {
tasks.logger.debug('Not verified - time run out')
return res.json(verificationResponse)
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/provider/src/tasks/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ export class Tasks {
const userSignature = hexToU8a(signature)
const blockNumber = await getCurrentBlockNumber(this.contract.api)
if (pendingRequest) {
// prevent this request hash from being used twice
await this.db.updateDappUserPendingStatus(requestHash)
const commit: UserCommitmentRecord = {
id: commitmentId,
userAccount: userAccount,
Expand Down
2 changes: 1 addition & 1 deletion packages/types-database/src/types/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export interface Database {

getDappUserPending(requestHash: string): Promise<PendingCaptchaRequest>

updateDappUserPendingStatus(userAccount: string, requestHash: string, approve: boolean): Promise<void>
updateDappUserPendingStatus(requestHash: string): Promise<void>

getAllCaptchasByDatasetId(datasetId: string, captchaState?: CaptchaStates): Promise<Captcha[] | undefined>

Expand Down

0 comments on commit 0320c57

Please sign in to comment.