...
Code Block | ||
---|---|---|
| ||
public class ExceptionLog { private String logMessage; public static void main(String[] args) { ExceptionLog log = new ExceptionLog(); //some security exception occurs here log.logMessage("Security Exception has occurred!"); log.writeLog(); } public void logMessage(String message) { logMessage = message; } public void writeLog() { try { // This can throw an exception and prevent logging. FileWriter fw = new FileWriter("log_file.txt", true); //this can throw an exception and prevent logging BufferedWriter br = new BufferedWriter(fw); br.write(logMessage + "\n"); br.close(); } catch (FileNotFoundException fnf){ logMessage("File Not Found Exception!"); } catch(IOException ie) { logMessage("IO Exception!"); } System.err.println(logMessage); //misses writingNo thelog originalmessage securityis exceptionwritten to logthe file, as logMessage has changed in the event of an exception. } } |
Compliant Solution
This compliant solution executes several statements that can possibly throw exceptions prior to performing any security critical operations and allows writeLog()
to throw an exception back to the caller in the event of a problem writing to the log file. As a result, exceptions do not result in the silent failure to log a message or a different message than intended being logged. While this is a stringent requirement, it is necessary in cases where an exception can be deliberately thrown to conceal an attacker's tracks. The logging mechanism must be robust and should be able to detect and handle such phenomena.
...