A caught exception's stack trace or full text is sent back in a response

tx0/csharp:exception-details-in-responserecommendedwarningsecurity/sensitive-datadepth fastsecurity, cwe-209

What it finds

ex.ToString() and ex.StackTrace contain type names, file paths and the structure of the code, which help an attacker more than a user. A generic message with a correlation id, and the details in the log, serve both. The rule reports them passed to the result helpers of ASP.NET controllers and to WriteAsync inside a catch that names the exception.

From the rule's source, exception_details_in_response.rs.

Example from the rule's tests
using System;using Microsoft.AspNetCore.Mvc; class OrdersController : ControllerBase{    IActionResult Get()    {        try        {            return Ok(Load());        }        catch (Exception ex)        {            return BadRequest(ex.ToString());tx0 reports: BadRequest sends the exception's details to the client        }    }     object Load() => null;}
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/csharp:exception-details-in-response" = "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/csharp:exception-details-in-response] reason="..."

tx0 explain tx0/csharp:exception-details-in-response shows the same in the terminal. Found a false positive? Report it.