# Add Account Lockout to a Passport Local Login

Yes. An agent counts failed logins per username in its own table, locks for a growing period without revealing which accounts exist, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-account-lockout/passport/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the verify function line by line, because lockout lives inside it. In an app like passport/todos-express-password, `routes/auth.js` gives `passport-local` a function that selects the user by `username`, answers `Incorrect username or password.` at once when there is no row, and otherwise runs `crypto.pbkdf2` with 310,000 iterations and compares the result with `crypto.timingSafeEqual`. `POST /login/password` passes `failureMessage: true`, so the message lands in `req.session.messages` and `views/login.ejs` prints it. Nothing counts attempts, and each guess at a real username costs the server a full password hash.

The first design decision is where the count lives, and the agent keeps it out of the `users` table. `db.js` creates its tables with `CREATE TABLE IF NOT EXISTS`, which does nothing to a `users` table already sitting in `var/db/todos.db`, so new columns there would be missing on every existing install. A new `login_attempts` table keyed by the submitted username is created on the next start without touching anything. It also lets the app count failures for usernames that do not exist. That matters: if only real accounts could become locked, the lockout itself would tell an attacker which usernames are real.

The policy goes at the top of the verify function, before the user lookup and the hash. If the username is locked, the function fails the login without hashing anything, which also takes away the cheap way to keep the server busy. After a set number of failures inside a window the username is locked for a period that grows with each further lock, and a successful login clears the row. The message stays `Incorrect username or password.` in every case, locked or not, so the login page says nothing new. The agent makes the threshold and the durations environment variables with defaults, and writes the count with a single upsert so two requests arriving together cannot both read the old value.

Then it drives the real form. `csurf` rejects a post without a token, so the agent fetches `/login` with a cookie jar, reads the `_csrf` field and posts with it, as a browser would. With the lock period set to a few seconds, it submits wrong passwords for the seeded user until the lock starts, then the correct password, which must be refused during the lock and accepted after it. It repeats the sequence for a username that was never registered and compares status, redirect and message with the real account's, since any difference is a leak. It adds unit tests with `node:test` for the policy function, with the clock passed in so nothing sleeps, and sets a `test` script, which `package.json` does not have.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "A Linear issue labelled security asks for account lockout on this Express and Passport app's password login. Read package.json, app.js, db.js, routes/auth.js, routes/index.js and views/login.ejs first. Count failed logins per submitted username in a new table; explain why not in new columns on users, given how db.js creates tables. Count attempts for usernames that do not exist exactly as for ones that do. Check the lock at the top of the LocalStrategy verify function, before the user lookup and before crypto.pbkdf2. After a threshold of failures inside a window, lock the username for a period that grows with repeated locks; clear the record on a successful login. Keep the failure message identical for a wrong password, an unknown username and a locked one. Read the threshold, window and durations from environment variables with sensible defaults and document them in the README. Write the counter with one atomic statement, and remove a username's record once its window and its lock have both expired. Test through the real form with curl and a cookie jar, taking the _csrf token from GET /login: wrong passwords up to the lock, the correct password refused while locked, the correct password accepted after the lock ends, and the same sequence for an unregistered username, showing that status, redirect and message match. Add node:test unit tests for the policy with an injected clock and add a test script. Do not add IP rate limiting or change the signup route; if signup reveals whether a username exists, report it without fixing it. Say in the pull request how someone could use the lockout to keep a real user out and what limits that. Open a pull request that links the issue.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The pull request changes `db.js`, `routes/auth.js`, `package.json` and the README, and adds a small policy module with its tests. The description states the policy in numbers the reader can change, names the environment variables, and shows the form-driven sequences as request and response pairs: each attempt's status, its redirect target and the message rendered on the login page. The two sequences, real username and unregistered one, are set side by side so the reviewer can see they are indistinguishable.

It is direct about what lockout costs. Anyone who knows a username can lock its owner out by failing on purpose, so the lock is temporary and starts short, and the description says how long a determined attacker could keep an account unavailable under the defaults. It reports what it found and left alone: `POST /signup` inserts into a column with a unique constraint and passes the database error to the error handler, so signing up with a taken username already reveals that the account exists. That is a separate fix, and the description says so.

A reviewer checks the order of operations in the verify function first, since a lock check placed after the hash would still let an attacker spend the server's time. They confirm that the counter is keyed by exactly what the user typed, including case, and decide whether usernames should be normalised. They also look at the table's growth: rows for random usernames accumulate, so the agent's change includes removing a row once its window and lock have both expired, and the reviewer decides if that is enough for the traffic the app sees.

## Run it on every Linear issue labelled security

An automation in the Project that holds the repository, with a `linear` trigger on `linear.issue.labeled` and `security` in its `labels`, starts one run when someone adds that label. The issue is placed after the saved prompt as data and fills in no placeholder, so a general prompt works best: read the issue that follows, make the change it describes in this repository, test it through the running app and open a pull request that links the issue. The details on this page, such as where the counter lives and what the message must say, belong in the issue.

For work like this a team may want to see the approach first. The same trigger also fires on `linear.issue.state_changed`: map a state name to `plan` and moving an issue there starts a read-only run that posts its plan as a comment on the issue, while a state mapped to `exec` starts an ordinary run. The automation needs its runner's Linear connection, made in the console under Settings → Connections, and it fires only for changes made by people in your Rush org.

## Why run it on Rush?

A lockout is a set of promises about behaviour over time: the attempt after the threshold is refused, the right password is refused while locked, and an unknown username looks exactly like a known one. None of that can be confirmed by reading the diff. In a Rush sandbox the agent starts the app, sends the attempts through the real form with real CSRF tokens, waits out a short lock and compares the two sequences, and the whole exchange is kept in the run's record for the reviewer to read.

Security changes also benefit from a clean machine. The app writes its databases under `var/`, which `.gitignore` excludes, so a developer's laptop carries whatever state earlier experiments left there. The sandbox starts with no database files, which is how a new deployment starts, and a second run against an existing database file shows the upgrade path too. Both are cheap to do when the machine is disposable.

## Frequently asked questions

### Why not lock by IP address instead of by username?

They answer different attacks. Per-IP limits slow one client trying many usernames; per-username lockout stops many clients trying one account. This page adds the second. A rate limiter in front of the login route is a good companion, and the request leaves it out so the pull request does one thing.

### Will users see a message that their account is locked?

Not with this design. The login page shows the same `Incorrect username or password.` text whether the password was wrong, the username unknown or the account locked, because a distinct message would confirm that the username exists. If you prefer to tell users, the agent can send the notice by email to the account owner instead, which you would ask for in the issue.

### Does it work with a session store other than SQLite?

Yes. The attempt counter is in the app's own database, not in the session, so it does not depend on `connect-sqlite3`. It has to be in a store every app instance shares, though: with several servers, an in-memory counter would let an attacker spread guesses across them.

### Can it reset a locked account for support staff?

The lock is one row, so clearing it is one statement, and the agent documents that statement in the README. An admin screen or command for it is beyond what the issue asked for; add it to the issue if support needs one, and the same run will build and test it.

## More Passport.js jobs

- [Add Google Login to an Express App with Passport](https://getrush.ai/run/add-google-login/passport/)
- [Fix "Error: req#logout requires a callback function" in Passport 0.6 and later](https://getrush.ai/run/fix-req-logout-requires-a-callback-function/passport/)
- [Review Pull Requests That Change Passport.js Login Code](https://getrush.ai/run/review-pull-requests/passport/)

## The same job on other stacks

- [Add Account Lockout to a Mongoose and Express App](https://getrush.ai/run/add-account-lockout/mongoose/)
- [Add Account Lockout After Failed Logins to a Node.js App](https://getrush.ai/run/add-account-lockout/nodejs/)

## More jobs like this: Turn a Linear label into a PR

- [Make Celery Tasks Survive a Worker Crash With acks_late](https://getrush.ai/run/add-acks-late/celery/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
