# V5621. OWASP. Error message contains potentially sensitive data that may be exposed.

The analyzer has detected the exposure of potentially sensitive data contained in the error message. Such data includes messages and stack traces of exceptions.

Errors related to the implicit exposure of sensitive data belong to the A04:2021 – Insecure Design category of the OWASP Top 10 Application Security Risks.

Let's consider an example:

    public void Foo(string value)
    {
      try
      {
        int intVal = int.Parse(value);
        ....
      }
      catch (Exception e)
      {
        Console.WriteLine(e.StackTrace);  // <=
      }
    }

It is not recommended to show stack traces of exceptions to users. This may lead to the exposure of project details. For example, the names of libraries used in the project may be exposed. If these libraries contain known vulnerabilities, an attacker can exploit this information to attack the project.

Also, stack trace of exception for standard .NET exception classes may be exposed with the help of the 'ToString' method:

    public void Foo(string value)
    {
      try
      {
        int intVal = int.Parse(value);
        ....
      }
      catch (Exception e)
      {
        Console.WriteLine(e.ToString());  // <=
      }
    }

Keep in mind that 'ToString' is called inside output methods that take 'object' as an argument:

    Console.WriteLine(e);

To solve this architecture issue, you can prevent the output of sensitive information. For example, you can use resources explicitly related to exceptions, but not containing sensitive information. Here's a simple example with 'enum':

    enum ErrorCode
    {
      /// <summary>
      /// ArgumentNull exception occurred
      /// </summary>
      ArgumentNull,
      ....
      Unknown
    }
    public void Foo(string value)
    {
      try
      {
        int intVal = int.Parse(value);
        ....
      }
      catch (Exception e)
      {
        ErrorCode errCode = e switch
        {
          ArgumentNullException => ErrorCode.ArgumentNull,
          ....
          _ => ErrorCode.Unknown
        };
        Console.WriteLine("An error has occurred: " + errCode);  // <=
      }
    }

This diagnostic is classified as:

CWE-209 OWASP-8.3.5