A servlet keeps per-request data in a mutable field that every thread sees

tx0/java:shared-servlet-staterecommendedwarningbug, security, cwe-362

What it finds

A servlet container creates one instance of a servlet and runs every request on it, each on its own thread. A field that a request writes is seen, and overwritten, by the requests running at the same time, which mixes up their data (CWE-362), as the servlet specification's notes on concurrency say.

Reported is a non-static, non-final field of a class that extends HttpServlet or GenericServlet. Fields that the container injects are left alone.

From the rule's source, servlet_instance_field.rs.

Example from the rule's tests
import javax.servlet.http.HttpServlet; class Counter extends HttpServlet {  private int hits;tx0 reports: hits is shared by all requests  private final String name = "counter";  private static int total;}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:shared-servlet-state" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:shared-servlet-state] reason="..."

tx0 explain tx0/java:shared-servlet-state shows the same in the terminal. Found a false positive? Report it.