# VNX-1254: Incorrect Comparison Logic Granularity

A secret (a MAC, HMAC, signature, digest, hash, token, key, password, OTP.

| Field | Value |
|---|---|
| Rule ID | `VNX-1254` |
| Severity | Low |
| SARIF level | `note` |
| Kind | `sast` |
| Languages | Java |
| CWE | [CWE-1254](https://cwe.mitre.org/data/definitions/1254.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:L/AP:RU/AL:S/IC:N/FC:LT/RP:RU/RL:S/AV:L/AS:N/IN:A/SC:A/BI:L/DI:H/EX:L/EC:N/P:L` |
| Tags | `timing` `comparison` `early-exit` `java` `cwe-1254` |
| Source | [`internal/sast/rules/vnx-1254.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-1254.rego) |

## Overview

VNX-1254 maps to [CWE-1254: Incorrect Comparison Logic Granularity](https://cwe.mitre.org/data/definitions/1254.html).

## Why this matters

A loop that compares a MAC, signature or token one element at a time and returns at the first mismatch takes longer the more leading bytes are right. By timing many requests an attacker can recover a valid value byte by byte.

## What gets flagged

```java
for (int i = 0; i < expectedMac.length; i++) {
    if (expectedMac[i] != providedMac[i]) return false;
}

for (int i = 0; i < storedToken.length(); i++) {
    if (storedToken.charAt(i) != presented.charAt(i)) {
        return false;
    }
}
```

Not flagged: `diff |= a[i] ^ b[i]` followed by one test, `MessageDigest.isEqual(a, b)`, loops over values that are not secrets.

## Remediation

1. Compare secrets with `MessageDigest.isEqual(expected, actual)`.
2. If you must write the loop, accumulate differences over the whole length and test once at the end.

## References

- [CWE-1254: Incorrect Comparison Logic Granularity](https://cwe.mitre.org/data/definitions/1254.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-1254
// nosec vnx-1254
```

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