Skip to content

fix(backend): check the account limit only when add_account creates an account - #8041

Open
yhabib wants to merge 1 commit into
mainfrom
fix/account-limit-assertion-in-add-account
Open

fix(backend): check the account limit only when add_account creates an account#8041
yhabib wants to merge 1 commit into
mainfrom
fix/account-limit-assertion-in-add-account

Conversation

@yhabib

@yhabib yhabib commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Motivation

add_account checked the account limit before it looked up the caller. At the limit, the call trapped for every caller, even one that already had an account. A trap rolls back state, so the one-off backfill that sets principal on a legacy account never ran. A legacy user then lost access to stored data, because get_account kept returning None until the backfill ran, and the frontend retried add_account on every sign-in and failed every time.

Two callers reach add_account: the add_account update endpoint (the only production caller), and a toy-data helper used only under test or toy_data_gen. The fix affects only the production endpoint.

Mainnet held 308,885 accounts against a limit of 330,000 on 2026-09-04, so the trap is not theoretical.

Changes

  • Moved the account-limit check into the branch that creates a new account, so an existing account or a legacy backfill never traps.
  • Added add_account_with_limit, a private function that takes the limit as a parameter, so add_account can still trap a new caller at ACCOUNT_LIMIT.
  • Fixed the panic message from "Pre migration account limit exceeded" to "Account limit exceeded".

Tests

Added four tests in rs/backend/src/accounts_store/tests.rs, each against a small store with a passed-in limit:

  • A new principal still traps at the limit, with the fixed message.
  • An existing principal returns false at the limit and does not trap.
  • A legacy account with principal: None gets the backfill at the limit and does not trap.
  • A new principal below the limit creates the account and returns true.

Ran ./scripts/lint-rs, cargo test, and cargo spellcheck -- --code 1. All pass.

Todos

  • Accessibility (a11y) – no impact, this is a backend-only change.
  • Changelog – not needed, ACCOUNT_LIMIT and the Candid interface are unchanged, and mainnet has not hit the limit yet.

…n account

add_account called assert_account_limit before it looked the caller up. At
the limit the call trapped for a caller that already had an account, and it
trapped before the one-off backfill that sets the principal on a legacy
account. A legacy user then lost access to stored data, because get_account
keeps returning None until the backfill runs.

Move the check into the branch that creates a new account. A new principal
still traps at the limit, which is the intended DoS protection.

Pass the limit as a parameter to add_account_with_limit so a test can reach
it without 330,000 accounts. Fix the stale panic text: the PRE_MIGRATION_LIMIT
name went away, but the message still said "Pre migration".
@yhabib
yhabib requested a review from a team as a code owner September 4, 2026 13:55
@yhabib
yhabib requested a lite review from Copilot September 4, 2026 13:55
@zeropath-ai

zeropath-ai Bot commented Sep 4, 2026

Copy link
Copy Markdown

No security or compliance issues detected. Reviewed everything up to 165bb42.

Security Overview
Detected Code Changes
Change Type Relevant files
Enhancement ► rs/backend/src/accounts_store.rs
    Add account limit handling with add_account_with_limit and parameterized limit
► rs/backend/src/accounts_store.rs
    Adjust assert_account_limit to accept limit and reflect new behavior
► rs/backend/src/accounts_store/tests.rs
    Add tests for limit behavior: add_account_at_limit_traps_for_new_account, add_account_at_limit_returns_false_for_existing_account, add_account_at_limit_backfills_legacy_principal, add_account_below_limit_creates_account

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approved

The change is small, directly addresses the stated failure mode, and is covered by targeted new tests exercising both limit and legacy-backfill scenarios.

Pull request overview

This PR adjusts AccountsStore::add_account so the account-limit check only runs when a new account would be created, preventing traps for existing accounts (including legacy accounts that need principal backfill) when the store is at its limit.

Changes:

  • Refactored add_account to delegate to a new add_account_with_limit helper and moved the limit assertion into the “create new account” branch.
  • Updated the account-limit panic message to “Account limit exceeded …”.
  • Added focused unit tests covering: trap-at-limit for new accounts, non-trap behavior for existing accounts, legacy principal backfill at limit, and successful create below limit.
File summaries
File Description
rs/backend/src/accounts_store.rs Moves the limit check to only apply when inserting a new account; introduces add_account_with_limit for testability; updates panic message.
rs/backend/src/accounts_store/tests.rs Adds unit tests validating limit behavior for new/existing/legacy accounts and below-limit creation.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants