WEBVTT

1
00:00:00.000 --> 00:00:03.520
<v Speaker 1>All right, listeners, get ready to dive deep. We've got

2
00:00:03.520 --> 00:00:05.519
<v Speaker 1>some fascinating stuff done pack today.

3
00:00:05.599 --> 00:00:07.360
<v Speaker 2>Yeah, this is exciting you guys sent.

4
00:00:07.280 --> 00:00:11.400
<v Speaker 1>Us excerpts from the cert Oracle Secure Coding Standard for Java.

5
00:00:11.519 --> 00:00:12.480
<v Speaker 2>Oh that's a good one.

6
00:00:12.560 --> 00:00:15.359
<v Speaker 1>It's like the holy grail of secure Java development.

7
00:00:15.439 --> 00:00:15.560
<v Speaker 2>Yea.

8
00:00:16.120 --> 00:00:18.920
<v Speaker 1>Even James Gosling himself endorses this thing.

9
00:00:18.960 --> 00:00:21.640
<v Speaker 2>He does. Yeah, it's the real deal. We're talking enterprise

10
00:00:21.679 --> 00:00:24.440
<v Speaker 2>grade best practices, the stuff they use to build the

11
00:00:24.519 --> 00:00:27.280
<v Speaker 2>systems that run well practically everything exactly.

12
00:00:27.399 --> 00:00:30.879
<v Speaker 1>And the goal today is to pull out the absolute

13
00:00:31.640 --> 00:00:35.479
<v Speaker 1>key nuggets of wisdom from this standard. Wow, the stuff

14
00:00:35.520 --> 00:00:38.840
<v Speaker 1>that's going to help you guys write seriously secure Java code.

15
00:00:38.920 --> 00:00:41.079
<v Speaker 2>Absolutely, And just to add to that, it's not just

16
00:00:41.119 --> 00:00:43.600
<v Speaker 2>theory we're talking about here, right. The introduction of this

17
00:00:43.679 --> 00:00:47.079
<v Speaker 2>standard actually points out that a crazy sixty four percent

18
00:00:47.079 --> 00:00:49.159
<v Speaker 2>of vulnerability is back in two thousand and four. And

19
00:00:49.240 --> 00:00:51.439
<v Speaker 2>this is still relevant today, by the way, Yeah, sixty

20
00:00:51.520 --> 00:00:54.679
<v Speaker 2>four percent. They were just simple coding errors. Wow, So

21
00:00:54.759 --> 00:00:56.600
<v Speaker 2>this is stuff that has real world implications.

22
00:00:56.880 --> 00:00:59.920
<v Speaker 1>That is a so bring statistic. Okay, So before we

23
00:01:00.079 --> 00:01:02.439
<v Speaker 1>get into the really nitty gritty details, let's just sort

24
00:01:02.479 --> 00:01:05.280
<v Speaker 1>of scent the stage a little bit. What exactly is

25
00:01:05.319 --> 00:01:09.000
<v Speaker 1>a coding standard and why should developers even care about

26
00:01:09.000 --> 00:01:09.519
<v Speaker 1>this stuff?

27
00:01:09.680 --> 00:01:12.799
<v Speaker 2>That's a great question. Think of a coding standard as

28
00:01:13.040 --> 00:01:17.040
<v Speaker 2>a blueprint for secure development. It's a set of rules,

29
00:01:17.120 --> 00:01:20.200
<v Speaker 2>a set of guidelines that are really there to eliminate

30
00:01:20.280 --> 00:01:23.280
<v Speaker 2>those really common coding mistakes, right, the ones that we

31
00:01:23.319 --> 00:01:25.799
<v Speaker 2>all make, let's be honest, that can just open up

32
00:01:25.799 --> 00:01:28.439
<v Speaker 2>these huge security holes in our applications.

33
00:01:28.519 --> 00:01:30.959
<v Speaker 1>Right, So it's all about being proactive exactly.

34
00:01:31.000 --> 00:01:34.400
<v Speaker 2>It's all about prevention, you know, preventing these issues before

35
00:01:34.439 --> 00:01:36.400
<v Speaker 2>they even have a chance to become a problem.

36
00:01:36.560 --> 00:01:39.439
<v Speaker 1>And this standard is serious business. I mean, it even

37
00:01:39.480 --> 00:01:43.560
<v Speaker 1>has different levels of conformants. Like there's nonconforming, which I

38
00:01:43.560 --> 00:01:44.920
<v Speaker 1>guess is self explanatory, so.

39
00:01:44.959 --> 00:01:46.359
<v Speaker 2>You're not following the rules.

40
00:01:46.120 --> 00:01:49.920
<v Speaker 1>Conforming, you're crying. And then there's the like gold standard

41
00:01:50.200 --> 00:01:52.239
<v Speaker 1>provably conforming, right.

42
00:01:52.359 --> 00:01:55.239
<v Speaker 2>And those levels are there to help organizations figure out

43
00:01:55.280 --> 00:01:57.280
<v Speaker 2>where they stand security wise.

44
00:01:57.359 --> 00:01:57.519
<v Speaker 1>Right.

45
00:01:58.439 --> 00:02:01.439
<v Speaker 2>But there's this really cool thing called the deviation procedure.

46
00:02:02.640 --> 00:02:06.799
<v Speaker 2>So sometimes there are very specific reasons why you might

47
00:02:06.879 --> 00:02:08.159
<v Speaker 2>need to deviate from a rule.

48
00:02:08.240 --> 00:02:09.800
<v Speaker 1>Okay, so it's kind of like a break glass in

49
00:02:09.840 --> 00:02:11.199
<v Speaker 1>case of emergency kind of thing.

50
00:02:11.360 --> 00:02:15.240
<v Speaker 2>Exactly, but the standards really really clear about this. Security

51
00:02:15.280 --> 00:02:21.280
<v Speaker 2>cannot be sacrificed for performance ever, that's non negotiable.

52
00:02:20.960 --> 00:02:22.560
<v Speaker 1>No shortcuts when it comes to security.

53
00:02:22.639 --> 00:02:26.360
<v Speaker 2>Got it now, Java itself is often considered a pretty

54
00:02:26.360 --> 00:02:29.240
<v Speaker 2>secure language, right, I mean, what is it about Java

55
00:02:29.280 --> 00:02:31.439
<v Speaker 2>that makes it so special in the security department.

56
00:02:31.719 --> 00:02:34.479
<v Speaker 1>Well, Java was really designed with security in mind from

57
00:02:34.520 --> 00:02:37.360
<v Speaker 1>the very beginning. Okay, So there are features like automatic

58
00:02:37.400 --> 00:02:41.319
<v Speaker 1>bounds checking. You know, there's no explicit pointers in Java,

59
00:02:41.360 --> 00:02:43.439
<v Speaker 1>which is a good thing, right. And there's this thing

60
00:02:43.479 --> 00:02:47.360
<v Speaker 1>called the bytecode verifier that's constantly checking for violations, you know,

61
00:02:47.759 --> 00:02:49.719
<v Speaker 1>making sure that the code is playing by the rules.

62
00:02:49.840 --> 00:02:52.240
<v Speaker 2>So Java itself is kind of like a fortress already

63
00:02:52.280 --> 00:02:55.080
<v Speaker 2>pretty well defended. Yeah, you could say that that even

64
00:02:55.120 --> 00:02:59.240
<v Speaker 2>the strongest fortress can be compromised if the people inside

65
00:02:59.280 --> 00:03:02.960
<v Speaker 2>aren't careful, right, the human element, that's often the weakest link.

66
00:03:03.080 --> 00:03:04.039
<v Speaker 2>We can make mistakes.

67
00:03:04.080 --> 00:03:06.840
<v Speaker 1>So even with the best intentions, developers can still mess

68
00:03:06.879 --> 00:03:09.080
<v Speaker 1>things up. What's a common area where things tend to

69
00:03:09.080 --> 00:03:09.479
<v Speaker 1>go wrong?

70
00:03:09.719 --> 00:03:15.840
<v Speaker 2>Ah concurrency. Ah concurrency powerful tool for building responsive applications,

71
00:03:15.840 --> 00:03:19.759
<v Speaker 2>but it's also like opening a Pandora's box of security issues.

72
00:03:19.879 --> 00:03:24.000
<v Speaker 1>Yeah, concurrency the double edged sword. You've got multiple threads

73
00:03:24.039 --> 00:03:26.800
<v Speaker 1>all running around, potentially step it on each other's toes,

74
00:03:26.800 --> 00:03:29.080
<v Speaker 1>messing with data. It's a lot to keep.

75
00:03:28.960 --> 00:03:31.840
<v Speaker 2>Track of exactly. You have data races where you've got

76
00:03:32.000 --> 00:03:35.080
<v Speaker 2>multiple threads trying to access and modify the same data

77
00:03:35.120 --> 00:03:39.319
<v Speaker 2>at the same time, right, leading to unpredictable results total chaos.

78
00:03:39.759 --> 00:03:43.280
<v Speaker 2>Then you have visibility issues where changes made by one

79
00:03:43.360 --> 00:03:45.520
<v Speaker 2>thread aren't immediately visible to the others.

80
00:03:45.840 --> 00:03:46.360
<v Speaker 1>Oh right.

81
00:03:46.439 --> 00:03:48.680
<v Speaker 2>And then, just to make things even more interesting, there's

82
00:03:48.759 --> 00:03:53.039
<v Speaker 2>the unpredictable order in which threads might actually execute. It

83
00:03:53.080 --> 00:03:55.159
<v Speaker 2>makes it so hard to figure out what the program's

84
00:03:55.199 --> 00:03:55.560
<v Speaker 2>going to do.

85
00:03:56.000 --> 00:03:59.319
<v Speaker 1>It sounds like a recipe for disaster. So how does

86
00:03:59.360 --> 00:04:03.759
<v Speaker 1>the cert standard help us navigate this concurrency mindfield? How

87
00:04:03.759 --> 00:04:04.560
<v Speaker 1>do we stay safe?

88
00:04:05.280 --> 00:04:08.120
<v Speaker 2>Well? One way it does this is by really emphasizing

89
00:04:08.120 --> 00:04:10.319
<v Speaker 2>the concept of happens before relationships.

90
00:04:10.439 --> 00:04:11.960
<v Speaker 1>Happens before relationships.

91
00:04:12.039 --> 00:04:14.680
<v Speaker 2>Okay, Yeah, it's a way to bring order to the

92
00:04:14.759 --> 00:04:19.759
<v Speaker 2>chaos of concurrency. It's basically defining a strict order of operations. Okay,

93
00:04:19.839 --> 00:04:22.399
<v Speaker 2>even when you have all these threads running around. For example,

94
00:04:22.439 --> 00:04:26.639
<v Speaker 2>think about releasing an object's intrinsic lock that always happens

95
00:04:26.680 --> 00:04:29.399
<v Speaker 2>before the next time that lock is acquired. It's a rule,

96
00:04:29.839 --> 00:04:32.079
<v Speaker 2>and that helps prevent those data races.

97
00:04:32.319 --> 00:04:34.720
<v Speaker 1>So happens before is like a set of traffic signals

98
00:04:34.720 --> 00:04:38.399
<v Speaker 1>for threads, making sure they don't collide at intersections exactly.

99
00:04:38.519 --> 00:04:42.000
<v Speaker 2>But how do you actually implement these happens before relationships

100
00:04:42.000 --> 00:04:44.600
<v Speaker 2>in your code. That's where synchronization methods come.

101
00:04:44.519 --> 00:04:47.319
<v Speaker 1>In, right, synchronization keeping those threads in check. Tell me

102
00:04:47.360 --> 00:04:49.000
<v Speaker 1>more about these methods and how they work.

103
00:04:49.240 --> 00:04:51.959
<v Speaker 2>Well, you've got several options, each with us own trade offs.

104
00:04:52.160 --> 00:04:55.360
<v Speaker 2>There's volatile variables, which guarantee that changes are visible to

105
00:04:55.439 --> 00:04:59.519
<v Speaker 2>all threads instantly. Then there are synchronized blocks, which allow

106
00:04:59.600 --> 00:05:02.639
<v Speaker 2>only one thread at a time to access a protected

107
00:05:02.639 --> 00:05:06.000
<v Speaker 2>section of code. And then you have atomic classes, which

108
00:05:06.040 --> 00:05:09.360
<v Speaker 2>provide thread safe operations on individual variables.

109
00:05:09.600 --> 00:05:12.040
<v Speaker 1>So many choices. How do we know which one to

110
00:05:12.079 --> 00:05:13.279
<v Speaker 1>pick in a given situation.

111
00:05:13.639 --> 00:05:16.560
<v Speaker 2>Well, the standard actually gives some excellent guidance on that.

112
00:05:16.720 --> 00:05:18.439
<v Speaker 2>It even quotes Brian Goats.

113
00:05:18.680 --> 00:05:21.959
<v Speaker 1>Oh yeah, I've heard of him, a concurrency guru, right.

114
00:05:21.839 --> 00:05:24.839
<v Speaker 2>Exactly, and he says atomic classes are perfect for low

115
00:05:24.920 --> 00:05:28.000
<v Speaker 2>contention situations. You know, when you don't have a ton

116
00:05:28.040 --> 00:05:31.199
<v Speaker 2>of threads fighting over the same variable. But for those

117
00:05:31.240 --> 00:05:34.720
<v Speaker 2>high contention situations blocks, you know, like those provided by

118
00:05:34.720 --> 00:05:37.040
<v Speaker 2>synchronized blocks, those are a better choice.

119
00:05:37.079 --> 00:05:39.720
<v Speaker 1>So choosing the right synchronization method is like choosing the

120
00:05:39.759 --> 00:05:42.000
<v Speaker 1>right tool for the job. You wouldn't use a hammer

121
00:05:42.040 --> 00:05:43.839
<v Speaker 1>to screw in a light bulb exactly.

122
00:05:44.319 --> 00:05:46.839
<v Speaker 2>And speaking of choosing the right tool, another crucial part

123
00:05:46.879 --> 00:05:48.920
<v Speaker 2>of secure coding is input validation.

124
00:05:49.439 --> 00:05:54.600
<v Speaker 1>Uh, input validation. The golden rule never trust anything that

125
00:05:54.600 --> 00:05:55.680
<v Speaker 1>comes from the outside world.

126
00:05:55.759 --> 00:05:59.720
<v Speaker 2>You said it, and that means any external input user's environment,

127
00:05:59.839 --> 00:06:03.519
<v Speaker 2>veriables data from the network, anything. Treat it all as

128
00:06:03.560 --> 00:06:07.720
<v Speaker 2>potentially malicious, validate it, sanitize it before you even think

129
00:06:07.759 --> 00:06:08.399
<v Speaker 2>about using it.

130
00:06:08.480 --> 00:06:11.120
<v Speaker 1>Because if we skip that validation step, what kind of

131
00:06:11.120 --> 00:06:11.879
<v Speaker 1>trouble are we in?

132
00:06:12.040 --> 00:06:15.319
<v Speaker 2>Oh, the consequences can be severe. We're talking SQL injection,

133
00:06:15.639 --> 00:06:19.800
<v Speaker 2>where malicious input messes with your database queries, command injection

134
00:06:19.959 --> 00:06:23.160
<v Speaker 2>where attackers can run commands on your system. And then

135
00:06:23.399 --> 00:06:27.000
<v Speaker 2>there are XML external entity attacks which exploit vulnerabilities in

136
00:06:27.160 --> 00:06:27.879
<v Speaker 2>XML parsing.

137
00:06:28.199 --> 00:06:31.000
<v Speaker 1>So basically, not validating input is like leaving your front

138
00:06:31.000 --> 00:06:33.360
<v Speaker 1>door wide open with a big neon sign that says

139
00:06:33.519 --> 00:06:34.360
<v Speaker 1>welcome hackers.

140
00:06:34.720 --> 00:06:37.839
<v Speaker 2>Pretty much, but luckily The standard gives some very specific

141
00:06:37.920 --> 00:06:41.560
<v Speaker 2>advice on how to protect yourself. Normalize those strings before validation,

142
00:06:41.639 --> 00:06:45.079
<v Speaker 2>you know, make sure they're formatted consistently, use whitelists to

143
00:06:45.160 --> 00:06:48.720
<v Speaker 2>only allow save input patterns, and sanitize that user input

144
00:06:48.759 --> 00:06:50.920
<v Speaker 2>before you log it so you don't accidentally inject any

145
00:06:50.959 --> 00:06:51.720
<v Speaker 2>malicious code.

146
00:06:51.759 --> 00:06:55.480
<v Speaker 1>Okay, speaking of sneaky attacks, our source material also mentions

147
00:06:55.600 --> 00:06:59.920
<v Speaker 1>character encodings as a potential security risk. Why is that

148
00:07:00.480 --> 00:07:02.360
<v Speaker 1>they seem like such a low level detail.

149
00:07:02.600 --> 00:07:06.160
<v Speaker 2>You'd be surprised how often character encoding mismatches can cause

150
00:07:06.279 --> 00:07:10.000
<v Speaker 2>major data correction, especially when you're working with file imputed

151
00:07:10.040 --> 00:07:13.240
<v Speaker 2>output or network stuff. Right, and the standard dives into

152
00:07:13.279 --> 00:07:17.199
<v Speaker 2>a really tricky issue called trailing byte problem. This happens

153
00:07:17.240 --> 00:07:18.600
<v Speaker 2>in multi byting codings.

154
00:07:18.720 --> 00:07:21.279
<v Speaker 1>Trailing byte sounds ominous, it can be.

155
00:07:21.319 --> 00:07:23.639
<v Speaker 2>Imagine you've got a buffer boundary right where your data

156
00:07:23.639 --> 00:07:25.920
<v Speaker 2>gets split up, and a multi byte character it gets

157
00:07:25.959 --> 00:07:29.600
<v Speaker 2>split across that boundary. Because of how these encodings work,

158
00:07:29.639 --> 00:07:32.639
<v Speaker 2>those split bytes, they can get interpreted completely differently than

159
00:07:32.639 --> 00:07:33.360
<v Speaker 2>what you intended.

160
00:07:33.480 --> 00:07:35.480
<v Speaker 1>So it's like a word getting cut in half, and

161
00:07:35.600 --> 00:07:38.319
<v Speaker 1>suddenly the second half takes on a whole new meaning.

162
00:07:38.360 --> 00:07:41.720
<v Speaker 2>Exactly and It can happen even if the data looks

163
00:07:41.720 --> 00:07:44.600
<v Speaker 2>fine at first glance. So the standard's advice is to

164
00:07:44.759 --> 00:07:48.000
<v Speaker 2>always be super aware of your platform's default encoding.

165
00:07:48.279 --> 00:07:52.120
<v Speaker 1>Okay, noted, always double check those encodings. Okay, get ready,

166
00:07:52.160 --> 00:07:54.879
<v Speaker 1>We're about to go deep here. The next section in

167
00:07:54.920 --> 00:07:59.040
<v Speaker 1>this standard is called Sneaky Subtleties. Yeah, what hidden dangers

168
00:07:59.079 --> 00:08:00.000
<v Speaker 1>are we about to uncot?

169
00:08:00.439 --> 00:08:02.519
<v Speaker 2>Oh? This is where it gets interesting, even for those

170
00:08:02.560 --> 00:08:06.199
<v Speaker 2>seasoned Java developers up there. It's all about those little gotchas,

171
00:08:06.240 --> 00:08:09.240
<v Speaker 2>those tiny details that can trip you up if you're

172
00:08:09.240 --> 00:08:10.319
<v Speaker 2>not paying close attention.

173
00:08:10.600 --> 00:08:14.040
<v Speaker 1>All right, color me intrigued. Give me the lowdown on

174
00:08:14.120 --> 00:08:16.560
<v Speaker 1>these sneaky subtleties. What kind of things should we be

175
00:08:16.600 --> 00:08:17.279
<v Speaker 1>watching out for?

176
00:08:18.040 --> 00:08:22.079
<v Speaker 2>Well, one example is ignoring return values. The standard talks

177
00:08:22.079 --> 00:08:25.759
<v Speaker 2>about the string dot replace method. Okay, because strings and

178
00:08:25.839 --> 00:08:29.519
<v Speaker 2>Java are immutable, that replace method. It doesn't modify the

179
00:08:29.560 --> 00:08:32.600
<v Speaker 2>original string, right, It returns a brand new string with

180
00:08:32.679 --> 00:08:35.840
<v Speaker 2>the replacements. So if you ignore that return value, you're

181
00:08:35.879 --> 00:08:37.679
<v Speaker 2>basically throwing away the results.

182
00:08:37.720 --> 00:08:40.080
<v Speaker 1>Ah, So you have to assign that return value to

183
00:08:40.360 --> 00:08:41.480
<v Speaker 1>a variable.

184
00:08:41.080 --> 00:08:45.080
<v Speaker 2>Exactly, otherwise it's gone poof. Another sneaky trap is comparing

185
00:08:45.159 --> 00:08:49.320
<v Speaker 2>boxed primitives, you know, things like integer or boolean, where

186
00:08:49.440 --> 00:08:52.240
<v Speaker 2>primitive type is wrapped up in an object. Right when

187
00:08:52.240 --> 00:08:55.720
<v Speaker 2>you use the double equals operator to compare those things

188
00:08:55.759 --> 00:08:57.840
<v Speaker 2>get a little weird because of memmoization.

189
00:08:58.200 --> 00:09:01.759
<v Speaker 1>Memoization. That's like when my web browser remembers my passwords

190
00:09:01.759 --> 00:09:03.559
<v Speaker 1>so I don't have to type them in every single time.

191
00:09:03.720 --> 00:09:06.879
<v Speaker 2>Sort of. It's basically cashing commonly used values to make

192
00:09:06.919 --> 00:09:11.480
<v Speaker 2>things faster, but with boxed primitives it can cause unexpected results.

193
00:09:12.000 --> 00:09:14.879
<v Speaker 2>Say you have two integer objects, both with the value

194
00:09:14.919 --> 00:09:19.279
<v Speaker 2>one hundred. You'd think comparing them with moocitrals would return.

195
00:09:18.960 --> 00:09:20.799
<v Speaker 1>True, right because they have the same value.

196
00:09:20.879 --> 00:09:23.720
<v Speaker 2>Right, But because of memmalization, Java might be comparing the

197
00:09:23.759 --> 00:09:27.080
<v Speaker 2>object references instead of the values themselves, so you might

198
00:09:27.080 --> 00:09:28.200
<v Speaker 2>get a false result.

199
00:09:28.320 --> 00:09:30.159
<v Speaker 1>So it's like you think you're comparing the contents of

200
00:09:30.159 --> 00:09:33.159
<v Speaker 1>two boxes, but you're actually comparing the boxes themselves. That

201
00:09:33.279 --> 00:09:34.519
<v Speaker 1>is tricky, tricky.

202
00:09:34.639 --> 00:09:38.320
<v Speaker 2>Indeed, the standard is full of these insights. Another one

203
00:09:38.360 --> 00:09:40.720
<v Speaker 2>is hidden side effects within assertions.

204
00:09:40.879 --> 00:09:43.879
<v Speaker 1>AH assertions, those are the statements we use to check

205
00:09:43.919 --> 00:09:45.840
<v Speaker 1>conditions that should be true, and if they're not, it

206
00:09:45.840 --> 00:09:46.559
<v Speaker 1>throws an error.

207
00:09:46.799 --> 00:09:50.600
<v Speaker 2>Exactly, But here's the catch. Assertions in Java can be

208
00:09:50.639 --> 00:09:53.000
<v Speaker 2>turned on or off at runtime, So if you put

209
00:09:53.080 --> 00:09:56.200
<v Speaker 2>code with side effects inside an assertion, it might not

210
00:09:56.240 --> 00:09:58.120
<v Speaker 2>always get executed when you expect it to.

211
00:09:58.320 --> 00:10:00.559
<v Speaker 1>Oh so it's like setting a trap, but it might

212
00:10:00.600 --> 00:10:02.919
<v Speaker 1>not actually spring when an intruder steps on it.

213
00:10:03.000 --> 00:10:04.360
<v Speaker 2>That's a great way to put it, and that can

214
00:10:04.440 --> 00:10:08.279
<v Speaker 2>lead to some really unexpected and potentially dangerous behavior.

215
00:10:08.440 --> 00:10:12.679
<v Speaker 1>Okay, so far we've talked about concurrency chaos, the importance

216
00:10:12.679 --> 00:10:18.799
<v Speaker 1>of input validation, sneaky character encodings, ignoring return values, boxed

217
00:10:18.879 --> 00:10:23.879
<v Speaker 1>primitive comparison, weirdness, and hidden side effects and assertions. I'm

218
00:10:23.919 --> 00:10:26.840
<v Speaker 1>starting to see why this standard is considered like the

219
00:10:26.919 --> 00:10:30.159
<v Speaker 1>gold standard for secure Java coding. What other landmines are

220
00:10:30.200 --> 00:10:31.759
<v Speaker 1>we going to encounter in this deep dive?

221
00:10:32.320 --> 00:10:36.200
<v Speaker 2>Well, locking, for one, it's crucial for thread safety, but

222
00:10:36.320 --> 00:10:39.000
<v Speaker 2>even the most experienced developers can make mistakes that lead

223
00:10:39.000 --> 00:10:39.679
<v Speaker 2>to deadlocks.

224
00:10:39.840 --> 00:10:42.879
<v Speaker 1>Deadlocks those sound scary. What are they and how do

225
00:10:42.919 --> 00:10:43.360
<v Speaker 1>they happen?

226
00:10:43.679 --> 00:10:47.240
<v Speaker 2>Imagine a classic traffic jam. Everyone's stuck because they're all

227
00:10:47.279 --> 00:10:50.320
<v Speaker 2>waiting for each other to move. That's essentially a deadlock.

228
00:10:50.399 --> 00:10:53.320
<v Speaker 2>In the world of multi threaded programming, it happens when

229
00:10:53.320 --> 00:10:56.320
<v Speaker 2>you have two or more threads that are blocked indefinitely,

230
00:10:56.799 --> 00:10:59.399
<v Speaker 2>waiting for each other to release the resources they need.

231
00:10:59.559 --> 00:11:01.840
<v Speaker 1>So it's like a very polite dinner party gone horribly

232
00:11:01.879 --> 00:11:05.720
<v Speaker 1>wrong because everyone's too busy being polite to actually eat exactly.

233
00:11:06.480 --> 00:11:10.360
<v Speaker 2>The standard recommends a specific pattern called the private Final

234
00:11:10.480 --> 00:11:15.159
<v Speaker 2>lock Object IDIOM to prevent these kinds of deadlocks. Basically,

235
00:11:15.240 --> 00:11:19.360
<v Speaker 2>you create a dedicated lock object that's only accessible within

236
00:11:19.399 --> 00:11:22.039
<v Speaker 2>the class that needs to protect its data. It keeps

237
00:11:22.080 --> 00:11:23.519
<v Speaker 2>external code from interfering.

238
00:11:23.720 --> 00:11:26.159
<v Speaker 1>Ah, So it's like having a separate lock and key

239
00:11:26.200 --> 00:11:28.200
<v Speaker 1>for each room in your house instead of one master

240
00:11:28.279 --> 00:11:30.120
<v Speaker 1>key that anyone could copy exactly.

241
00:11:30.679 --> 00:11:34.159
<v Speaker 2>And to further avoid deadlocks, the standard says, hey, release

242
00:11:34.200 --> 00:11:37.039
<v Speaker 2>those locks in the same order you acquired them. It

243
00:11:37.080 --> 00:11:41.080
<v Speaker 2>even uses that classic philosopher's dining analogy to illustrate the point.

244
00:11:41.159 --> 00:11:42.799
<v Speaker 1>Philosopher's dining Okay, I'm listening.

245
00:11:43.240 --> 00:11:45.879
<v Speaker 2>Imagine a bunch of philosophers sitting around a table, and

246
00:11:45.919 --> 00:11:48.559
<v Speaker 2>they each need two forks to eat. If they all

247
00:11:48.600 --> 00:11:50.639
<v Speaker 2>grab their right fork first and then wait for the

248
00:11:50.720 --> 00:11:53.240
<v Speaker 2>left fork, nobody eats. They're all stuck.

249
00:11:53.480 --> 00:11:56.799
<v Speaker 1>So it's like a philosophical discussion that descends into chaos

250
00:11:57.120 --> 00:12:00.000
<v Speaker 1>because everyone is too busy holding onto their own idea

251
00:12:00.279 --> 00:12:02.720
<v Speaker 1>is to actually listen to anyone else precisely.

252
00:12:03.200 --> 00:12:07.039
<v Speaker 2>And this brings us to serialization, a powerful tool, no doubt.

253
00:12:07.279 --> 00:12:09.919
<v Speaker 2>It lets you convert objects into a stream of bites

254
00:12:10.240 --> 00:12:12.120
<v Speaker 2>so you can easily send them over a network or

255
00:12:12.159 --> 00:12:13.039
<v Speaker 2>store them in a file.

256
00:12:13.200 --> 00:12:17.399
<v Speaker 1>Right. Serialization making those objects travel ready. But I have

257
00:12:17.399 --> 00:12:19.320
<v Speaker 1>a feeling there's a butt coming there is.

258
00:12:19.960 --> 00:12:24.200
<v Speaker 2>Serialization is great, but it has its security risks. Attackers

259
00:12:24.240 --> 00:12:27.440
<v Speaker 2>can potentially mess with the state of an object during reconstitution.

260
00:12:27.799 --> 00:12:30.159
<v Speaker 2>You know when those bites are being converted back into

261
00:12:30.159 --> 00:12:30.720
<v Speaker 2>an object.

262
00:12:30.799 --> 00:12:33.080
<v Speaker 1>So it's like sending a package through the mail and

263
00:12:33.120 --> 00:12:37.000
<v Speaker 1>someone tampers with the contents en rout, replacing those delicious

264
00:12:37.000 --> 00:12:39.679
<v Speaker 1>cookies with something less hepatizing.

265
00:12:39.759 --> 00:12:42.320
<v Speaker 2>That's a great analogy to prevent that kind of tampering.

266
00:12:42.360 --> 00:12:44.960
<v Speaker 2>The Standard recommends what they call the sign then seal

267
00:12:45.080 --> 00:12:48.840
<v Speaker 2>approach for sensitive data. It combines digital signatures.

268
00:12:48.240 --> 00:12:51.039
<v Speaker 1>And encryption sign then seal tell me more.

269
00:12:51.279 --> 00:12:54.960
<v Speaker 2>First, you digitally sign the object, which is like adding

270
00:12:55.000 --> 00:12:58.159
<v Speaker 2>a tamperproof seal. Think of it like a wax seal

271
00:12:58.279 --> 00:13:00.720
<v Speaker 2>on an old fashioned letter. And then you encrypt the

272
00:13:00.840 --> 00:13:02.919
<v Speaker 2>whole thing to protect the data.

273
00:13:02.639 --> 00:13:04.840
<v Speaker 1>Double, the protection, double the peace of mind.

274
00:13:04.759 --> 00:13:08.159
<v Speaker 2>Exactly, and the standard goes even further. It says, always

275
00:13:08.279 --> 00:13:12.600
<v Speaker 2>validate an object state after de serialization, especially if you're

276
00:13:12.639 --> 00:13:15.559
<v Speaker 2>dealing with sensitive stuff like credit card numbers.

277
00:13:15.679 --> 00:13:20.480
<v Speaker 1>Smart move. Never take anything for granted, especially when dealing

278
00:13:20.519 --> 00:13:23.799
<v Speaker 1>with sensitive data. Okay, we've covered a ton of ground

279
00:13:23.840 --> 00:13:29.279
<v Speaker 1>here already, concurrency and synchronization, input validation, character encoding, those

280
00:13:29.279 --> 00:13:33.360
<v Speaker 1>sneaky subtleties with strings and primitives, locking in deadlocks, and

281
00:13:33.399 --> 00:13:37.080
<v Speaker 1>even the security considerations of serialization. I'm starting to feel

282
00:13:37.080 --> 00:13:38.559
<v Speaker 1>like a secure coding ninja.

283
00:13:38.600 --> 00:13:40.879
<v Speaker 2>That's great, But remember this is just the beginning.

284
00:13:40.960 --> 00:13:41.519
<v Speaker 1>Oh there's more.

285
00:13:41.600 --> 00:13:43.840
<v Speaker 2>There is a lot more to explore in the world

286
00:13:43.840 --> 00:13:45.120
<v Speaker 2>of secure Java coding.

287
00:13:45.200 --> 00:13:47.320
<v Speaker 1>I'm ready for more. Bring on the next round of

288
00:13:47.360 --> 00:13:48.840
<v Speaker 1>security wisdom, you got it.

289
00:13:49.440 --> 00:13:50.879
<v Speaker 2>In the next part of our deep dive, we're going

290
00:13:50.960 --> 00:13:54.440
<v Speaker 2>to tackle exception handling, how to deal with those unexpected

291
00:13:54.480 --> 00:13:56.799
<v Speaker 2>events in a way that doesn't compromise security.

292
00:13:57.080 --> 00:13:59.639
<v Speaker 1>Exception handling. Though it sounds like we're about to venture

293
00:13:59.639 --> 00:14:02.279
<v Speaker 1>into the dark underbelly of Java code. I can't wait.

294
00:14:02.440 --> 00:14:05.159
<v Speaker 2>It's going to be good. Okay, are you ready to

295
00:14:05.240 --> 00:14:07.799
<v Speaker 2>jump back into the world of secure Java coding.

296
00:14:08.279 --> 00:14:12.720
<v Speaker 1>Absolutely, I'm still buzzing from our last conversation. Where are

297
00:14:12.759 --> 00:14:15.440
<v Speaker 1>we headed next on this secure coding journey.

298
00:14:15.639 --> 00:14:17.759
<v Speaker 2>Well, there's a section in the search standard that I

299
00:14:17.759 --> 00:14:22.639
<v Speaker 2>think you'll find particularly interesting. It's all about exceptional behavior.

300
00:14:23.720 --> 00:14:27.799
<v Speaker 1>Exceptional behavior hmmm, sounds intriguing. Are we talking about, like

301
00:14:28.559 --> 00:14:31.279
<v Speaker 1>how to handle exceptions in a way that doesn't compromise

302
00:14:31.279 --> 00:14:32.480
<v Speaker 1>security exactly?

303
00:14:32.559 --> 00:14:35.919
<v Speaker 2>Handling exceptions is crucial, but it's often overlooked from a

304
00:14:35.919 --> 00:14:36.799
<v Speaker 2>security standpoint.

305
00:14:36.879 --> 00:14:39.799
<v Speaker 1>Okay, I'm all ears. What kind of security risks can

306
00:14:40.000 --> 00:14:41.919
<v Speaker 1>exceptions actually pose? Fill me in?

307
00:14:42.240 --> 00:14:45.720
<v Speaker 2>Well, think about it. Exceptions they often carry sensitive information,

308
00:14:46.120 --> 00:14:48.840
<v Speaker 2>oh right, Like a file not found exception that might

309
00:14:48.960 --> 00:14:52.879
<v Speaker 2>accidentally reveal the structure of your file system to an attacker, okay,

310
00:14:53.080 --> 00:14:56.000
<v Speaker 2>or SQL exception it could hint at the details of

311
00:14:56.039 --> 00:14:58.399
<v Speaker 2>your database schema, stuff you don't want getting out.

312
00:14:58.600 --> 00:15:01.120
<v Speaker 1>So it's like those detective shows where they find a

313
00:15:01.200 --> 00:15:05.120
<v Speaker 1>seemingly insignificant clue, like a stray fiber, and it leads

314
00:15:05.159 --> 00:15:07.679
<v Speaker 1>them to the whole crime scene. You're saying, those little

315
00:15:07.720 --> 00:15:11.080
<v Speaker 1>exception details can be like breadcrumbs for attackers.

316
00:15:11.159 --> 00:15:15.559
<v Speaker 2>Precisely, attackers can piece together these seemingly minor leaks to

317
00:15:15.600 --> 00:15:19.159
<v Speaker 2>get a much bigger picture of your system's weaknesses. It's

318
00:15:19.200 --> 00:15:23.120
<v Speaker 2>like a puzzle, and exceptions can give them those missing pieces.

319
00:15:23.320 --> 00:15:26.679
<v Speaker 1>Okay, so how do we prevent those leaks? Do we

320
00:15:26.759 --> 00:15:29.519
<v Speaker 1>just swallow all exceptions and pretend nothing went wrong.

321
00:15:29.639 --> 00:15:32.559
<v Speaker 2>That's not the best approach. The key is to handle

322
00:15:32.559 --> 00:15:37.200
<v Speaker 2>those exceptions gracefully without giving away more information than absolutely necessary.

323
00:15:37.919 --> 00:15:41.279
<v Speaker 2>One important rule to standard highlights is to avoid exposing

324
00:15:41.320 --> 00:15:45.000
<v Speaker 2>the original exception when you're rethrowing it. They actually give

325
00:15:45.000 --> 00:15:48.039
<v Speaker 2>a non compliant example where a file not found exception

326
00:15:48.120 --> 00:15:50.559
<v Speaker 2>gets wrapped in a more general io exception.

327
00:15:50.759 --> 00:15:54.360
<v Speaker 1>But isn't wrapping exceptions considered good practice. It makes the

328
00:15:54.399 --> 00:15:57.039
<v Speaker 1>code cleaner and it allows you to handle errors at

329
00:15:57.080 --> 00:15:57.759
<v Speaker 1>a higher level.

330
00:15:58.039 --> 00:16:00.000
<v Speaker 2>It is good practice, but you have to be very

331
00:16:00.159 --> 00:16:03.559
<v Speaker 2>careful about what information you're propagating up the chain. Even

332
00:16:03.639 --> 00:16:06.759
<v Speaker 2>if that wrapped exception isn't directly visible to the user,

333
00:16:07.159 --> 00:16:11.039
<v Speaker 2>it might get logged somewhere, and those logs, they could

334
00:16:11.159 --> 00:16:13.080
<v Speaker 2>be accessible to someone with bad intentions.

335
00:16:13.399 --> 00:16:15.960
<v Speaker 1>Ah So even those hidden exceptions can come back to

336
00:16:16.000 --> 00:16:18.639
<v Speaker 1>haunt you. It's like sweeping dirt under the rug. It

337
00:16:18.720 --> 00:16:21.480
<v Speaker 1>might look clean on the surface, but it's still there.

338
00:16:21.559 --> 00:16:23.080
<v Speaker 1>Lurking beneath exactly.

339
00:16:23.440 --> 00:16:26.320
<v Speaker 2>And speaking of logging, the standard actually has a whole

340
00:16:26.440 --> 00:16:29.799
<v Speaker 2>rule dedicated to making sure logging itself doesn't break because

341
00:16:29.840 --> 00:16:30.519
<v Speaker 2>of exceptions.

342
00:16:30.799 --> 00:16:33.759
<v Speaker 1>Wait, how can logging break? It seems like such a

343
00:16:33.840 --> 00:16:36.919
<v Speaker 1>simple operation, just writing some text to a file or something.

344
00:16:37.039 --> 00:16:39.039
<v Speaker 2>You'd think so, But imagine you're trying to log a

345
00:16:39.080 --> 00:16:43.320
<v Speaker 2>critical security exception to the standard error stream. Now, what

346
00:16:43.440 --> 00:16:46.080
<v Speaker 2>happens if that stream is closed or filled up? Oh,

347
00:16:46.120 --> 00:16:49.159
<v Speaker 2>you lose that crucial information. Poof, it's gone.

348
00:16:49.320 --> 00:16:51.799
<v Speaker 1>Yikes. So it's like having a security alarm that goes

349
00:16:51.840 --> 00:16:53.600
<v Speaker 1>silent at the worst possible moment.

350
00:16:53.799 --> 00:16:56.679
<v Speaker 2>Not very helpful, Not helpful at all. That's why the

351
00:16:56.799 --> 00:17:01.600
<v Speaker 2>standard strongly recommends using a robust logging framework like Java,

352
00:17:01.679 --> 00:17:06.519
<v Speaker 2>dot util, dot Logging, Logger, or log forge. They're designed

353
00:17:06.519 --> 00:17:09.279
<v Speaker 2>to handle these errors gracefully, making sure your logs are

354
00:17:09.279 --> 00:17:10.759
<v Speaker 2>always available when you need them.

355
00:17:10.960 --> 00:17:14.079
<v Speaker 1>Right, So, even for something as seemingly basic as logging,

356
00:17:14.440 --> 00:17:18.359
<v Speaker 1>use the right tools for the job. Solid advice. Now

357
00:17:18.440 --> 00:17:21.000
<v Speaker 1>there was another rule that caught my eye. Do not

358
00:17:21.200 --> 00:17:25.680
<v Speaker 1>throw undeclared checked exceptions. Why is that such a big no? No?

359
00:17:25.920 --> 00:17:28.960
<v Speaker 2>Okay? So, in Java, checked exceptions are those that you're

360
00:17:29.000 --> 00:17:32.079
<v Speaker 2>supposed to declare in a method signature. You use that

361
00:17:32.119 --> 00:17:34.920
<v Speaker 2>throw as keyword, right, And that's so you're giving callers

362
00:17:34.960 --> 00:17:37.160
<v Speaker 2>a heads up about what exceptions they might need to handle.

363
00:17:37.279 --> 00:17:39.559
<v Speaker 1>It's like a warning label on a package. Handle with

364
00:17:39.640 --> 00:17:40.599
<v Speaker 1>care may throw.

365
00:17:40.400 --> 00:17:44.000
<v Speaker 2>Exceptions exactly, but there are sneaky ways to bypass this

366
00:17:44.039 --> 00:17:47.000
<v Speaker 2>whole mechanism, like using reflection or the class dot new

367
00:17:47.000 --> 00:17:49.920
<v Speaker 2>instance method. They can throw undeclared exceptions at runtime.

368
00:17:50.519 --> 00:17:53.200
<v Speaker 1>Ah. So it's like sneaking a dangerous item into a

369
00:17:53.240 --> 00:17:56.279
<v Speaker 1>package without putting that warning label on it. Someone's going

370
00:17:56.319 --> 00:17:57.519
<v Speaker 1>to get a nasty surprise.

371
00:17:57.799 --> 00:17:59.680
<v Speaker 2>That's a great way to put it, and it can

372
00:17:59.759 --> 00:18:02.599
<v Speaker 2>cause all sorts of chaos because the caller isn't prepared

373
00:18:02.640 --> 00:18:06.480
<v Speaker 2>to handle those unexpected exceptions. The standard is super clear

374
00:18:06.559 --> 00:18:10.160
<v Speaker 2>on this, don't break the contract of checked exceptions. It's

375
00:18:10.200 --> 00:18:11.839
<v Speaker 2>like a fundamental rule of the game.

376
00:18:11.920 --> 00:18:16.160
<v Speaker 1>All right, no surprise exceptions, got it? What other surprises

377
00:18:16.160 --> 00:18:18.640
<v Speaker 1>await us in the world of exceptional behavior?

378
00:18:18.799 --> 00:18:21.680
<v Speaker 2>Well, another important rule is to always handle exceptions and

379
00:18:21.759 --> 00:18:23.160
<v Speaker 2>finally blocks right.

380
00:18:23.279 --> 00:18:26.559
<v Speaker 1>Finally blocks, those are the code blocks that always execute

381
00:18:26.559 --> 00:18:29.680
<v Speaker 1>no matter what, even if an exception pops up. They're

382
00:18:29.759 --> 00:18:33.000
<v Speaker 1>essential for cleanup tasks like closing those resources.

383
00:18:32.519 --> 00:18:36.119
<v Speaker 2>We talked about exactly. But here's the catch. The standard

384
00:18:36.160 --> 00:18:38.759
<v Speaker 2>warns against putting any code in a finally block that

385
00:18:38.839 --> 00:18:40.480
<v Speaker 2>could itself throw an exception.

386
00:18:40.640 --> 00:18:43.039
<v Speaker 1>Wait, why is that a problem? Wouldn't the finally block

387
00:18:43.119 --> 00:18:45.480
<v Speaker 1>still execute even if it throws its own exception?

388
00:18:45.799 --> 00:18:48.759
<v Speaker 2>It would, But here's the thing. If a finally block

389
00:18:48.839 --> 00:18:51.960
<v Speaker 2>throws an exception, it masks any exceptions that were thrown

390
00:18:51.960 --> 00:18:53.279
<v Speaker 2>in the triblock I'm masking.

391
00:18:54.079 --> 00:18:57.119
<v Speaker 1>So it's like one loud sound drowning out another quieter,

392
00:18:57.240 --> 00:18:58.440
<v Speaker 1>but more important sound.

393
00:18:58.759 --> 00:19:03.000
<v Speaker 2>Precisely, you lose valuable information about the original exception that

394
00:19:03.039 --> 00:19:06.079
<v Speaker 2>caused the problem in the first place. The standard even

395
00:19:06.119 --> 00:19:10.200
<v Speaker 2>gives an example. Imagine a filestream that fails to close properly,

396
00:19:10.680 --> 00:19:14.440
<v Speaker 2>and that resulting io exception masks a more serious security

397
00:19:14.440 --> 00:19:15.640
<v Speaker 2>exception that happened earlier.

398
00:19:15.759 --> 00:19:18.799
<v Speaker 1>So it's like trying to diagnose a patient's illness, but

399
00:19:18.960 --> 00:19:22.440
<v Speaker 1>a minor symptom is hiding a much more serious underlying condition.

400
00:19:22.640 --> 00:19:26.680
<v Speaker 2>Exactly. The takeaway here, Handle those exceptions and finally blocks

401
00:19:26.680 --> 00:19:29.960
<v Speaker 2>for sure, but make absolutely certain that those blocks can't

402
00:19:29.960 --> 00:19:32.839
<v Speaker 2>throw any new exceptions. You don't want to mask the

403
00:19:32.880 --> 00:19:33.720
<v Speaker 2>real problem.

404
00:19:33.799 --> 00:19:37.720
<v Speaker 1>Got it handle with care and don't mask those critical exceptions.

405
00:19:38.160 --> 00:19:41.039
<v Speaker 1>This exception handling stuff is more nuanced than I realized.

406
00:19:41.480 --> 00:19:43.039
<v Speaker 1>What's next on the agenda.

407
00:19:42.960 --> 00:19:46.000
<v Speaker 2>Let's shift gears and talk about thread APIs. These are

408
00:19:46.039 --> 00:19:48.920
<v Speaker 2>the building blocks for creating and managing threads, and as

409
00:19:48.920 --> 00:19:53.039
<v Speaker 2>we talked about earlier, concurrency, when not handled carefully, can

410
00:19:53.079 --> 00:19:55.200
<v Speaker 2>be a breeding ground for security issues.

411
00:19:55.319 --> 00:19:57.480
<v Speaker 1>All right, bring it on, I'm ready to wrangle some

412
00:19:57.519 --> 00:19:59.519
<v Speaker 1>threads and make sure they don't go rogue. What kind

413
00:19:59.559 --> 00:20:02.200
<v Speaker 1>of trouble can these thread APIs get us into? Well?

414
00:20:02.240 --> 00:20:05.359
<v Speaker 2>One very common mistake is using the thread group class

415
00:20:05.359 --> 00:20:09.799
<v Speaker 2>for managing threads. The standard actually recommends against using.

416
00:20:09.599 --> 00:20:12.640
<v Speaker 1>It, really, but thread groups sounds like it was designed

417
00:20:12.680 --> 00:20:16.559
<v Speaker 1>specifically for managing threads. What's the issue with it?

418
00:20:16.559 --> 00:20:19.200
<v Speaker 2>It turns out thread group has some limitations that can

419
00:20:19.279 --> 00:20:23.000
<v Speaker 2>lead to unexpected behavior. For example, that active count method,

420
00:20:23.200 --> 00:20:24.960
<v Speaker 2>the one that's supposed to tell you how many threads

421
00:20:24.960 --> 00:20:27.400
<v Speaker 2>are running in a group right, Well, it can actually

422
00:20:27.400 --> 00:20:31.000
<v Speaker 2>be inaccurate because of timing issues and other subtleties.

423
00:20:31.119 --> 00:20:33.400
<v Speaker 1>So it's like relying on a faulty speedometer that might

424
00:20:33.440 --> 00:20:36.279
<v Speaker 1>give you the wrong speed. Not ideal when you're trying

425
00:20:36.279 --> 00:20:39.400
<v Speaker 1>to manage a fleet of threads, you need accurate.

426
00:20:38.960 --> 00:20:42.400
<v Speaker 2>Information exactly, and just to add to that, thread group

427
00:20:42.480 --> 00:20:44.720
<v Speaker 2>methods tend to operate on all the threads in a group,

428
00:20:44.799 --> 00:20:48.240
<v Speaker 2>which is often too broad. The standard recommends using more

429
00:20:48.319 --> 00:20:52.279
<v Speaker 2>modern concurrency utilities like the executor framework, which gives you

430
00:20:52.359 --> 00:20:54.720
<v Speaker 2>much finer control and more robust features.

431
00:20:54.799 --> 00:20:57.839
<v Speaker 1>Okay, so ditch the old school thread group and embrace

432
00:20:58.000 --> 00:21:02.000
<v Speaker 1>the modern executor framework. Got it? What other thread API

433
00:21:02.079 --> 00:21:03.400
<v Speaker 1>pitfalls should we watch out for?

434
00:21:03.759 --> 00:21:07.279
<v Speaker 2>Another one is misusing the notify and notifile methods.

435
00:21:07.559 --> 00:21:10.519
<v Speaker 1>Oh right, those are the methods you use to wake

436
00:21:10.559 --> 00:21:13.119
<v Speaker 1>up threads that are waiting on a condition like ringing

437
00:21:13.119 --> 00:21:15.640
<v Speaker 1>a doorbell to let them know it's their turn exactly.

438
00:21:15.920 --> 00:21:20.200
<v Speaker 2>But the standard cautions against using notify. It only wakes

439
00:21:20.279 --> 00:21:23.640
<v Speaker 2>up one arbitrary thread, and you have no guarantee which

440
00:21:23.680 --> 00:21:25.119
<v Speaker 2>thread it's going to be. It might not even be

441
00:21:25.160 --> 00:21:26.720
<v Speaker 2>the one you want to wake up, so.

442
00:21:26.640 --> 00:21:29.000
<v Speaker 1>It's like sending a vague text message to a group

443
00:21:29.119 --> 00:21:31.000
<v Speaker 1>chat and hoping the right person sees it.

444
00:21:31.000 --> 00:21:35.599
<v Speaker 2>And responds exactly. A much better approach is to use notifile,

445
00:21:35.920 --> 00:21:38.799
<v Speaker 2>which wakes up all the waiting threads and then they

446
00:21:38.839 --> 00:21:40.960
<v Speaker 2>can figure out amongst themselves who should go next.

447
00:21:41.160 --> 00:21:44.640
<v Speaker 1>So notifile is like making a clear announcement to the

448
00:21:44.799 --> 00:21:48.559
<v Speaker 1>entire team, making sure everyone's on the same page. Makes sense.

449
00:21:48.920 --> 00:21:51.079
<v Speaker 1>Is there anything else we need to know about managing

450
00:21:51.160 --> 00:21:52.119
<v Speaker 1>those waiting threads?

451
00:21:52.599 --> 00:21:55.839
<v Speaker 2>The standard highlights the importance of making sure waiting threads

452
00:21:55.839 --> 00:21:58.920
<v Speaker 2>can be terminated properly. You don't want any zombie threads

453
00:21:58.920 --> 00:22:01.000
<v Speaker 2>hanging around after they've their work.

454
00:22:00.880 --> 00:22:04.680
<v Speaker 1>Right, No zombie threads. How do we ensure a clean shutdown?

455
00:22:04.920 --> 00:22:07.640
<v Speaker 2>It's all about avoiding those operations that can leave a

456
00:22:07.680 --> 00:22:11.680
<v Speaker 2>thread hanging indefinitely, waiting for a network connection, for example,

457
00:22:11.799 --> 00:22:14.720
<v Speaker 2>or reading from a stream. If a thread gets stuck

458
00:22:14.720 --> 00:22:17.559
<v Speaker 2>in one of those blocking operations, it might never respond

459
00:22:17.640 --> 00:22:18.960
<v Speaker 2>to a termination request.

460
00:22:19.079 --> 00:22:21.559
<v Speaker 1>It's like being put on hold forever, never getting to

461
00:22:21.559 --> 00:22:23.960
<v Speaker 1>talk to a human. Incredibly frustrating.

462
00:22:24.039 --> 00:22:27.319
<v Speaker 2>Exactly. That's why the standard strongly recommends using timeouts or

463
00:22:27.359 --> 00:22:32.079
<v Speaker 2>other mechanisms to interrupt those blocking operations, give those threads

464
00:22:32.119 --> 00:22:33.880
<v Speaker 2>a chance to exit gracefully.

465
00:22:34.000 --> 00:22:36.640
<v Speaker 1>So it's all about giving those threads an escape route

466
00:22:36.799 --> 00:22:39.039
<v Speaker 1>just in case they get trapped in a blocking operation.

467
00:22:39.200 --> 00:22:43.000
<v Speaker 1>Good advice. Any other wisdom about thread APIs that we

468
00:22:43.039 --> 00:22:43.440
<v Speaker 1>should know.

469
00:22:43.759 --> 00:22:47.240
<v Speaker 2>One thing. The standard is very explicit about never use

470
00:22:47.359 --> 00:22:50.319
<v Speaker 2>the thread dot stop method to terminate a thread.

471
00:22:50.480 --> 00:22:53.519
<v Speaker 1>Oh really, but stop seems like the most straightforward way

472
00:22:53.559 --> 00:22:56.359
<v Speaker 1>to shut a thread down. What's wrong with it?

473
00:22:56.359 --> 00:22:59.319
<v Speaker 2>It turns out stop is like using a sledgehammer to

474
00:22:59.400 --> 00:23:02.160
<v Speaker 2>crack a nut. It's a very blunt instrument, and it

475
00:23:02.160 --> 00:23:06.640
<v Speaker 2>can have all sorts of unpredictable and potentially dangerous side effects. Okay,

476
00:23:06.839 --> 00:23:10.160
<v Speaker 2>when you call stop on a thread, it immediately throws

477
00:23:10.200 --> 00:23:13.599
<v Speaker 2>a thread death exception, which can interrupt those critical operations

478
00:23:13.599 --> 00:23:17.039
<v Speaker 2>and leave your application in an inconsistent state. It's not

479
00:23:17.160 --> 00:23:18.480
<v Speaker 2>a clean shutdown, so.

480
00:23:18.440 --> 00:23:20.519
<v Speaker 1>It's like yanking the power cord out of a server

481
00:23:20.559 --> 00:23:22.720
<v Speaker 1>in the middle of a critical operation. Not a good

482
00:23:22.720 --> 00:23:23.759
<v Speaker 1>idea exactly.

483
00:23:24.160 --> 00:23:28.000
<v Speaker 2>The standard recommends more graceful shut down techniques, like setting

484
00:23:28.039 --> 00:23:31.279
<v Speaker 2>a flag that the thread checks periodically. Then it can

485
00:23:31.319 --> 00:23:33.319
<v Speaker 2>exit cleanly when it sees that signal.

486
00:23:33.440 --> 00:23:36.240
<v Speaker 1>So a gentle nudge rather than a forceful shove.

487
00:23:36.480 --> 00:23:39.640
<v Speaker 2>Much better, much better, and that covers some critical ground

488
00:23:39.680 --> 00:23:43.480
<v Speaker 2>when it comes to thread APIs always remember concurrency is

489
00:23:43.519 --> 00:23:47.519
<v Speaker 2>a powerful tool, but handle it with care. Be aware

490
00:23:47.559 --> 00:23:50.440
<v Speaker 2>of those potential pitfalls, and you'll be much better off.

491
00:23:50.640 --> 00:23:53.400
<v Speaker 1>My head is spinning with all this knowledge. It's amazing

492
00:23:53.400 --> 00:23:55.680
<v Speaker 1>how many subtle things can go wrong when you're dealing

493
00:23:55.720 --> 00:23:59.680
<v Speaker 1>with multiple threads running around. This standard is definitely proving

494
00:23:59.680 --> 00:24:02.359
<v Speaker 1>its earth as a guide through this treacherous terrain.

495
00:24:02.680 --> 00:24:06.119
<v Speaker 2>Absolutely, and we're not finished yet. There's more to explore

496
00:24:06.200 --> 00:24:09.960
<v Speaker 2>in this world of secure Java coding. In the next part,

497
00:24:10.000 --> 00:24:12.920
<v Speaker 2>we'll dive into thread pools and how to manage those

498
00:24:12.960 --> 00:24:16.720
<v Speaker 2>groups of worker threads securely and efficiently. It's going to

499
00:24:16.759 --> 00:24:19.079
<v Speaker 2>be a fascinating discussion, so make sure you tune in

500
00:24:19.480 --> 00:24:21.000
<v Speaker 2>all right, ready to dive back in?

501
00:24:21.240 --> 00:24:24.160
<v Speaker 1>You bet, I'm eager to hear what other secure coding

502
00:24:24.200 --> 00:24:26.799
<v Speaker 1>wisdom this certain standard has in store for us.

503
00:24:27.079 --> 00:24:29.480
<v Speaker 2>Well. Next up, we're going to tackle thread pools. They're

504
00:24:29.519 --> 00:24:34.319
<v Speaker 2>a really fundamental tool for managing concurrency efficiently, especially in

505
00:24:34.359 --> 00:24:35.440
<v Speaker 2>server applications.

506
00:24:35.599 --> 00:24:37.680
<v Speaker 1>Right, thread pools, it's like having a whole crew of

507
00:24:37.720 --> 00:24:41.039
<v Speaker 1>worker threads just waiting around, ready to take on tasks

508
00:24:41.079 --> 00:24:41.680
<v Speaker 1>as they come in.

509
00:24:41.839 --> 00:24:46.160
<v Speaker 2>Exactly, instead of constantly creating and destroying threads, you've got

510
00:24:46.160 --> 00:24:49.440
<v Speaker 2>this pool of eager beavers ready to jump into action.

511
00:24:49.519 --> 00:24:50.720
<v Speaker 2>It's much more efficient.

512
00:24:51.000 --> 00:24:53.480
<v Speaker 1>I like that analogy. So what are the key things

513
00:24:53.480 --> 00:24:55.160
<v Speaker 1>we need to keep in mind when we're working with

514
00:24:55.240 --> 00:24:57.519
<v Speaker 1>threadpools to make sure we're doing things securely.

515
00:24:57.920 --> 00:25:00.480
<v Speaker 2>One of the biggest mistakes people make is using an

516
00:25:00.599 --> 00:25:01.799
<v Speaker 2>unbounded threadpool.

517
00:25:01.960 --> 00:25:05.359
<v Speaker 1>Unbounded yeah, meaning there's no limit to how many threads.

518
00:25:05.119 --> 00:25:08.039
<v Speaker 2>Can be in the pool exactly. It might seem convenient,

519
00:25:08.119 --> 00:25:10.079
<v Speaker 2>you know, like a workforce that never says no to

520
00:25:10.119 --> 00:25:14.039
<v Speaker 2>more work, but in reality that can lead to resource exhaustion.

521
00:25:14.240 --> 00:25:16.720
<v Speaker 2>Your application could just crash under heavy load.

522
00:25:16.880 --> 00:25:19.279
<v Speaker 1>So it's like throwing a party and inviting everyone, you know,

523
00:25:19.359 --> 00:25:23.079
<v Speaker 1>assuming you have unlimited food in space, things could get

524
00:25:23.079 --> 00:25:24.160
<v Speaker 1>out a hand pretty quickly.

525
00:25:24.240 --> 00:25:27.000
<v Speaker 2>That's a great way to put it. The standard strongly

526
00:25:27.039 --> 00:25:30.680
<v Speaker 2>recommends using a bounded threadpool instead. You set a limit

527
00:25:30.720 --> 00:25:32.720
<v Speaker 2>on the number of threads that can be active at

528
00:25:32.759 --> 00:25:36.279
<v Speaker 2>any given time. It prevents your application from getting overwhelmed

529
00:25:36.319 --> 00:25:36.880
<v Speaker 2>and crashing.

530
00:25:37.000 --> 00:25:39.640
<v Speaker 1>So it's like setting a reasonable guest list for your party,

531
00:25:39.880 --> 00:25:41.680
<v Speaker 1>making sure you don't run out of chips and dip.

532
00:25:42.240 --> 00:25:47.079
<v Speaker 1>Bounded threadpool, got it? What other threadpool traps should we

533
00:25:47.119 --> 00:25:47.680
<v Speaker 1>be aware of?

534
00:25:48.079 --> 00:25:51.000
<v Speaker 2>Another common pitfall is creating a threadpool with a custom

535
00:25:51.119 --> 00:25:53.920
<v Speaker 2>thread factory that doesn't set a proper thread group.

536
00:25:54.000 --> 00:25:55.839
<v Speaker 1>Okay, remind me what a thread group is. Again.

537
00:25:56.039 --> 00:25:59.440
<v Speaker 2>It's basically a way to group threads together for management purposes.

538
00:25:59.599 --> 00:26:02.359
<v Speaker 2>But if you're not careful, your threads might end up

539
00:26:02.400 --> 00:26:05.839
<v Speaker 2>getting created in unexpected places, making them hard to manage

540
00:26:05.839 --> 00:26:06.440
<v Speaker 2>and monitor.

541
00:26:06.680 --> 00:26:08.759
<v Speaker 1>So it's like having a bunch of construction workers show

542
00:26:08.799 --> 00:26:10.960
<v Speaker 1>up at your site but you don't know what team

543
00:26:11.000 --> 00:26:13.680
<v Speaker 1>they belong to or who their forman is. It's a

544
00:26:13.720 --> 00:26:16.240
<v Speaker 1>recipe for chaos and potential accidents.

545
00:26:16.319 --> 00:26:19.359
<v Speaker 2>Exactly the standard says you should either use the default

546
00:26:19.400 --> 00:26:23.519
<v Speaker 2>thread factory or create a custom one that explicitly assigns

547
00:26:23.559 --> 00:26:25.759
<v Speaker 2>a thread group. That way, you know where all your

548
00:26:25.799 --> 00:26:28.279
<v Speaker 2>threads belong and you can keep them under control.

549
00:26:28.519 --> 00:26:32.400
<v Speaker 1>Okay, thread organization check any other threadpool wisdom you want

550
00:26:32.440 --> 00:26:33.559
<v Speaker 1>to impart before we move on.

551
00:26:34.039 --> 00:26:38.319
<v Speaker 2>Yes, there's one more, really nasty problem called thread starvation deadlock.

552
00:26:38.680 --> 00:26:41.559
<v Speaker 2>It happens when the tasks in your threadpool actually depend

553
00:26:41.599 --> 00:26:42.839
<v Speaker 2>on each other to complete.

554
00:26:43.000 --> 00:26:45.279
<v Speaker 1>Wait, how can threads get stuck waiting for each other

555
00:26:45.279 --> 00:26:47.359
<v Speaker 1>if they're supposed to be working independently in a pool.

556
00:26:47.400 --> 00:26:48.200
<v Speaker 1>That doesn't sound right.

557
00:26:48.319 --> 00:26:51.920
<v Speaker 2>It's a tricky situation. Imagine Task A needs a result

558
00:26:51.920 --> 00:26:54.680
<v Speaker 2>from task B. To finish, but task B is stuck

559
00:26:54.759 --> 00:26:57.400
<v Speaker 2>waiting for task A to do something first. They're both

560
00:26:57.480 --> 00:27:00.799
<v Speaker 2>blocked waiting for each other, and the word just grinds

561
00:27:00.839 --> 00:27:01.400
<v Speaker 2>to a halt.

562
00:27:01.640 --> 00:27:04.680
<v Speaker 1>It's like a bureaucratic nightmare where you need approval from

563
00:27:04.720 --> 00:27:07.839
<v Speaker 1>Department A to get something from Department B, but Department

564
00:27:07.880 --> 00:27:11.599
<v Speaker 1>B needs a sign off from Department A first. Nothing

565
00:27:12.079 --> 00:27:12.759
<v Speaker 1>gets done.

566
00:27:12.880 --> 00:27:17.279
<v Speaker 2>A perfect analogy. The standard emphasizes designing your tasks to

567
00:27:17.319 --> 00:27:21.359
<v Speaker 2>be independent whenever possible, but if you absolutely must have

568
00:27:21.440 --> 00:27:25.559
<v Speaker 2>those dependencies, consider using techniques like fork join. It breaks

569
00:27:25.599 --> 00:27:28.960
<v Speaker 2>down those dependent tasks into smaller, more manageable chunks that

570
00:27:29.000 --> 00:27:30.440
<v Speaker 2>can be executed in parallel.

571
00:27:30.640 --> 00:27:33.599
<v Speaker 1>Fork join sounds a bit fancy. What's the basic idea

572
00:27:33.599 --> 00:27:34.079
<v Speaker 1>behind that?

573
00:27:34.440 --> 00:27:37.759
<v Speaker 2>It's basically a divide and conquer strategy. You split a

574
00:27:37.759 --> 00:27:41.960
<v Speaker 2>big task into smaller subtasks, you tackle those subtasks concurrently,

575
00:27:42.319 --> 00:27:44.640
<v Speaker 2>and then you combine the results at the end. Much

576
00:27:44.640 --> 00:27:45.240
<v Speaker 2>more efficient.

577
00:27:45.359 --> 00:27:47.680
<v Speaker 1>Ah. So it's like having a team of chefs working

578
00:27:47.680 --> 00:27:49.640
<v Speaker 1>on different parts of a meal at the same time

579
00:27:49.839 --> 00:27:51.839
<v Speaker 1>so you can get everything on the table faster. Yeah,

580
00:27:51.920 --> 00:27:54.319
<v Speaker 1>much more efficient than having one chef try to do

581
00:27:54.519 --> 00:27:56.200
<v Speaker 1>everything sequentially precisely.

582
00:27:56.519 --> 00:27:58.640
<v Speaker 2>Now, let's talk about thread local variables, right.

583
00:27:58.680 --> 00:28:01.079
<v Speaker 1>Thread local those are the very that are specific to

584
00:28:01.119 --> 00:28:03.640
<v Speaker 1>each thread. Right. It's like giving each worker their own

585
00:28:03.680 --> 00:28:05.839
<v Speaker 1>personal toolbox that nobody else can touch.

586
00:28:06.119 --> 00:28:08.920
<v Speaker 2>That's the idea. They can be really useful, but the

587
00:28:08.960 --> 00:28:12.920
<v Speaker 2>standard warns against reusing thread local variables in thread pools.

588
00:28:13.160 --> 00:28:17.000
<v Speaker 1>Really, if the threadpool is reusing threads, wouldn't the thread

589
00:28:17.000 --> 00:28:20.359
<v Speaker 1>local variables just get reused along with them? It seems logical.

590
00:28:20.960 --> 00:28:24.039
<v Speaker 2>The tricky part is when a thread finishes its task

591
00:28:24.200 --> 00:28:27.200
<v Speaker 2>and goes back to the pool, those thread local variables

592
00:28:27.240 --> 00:28:30.920
<v Speaker 2>aren't automatically cleared, so if that thread gets picked up

593
00:28:30.960 --> 00:28:33.960
<v Speaker 2>for a different task, the old values from the previous

594
00:28:34.039 --> 00:28:37.599
<v Speaker 2>task might still be hanging around, potentially causing weird and

595
00:28:37.720 --> 00:28:39.079
<v Speaker 2>unexpected behavior. Oh.

596
00:28:39.119 --> 00:28:40.799
<v Speaker 1>I see, so it's like getting a rental car and

597
00:28:40.839 --> 00:28:43.240
<v Speaker 1>finding someone else's stuff in the glove comparted. Yeah, not

598
00:28:43.279 --> 00:28:44.319
<v Speaker 1>a very pleasant surprise.

599
00:28:44.559 --> 00:28:49.039
<v Speaker 2>Exactly. The standard recommends clearing those thread local variables explicitly

600
00:28:49.119 --> 00:28:51.680
<v Speaker 2>before a thread goes back to the pool, or you

601
00:28:51.680 --> 00:28:54.480
<v Speaker 2>could just pass data as arguments to tasks, which is

602
00:28:54.559 --> 00:28:55.759
<v Speaker 2>often a cleaner solution.

603
00:28:56.119 --> 00:28:59.160
<v Speaker 1>Okay, thread local cleanup, got it. I'm really starting to

604
00:28:59.200 --> 00:29:01.759
<v Speaker 1>appreciate how many subtle details that are to consider when

605
00:29:01.799 --> 00:29:05.920
<v Speaker 1>it comes to multi threaded programming. This standard is incredibly

606
00:29:05.960 --> 00:29:08.000
<v Speaker 1>helpful for navigating this tricky terrain.

607
00:29:08.359 --> 00:29:11.599
<v Speaker 2>I agree, it's a real life saver. Now let's zoom

608
00:29:11.599 --> 00:29:14.920
<v Speaker 2>out a bit and consider some general thread safety principles

609
00:29:14.960 --> 00:29:18.160
<v Speaker 2>that apply beyond just specific APIs.

610
00:29:17.920 --> 00:29:20.640
<v Speaker 1>Thread safety making sure our code works correctly no matter

611
00:29:20.680 --> 00:29:22.680
<v Speaker 1>how many threads are running around doing their thing.

612
00:29:22.839 --> 00:29:22.920
<v Speaker 2>Ye.

613
00:29:23.319 --> 00:29:25.400
<v Speaker 1>What are some of those fundamental principles.

614
00:29:25.480 --> 00:29:28.640
<v Speaker 2>One of the most important rules is to avoid publishing

615
00:29:29.160 --> 00:29:31.920
<v Speaker 2>this reference during object construction.

616
00:29:32.279 --> 00:29:35.720
<v Speaker 1>Publishing that this reference that sounds a bit cryptic. What

617
00:29:35.759 --> 00:29:37.480
<v Speaker 1>does that actually mean? In practice?

618
00:29:37.680 --> 00:29:41.480
<v Speaker 2>It basically means exposing a reference to an object that's

619
00:29:41.519 --> 00:29:45.680
<v Speaker 2>still under construction before it's fully initialized. Okay, like assigning

620
00:29:45.720 --> 00:29:48.559
<v Speaker 2>this to a static variable or passing it as an

621
00:29:48.680 --> 00:29:52.200
<v Speaker 2>argument to another method during the constructor.

622
00:29:51.880 --> 00:29:53.920
<v Speaker 1>So it's like showing off a half built house before

623
00:29:53.960 --> 00:29:56.160
<v Speaker 1>the roof is even on. Yeah, people might get the

624
00:29:56.200 --> 00:29:57.519
<v Speaker 1>wrong impression exactly.

625
00:29:57.839 --> 00:30:01.079
<v Speaker 2>The problem is that another thread could see that partially

626
00:30:01.119 --> 00:30:03.720
<v Speaker 2>constructed object and try to use it before it's ready,

627
00:30:03.920 --> 00:30:06.200
<v Speaker 2>and that can lead to some really bad consequences.

628
00:30:06.319 --> 00:30:08.279
<v Speaker 1>Ouch. Yeah, it's like trying to move into a house

629
00:30:08.279 --> 00:30:10.839
<v Speaker 1>that's still a construction zone. Bad idea.

630
00:30:11.000 --> 00:30:14.599
<v Speaker 2>The standard's message is clear, finish building that object before

631
00:30:14.599 --> 00:30:17.480
<v Speaker 2>you give out any references to it. Don't let those

632
00:30:17.480 --> 00:30:20.440
<v Speaker 2>other threads peek behind the curtain before the show is ready.

633
00:30:20.559 --> 00:30:24.720
<v Speaker 1>Object construction safety check. What other thread safety gems does

634
00:30:24.720 --> 00:30:25.839
<v Speaker 1>this standard have for us?

635
00:30:26.240 --> 00:30:29.640
<v Speaker 2>Another crucial one is protecting shared variables. If you've got

636
00:30:29.720 --> 00:30:34.200
<v Speaker 2>multiple threads accessing the same variable without proper synchronization, things

637
00:30:34.240 --> 00:30:35.559
<v Speaker 2>can get messy fast.

638
00:30:35.720 --> 00:30:38.480
<v Speaker 1>It's like having multiple cooks all trying to use the

639
00:30:38.480 --> 00:30:41.720
<v Speaker 1>same mixing bowl at the same time. Without any coordination,

640
00:30:42.279 --> 00:30:43.759
<v Speaker 1>ingredients would be flying everywhere.

641
00:30:43.839 --> 00:30:47.920
<v Speaker 2>It's a great analogy. The standard says, use synchronization mechanisms

642
00:30:48.000 --> 00:30:51.319
<v Speaker 2>like locks or atomic variables to protect that shared data

643
00:30:51.359 --> 00:30:54.559
<v Speaker 2>from the chaos of multiple threads. Make sure everyone takes

644
00:30:54.599 --> 00:30:55.640
<v Speaker 2>turns and plays nicely.

645
00:30:55.839 --> 00:30:58.839
<v Speaker 1>Shared data protection check anything else we need to be

646
00:30:58.920 --> 00:30:59.279
<v Speaker 1>aware of.

647
00:30:59.279 --> 00:31:03.160
<v Speaker 2>When it comes to safety, the standard specifically addresses a

648
00:31:03.160 --> 00:31:05.200
<v Speaker 2>technique called double checked locking.

649
00:31:05.519 --> 00:31:07.559
<v Speaker 1>Double checked locking, I'm not familiar with that.

650
00:31:07.880 --> 00:31:10.519
<v Speaker 2>It's a way people try to optimize locking by checking

651
00:31:10.519 --> 00:31:13.799
<v Speaker 2>a condition twice, once without a lock, and then again

652
00:31:13.839 --> 00:31:15.759
<v Speaker 2>with a lock. If the first check passes. It's an

653
00:31:15.799 --> 00:31:20.319
<v Speaker 2>attempt to avoid the overhead of acquiring a lock unnecessarily.

654
00:31:19.839 --> 00:31:21.880
<v Speaker 1>So it's like checking if a door is locked twice

655
00:31:21.960 --> 00:31:24.720
<v Speaker 1>before you try to open. It seems a little excessive.

656
00:31:24.839 --> 00:31:28.039
<v Speaker 2>The intention is good, but the standard warns that double

657
00:31:28.119 --> 00:31:30.680
<v Speaker 2>checked locking is super tricky to get right. In Java,

658
00:31:31.480 --> 00:31:34.720
<v Speaker 2>there are these subtle memory visibility issues that can really

659
00:31:34.759 --> 00:31:36.680
<v Speaker 2>mess things up, can give you a false sense of

660
00:31:36.720 --> 00:31:39.880
<v Speaker 2>security and lead to those really hard to find bugs.

661
00:31:40.160 --> 00:31:45.160
<v Speaker 1>So double checked locking proceed with extreme caution. I'm starting

662
00:31:45.200 --> 00:31:47.759
<v Speaker 1>to notice a pattern here. It seems like multi threaded

663
00:31:47.799 --> 00:31:51.759
<v Speaker 1>programming is full of these seemingly clever optimizations that can

664
00:31:51.799 --> 00:31:55.000
<v Speaker 1>backfire spectacularly if you're not incredibly careful.

665
00:31:55.279 --> 00:31:57.759
<v Speaker 2>You're absolutely right, and that's exactly why this standard is

666
00:31:57.799 --> 00:32:01.240
<v Speaker 2>so valuable. It's a distillation of hard one experience guiding

667
00:32:01.319 --> 00:32:04.240
<v Speaker 2>us away from those tempting but often treacherous paths.

668
00:32:04.519 --> 00:32:08.400
<v Speaker 1>I completely agree. Now let's talk about fileio. It might

669
00:32:08.400 --> 00:32:11.480
<v Speaker 1>seem like a pretty straightforward area of programming, but I

670
00:32:11.519 --> 00:32:13.839
<v Speaker 1>suspect there are some security nuances we need to be

671
00:32:13.839 --> 00:32:14.240
<v Speaker 1>aware of.

672
00:32:14.559 --> 00:32:18.000
<v Speaker 2>You are absolutely right. Fileio can be surprisingly tricky when

673
00:32:18.000 --> 00:32:21.359
<v Speaker 2>it comes to security. One of the most basic rules

674
00:32:21.440 --> 00:32:25.200
<v Speaker 2>is to always work within what we call a secure directory.

675
00:32:24.799 --> 00:32:27.359
<v Speaker 1>A secure directory, so it's not enough to just check

676
00:32:27.400 --> 00:32:30.319
<v Speaker 1>file permissions. We need to worry about the directory itself.

677
00:32:30.480 --> 00:32:34.880
<v Speaker 2>Exactly. A secure directory is one where only authorized users

678
00:32:34.960 --> 00:32:39.240
<v Speaker 2>and of course the system administrator have control. This prevents

679
00:32:39.279 --> 00:32:42.440
<v Speaker 2>attackers from messing with files or creating new files where

680
00:32:42.440 --> 00:32:43.599
<v Speaker 2>they shouldn't, so it's.

681
00:32:43.480 --> 00:32:46.200
<v Speaker 1>Like having a vault where only certain people have the combination.

682
00:32:46.559 --> 00:32:50.680
<v Speaker 2>Precisely, the standard really stresses understanding this concept of secure

683
00:32:50.720 --> 00:32:54.440
<v Speaker 2>directories and making sure your application is playing within those boundaries.

684
00:32:54.599 --> 00:32:59.200
<v Speaker 1>Secure directories. Check what other file io pitfalls should we

685
00:32:59.240 --> 00:32:59.839
<v Speaker 1>watch out for.

686
00:33:00.519 --> 00:33:04.359
<v Speaker 2>A really common mistake is forgetting to clean up temporary files.

687
00:33:04.680 --> 00:33:07.119
<v Speaker 2>It's easy to create a temporary file for some operation

688
00:33:07.240 --> 00:33:09.440
<v Speaker 2>and then totally forget to delete it when you're done.

689
00:33:09.599 --> 00:33:13.759
<v Speaker 2>But over time those leftover temporary files they can clutter

690
00:33:13.880 --> 00:33:16.680
<v Speaker 2>up your system and potentially leak sensitive information.

691
00:33:17.000 --> 00:33:19.000
<v Speaker 1>It's like leaving your trash out on the curb and

692
00:33:19.039 --> 00:33:22.160
<v Speaker 1>never bothering to take it to the dump. Eventually, it's

693
00:33:22.160 --> 00:33:23.920
<v Speaker 1>going to attract some unwonted attention.

694
00:33:24.160 --> 00:33:28.160
<v Speaker 2>Exactly. The standard suggests a few different mechanisms. There's a

695
00:33:28.160 --> 00:33:31.920
<v Speaker 2>method called file dot delete on exit. And you can

696
00:33:31.960 --> 00:33:34.799
<v Speaker 2>also use those try with resources blocks to make sure

697
00:33:34.799 --> 00:33:38.160
<v Speaker 2>your temporary files get cleaned up even if something goes.

698
00:33:38.000 --> 00:33:41.279
<v Speaker 1>Wrong temporary file cleanup check. Yeah, what else do we

699
00:33:41.279 --> 00:33:43.400
<v Speaker 1>need to be mindful of when we're dealing with files?

700
00:33:43.519 --> 00:33:46.519
<v Speaker 2>Another area that can trip people up is file links.

701
00:33:46.839 --> 00:33:50.039
<v Speaker 2>You know those aliases or shortcuts that point to other files.

702
00:33:50.119 --> 00:33:50.319
<v Speaker 1>Right.

703
00:33:50.640 --> 00:33:54.319
<v Speaker 2>Attackers can actually exploit those links to trick your application

704
00:33:54.400 --> 00:33:56.160
<v Speaker 2>into accessing files that it shouldn't.

705
00:33:56.200 --> 00:33:58.079
<v Speaker 1>Oh, I see, So it's like clicking on a suspicious

706
00:33:58.119 --> 00:34:00.000
<v Speaker 1>link in an email that takes you to a militia

707
00:34:00.240 --> 00:34:02.559
<v Speaker 1>website disguised as something legitimate.

708
00:34:02.680 --> 00:34:06.200
<v Speaker 2>That's a great analogy. The standard recommends using methods like

709
00:34:06.319 --> 00:34:10.239
<v Speaker 2>file dot get canonical path, which will resolve those links

710
00:34:10.320 --> 00:34:12.559
<v Speaker 2>and make sure you're actually dealing with the file you

711
00:34:12.599 --> 00:34:15.119
<v Speaker 2>intended to and not some malicious substitute.

712
00:34:15.199 --> 00:34:18.800
<v Speaker 1>Following verification check any other file io wisdom you want

713
00:34:18.840 --> 00:34:19.119
<v Speaker 1>to share.

714
00:34:19.280 --> 00:34:22.440
<v Speaker 2>One more. When you're reading data from a file stream,

715
00:34:22.599 --> 00:34:24.960
<v Speaker 2>make sure you read the entire thing. Don't leave any

716
00:34:25.039 --> 00:34:25.639
<v Speaker 2>data behind.

717
00:34:25.800 --> 00:34:26.800
<v Speaker 1>Why is that important?

718
00:34:27.000 --> 00:34:30.440
<v Speaker 2>Well, if there's leftover data, an attacker won't be able

719
00:34:30.440 --> 00:34:33.639
<v Speaker 2>to get to it. So the standard says keep reading

720
00:34:33.719 --> 00:34:36.159
<v Speaker 2>until you hit the end of the file or an

721
00:34:36.199 --> 00:34:37.039
<v Speaker 2>exception occurs.

722
00:34:37.519 --> 00:34:40.119
<v Speaker 1>So it's like finishing your plate at a restaurant, even

723
00:34:40.119 --> 00:34:42.400
<v Speaker 1>if you're full. Make sure no one can sneak a itte.

724
00:34:42.320 --> 00:34:44.599
<v Speaker 2>Exactly, filestream, cleanup, check.

725
00:34:44.559 --> 00:34:47.960
<v Speaker 1>This file io. Stuff is trickier than I thought. Right, Okay,

726
00:34:48.000 --> 00:34:49.079
<v Speaker 1>what's next on the agenda.

727
00:34:49.320 --> 00:34:52.760
<v Speaker 2>Let's talk about platform security. This is all about understanding

728
00:34:52.760 --> 00:34:56.719
<v Speaker 2>the security features and limitations of the Java platform itself.

729
00:34:56.840 --> 00:34:59.599
<v Speaker 1>Platform security sounds like we're getting down to the nuts

730
00:34:59.639 --> 00:35:01.400
<v Speaker 1>and bolts the Java virtual machine. Now.

731
00:35:01.559 --> 00:35:05.280
<v Speaker 2>You could say that the standard highlights features like security

732
00:35:05.280 --> 00:35:08.760
<v Speaker 2>managers and security policies which you can use to control

733
00:35:08.800 --> 00:35:12.079
<v Speaker 2>what code can actually do. You can prevent those malicious

734
00:35:12.119 --> 00:35:13.239
<v Speaker 2>actions before they happen.

735
00:35:13.519 --> 00:35:17.159
<v Speaker 1>So it's like having security guards and access control systems

736
00:35:17.159 --> 00:35:20.719
<v Speaker 1>in place to protect a building from intruders exactly.

737
00:35:21.199 --> 00:35:25.119
<v Speaker 2>The standard points out common mistakes like putting security sensitive

738
00:35:25.119 --> 00:35:28.800
<v Speaker 2>code in multiple jrr files. It's like scattering your valuables

739
00:35:28.840 --> 00:35:31.840
<v Speaker 2>all over the place, making them easier to steal. The

740
00:35:31.880 --> 00:35:35.800
<v Speaker 2>recommendation is to consolidate that code into a single signed

741
00:35:35.960 --> 00:35:38.920
<v Speaker 2>jar file, making it much harder to compromise.

742
00:35:39.320 --> 00:35:43.199
<v Speaker 1>Consolidated security check what other platform security tips do we

743
00:35:43.239 --> 00:35:44.039
<v Speaker 1>need to keep in mind.

744
00:35:44.320 --> 00:35:47.920
<v Speaker 2>Another really important one is to use reflection carefully. It's

745
00:35:47.960 --> 00:35:51.519
<v Speaker 2>a powerful tool for accessing and manipulating code at runtime,

746
00:35:51.920 --> 00:35:55.480
<v Speaker 2>but it can also introduce security vulnerabilities if you're not careful.

747
00:35:55.599 --> 00:35:57.400
<v Speaker 1>Reflection that's the one that lets you do all sorts

748
00:35:57.400 --> 00:35:59.719
<v Speaker 1>of magical things with code, but also has the potential

749
00:35:59.719 --> 00:36:00.639
<v Speaker 1>to things if.

750
00:36:00.599 --> 00:36:04.360
<v Speaker 2>You're not careful exactly, and the standard specifically warns against

751
00:36:04.480 --> 00:36:08.039
<v Speaker 2>using reflection to increase the accessibility of classes, methods, or

752
00:36:08.079 --> 00:36:10.079
<v Speaker 2>fields that should be private or protected.

753
00:36:10.320 --> 00:36:12.679
<v Speaker 1>So it's like using a master key to unlock every

754
00:36:12.679 --> 00:36:15.079
<v Speaker 1>door in a building, including the ones that should.

755
00:36:14.880 --> 00:36:18.440
<v Speaker 2>Be off limits precisely. Reflection is a powerful tool, but

756
00:36:18.519 --> 00:36:21.239
<v Speaker 2>it should only be used when absolutely necessary, and you

757
00:36:21.320 --> 00:36:24.119
<v Speaker 2>need to take precautions to avoid those security risks.

758
00:36:24.480 --> 00:36:28.559
<v Speaker 1>Reflection security check anything else on the platform security front.

759
00:36:28.880 --> 00:36:32.960
<v Speaker 2>One more, the standard has a rule about deploying applications

760
00:36:33.000 --> 00:36:35.880
<v Speaker 2>that can be remotely monitored whoa while that can be

761
00:36:36.000 --> 00:36:39.719
<v Speaker 2>useful for things like debugging and performance tuning, it can

762
00:36:39.760 --> 00:36:42.559
<v Speaker 2>also be a security risk if you leave those features

763
00:36:42.639 --> 00:36:44.960
<v Speaker 2>enabled in a production environment, so it's.

764
00:36:44.920 --> 00:36:48.119
<v Speaker 1>Like leaving a surveillance camera running in your house even

765
00:36:48.119 --> 00:36:50.360
<v Speaker 1>when you're not home, anyone could be watching.

766
00:36:50.480 --> 00:36:53.360
<v Speaker 2>That's a great way to think about it. The recommendation

767
00:36:53.519 --> 00:36:56.920
<v Speaker 2>is to disable those remote monitoring features in production to

768
00:36:57.039 --> 00:37:02.400
<v Speaker 2>prevent attackers from gaining access or missing with your applications behavior.

769
00:37:02.000 --> 00:37:06.559
<v Speaker 1>Remote monitoring lockdown check. Okay, we've covered a lot of

770
00:37:06.599 --> 00:37:10.400
<v Speaker 1>ground with platform security. What's next on our deep dive itinerary.

771
00:37:10.480 --> 00:37:13.760
<v Speaker 2>We're down to the miscellaneous category. These are those rules

772
00:37:13.760 --> 00:37:16.320
<v Speaker 2>that don't fit neatly into other categories, but they're still

773
00:37:16.360 --> 00:37:18.400
<v Speaker 2>really important for writing secure Java code.

774
00:37:18.440 --> 00:37:20.880
<v Speaker 1>Bring on that grab bag of security tips. I'm ready

775
00:37:20.920 --> 00:37:22.000
<v Speaker 1>for anything, right.

776
00:37:22.280 --> 00:37:25.719
<v Speaker 2>One really important rule is to always generate strong random

777
00:37:25.800 --> 00:37:27.079
<v Speaker 2>numbers when you need them.

778
00:37:27.440 --> 00:37:30.000
<v Speaker 1>Random numbers they're used for all sorts of things, from

779
00:37:30.000 --> 00:37:33.719
<v Speaker 1>generating passwords to creating encryption keys exactly.

780
00:37:34.000 --> 00:37:37.320
<v Speaker 2>But the standard cautions against using the basic Java dot

781
00:37:37.400 --> 00:37:41.519
<v Speaker 2>util random class for security sensitive applications. Its output is

782
00:37:41.559 --> 00:37:45.079
<v Speaker 2>actually pretty predictable, especially if you use the same seed value, so.

783
00:37:45.039 --> 00:37:47.480
<v Speaker 1>It's like using a weak password that's easy for attackers

784
00:37:47.519 --> 00:37:48.679
<v Speaker 1>to guess precisely.

785
00:37:48.800 --> 00:37:52.400
<v Speaker 2>The recommendation is to use the Java dot security secure

786
00:37:52.480 --> 00:37:56.400
<v Speaker 2>random class instead. It produces much stronger random numbers that

787
00:37:56.440 --> 00:37:57.920
<v Speaker 2>are much harder to predict.

788
00:37:58.320 --> 00:38:01.719
<v Speaker 1>Secure randomness Check what other miscellaneous wisdom can you share?

789
00:38:02.079 --> 00:38:05.199
<v Speaker 2>Never hard code sensitive information directly into your code.

790
00:38:05.320 --> 00:38:08.599
<v Speaker 1>Hard coding sensitive information. That's like writing your passwords on

791
00:38:08.639 --> 00:38:11.639
<v Speaker 1>a sticky note and attaching it to your monitor. Anyone

792
00:38:11.679 --> 00:38:12.239
<v Speaker 1>could see them.

793
00:38:12.719 --> 00:38:16.239
<v Speaker 2>It's a surprisingly common practice, and it's a really bad idea.

794
00:38:16.440 --> 00:38:18.920
<v Speaker 2>Those secrets are just sitting there waiting to be discovered.

795
00:38:19.440 --> 00:38:23.559
<v Speaker 2>The standard recommends storing that sensitive information in external configuration

796
00:38:23.679 --> 00:38:27.960
<v Speaker 2>files or using environment variables. That way, you can keep

797
00:38:27.960 --> 00:38:31.599
<v Speaker 2>those secrets out of your code and much safer secret stores.

798
00:38:31.719 --> 00:38:35.559
<v Speaker 1>Check anything else Before we wrap up this deep dive.

799
00:38:35.679 --> 00:38:38.320
<v Speaker 2>Just a few quick points about memory management. The standard

800
00:38:38.360 --> 00:38:41.639
<v Speaker 2>emphasizes avoiding memory leaks, which can really slow down your

801
00:38:41.679 --> 00:38:43.800
<v Speaker 2>application and even cause it to crash.

802
00:38:43.960 --> 00:38:47.280
<v Speaker 1>Memory leaks those are like those tiny drips from a

803
00:38:47.320 --> 00:38:50.280
<v Speaker 1>leaky faucet. They seem small, but they can waste a

804
00:38:50.320 --> 00:38:51.360
<v Speaker 1>lot of water over time.

805
00:38:51.559 --> 00:38:54.679
<v Speaker 2>A perfect analogy. The standard recommends a few techniques, like

806
00:38:54.800 --> 00:38:58.119
<v Speaker 2>using weak references and being very diligent about cleaning up

807
00:38:58.159 --> 00:39:01.239
<v Speaker 2>resources when you're done with them. Plug those leaks.

808
00:39:01.639 --> 00:39:05.039
<v Speaker 1>Memory leak prevention check. Wow, we've covered a ton of

809
00:39:05.039 --> 00:39:09.920
<v Speaker 1>ground in this deep dive. Concurrency threadpools file io platform security,

810
00:39:10.119 --> 00:39:13.400
<v Speaker 1>even those little details like random numbers and memory management.

811
00:39:13.840 --> 00:39:17.079
<v Speaker 1>I feel like I've leveled up my secure coding skills significantly.

812
00:39:17.280 --> 00:39:20.039
<v Speaker 2>I'm glad to hear it. But remember, secure coding is

813
00:39:20.039 --> 00:39:23.360
<v Speaker 2>an ongoing process. There are always new threats and vulnerabilities

814
00:39:23.360 --> 00:39:25.119
<v Speaker 2>popping up, so it's important to stay up to date

815
00:39:25.159 --> 00:39:27.519
<v Speaker 2>with the latest best practices keep learning.

816
00:39:27.840 --> 00:39:32.000
<v Speaker 1>Absolutely, this cert Oracle Secure Coding Standard has been such

817
00:39:32.039 --> 00:39:34.679
<v Speaker 1>a valuable guide for us. I highly recommend it to

818
00:39:34.719 --> 00:39:37.800
<v Speaker 1>any Java developer out there who wants to write more secure,

819
00:39:37.920 --> 00:39:40.360
<v Speaker 1>robust code. Thanks so much for taking us on this

820
00:39:40.679 --> 00:39:41.639
<v Speaker 1>incredible journey.

821
00:39:41.679 --> 00:39:44.119
<v Speaker 2>It was my pleasure. And always remember everyone has a

822
00:39:44.199 --> 00:39:47.239
<v Speaker 2>role to play in security. By putting these principles into practice,

823
00:39:47.280 --> 00:39:49.880
<v Speaker 2>we can all contribute to making the digital world a

824
00:39:49.960 --> 00:39:50.639
<v Speaker 2>safer place.
