# VNX-303: Incorrect Implementation of Authentication Algorithm

Detects password checks that cannot give the right answer: bcrypt.compare()/argon2.verify() used without await (a Promise is always truthy.

| Field | Value |
|---|---|
| Rule ID | `VNX-303` |
| Severity | Medium |
| SARIF level | `warning` |
| Kind | `sast` |
| Languages | Java, JavaScript / TypeScript, Python |
| CWE | [CWE-303](https://cwe.mitre.org/data/definitions/303.html) |
| CAPEC | [CAPEC-66](https://capec.mitre.org/data/definitions/66.html) |
| ATT&CK | [T1190](https://attack.mitre.org/techniques/T1190/) |
| CWSS vector | `CWSS:1.0/TI:M/AP:RU/AL:S/IC:N/FC:LT/RP:RU/RL:S/AV:L/AS:N/IN:A/SC:A/BI:M/DI:H/EX:M/EC:N/P:C` |
| Tags | `auth` `password-verification` `bcrypt` `cwe-303` |
| Source | [`internal/sast/rules/vnx-303.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-303.rego) |

## Overview

VNX-303 maps to [CWE-303: Incorrect Implementation of Authentication Algorithm](https://cwe.mitre.org/data/definitions/303.html). The right primitive is in use, but it is called in a way that defeats it.

1. **An asynchronous verifier used as a boolean (JavaScript/TypeScript).** `bcrypt.compare()`, `bcryptjs.compare()` and `argon2.verify()` return a Promise. Without `await`, `.then()` or a callback, `if (bcrypt.compare(pw, hash))` is always true and every password is accepted; `if (!argon2.verify(...))` never rejects. Reported inside an `if`, `&&`, `||`, `!` or ternary, or when assigned to a name that a condition within five lines tests.
2. **Re-hashing with a fresh salt, then comparing (Python, Java, JavaScript/TypeScript).** `bcrypt.hashpw(pw, bcrypt.gensalt()) == stored`, `generate_password_hash(pw) == stored`, a passlib/argon2 hasher's `.hash(pw) ==`, `passwordEncoder.encode(pw).equals(stored)`, `BCrypt.hashpw(pw, BCrypt.gensalt()).equals(...)`, `bcrypt.hash(pw, 10) === stored`, inline or through a name assigned from the hash. A salted hash never repeats, so the check always fails, and is typically "fixed" by weakening it.

## What gets flagged

```js
if (user && bcrypt.compare(req.body.password, user.passwordHash)) {   // FLAGGED: always truthy
  req.session.userId = user.id;
}
```

```python
if bcrypt.hashpw(password.encode(), bcrypt.gensalt()) == user.password_hash:   # FLAGGED
    return user
```

```java
if (passwordEncoder.encode(rawPassword).equals(user.getPassword())) {   // FLAGGED
```

## What is not flagged

- `await bcrypt.compare(...)`, the callback and `.then()` forms, and a returned Promise.
- `compareSync`, `bcrypt.checkpw`, `check_password_hash`, `passwordEncoder.matches`.
- Re-hashing with the stored hash as the salt (`bcrypt.hashpw(pw, stored) == stored`) and hashing at registration.
- Comments and string literals.

## Remediation

Use the library's verifier and await it:

```js
if (user && await bcrypt.compare(req.body.password, user.passwordHash)) { ... }
```

```java
if (passwordEncoder.matches(rawPassword, user.getPassword())) { ... }
```

```python
if bcrypt.checkpw(password.encode(), user.password_hash): ...
```

## References

- [CWE-303: Incorrect Implementation of Authentication Algorithm](https://cwe.mitre.org/data/definitions/303.html)
- [OWASP Password Storage Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html)

## Suppressing this rule

Put a `nosec` comment on the line, or on line 1 to cover the whole file. Use the comment marker of the file's language (`#`, `//`, `--` or `;`):

```text
# nosec vnx-303
// nosec vnx-303
```

Or record an ignore rule: `vulnetix ignore add --rule vnx-303 --reason "..."`. See [Suppressing findings](/sast/guides/suppressing-findings.md).
