In this lesson, we're going to talk about
Improper Error Handling.
Now, if you've used a computer for any length of time,
you've probably gotten an error.
Now that error can be really detailed or not so detailed.
It really depends on how it was coded.
And what we're going to talk about is how a programmer
should really create a well-written application
and to be able to handle
errors and exceptions more gracefully.
I'm not going to bother to define an error here,
because we all know what errors are,
but what causes an error?
Well, errors could be caused by invalid user input,
a loss of network connectivity,
or another server/process failing.
Essentially, something went wrong,
and so we're going to generate some kind of a message.
Now, when we talk about an error handler,
this is some kind of coding method
that's being used to anticipate and deal with exceptions
that are thrown during the execution of a process.
Now, error handling is critically important for us,
because error handling can prevent the application
from failing in a way that allows the attacker
to execute code or perform some sort of injection attack.
And so if we don't do proper error handling, for instance,
checking for a buffer to make sure it has enough room
before putting something in it,
you can have something like a buffer overflow,
that would allow some kind of an injection attack
or a code execution to occur,
and so we want to make sure we're doing
proper error handling.
Let me give you a really silly example here,
using some pseudocode.
Here's a program I wrote.
Now, this isn't a real program
or any real programming language,
it's just something that looks kind of like code,
so we call it pseudocode.
Now here I'm initializing four variables.
I'm initializing x, y, z, and total.
Then I'm going to take in as input x, y, and z from my user.
Then I have this program,
and the first thing I'm going to do is I'm going to test
is z equal to 0?
Now if z is equal to 0,
then I'm going to print to the screen an error message,
Z cannot be zero,
otherwise, I'm going to do some calculations.
I'm going to set the value of
total to x plus y divided by z,
essentially add together x and y divide by z
to give me some sort of a number.
Now, once I have that, I'm going to print the averages
and then the variable total that I just calculated.
This is a really simple program, right?
But it does show you the concept of error handling.
Now, let's go through this and see what happens
if I give some values to x, y, and z.
First, let's say that I have x equals 5,
y equals 10 and z equals 5, what's going to happen?
Well, we're going to go through,
we're going to see that z is 5, it's not equal to 0,
so we're going to skip the error and do the L statement,
which in this case means we're going to set the total
equal to x plus y divided by z,
or 5 plus 10 divided by 5,
which in this case, would be 15 divide by 5,
which gives me 3.
And then I'm going to print that to the screen
and say the average is 3.
That works perfectly fine.
All right, so let's take a look at another example.
This time I'm going to use the same program,
but I'm going to give x, y, and z
the values of 5, 10, and 0.
Now, when I go to run that program,
the first thing I'm going to test is, is z equal to 0?
Well, it is, so I'm going to print the error to the screen,
Z cannot be zero.
Now, why do I do this?
Well, if I didn't have that, if then statement
and I just said, let's just use total and print,
what would happen?
I would try to divide by 0.
If you know anything about math,
you know that you cannot divide by 0,
it'll give you an error.
And so if I tried to just divide by 0,
the program would crash.
I can divide by any other number in the whole world,
but I cannot divide by 0.
And so therefore, I put in a little bit of error handling
to prevent the system from crashing.
Now, let me give you another warning here
when it comes to error messages.
Notice in this, I actually use my own error message.
I said, Z cannot be zero,
that was the error message I want to present.
But the warning I want to give you is this,
sometimes default error messages
could leak sensitive information.
Now, what I mean by this is sometimes your programs
will just give out the error messages by default
and not something you told them to give.
So if you're using something
that's going to be calling a database function
and you have an error on the SQL statement,
it might actually print out the full SQL statement,
including the file pass and stack traces
and other information that could really be used
by an attacker to hack your system.
So when you're creating error messages
and you're doing error handling,
you want to make sure that you're creative
and think about using custom error handlers
to be able to prevent accidental leakage,
just like I did in my little program,
because if I didn't have that error message
and I tried to divide by 0,
it could have actually done a brain dump
of the memory onto the screen
and that could have had sensitive information in it.
So by me catching that error
before running the application, I'm handling that error
and giving you a message that makes more sense.
Now, one more area that I like to think about this
when we talk about errors,
let's talk about your login system.
Oftentimes people will create their website
and they'll have a login with username and password.
Well, what if you have a message that looks like this?
ERROR Incorrect password.
So I go to your website, I type in my username,
I type in my password, I hit login,
and I get a message that says, ERROR Incorrect password.
What does this tell me?
Well, if I'm an attacker,
this is actually valuable information,
because this tells me the username I submitted was correct,
but the password was wrong.
So now I can keep using that username
and then I can keep guessing passwords.
So what would I want to use instead of this?
Maybe I would use Error Incorrect username/password,
Incorrect username or password, something like that,
because then they don't know which one is incorrect.
And now that makes it harder for them
to continue to get in your system and try to hack into it.