# VNX-536: Servlet Runtime Error Message Containing Sensitive Information

Detects Java servlets and JSPs that catch an exception and write its message or stack trace to the HTTP response.

| Field | Value |
|---|---|
| Rule ID | `VNX-536` |
| Severity | Medium |
| SARIF level | `warning` |
| Kind | `sast` |
| Languages | Java |
| CWE | [CWE-536](https://cwe.mitre.org/data/definitions/536.html) |
| CAPEC | [CAPEC-97](https://capec.mitre.org/data/definitions/97.html) |
| ATT&CK | [T1557](https://attack.mitre.org/techniques/T1557/) |
| 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 | `servlet` `error-handling` `information-exposure` `cwe-536` |
| Source | [`internal/sast/rules/vnx-536.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-536.rego) |

## Overview

Java Servlets that catch exceptions and write stack traces or raw exception messages to the HTTP response expose internal implementation details, class names, method signatures, file paths, and line numbers, that attackers use to target follow-on exploits.

## Why this matters

A stack trace tells an attacker exactly what framework versions, libraries, and code paths exist. It reveals SQL queries, file paths, and configuration details. Error details are routinely exploited in reconnaissance phases of targeted attacks.

## What gets flagged

```java
try {
    process(request);
} catch (Exception e) {
    response.getWriter().println("Error: " + e.getMessage());   // FLAGGED
}

catch (Exception e) {
    response.sendError(500, e.getLocalizedMessage());           // FLAGGED
}

catch (IOException ex) {
    ex.printStackTrace(resp.getWriter());                       // FLAGGED
}

catch (NoSuchAlgorithmException
        | InvalidKeyException problem) {                          // a wrapped multi-catch header
    problem.printStackTrace(response.getWriter());               // FLAGGED
}
```

```jsp
<p><%= exception.getMessage() %></p>                            <%-- FLAGGED --%>
```

Not flagged: exceptions that are logged (`log.error(..., e)`, `e.printStackTrace()` to stderr), generic messages written to the response, values written outside a catch block, comments and test code. Stack traces returned by other frameworks, and database error messages in general, are [VNX-209](/sast/rules/vnx-209.md); a servlet line reported here is not also reported by VNX-209.

## Remediation

Log exceptions server-side and return a generic error to the client:

```java
// SAFE: Log internally, return generic message
try {
    doSomething();
} catch (Exception e) {
    logger.error("Operation failed", e);  // Log with full details
    response.setStatus(500);
    response.getWriter().println("{\"error\": \"An internal error occurred\"}");
}
```

Configure a custom error page in `web.xml` to catch unhandled exceptions.

## References

- [CWE-536](https://cwe.mitre.org/data/definitions/536.html)
- [OWASP Error Handling Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Error_Handling_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-536
// nosec vnx-536
```

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