Lesson 2 of 2
Error handling
Listen to an AI summary of Error handling
Every program meets errors, such as a missing file or text where the code expects a number. Error handling is the code that decides what a program does after an error.
A door that unlocks in a power cut
The front door of a sports centre has an electric lock. In a power cut one kind of door unlocks, and anyone can walk in. Another kind of door stays locked until the power returns.
In software the lock is a check, and the power cut is an error in the check. Failing open (CWE-636) is treating an error in a check as a pass. Failing closed is treating the error as a refusal. A security check must fail closed.
What is an exception
An exception is the signal a program raises when an error happens, which stops the normal flow of the code. Python raises an exception when the function int gets text that is not a number. With no code to catch the exception, the program stops.
A catch block is the code that runs when an exception happens in the lines the block covers. In Python a catch block is written with the words try and except. The picture labels a catch block in a small program.
catch in place of except.After a catch block runs, the program carries on with the line after the block.
The ownership check in the sports shop
Each order of the sports shop has a number. The ownership check confirms an order belongs to the signed-in customer.
The server runs the function in the picture for each request for an order page. Look at lines 7, 8 and 9.
session holds what the server remembers about the signed-in customer, and != means is not equal to.Sam, a customer, is signed in and asks for order 1043, which belongs to Priya. Line 4 finds that Priya is the owner, so line 6 sends Sam a page that says not found.
Sam asks again when the database is too busy to answer. Line 4 raises an exception. Python skips lines 5 and 6 and runs the catch block. Line 8 writes the error to a log.
The catch block ends, and the program carries on with line 9. Sam sees Priya's order.
An attacker can cause the error with many requests sent together (Limits on what a request can use).
The safer catch block
The safer function adds one line to the catch block, the third line of the four:
except Exception: log_error("ownership check failed") return error_page() return order_page(order_id)
The new line sends an error page and ends the function, so the last line never runs after an exception. An error now leads to a refusal, the same as a fail, and the order page fails closed.
Reading a catch block
A reviewer reads each catch block and asks what the program does next.
- An empty catch block
- The program carries on, and nothing records the error.
- A catch block that writes to the log and carries on
- The lines after the block use a value that was never set, or a check that never finished.
- A catch block for every kind of exception
except Exceptiontreats text that is not a number and a server with no memory left in the same way.
Some functions report an error by returning a value such as False. Code that never reads the returned value hides the error as well (Rules the code must enforce).
What is an error handler
A catch block does not cover every line of a program. The sports shop uses a framework named Flask. An error handler is one piece of code that receives every exception the rest of the code did not catch.
A stack trace lists the functions that were running when an error happened, with file names and line numbers. Look at what line 3 sends to the customer.
str(error) is the message of the exception, and traceback.format_exc() is the stack trace.A verbose error message shows a customer details of the code, such as a stack trace. The weakness is named generation of an error message containing sensitive information (CWE-209). An attacker causes an error on purpose, and the message can name the files, tables and queries the code uses.
The safer error handler
The safer error handler sends the details to the log and a fixed message to the customer. Look at lines 3, 4 and 5.
@app.errorhandler(Exception) def handle_error(error): reference = new_reference() log_error(reference, error, request.path) return error_page("Something went wrong. Reference " + reference)
Line 3 makes a short random code called a reference. Line 4 writes the reference and the exception to the log, with request.path, the page the customer asked for. Line 5 sends the customer a fixed message and the reference.
Examples of what never goes to the customer
- The kind of exception and the message of the exception, which can name a file, a table or a query.
- A stack trace.
- The names and IP addresses of the shop's own servers.
- A different message for each cause of a failed sign-in, such as
No account with that usernameandWrong password. The two messages tell an attacker which usernames are real (Signing in).
An application with no error handler uses the error page of the framework. In debug mode, the framework's page shows the stack trace (Framework configuration).
Examples of what to look for
- A catch block around a sign-in check or an ownership check, with no line that ends the request.
- A catch block that is empty, or that writes to the log and carries on.
- A message to the customer built from an exception, such as
str(error)in Python orerr.stack, the stack trace in JavaScript. - An application with no error handler, where debug mode can be on in production.
- A different error message for each cause on a sign-in page.
- A function that returns
Falsefor an error, and code that never reads the returned value. - A JavaScript promise whose error no code catches.
Check your understanding
Name two things that an error message to a customer never contains.
Show the answer
A stack trace and the message of the exception. The names and IP addresses of the shop's own servers also count.
The database does not answer line 4 of
show_order. Which lines let Sam's request carry on to Priya's order?Show the answer
Lines 7 and 8, the catch block. The catch block writes to the log and does not end the request, so line 9 sends the order page.
Give one reason why a security check must fail closed.
Show the answer
A check that fails open treats an error as a pass. An attacker who can cause an error then gets past the check.
Practise it
Challenges with a weakness this lesson teaches.
- Gift searchGiftwrap, BeginnerOpen
- Year in reviewBrightledger, IntermediateOpen
- Delete a postChirp, IntermediateOpen