Error and exception handling routines are a part of input validation, and they ensure that an application can handle an error gracefully. If you’re planning to take the Security+ exam, you should have a basic understanding of secure coding concepts such as input validation and error & exception handling.
For example, can you answer this question?
Q. Web developers are implementing error and exception handling in a web site application. Which of the following represents a best practice for this?
A. Displaying a detailed error message but logging generic information on the error
B. Displaying a generic error message but logging detailed information on the error
C. Displaying a generic error message and logging generic information on the error
D. Displaying a detailed error message and logging detailed information on the error
More, do you know why the correct answer is correct and the incorrect answers are incorrect? The answer and explanation is available at the end of this post.
Avoiding Race Conditions
When two or more modules of an application, or two or more applications, attempt to access a resource at the same time, it can cause a conflict known as a race condition. Most application developers are aware of race conditions and include methods to avoid them when writing code. However, when new developers aren’t aware of race conditions, or they ignore them, a race condition can cause significant problems.
As a simple example of a potential problem, imagine you are buying a plane ticket online and use the web application to pick your seat. You find a window seat and select it. However, at the same time you’re selecting this window seat, someone else is, too. You both make the purchase at the same time and you both have tickets with the same seat number. You arrive after the other person and he’s unwilling to move, showing his ticket with the seat number. A flight attendant ultimately helps you find a seat. Unfortunately, it’s between two burly gentlemen who have been on an all-cabbage diet for the last week. You probably wouldn’t be too happy.
In reality, online ticketing applications for planes, concerts, and more avoid this type of race condition. In some cases, they lock the selection before offering it to a customer. In other cases, they double-check for a conflict later in the process. Most database applications have internal concurrency control processes to prevent two entities from modifying a value at the same time. However, inexperienced web application developers often overlook race conditions.
Error and Exception Handling
Error and exception handling routines are a part of input validation, and they ensure that an application can handle an error gracefully. They catch errors and provide user-friendly feedback to the user. When an application doesn’t catch an error, it can cause the application to fail. In the worst-case scenario, a faulty application can cause the operating system to crash. Using effective error- and exception-handling routines protects the integrity of the underlying operating system.
When an application doesn’t catch an error, it often provides debugging information that attackers can use against the application. In contrast, when an application catches the error, it can control what information it shows to the user. There are two important points about error reporting:
- Errors to users should be general. Detailed errors provide information that attackers can use against the system, so the errors should be general. Attackers can analyze the errors to determine details about the system. For example, if an application is unable to connect with a database, the returned error can let the attacker know exactly what type of database the system is running. This indirectly lets the attacker know what types of commands the system will accept. Also, detailed errors confuse most users.
- Detailed information should be logged. Detailed information on the errors typically includes debugging information. This information makes it easier for developers to identify what caused the error and how to resolve it.
Remember this
Error and exception handling helps protect the integrity of the operating system and controls the errors shown to users. Applications should show generic error messages to users but log detailed information.
Q. Web developers are implementing error and exception handling in a web site application. Which of the following represents a best practice for this?
A. Displaying a detailed error message but logging generic information on the error
B. Displaying a generic error message but logging detailed information on the error
C. Displaying a generic error message and logging generic information on the error
D. Displaying a detailed error message and logging detailed information on the error
Answer is B. You should display a generic error message but log detailed information on the error.
Detailed error messages to the user are often confusing to them and give attackers information they can use against the system.
Logging generic information makes it more difficult to troubleshoot the problem later.