C Sharp Stack Trace In Endpoint Result
Description
This detector identifies C# Minimal API route handler endpoints that return or expose stack trace information directly in HTTP responses. Leaking stack traces in endpoint results is a security risk because it reveals internal application structure, file paths, method names, and framework details to potential attackers, aiding in reconnaissance and exploitation.
Detection Strategy
• Identify route mapping declarations in the source code — these are statements that register HTTP route handlers (e.g., app.MapGet, app.MapPost, app.MapPut, etc.) in Minimal API-style C# code.
• For each identified route mapping, extract the associated handler block (the lambda or method body that processes the HTTP request and produces a response).
• Within each handler block, inspect all return statements and result-producing expressions for values that contain or propagate stack trace data — specifically looking for cases where exception stack trace properties (e.g., Exception.StackTrace) flow into the returned response value.
• A vulnerability is reported when a handler block contains a return path where stack trace information is directly included in the endpoint's response, without sanitization or filtering, meaning the raw stack trace content could be sent back to the HTTP client.
Vulnerable code example
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
var app = WebApplication.Create();
// UNSAFE: Exception details leaked to client via Results.Problem
app.MapGet("/a", () => {...✅ Secure code example
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
var app = WebApplication.Create();
app.MapGet("/a", () => {
try { throw new Exception("secret"); }...Search for vulnerabilities in your apps for free with Fluid Attacks' automated security testing! Start your 21-day free trial and discover the benefits of the Essential plan. If you prefer the Advanced plan, which includes the expertise of Fluid Attacks' pentesting team, fill out this contact form.