Skip to content

Commit

Permalink
Perform pessimistic handle check when updating handle (#1300)
Browse files Browse the repository at this point in the history
perform pessimistic handle check when updating handle
  • Loading branch information
devinivy authored Jul 7, 2023
1 parent 08dc2b7 commit 2e52f38
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/pds/src/api/com/atproto/identity/updateHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ export default function (server: Server, ctx: AppContext) {
}
}

// Pessimistic check to handle spam: also enforced by updateHandle() and the db.
const available = await ctx.services
.account(ctx.db)
.isHandleAvailable(handle)
if (!available) {
throw new InvalidRequestError(`Handle already taken: ${handle}`)
}

const seqHandleTok = await ctx.db.transaction(async (dbTxn) => {
let tok: HandleSequenceToken
try {
Expand Down
11 changes: 11 additions & 0 deletions packages/pds/src/services/account/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export class AccountService {
.set({ handle })
.where('did', '=', did)
.whereNotExists(
// @NOTE see also condition in isHandleAvailable()
this.db.db
.selectFrom('did_handle')
.where('handle', '=', handle)
Expand All @@ -178,6 +179,16 @@ export class AccountService {
await sequencer.sequenceEvt(this.db, seqEvt)
}

async isHandleAvailable(handle: string) {
// @NOTE see also condition in updateHandle()
const found = await this.db.db
.selectFrom('did_handle')
.where('handle', '=', handle)
.select('handle')
.executeTakeFirst()
return !found
}

async updateEmail(did: string, email: string) {
await this.db.db
.updateTable('user_account')
Expand Down

0 comments on commit 2e52f38

Please sign in to comment.