CS.EXCEPT.NO_LOG

Ensure all exceptions are either logged with a standard logger or rethrown.

This rule identifies code that does not log caught exceptions with a standard logger or rethrow caught exceptions.

Mitigation and prevention

Using a logging mechanism to keep track of caught exceptions can provide a clearer and more secure overview of the possible security vulnerabilities, and this information could help you implement a prompt and accurate fix.

Enforcing this rule will help to protect against the OWASP 2007 Top 10 application vulnerability "A6 - Information Leakage and Improper Error Handling".

Security training

Application security training materials provided by Secure Code Warrior.

Vulnerable code example

Copy
  public class Example
  {
      public void readFile(String fileName)
      {
          try
          {
              FileInfo fi = new FileInfo(fileName);
              FileStream fs = fi.OpenRead();
              fs.Close();
         }
         catch (IOException e)
         {
             Console.WriteLine("Exception found");
         }
     }
 }

Violation is reported on line 11.

Fixed code example

Copy
  public class Example
  {
      public void readFile(String fileName)
      {
          try
          {
             FileInfo fi = new FileInfo(fileName);
              FileStream fs = fi.OpenRead();
              fs.Close();
         }
         catch (IOException e) // FIX
         {
             (new Logger()).Error("Failed to read file. " + e.Message);
         }
     }
     public class Logger
     {
         public void Error(string errorDetails)
         {
             /* Logging the error */
         }
     }
 }