1
1

00:00:00,330  -->  00:00:02,700
<v Instructor>Improper Error Handling.</v>
2

2

00:00:02,700  -->  00:00:04,530
In this lesson, we're going to talk about
3

3

00:00:04,530  -->  00:00:06,450
improper error handling.
4

4

00:00:06,450  -->  00:00:08,610
Now, if you've used a computer for any length of time,
5

5

00:00:08,610  -->  00:00:10,320
you've probably gotten an error.
6

6

00:00:10,320  -->  00:00:13,320
Now, that error can be really detailed or not so detailed.
7

7

00:00:13,320  -->  00:00:15,390
It really depends on how it was coded,
8

8

00:00:15,390  -->  00:00:17,340
and what we're going to talk about is how a programmer
9

9

00:00:17,340  -->  00:00:19,560
should really create a well-written application
10

10

00:00:19,560  -->  00:00:20,970
and to be able to handle errors
11

11

00:00:20,970  -->  00:00:22,920
and exceptions more gracefully.
12

12

00:00:22,920  -->  00:00:24,840
I'm not going to bother to define an error here
13

13

00:00:24,840  -->  00:00:26,520
because we all know what errors are,
14

14

00:00:26,520  -->  00:00:27,990
but what causes an error?
15

15

00:00:27,990  -->  00:00:30,960
Well, errors could be caused by invalid user input,
16

16

00:00:30,960  -->  00:00:32,400
a loss of network connectivity
17

17

00:00:32,400  -->  00:00:34,530
or another service or process failing.
18

18

00:00:34,530  -->  00:00:36,330
Essentially, something went wrong,
19

19

00:00:36,330  -->  00:00:38,700
and so we're going to generate some kind of a message.
20

20

00:00:38,700  -->  00:00:40,650
Now, when we talk about an error handler,
21

21

00:00:40,650  -->  00:00:42,270
this is some kind of coding method
22

22

00:00:42,270  -->  00:00:43,680
that's being used to anticipate
23

23

00:00:43,680  -->  00:00:45,510
and deal with exceptions that are thrown
24

24

00:00:45,510  -->  00:00:47,790
during the execution of a process.
25

25

00:00:47,790  -->  00:00:50,190
Now, error handling is critically important for us
26

26

00:00:50,190  -->  00:00:52,770
because error handling can prevent the application
27

27

00:00:52,770  -->  00:00:54,780
from failing in a way that allows the attacker
28

28

00:00:54,780  -->  00:00:58,500
to execute code or perform some sort of injection attack.
29

29

00:00:58,500  -->  00:01:00,750
And so if we don't do proper error handling,
30

30

00:01:00,750  -->  00:01:02,490
for instance, checking for a buffer
31

31

00:01:02,490  -->  00:01:03,690
to make sure it has enough room
32

32

00:01:03,690  -->  00:01:05,160
before putting something in it,
33

33

00:01:05,160  -->  00:01:07,140
you can have something like a buffer overflow.
34

34

00:01:07,140  -->  00:01:09,210
That would allow some kind of an injection attack
35

35

00:01:09,210  -->  00:01:10,860
or a code execution to occur.
36

36

00:01:10,860  -->  00:01:13,470
And so we want to make sure we're doing proper error handling.
37

37

00:01:13,470  -->  00:01:14,970
Let me give you a really silly example here
38

38

00:01:14,970  -->  00:01:16,440
using some pseudo code.
39

39

00:01:16,440  -->  00:01:17,940
Here is a program I wrote.
40

40

00:01:17,940  -->  00:01:19,110
Now, this isn't a real program
41

41

00:01:19,110  -->  00:01:20,280
or any real programming language.
42

42

00:01:20,280  -->  00:01:22,260
It's just something that looks kind of like code.
43

43

00:01:22,260  -->  00:01:24,030
So we call it pseudo code.
44

44

00:01:24,030  -->  00:01:26,790
Now, here I am initializing four variables.
45

45

00:01:26,790  -->  00:01:30,180
I'm initializing X, Y, Z, and total.
46

46

00:01:30,180  -->  00:01:34,170
Then I'm going to take in as input X, Y, and Z from my user.
47

47

00:01:34,170  -->  00:01:35,247
Then I have this program,
48

48

00:01:35,247  -->  00:01:37,200
and the first thing I'm going to do is I'm going to test,
49

49

00:01:37,200  -->  00:01:39,210
is Z equal to zero?
50

50

00:01:39,210  -->  00:01:40,920
Now, if Z is equal to zero,
51

51

00:01:40,920  -->  00:01:43,297
then I'm going to print to the screen an error message,
52

52

00:01:43,297  -->  00:01:45,090
"Z cannot be zero."
53

53

00:01:45,090  -->  00:01:47,550
Otherwise, I'm going to do some calculations.
54

54

00:01:47,550  -->  00:01:49,260
I'm going to set the value of total
55

55

00:01:49,260  -->  00:01:51,270
to X plus Y divided by Z,
56

56

00:01:51,270  -->  00:01:54,510
essentially add together X and Y divide by Z
57

57

00:01:54,510  -->  00:01:56,490
to give me some sort of a number.
58

58

00:01:56,490  -->  00:01:59,490
Now, once I have that, I'm going to print, "The average is,"
59

59

00:01:59,490  -->  00:02:02,250
and then the variable total that I just calculated.
60

60

00:02:02,250  -->  00:02:03,990
This is a really simple program, right?
61

61

00:02:03,990  -->  00:02:06,510
But it does show you the concept of error handling.
62

62

00:02:06,510  -->  00:02:08,220
Now, let's go through this and see what happens
63

63

00:02:08,220  -->  00:02:10,800
if I give some values to X, Y, and Z.
64

64

00:02:10,800  -->  00:02:13,440
First, let's say that I have X equals five,
65

65

00:02:13,440  -->  00:02:15,690
Y equals 10 and Z equals five.
66

66

00:02:15,690  -->  00:02:17,010
What's going to happen?
67

67

00:02:17,010  -->  00:02:18,690
Well, we're going to go through.
68

68

00:02:18,690  -->  00:02:21,600
We're going to see that Z is five. It's not equal to zero.
69

69

00:02:21,600  -->  00:02:24,060
So we're going to skip the error, and do the L statement,
70

70

00:02:24,060  -->  00:02:26,250
which in this case means we're going to set the total
71

71

00:02:26,250  -->  00:02:28,800
equal to X plus Y divided by Z,
72

72

00:02:28,800  -->  00:02:31,800
or five plus 10 divided by five,
73

73

00:02:31,800  -->  00:02:33,750
which in this case will be 15 divided by five,
74

74

00:02:33,750  -->  00:02:35,070
which gives me three.
75

75

00:02:35,070  -->  00:02:36,450
And then I'm going to print that to the screen
76

76

00:02:36,450  -->  00:02:38,340
and say the average is three.
77

77

00:02:38,340  -->  00:02:39,840
That works perfectly fine.
78

78

00:02:39,840  -->  00:02:42,120
All right, so let's take a look at another example.
79

79

00:02:42,120  -->  00:02:43,950
This time I'm going to use the same program,
80

80

00:02:43,950  -->  00:02:45,990
but I'm going to give X, Y and Z the values
81

81

00:02:45,990  -->  00:02:47,700
of five, 10, and zero.
82

82

00:02:47,700  -->  00:02:49,440
Now, when I go to run that program,
83

83

00:02:49,440  -->  00:02:50,730
the first thing I'm going to test is,
84

84

00:02:50,730  -->  00:02:52,590
is Z equal to zero?
85

85

00:02:52,590  -->  00:02:55,297
Well, it is, so I'm going to print the error to the screen,
86

86

00:02:55,297  -->  00:02:56,970
"Z cannot be zero."
87

87

00:02:56,970  -->  00:02:58,500
Now, why do I do this?
88

88

00:02:58,500  -->  00:03:01,380
Well, if I didn't have that if-then statement
89

89

00:03:01,380  -->  00:03:04,740
and I just said, "Let's just use total and print,"
90

90

00:03:04,740  -->  00:03:05,880
what would happen?
91

91

00:03:05,880  -->  00:03:06,947
I would try to divide by zero.
92

92

00:03:06,947  -->  00:03:08,400
And if you know anything about math,
93

93

00:03:08,400  -->  00:03:10,170
you know that you cannot divide by zero.
94

94

00:03:10,170  -->  00:03:11,190
It'll give you an error.
95

95

00:03:11,190  -->  00:03:13,350
And so if I tried to just divide by zero,
96

96

00:03:13,350  -->  00:03:14,910
the program would crash.
97

97

00:03:14,910  -->  00:03:17,040
I can divide by any other number in the whole world,
98

98

00:03:17,040  -->  00:03:18,780
but I cannot divide by zero.
99

99

00:03:18,780  -->  00:03:21,270
And so therefore I put in a little bit of error handling
100

100

00:03:21,270  -->  00:03:23,340
to prevent the system from crashing.
101

101

00:03:23,340  -->  00:03:24,840
Now, let me give you another warning here
102

102

00:03:24,840  -->  00:03:25,950
when it comes to error messages.
103

103

00:03:25,950  -->  00:03:28,500
Notice in this, I actually used my own error message.
104

104

00:03:28,500  -->  00:03:30,360
I said, "Z cannot be zero."
105

105

00:03:30,360  -->  00:03:32,340
That was the error message I wanted to present.
106

106

00:03:32,340  -->  00:03:33,840
But the warning I want to give you is this,
107

107

00:03:33,840  -->  00:03:35,910
sometimes default error messages
108

108

00:03:35,910  -->  00:03:37,710
could leak sensitive information.
109

109

00:03:37,710  -->  00:03:39,630
Now, what I mean by this is sometimes your programs
110

110

00:03:39,630  -->  00:03:41,850
will just give out the error messages by default
111

111

00:03:41,850  -->  00:03:43,680
and not something you told them to give.
112

112

00:03:43,680  -->  00:03:44,700
So if you're using something
113

113

00:03:44,700  -->  00:03:46,620
that's going to be calling a database function,
114

114

00:03:46,620  -->  00:03:48,390
and you have an error on the SQL statement,
115

115

00:03:48,390  -->  00:03:50,820
it might actually print out the full SQL statement
116

116

00:03:50,820  -->  00:03:53,130
including the file pass and stack traces
117

117

00:03:53,130  -->  00:03:54,810
and other information that could really be used
118

118

00:03:54,810  -->  00:03:56,970
by an attacker to hack your system.
119

119

00:03:56,970  -->  00:03:59,220
So when you're creating error messages,
120

120

00:03:59,220  -->  00:04:00,450
and you're doing error handling,
121

121

00:04:00,450  -->  00:04:02,130
you want to make sure that you're creative
122

122

00:04:02,130  -->  00:04:04,800
and think about using custom error handlers
123

123

00:04:04,800  -->  00:04:06,570
to be able to prevent accidental leakage,
124

124

00:04:06,570  -->  00:04:08,100
just like I did in my little program.
125

125

00:04:08,100  -->  00:04:09,780
Because if I didn't have that error message
126

126

00:04:09,780  -->  00:04:11,430
and I tried to divide by zero,
127

127

00:04:11,430  -->  00:04:13,230
it could have actually done a brain dump
128

128

00:04:13,230  -->  00:04:15,030
of the memory onto the screen,
129

129

00:04:15,030  -->  00:04:17,070
and that could have had sensitive information in it.
130

130

00:04:17,070  -->  00:04:20,010
So by me catching that error before running the application,
131

131

00:04:20,010  -->  00:04:21,330
I'm handling that error
132

132

00:04:21,330  -->  00:04:23,910
and giving you a message that makes more sense.
133

133

00:04:23,910  -->  00:04:25,800
Now, one more area that I like to think about this
134

134

00:04:25,800  -->  00:04:26,940
when we talk about errors,
135

135

00:04:26,940  -->  00:04:29,070
let's talk about your login system.
136

136

00:04:29,070  -->  00:04:30,750
Oftentimes people will create their website,
137

137

00:04:30,750  -->  00:04:32,910
and they'll have a login with username and password.
138

138

00:04:32,910  -->  00:04:35,287
Well, what if you have a message that looks like this,
139

139

00:04:35,287  -->  00:04:37,350
"Error: incorrect password"?
140

140

00:04:37,350  -->  00:04:39,690
So I go to your website. I type in my username.
141

141

00:04:39,690  -->  00:04:40,770
I type in my password.
142

142

00:04:40,770  -->  00:04:42,877
I hit login, and I get a message that says,
143

143

00:04:42,877  -->  00:04:44,790
"Error: incorrect password."
144

144

00:04:44,790  -->  00:04:46,290
What does this tell me?
145

145

00:04:46,290  -->  00:04:47,340
Well, if I'm an attacker,
146

146

00:04:47,340  -->  00:04:49,170
this is actually valuable information
147

147

00:04:49,170  -->  00:04:52,080
because this tells me the username I submitted was correct,
148

148

00:04:52,080  -->  00:04:53,550
but the password was wrong.
149

149

00:04:53,550  -->  00:04:55,530
So now I can keep using that username,
150

150

00:04:55,530  -->  00:04:57,540
and then I can keep guessing passwords.
151

151

00:04:57,540  -->  00:04:59,790
So what would I want to use instead of this?
152

152

00:04:59,790  -->  00:05:03,690
Maybe I would use error: incorrect username/password,
153

153

00:05:03,690  -->  00:05:06,810
incorrect username or password, something like that
154

154

00:05:06,810  -->  00:05:08,670
because then they don't know which one is incorrect.
155

155

00:05:08,670  -->  00:05:10,200
And now that makes it harder for them
156

156

00:05:10,200  -->  00:05:13,000
to continue to game your system and try to hack into it.
