# Is AI-generated login code safe? Four checks before shipping

URL: https://systemdesignsimple.com/is-ai-generated-login-code-safe/
Published: 2026-11-15
Category: AI Systems
Publisher: System Design Simple (https://systemdesignsimple.com)

## Summary

AI-generated login code can be safe, but review it first. Four checks from official guidance catch common password-verification defects: parameterized SQL, Argon2id at OWASP minimum cost or higher, a committed cap on failed tries per account (NIST sets the ceiling at 100), and one generic error after a dummy verify. Sessions, TLS, CSRF and recovery need their own review.

## Key facts

- **Veracode 2025 GenAI Code Security Report:** AI-generated code introduced risky security flaws in 45% of tests: 80 coding tasks (4 languages x 4 CWEs, 5 each) given to over 100 LLMs
- **Scope of that figure:** A benchmark of tasks chosen to hit known weaknesses, not a failure rate for logins
- **Our lab (AI 34):** 72 hand-built handlers, no AI-generated code: the message check missed 6 modeled timing-enumeration weaknesses; an extra probe found unequal verification-call counts in all 6
- **OWASP Password Storage:** Argon2id first; one minimum configuration is m=19456 (19 MiB), t=2, p=1; benchmark on the target system
- **NIST SP 800-63B-4:** The verifier SHALL limit consecutive failed attempts with one authenticator on one account to no more than 100; a disabled authenticator must be rebound before reuse
- **OWASP Authentication:** Login, reset and recovery must return a generic error whether the account exists, the password is wrong, or the account is locked

AI-generated login code can be safe, but you should not assume it is. Before you ship a login an AI assistant wrote, run four checks from official guidance: parameterized SQL, Argon2id password hashing at OWASP minimum cost or higher, a committed cap on failed tries per account, and one generic error after a dummy verify for unknown emails.

These checks cover common defects in password verification. Shipping also requires reviewing sessions, TLS, CSRF protection and account recovery, as the [OWASP Authentication Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html) lays out.

## Is AI-generated login code safe? {#veracode}

Veracode's 2025 GenAI Code Security Report says AI-generated code "introduced risky security flaws in 45% of tests" ([Veracode](https://www.veracode.com/resources/analyst-reports/2025-genai-code-security-report/)).

[Image: Veracode report landing page captured in Chrome, card reading: AI-generated code introduced risky security flaws in 45% of tests | Veracode's report page, captured in Chrome: the 45% line.]

The method section describes 80 coding tasks: four languages (Java, JavaScript, C# and Python) times four weakness types, five tasks each, given to over 100 LLMs ([report PDF](https://www.veracode.com/wp-content/uploads/2025_GenAI_Code_Security_Report_Final.pdf)). The weakness types are SQL injection (CWE 89), cross-site scripting (CWE 80), log injection (CWE 117) and a broken or risky crypto algorithm (CWE 327). Each prompt left the safe or unsafe choice to the model, and Veracode's static analysis checked the result.

Those tasks were chosen to hit known weak spots, so the 45% says nothing about how often AI-written logins fail. It does show that a model will not always pick the safe version when the prompt is silent about security. If one agent writes and another reviews, as in [one AI writes, another reviews](/one-ai-writes-one-ai-reviews/), give the reviewer these four checks.

## Check 1: is every query parameterized? {#sql}

A parameterized query sends the SQL text and the user's values to the database separately, so a value cannot change the query's structure. The [OWASP SQL Injection Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html) says to define the SQL first and bind values through the database API. Parts that cannot be bound, such as a sort column, come from a fixed list your application owns.

Search the login for any query built by joining strings or formatting the email into it. Each one becomes a placeholder (`?`, `$1` or `:email`) plus a bound value. Our article on [what SQL injection is and how to prevent it](/what-is-sql-injection-and-how-to-prevent-it/) walks through this step by step.

## Check 2: how are passwords stored? {#hash}

A stored password hash must be slow and salted, so each guess against a stolen table costs real time. The [OWASP Password Storage Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html) lists Argon2id first, with five minimum configurations of equal strength. One is m=19456 (19 MiB), t=2, p=1: memory in KiB, passes and parallelism. OWASP says to benchmark on your own server and keep a hash under one second as a general rule.

Vetted libraries generate the salt and store it, with the parameters, inside the hash string. Look for MD5, SHA-1 or plain SHA-256 on passwords, a hand-made salt, or a hashing routine written from scratch. [How a leaked password check works](/is-my-password-leaked-how-the-check-works/) explains why SHA-1 there is only a lookup key.

## Check 3: are failed tries capped? {#rate}

NIST SP 800-63B-4 says the verifier SHALL limit consecutive failed attempts with one authenticator on one account to "no more than 100" ([NIST](https://pages.nist.gov/800-63-4/sp800-63b.html#rate-limiting)). Lower limits are allowed, and NIST suggests a bot challenge or growing waits so the real user is not locked out. The count resets after a successful login.

The counter is shared state, so it has the usual race. If two requests read a count of 9 before either writes, both pass a cap of 10. The read, the decision and the update must happen in one serialized transaction, and that transaction must commit. For other unusual patterns, see [login anomaly detection](/login-anomaly-detection-3am-rule/).

## Check 4: does the login reveal which emails exist? {#enum}

User enumeration is when a login, reset or sign-up page tells an outsider whether an email has an account. The [OWASP Authentication Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html) says these pages must "respond with a generic error message" whether the email is unknown, the password is wrong or the account is locked. Keep failure status codes and redirects consistent too; OWASP notes that a different HTTP code or URL leaks the same fact ([OWASP](https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html#error-codes-and-urls)).

Timing can also leak. If the code returns early for an unknown email and only hashes for a real one, the same message arrives faster for unknown emails. A dummy verification removes the early-return shortcut. Mixed hash costs and database work can still reveal timing differences, so measure failure paths under load.

Hash costs differ because of how Argon2 hashes are encoded. A stored hash looks like `$argon2id$v=19$m=19456,t=2,p=1$<salt>$<hash>`, and argon2-cffi's `verify` reads the cost from that string, not from your current settings ([argon2-cffi API](https://argon2-cffi.readthedocs.io/en/stable/api.html)). An account hashed years ago at a lower cost verifies faster than a dummy made today. And a real account's failure writes and commits a counter update that an unknown email skips.

## What we saw when we ran it {#we-ran-it}

We ran `node labs/ai-34/probe-ai-login-security-checks.mjs` on 2026-11-15. It finished in 1.7 s and printed one table: 72 hand-built handlers, with weaknesses we planted ourselves spread across the set, scored against three checks.

![Terminal output of our ai-34 run: node probe-ai-login-security-checks.mjs](/img/posts/is-ai-generated-login-code-safe/lab-run.webp)
*Our ai-34 run in the terminal, with planted, caught, missed and false alarms for each weakness.*

SQL injection, storage algorithm/salt weaknesses and brute force came back clean: 48 of 48, 54 of 54 and 36 of 36 caught, with 0 false alarms. User enumeration was the only row with misses: 24 of 30 caught, and all 6 misses were modeled timing leaks. We did not expect the next line: only 2 handlers passed all 3 checks, and 1 of those still had a planted weakness. The run also noted that 18 of 18 scrypt fixtures use N=1024, below the OWASP minimum of N=2^17, and the storage check flagged none of them because it does not grade cost. If your login passes these checks, still read the timing path and the hash cost settings by hand before you trust it.

![Screenshot of the official page: Veracode: 2025 GenAI Code Security Report (PDF, methodology)](/img/posts/is-ai-generated-login-code-safe/source-1.webp)
*The methodology section of Veracode's 2025 GenAI Code Security Report PDF.*

## The four checks in one function {#code}

This version uses Python's `sqlite3` and `argon2-cffi` (`pip install argon2-cffi`), whose `PasswordHasher` defaults to Argon2id. It turns off `sqlite3`'s implicit transactions ([Python docs](https://docs.python.org/3/library/sqlite3.html#transaction-control)) and commits explicitly.

```python
# login.py: the four checks in one function (sqlite3 + argon2-cffi)
import sqlite3

from argon2 import PasswordHasher
from argon2.exceptions import VerifyMismatchError

ph = PasswordHasher(time_cost=2, memory_cost=19456, parallelism=1)  # Argon2id, OWASP minimum
DUMMY_HASH = ph.hash("not-a-real-password")  # verified for unknown emails and capped accounts
GENERIC = "Invalid email or password."
MAX_FAILS = 10  # assumed: your own cap, at or below the NIST ceiling of 100

def connect(path):
    # isolation_level=None: no implicit transactions, we BEGIN and COMMIT ourselves
    return sqlite3.connect(path, isolation_level=None, timeout=10)

def login(db, email, password):
    # Serialization demo. Assumed: a per-IP and per-email throttle ran before this call.
    db.execute("BEGIN IMMEDIATE")  # write lock first: one counter decision at a time
    try:
        # Check 1: parameterized query, the email travels as data
        row = db.execute(
            "SELECT id, password_hash, failed_count FROM users WHERE email = ?", (email,)
        ).fetchone()

        # Check 3: a capped account gets the dummy hash, never its real one
        real = row is not None and row[2] < MAX_FAILS

        # Check 4: a verify runs for unknown, capped and real accounts alike
        try:
            ok = ph.verify(row[1] if real else DUMMY_HASH, password) and real
        except VerifyMismatchError:
            ok = False

        # Checks 3 and 4: every failure gets one message; only real checks count
        if not ok:
            if real:
                db.execute("UPDATE users SET failed_count = failed_count + 1 WHERE id = ?", (row[0],))
            db.execute("COMMIT")
            return GENERIC

        # Check 2: rehash only when the stored profile needs an approved upgrade (not shown)
        db.execute("UPDATE users SET failed_count = 0 WHERE id = ?", (row[0],))
        db.execute("COMMIT")
        return row[0]
    except BaseException:
        db.execute("ROLLBACK")
        raise
```

The function demonstrates serialization: `BEGIN IMMEDIATE` takes SQLite's write lock before the read, so a second login waits until the first commits and then sees the new count. SQLite allows one writer per database ([SQLite docs](https://www.sqlite.org/lang_transaction.html)), and this code holds that lock through a hash, so a burst of requests, unknown emails included, stalls every other login and write. Throttle per IP and per email before the transaction opens. In production, lock only the user's row (`SELECT ... FOR UPDATE` on PostgreSQL or MySQL) and cap concurrent hashes, for example with a semaphore.

The code also leaves rehashing out. argon2-cffi's `check_needs_rehash` flags any parameter difference, so a hash stored at a higher cost than `ph` would be "upgraded" downward ([argon2-cffi API](https://argon2-cffi.readthedocs.io/en/stable/api.html#argon2.PasswordHasher.check_needs_rehash)). Rehash only when the stored profile needs an approved upgrade.

The test starts one try below the cap: five parallel wrong guesses must run exactly one real verification, and the count must survive a reconnect.

```python
# test_login_cap.py: one real verify before the cap, committed across reconnects (pytest)
import threading
from concurrent.futures import ThreadPoolExecutor

import login as auth

def test_cap_admits_one_real_verify(tmp_path, monkeypatch):
    path = tmp_path / "app.db"
    db = auth.connect(path)
    db.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT UNIQUE,"
               " password_hash TEXT, failed_count INTEGER NOT NULL DEFAULT 0)")
    db.execute("INSERT INTO users (email, password_hash, failed_count) VALUES (?, ?, ?)",
               ("a@example.com", auth.ph.hash("right"), auth.MAX_FAILS - 1))
    db.close()

    calls, guard, hasher = {"real": 0, "dummy": 0}, threading.Lock(), auth.ph

    class CountingHasher:  # wraps the real hasher and counts which hash each call used
        def verify(self, stored_hash, password):
            with guard:
                calls["dummy" if stored_hash == auth.DUMMY_HASH else "real"] += 1
            return hasher.verify(stored_hash, password)

    monkeypatch.setattr(auth, "ph", CountingHasher())

    def wrong_guess():
        conn = auth.connect(path)
        try:
            return auth.login(conn, "a@example.com", "wrong")
        finally:
            conn.close()

    with ThreadPoolExecutor(max_workers=5) as pool:
        futures = [pool.submit(wrong_guess) for _ in range(5)]
        results = [f.result() for f in futures]  # re-raises any worker exception

    assert results == [auth.GENERIC] * 5
    assert calls == {"real": 1, "dummy": 4}

    db = auth.connect(path)  # fresh connection: only committed writes are visible
    assert db.execute("SELECT failed_count FROM users").fetchone()[0] == auth.MAX_FAILS
    assert auth.login(db, "a@example.com", "right") == auth.GENERIC  # capped even with the right password
```

This example omits recovery. NIST requires a disabled authenticator to be rebound before reuse.

## What our lab found {#lab}

Our simulation used 72 hand-built handlers. The message check missed 6 modeled timing-enumeration weaknesses; an extra probe detected unequal verification-call counts. No AI generated these fixtures: we planted the weaknesses ourselves to test the checks, so the lab says nothing about how often any assistant writes them.

| Check | Planted | Caught | Missed |
|---|---|---|---|
| SQL injection | 48 | 48 | 0 |
| Password storage (algorithm and salt) | 54 | 54 | 0 |
| Brute force | 36 | 36 | 0 |
| Enumeration | 30 | 24 | 6 |

The 6 misses returned the generic message but skipped the hash for unknown users. The probe counted verifications per request; it did not measure response times, so this is modeled work, not a measured timing leak. The storage check also left all 18 low-cost scrypt fixtures unflagged, because it grades algorithm and salt, not cost. Numbers from [results.json](https://github.com/systemdesignsimple/labs/blob/main/labs/ai-34/results.json).

## Login review checklist (copy this) {#checklist}

```text
Login review checklist (password verification only)
[ ] 1. SQL: every query uses placeholders and bound values; no email or password joined into SQL text.
[ ] 2. Passwords: Argon2id from a vetted library, at an OWASP minimum (e.g. m=19456, t=2, p=1) or higher,
       timed on your own server. No MD5, SHA-1, plain SHA-256 or hand-made salt.
[ ] 3. Failed tries: counted per account, read and updated in one committed, serialized transaction,
       capped (NIST ceiling: 100), reset on success. Test after reconnect and with parallel tries.
[ ] 4. Errors: one generic message, status code and redirect for unknown email, wrong password
       and locked account; a dummy verify for unknown and locked accounts; body, status, redirect
       and timing tested together under load.
[ ] Separately: sessions, TLS, CSRF protection and account recovery.
```

To have your AI assistant run the same review, paste this:

```text
Review my login: parameterized SQL, Argon2id at OWASP minimum cost or higher, failed-try cap in a committed transaction, one generic error, dummy verify for unknown emails.
```

Read its answer against the checklist.

## Our take {#our-take}

Use these four checks when reviewing password-verification code. The two we would read slowest are the counter and the timing: our lab's misses were all timing, and the counter race looks like correct code until two requests arrive together.

This article summarizes public guidance from OWASP, NIST, Python, argon2-cffi and Veracode's published report as of 2026-11-16, plus our own simulation. It does not describe or test any specific AI product. The code is a minimal example; adapt storage, logging and recovery to your own system.

## Frequently asked questions

**How do I check if AI-written login code is safe?**
Check four things: every query is parameterized, passwords are hashed with Argon2id at OWASP minimum cost or higher, failed tries per account are capped in a committed transaction (NIST's ceiling is 100), and every failure returns one generic error after a verify. Then review sessions, TLS, CSRF and account recovery separately.

**What Argon2id settings does OWASP recommend?**
OWASP lists minimum configurations of equal strength, such as m=19456 (19 MiB), t=2, p=1 or m=47104 (46 MiB), t=1, p=1. It says to benchmark the chosen parameters on your own server and keep a hash under one second as a general rule.

**What is user enumeration on a login page?**
User enumeration is when a login, reset or sign-up page reveals whether an email has an account, through different messages or different response times. OWASP says to return one generic error and do the same processing in every case.

## Sources

- [Veracode: 2025 GenAI Code Security Report (PDF, methodology)](https://www.veracode.com/wp-content/uploads/2025_GenAI_Code_Security_Report_Final.pdf)
- [OWASP Cheat Sheet Series: SQL Injection Prevention](https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html)
- [OWASP Cheat Sheet Series: Password Storage](https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html)
- [NIST SP 800-63B-4: Rate Limiting (Throttling)](https://pages.nist.gov/800-63-4/sp800-63b.html#rate-limiting)
- [OWASP Cheat Sheet Series: Authentication](https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html)
- [Python docs: sqlite3 transaction control](https://docs.python.org/3/library/sqlite3.html#transaction-control)
- [argon2-cffi API reference](https://argon2-cffi.readthedocs.io/en/stable/api.html)
