# VNX-820: Missing Synchronization

A Java object that one instance serves every request thread (a class extending HttpServlet/GenericServlet/HttpFilter.

| Field | Value |
|---|---|
| Rule ID | `VNX-820` |
| Severity | Medium |
| SARIF level | `warning` |
| Kind | `sast` |
| Languages | Java |
| CWE | [CWE-820](https://cwe.mitre.org/data/definitions/820.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 | `missing-sync` `cwe-820` `concurrency` `race-condition` |
| Source | [`internal/sast/rules/vnx-820.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-820.rego) |

## Overview

VNX-820 maps to [CWE-820: Missing Synchronization](https://cwe.mitre.org/data/definitions/820.html). Servlets and Spring singletons are shared by every request thread; a field written from a request handler without a lock is a data race.

## What gets flagged

```java
public class HitServlet extends HttpServlet {
    private int hits;
    private String lastUser;
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        hits++;                                   // FLAGGED
        lastUser = req.getParameter("user");      // FLAGGED: one user's value seen by another
    }
}

@Component
public class SessionCache {
    private final Map<String, String> sessions = new HashMap<>();
    public void remember(String id, String user) {
        sessions.put(id, user);                   // FLAGGED: HashMap mutated concurrently
    }
}
```

Not flagged: `ConcurrentHashMap`/`AtomicInteger` fields, writes inside `synchronized`, constructor and `init()` writes, `@Scope("prototype")` beans, locals that shadow the field.

```java
// FLAGGED: a classic servlet Filter shares one instance
public class RequestCounterFilter implements Filter {
    private long served;
    public void doFilter(ServletRequest rq, ServletResponse rs, FilterChain c) {
        served++;
    }
}
```

Not flagged: writes made between `lock.lock()` and `lock.unlock()` on a `ReentrantLock` field.

## Remediation

1. Keep per-request data in locals or the request/session, not in fields.
2. Use concurrent types (`ConcurrentHashMap`, `AtomicLong`, `LongAdder`) for shared counters and caches.
3. Guard compound updates with `synchronized` or a `Lock`.

## References

- [CWE-820: Missing Synchronization](https://cwe.mitre.org/data/definitions/820.html)
- [SEI CERT VNA02-J](https://wiki.sei.cmu.edu/confluence/display/java/VNA02-J.+Ensure+that+compound+operations+on+shared+variables+are+atomic)

## 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-820
// nosec vnx-820
```

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