Latest / CyberSecurity Summary / CERT Oracle Secure Coding Standard for Java, The (SEI Series in Software Engineering)
Transcript
- 0:00Speaker 1: All right, listeners, get ready to dive deep. We've got
- 0:03Speaker 1: some fascinating stuff done pack today.
- 0:05Speaker 2: Yeah, this is exciting you guys sent.
- 0:07Speaker 1: Us excerpts from the cert Oracle Secure Coding Standard for Java.
- 0:11Speaker 2: Oh that's a good one.
- 0:12Speaker 1: It's like the holy grail of secure Java development.
- 0:15Speaker 2: Yea.
- 0:16Speaker 1: Even James Gosling himself endorses this thing.
- 0:18Speaker 2: He does. Yeah, it's the real deal. We're talking enterprise
- 0:21Speaker 2: grade best practices, the stuff they use to build the
- 0:24Speaker 2: systems that run well practically everything exactly.
- 0:27Speaker 1: And the goal today is to pull out the absolute
- 0:31Speaker 1: key nuggets of wisdom from this standard. Wow, the stuff
- 0:35Speaker 1: that's going to help you guys write seriously secure Java code.
- 0:38Speaker 2: Absolutely, And just to add to that, it's not just
- 0:41Speaker 2: theory we're talking about here, right. The introduction of this
- 0:43Speaker 2: standard actually points out that a crazy sixty four percent
- 0:47Speaker 2: of vulnerability is back in two thousand and four. And
- 0:49Speaker 2: this is still relevant today, by the way, Yeah, sixty
- 0:51Speaker 2: four percent. They were just simple coding errors. Wow, So
- 0:54Speaker 2: this is stuff that has real world implications.
- 0:56Speaker 1: That is a so bring statistic. Okay, So before we
- 1:00Speaker 1: get into the really nitty gritty details, let's just sort
- 1:02Speaker 1: of scent the stage a little bit. What exactly is
- 1:05Speaker 1: a coding standard and why should developers even care about
- 1:09Speaker 1: this stuff?
- 1:09Speaker 2: That's a great question. Think of a coding standard as
- 1:13Speaker 2: a blueprint for secure development. It's a set of rules,
- 1:17Speaker 2: a set of guidelines that are really there to eliminate
- 1:20Speaker 2: those really common coding mistakes, right, the ones that we
- 1:23Speaker 2: all make, let's be honest, that can just open up
- 1:25Speaker 2: these huge security holes in our applications.
- 1:28Speaker 1: Right, So it's all about being proactive exactly.
- 1:31Speaker 2: It's all about prevention, you know, preventing these issues before
- 1:34Speaker 2: they even have a chance to become a problem.
- 1:36Speaker 1: And this standard is serious business. I mean, it even
- 1:39Speaker 1: has different levels of conformants. Like there's nonconforming, which I
- 1:43Speaker 1: guess is self explanatory, so.
- 1:44Speaker 2: You're not following the rules.
- 1:46Speaker 1: Conforming, you're crying. And then there's the like gold standard
- 1:50Speaker 1: provably conforming, right.
- 1:52Speaker 2: And those levels are there to help organizations figure out
- 1:55Speaker 2: where they stand security wise.
- 1:57Speaker 1: Right.
- 1:58Speaker 2: But there's this really cool thing called the deviation procedure.
- 2:02Speaker 2: So sometimes there are very specific reasons why you might
- 2:06Speaker 2: need to deviate from a rule.
- 2:08Speaker 1: Okay, so it's kind of like a break glass in
- 2:09Speaker 1: case of emergency kind of thing.
- 2:11Speaker 2: Exactly, but the standards really really clear about this. Security
- 2:15Speaker 2: cannot be sacrificed for performance ever, that's non negotiable.
- 2:20Speaker 1: No shortcuts when it comes to security.
- 2:22Speaker 2: Got it now, Java itself is often considered a pretty
- 2:26Speaker 2: secure language, right, I mean, what is it about Java
- 2:29Speaker 2: that makes it so special in the security department.
- 2:31Speaker 1: Well, Java was really designed with security in mind from
- 2:34Speaker 1: the very beginning. Okay, So there are features like automatic
- 2:37Speaker 1: bounds checking. You know, there's no explicit pointers in Java,
- 2:41Speaker 1: which is a good thing, right. And there's this thing
- 2:43Speaker 1: called the bytecode verifier that's constantly checking for violations, you know,
- 2:47Speaker 1: making sure that the code is playing by the rules.
- 2:49Speaker 2: So Java itself is kind of like a fortress already
- 2:52Speaker 2: pretty well defended. Yeah, you could say that that even
- 2:55Speaker 2: the strongest fortress can be compromised if the people inside
- 2:59Speaker 2: aren't careful, right, the human element, that's often the weakest link.
- 3:03Speaker 2: We can make mistakes.
- 3:04Speaker 1: So even with the best intentions, developers can still mess
- 3:06Speaker 1: things up. What's a common area where things tend to
- 3:09Speaker 1: go wrong?
- 3:09Speaker 2: Ah concurrency. Ah concurrency powerful tool for building responsive applications,
- 3:15Speaker 2: but it's also like opening a Pandora's box of security issues.
- 3:19Speaker 1: Yeah, concurrency the double edged sword. You've got multiple threads
- 3:24Speaker 1: all running around, potentially step it on each other's toes,
- 3:26Speaker 1: messing with data. It's a lot to keep.
- 3:28Speaker 2: Track of exactly. You have data races where you've got
- 3:32Speaker 2: multiple threads trying to access and modify the same data
- 3:35Speaker 2: at the same time, right, leading to unpredictable results total chaos.
- 3:39Speaker 2: Then you have visibility issues where changes made by one
- 3:43Speaker 2: thread aren't immediately visible to the others.
- 3:45Speaker 1: Oh right.
- 3:46Speaker 2: And then, just to make things even more interesting, there's
- 3:48Speaker 2: the unpredictable order in which threads might actually execute. It
- 3:53Speaker 2: makes it so hard to figure out what the program's
- 3:55Speaker 2: going to do.
- 3:56Speaker 1: It sounds like a recipe for disaster. So how does
- 3:59Speaker 1: the cert standard help us navigate this concurrency mindfield? How
- 4:03Speaker 1: do we stay safe?
- 4:05Speaker 2: Well? One way it does this is by really emphasizing
- 4:08Speaker 2: the concept of happens before relationships.
- 4:10Speaker 1: Happens before relationships.
- 4:12Speaker 2: Okay, Yeah, it's a way to bring order to the
- 4:14Speaker 2: chaos of concurrency. It's basically defining a strict order of operations. Okay,
- 4:19Speaker 2: even when you have all these threads running around. For example,
- 4:22Speaker 2: think about releasing an object's intrinsic lock that always happens
- 4:26Speaker 2: before the next time that lock is acquired. It's a rule,
- 4:29Speaker 2: and that helps prevent those data races.
- 4:32Speaker 1: So happens before is like a set of traffic signals
- 4:34Speaker 1: for threads, making sure they don't collide at intersections exactly.
- 4:38Speaker 2: But how do you actually implement these happens before relationships
- 4:42Speaker 2: in your code. That's where synchronization methods come.
- 4:44Speaker 1: In, right, synchronization keeping those threads in check. Tell me
- 4:47Speaker 1: more about these methods and how they work.
- 4:49Speaker 2: Well, you've got several options, each with us own trade offs.
- 4:52Speaker 2: There's volatile variables, which guarantee that changes are visible to
- 4:55Speaker 2: all threads instantly. Then there are synchronized blocks, which allow
- 4:59Speaker 2: only one thread at a time to access a protected
- 5:02Speaker 2: section of code. And then you have atomic classes, which
- 5:06Speaker 2: provide thread safe operations on individual variables.
- 5:09Speaker 1: So many choices. How do we know which one to
- 5:12Speaker 1: pick in a given situation.
- 5:13Speaker 2: Well, the standard actually gives some excellent guidance on that.
- 5:16Speaker 2: It even quotes Brian Goats.
- 5:18Speaker 1: Oh yeah, I've heard of him, a concurrency guru, right.
- 5:21Speaker 2: Exactly, and he says atomic classes are perfect for low
- 5:24Speaker 2: contention situations. You know, when you don't have a ton
- 5:28Speaker 2: of threads fighting over the same variable. But for those
- 5:31Speaker 2: high contention situations blocks, you know, like those provided by
- 5:34Speaker 2: synchronized blocks, those are a better choice.
- 5:37Speaker 1: So choosing the right synchronization method is like choosing the
- 5:39Speaker 1: right tool for the job. You wouldn't use a hammer
- 5:42Speaker 1: to screw in a light bulb exactly.
- 5:44Speaker 2: And speaking of choosing the right tool, another crucial part
- 5:46Speaker 2: of secure coding is input validation.
- 5:49Speaker 1: Uh, input validation. The golden rule never trust anything that
- 5:54Speaker 1: comes from the outside world.
- 5:55Speaker 2: You said it, and that means any external input user's environment,
- 5:59Speaker 2: veriables data from the network, anything. Treat it all as
- 6:03Speaker 2: potentially malicious, validate it, sanitize it before you even think
- 6:07Speaker 2: about using it.
- 6:08Speaker 1: Because if we skip that validation step, what kind of
- 6:11Speaker 1: trouble are we in?
- 6:12Speaker 2: Oh, the consequences can be severe. We're talking SQL injection,
- 6:15Speaker 2: where malicious input messes with your database queries, command injection
- 6:19Speaker 2: where attackers can run commands on your system. And then
- 6:23Speaker 2: there are XML external entity attacks which exploit vulnerabilities in
- 6:27Speaker 2: XML parsing.
- 6:28Speaker 1: So basically, not validating input is like leaving your front
- 6:31Speaker 1: door wide open with a big neon sign that says
- 6:33Speaker 1: welcome hackers.
- 6:34Speaker 2: Pretty much, but luckily The standard gives some very specific
- 6:37Speaker 2: advice on how to protect yourself. Normalize those strings before validation,
- 6:41Speaker 2: you know, make sure they're formatted consistently, use whitelists to
- 6:45Speaker 2: only allow save input patterns, and sanitize that user input
- 6:48Speaker 2: before you log it so you don't accidentally inject any
- 6:50Speaker 2: malicious code.
- 6:51Speaker 1: Okay, speaking of sneaky attacks, our source material also mentions
- 6:55Speaker 1: character encodings as a potential security risk. Why is that
- 7:00Speaker 1: they seem like such a low level detail.
- 7:02Speaker 2: You'd be surprised how often character encoding mismatches can cause
- 7:06Speaker 2: major data correction, especially when you're working with file imputed
- 7:10Speaker 2: output or network stuff. Right, and the standard dives into
- 7:13Speaker 2: a really tricky issue called trailing byte problem. This happens
- 7:17Speaker 2: in multi byting codings.
- 7:18Speaker 1: Trailing byte sounds ominous, it can be.
- 7:21Speaker 2: Imagine you've got a buffer boundary right where your data
- 7:23Speaker 2: gets split up, and a multi byte character it gets
- 7:25Speaker 2: split across that boundary. Because of how these encodings work,
- 7:29Speaker 2: those split bytes, they can get interpreted completely differently than
- 7:32Speaker 2: what you intended.
- 7:33Speaker 1: So it's like a word getting cut in half, and
- 7:35Speaker 1: suddenly the second half takes on a whole new meaning.
- 7:38Speaker 2: Exactly and It can happen even if the data looks
- 7:41Speaker 2: fine at first glance. So the standard's advice is to
- 7:44Speaker 2: always be super aware of your platform's default encoding.
- 7:48Speaker 1: Okay, noted, always double check those encodings. Okay, get ready,
- 7:52Speaker 1: We're about to go deep here. The next section in
- 7:54Speaker 1: this standard is called Sneaky Subtleties. Yeah, what hidden dangers
- 7:59Speaker 1: are we about to uncot?
- 8:00Speaker 2: Oh? This is where it gets interesting, even for those
- 8:02Speaker 2: seasoned Java developers up there. It's all about those little gotchas,
- 8:06Speaker 2: those tiny details that can trip you up if you're
- 8:09Speaker 2: not paying close attention.
- 8:10Speaker 1: All right, color me intrigued. Give me the lowdown on
- 8:14Speaker 1: these sneaky subtleties. What kind of things should we be
- 8:16Speaker 1: watching out for?
- 8:18Speaker 2: Well, one example is ignoring return values. The standard talks
- 8:22Speaker 2: about the string dot replace method. Okay, because strings and
- 8:25Speaker 2: Java are immutable, that replace method. It doesn't modify the
- 8:29Speaker 2: original string, right, It returns a brand new string with
- 8:32Speaker 2: the replacements. So if you ignore that return value, you're
- 8:35Speaker 2: basically throwing away the results.
- 8:37Speaker 1: Ah, So you have to assign that return value to
- 8:40Speaker 1: a variable.
- 8:41Speaker 2: Exactly, otherwise it's gone poof. Another sneaky trap is comparing
- 8:45Speaker 2: boxed primitives, you know, things like integer or boolean, where
- 8:49Speaker 2: primitive type is wrapped up in an object. Right when
- 8:52Speaker 2: you use the double equals operator to compare those things
- 8:55Speaker 2: get a little weird because of memmoization.
- 8:58Speaker 1: Memoization. That's like when my web browser remembers my passwords
- 9:01Speaker 1: so I don't have to type them in every single time.
- 9:03Speaker 2: Sort of. It's basically cashing commonly used values to make
- 9:06Speaker 2: things faster, but with boxed primitives it can cause unexpected results.
- 9:12Speaker 2: Say you have two integer objects, both with the value
- 9:14Speaker 2: one hundred. You'd think comparing them with moocitrals would return.
- 9:18Speaker 1: True, right because they have the same value.
- 9:20Speaker 2: Right, But because of memmalization, Java might be comparing the
- 9:23Speaker 2: object references instead of the values themselves, so you might
- 9:27Speaker 2: get a false result.
- 9:28Speaker 1: So it's like you think you're comparing the contents of
- 9:30Speaker 1: two boxes, but you're actually comparing the boxes themselves. That
- 9:33Speaker 1: is tricky, tricky.
- 9:34Speaker 2: Indeed, the standard is full of these insights. Another one
- 9:38Speaker 2: is hidden side effects within assertions.
- 9:40Speaker 1: AH assertions, those are the statements we use to check
- 9:43Speaker 1: conditions that should be true, and if they're not, it
- 9:45Speaker 1: throws an error.
- 9:46Speaker 2: Exactly, But here's the catch. Assertions in Java can be
- 9:50Speaker 2: turned on or off at runtime, So if you put
- 9:53Speaker 2: code with side effects inside an assertion, it might not
- 9:56Speaker 2: always get executed when you expect it to.
- 9:58Speaker 1: Oh so it's like setting a trap, but it might
- 10:00Speaker 1: not actually spring when an intruder steps on it.
- 10:03Speaker 2: That's a great way to put it, and that can
- 10:04Speaker 2: lead to some really unexpected and potentially dangerous behavior.
- 10:08Speaker 1: Okay, so far we've talked about concurrency chaos, the importance
- 10:12Speaker 1: of input validation, sneaky character encodings, ignoring return values, boxed
- 10:18Speaker 1: primitive comparison, weirdness, and hidden side effects and assertions. I'm
- 10:23Speaker 1: starting to see why this standard is considered like the
- 10:26Speaker 1: gold standard for secure Java coding. What other landmines are
- 10:30Speaker 1: we going to encounter in this deep dive?
- 10:32Speaker 2: Well, locking, for one, it's crucial for thread safety, but
- 10:36Speaker 2: even the most experienced developers can make mistakes that lead
- 10:39Speaker 2: to deadlocks.
- 10:39Speaker 1: Deadlocks those sound scary. What are they and how do
- 10:42Speaker 1: they happen?
- 10:43Speaker 2: Imagine a classic traffic jam. Everyone's stuck because they're all
- 10:47Speaker 2: waiting for each other to move. That's essentially a deadlock.
- 10:50Speaker 2: In the world of multi threaded programming, it happens when
- 10:53Speaker 2: you have two or more threads that are blocked indefinitely,
- 10:56Speaker 2: waiting for each other to release the resources they need.
- 10:59Speaker 1: So it's like a very polite dinner party gone horribly
- 11:01Speaker 1: wrong because everyone's too busy being polite to actually eat exactly.
- 11:06Speaker 2: The standard recommends a specific pattern called the private Final
- 11:10Speaker 2: lock Object IDIOM to prevent these kinds of deadlocks. Basically,
- 11:15Speaker 2: you create a dedicated lock object that's only accessible within
- 11:19Speaker 2: the class that needs to protect its data. It keeps
- 11:22Speaker 2: external code from interfering.
- 11:23Speaker 1: Ah, So it's like having a separate lock and key
- 11:26Speaker 1: for each room in your house instead of one master
- 11:28Speaker 1: key that anyone could copy exactly.
- 11:30Speaker 2: And to further avoid deadlocks, the standard says, hey, release
- 11:34Speaker 2: those locks in the same order you acquired them. It
- 11:37Speaker 2: even uses that classic philosopher's dining analogy to illustrate the point.
- 11:41Speaker 1: Philosopher's dining Okay, I'm listening.
- 11:43Speaker 2: Imagine a bunch of philosophers sitting around a table, and
- 11:45Speaker 2: they each need two forks to eat. If they all
- 11:48Speaker 2: grab their right fork first and then wait for the
- 11:50Speaker 2: left fork, nobody eats. They're all stuck.
- 11:53Speaker 1: So it's like a philosophical discussion that descends into chaos
- 11:57Speaker 1: because everyone is too busy holding onto their own idea
- 12:00Speaker 1: is to actually listen to anyone else precisely.
- 12:03Speaker 2: And this brings us to serialization, a powerful tool, no doubt.
- 12:07Speaker 2: It lets you convert objects into a stream of bites
- 12:10Speaker 2: so you can easily send them over a network or
- 12:12Speaker 2: store them in a file.
- 12:13Speaker 1: Right. Serialization making those objects travel ready. But I have
- 12:17Speaker 1: a feeling there's a butt coming there is.
- 12:19Speaker 2: Serialization is great, but it has its security risks. Attackers
- 12:24Speaker 2: can potentially mess with the state of an object during reconstitution.
- 12:27Speaker 2: You know when those bites are being converted back into
- 12:30Speaker 2: an object.
- 12:30Speaker 1: So it's like sending a package through the mail and
- 12:33Speaker 1: someone tampers with the contents en rout, replacing those delicious
- 12:37Speaker 1: cookies with something less hepatizing.
- 12:39Speaker 2: That's a great analogy to prevent that kind of tampering.
- 12:42Speaker 2: The Standard recommends what they call the sign then seal
- 12:45Speaker 2: approach for sensitive data. It combines digital signatures.
- 12:48Speaker 1: And encryption sign then seal tell me more.
- 12:51Speaker 2: First, you digitally sign the object, which is like adding
- 12:55Speaker 2: a tamperproof seal. Think of it like a wax seal
- 12:58Speaker 2: on an old fashioned letter. And then you encrypt the
- 13:00Speaker 2: whole thing to protect the data.
- 13:02Speaker 1: Double, the protection, double the peace of mind.
- 13:04Speaker 2: Exactly, and the standard goes even further. It says, always
- 13:08Speaker 2: validate an object state after de serialization, especially if you're
- 13:12Speaker 2: dealing with sensitive stuff like credit card numbers.
- 13:15Speaker 1: Smart move. Never take anything for granted, especially when dealing
- 13:20Speaker 1: with sensitive data. Okay, we've covered a ton of ground
- 13:23Speaker 1: here already, concurrency and synchronization, input validation, character encoding, those
- 13:29Speaker 1: sneaky subtleties with strings and primitives, locking in deadlocks, and
- 13:33Speaker 1: even the security considerations of serialization. I'm starting to feel
- 13:37Speaker 1: like a secure coding ninja.
- 13:38Speaker 2: That's great, But remember this is just the beginning.
- 13:40Speaker 1: Oh there's more.
- 13:41Speaker 2: There is a lot more to explore in the world
- 13:43Speaker 2: of secure Java coding.
- 13:45Speaker 1: I'm ready for more. Bring on the next round of
- 13:47Speaker 1: security wisdom, you got it.
- 13:49Speaker 2: In the next part of our deep dive, we're going
- 13:50Speaker 2: to tackle exception handling, how to deal with those unexpected
- 13:54Speaker 2: events in a way that doesn't compromise security.
- 13:57Speaker 1: Exception handling. Though it sounds like we're about to venture
- 13:59Speaker 1: into the dark underbelly of Java code. I can't wait.
- 14:02Speaker 2: It's going to be good. Okay, are you ready to
- 14:05Speaker 2: jump back into the world of secure Java coding.
- 14:08Speaker 1: Absolutely, I'm still buzzing from our last conversation. Where are
- 14:12Speaker 1: we headed next on this secure coding journey.
- 14:15Speaker 2: Well, there's a section in the search standard that I
- 14:17Speaker 2: think you'll find particularly interesting. It's all about exceptional behavior.
- 14:23Speaker 1: Exceptional behavior hmmm, sounds intriguing. Are we talking about, like
- 14:28Speaker 1: how to handle exceptions in a way that doesn't compromise
- 14:31Speaker 1: security exactly?
- 14:32Speaker 2: Handling exceptions is crucial, but it's often overlooked from a
- 14:35Speaker 2: security standpoint.
- 14:36Speaker 1: Okay, I'm all ears. What kind of security risks can
- 14:40Speaker 1: exceptions actually pose? Fill me in?
- 14:42Speaker 2: Well, think about it. Exceptions they often carry sensitive information,
- 14:46Speaker 2: oh right, Like a file not found exception that might
- 14:48Speaker 2: accidentally reveal the structure of your file system to an attacker, okay,
- 14:53Speaker 2: or SQL exception it could hint at the details of
- 14:56Speaker 2: your database schema, stuff you don't want getting out.
- 14:58Speaker 1: So it's like those detective shows where they find a
- 15:01Speaker 1: seemingly insignificant clue, like a stray fiber, and it leads
- 15:05Speaker 1: them to the whole crime scene. You're saying, those little
- 15:07Speaker 1: exception details can be like breadcrumbs for attackers.
- 15:11Speaker 2: Precisely, attackers can piece together these seemingly minor leaks to
- 15:15Speaker 2: get a much bigger picture of your system's weaknesses. It's
- 15:19Speaker 2: like a puzzle, and exceptions can give them those missing pieces.
- 15:23Speaker 1: Okay, so how do we prevent those leaks? Do we
- 15:26Speaker 1: just swallow all exceptions and pretend nothing went wrong.
- 15:29Speaker 2: That's not the best approach. The key is to handle
- 15:32Speaker 2: those exceptions gracefully without giving away more information than absolutely necessary.
- 15:37Speaker 2: One important rule to standard highlights is to avoid exposing
- 15:41Speaker 2: the original exception when you're rethrowing it. They actually give
- 15:45Speaker 2: a non compliant example where a file not found exception
- 15:48Speaker 2: gets wrapped in a more general io exception.
- 15:50Speaker 1: But isn't wrapping exceptions considered good practice. It makes the
- 15:54Speaker 1: code cleaner and it allows you to handle errors at
- 15:57Speaker 1: a higher level.
- 15:58Speaker 2: It is good practice, but you have to be very
- 16:00Speaker 2: careful about what information you're propagating up the chain. Even
- 16:03Speaker 2: if that wrapped exception isn't directly visible to the user,
- 16:07Speaker 2: it might get logged somewhere, and those logs, they could
- 16:11Speaker 2: be accessible to someone with bad intentions.
- 16:13Speaker 1: Ah So even those hidden exceptions can come back to
- 16:16Speaker 1: haunt you. It's like sweeping dirt under the rug. It
- 16:18Speaker 1: might look clean on the surface, but it's still there.
- 16:21Speaker 1: Lurking beneath exactly.
- 16:23Speaker 2: And speaking of logging, the standard actually has a whole
- 16:26Speaker 2: rule dedicated to making sure logging itself doesn't break because
- 16:29Speaker 2: of exceptions.
- 16:30Speaker 1: Wait, how can logging break? It seems like such a
- 16:33Speaker 1: simple operation, just writing some text to a file or something.
- 16:37Speaker 2: You'd think so, But imagine you're trying to log a
- 16:39Speaker 2: critical security exception to the standard error stream. Now, what
- 16:43Speaker 2: happens if that stream is closed or filled up? Oh,
- 16:46Speaker 2: you lose that crucial information. Poof, it's gone.
- 16:49Speaker 1: Yikes. So it's like having a security alarm that goes
- 16:51Speaker 1: silent at the worst possible moment.
- 16:53Speaker 2: Not very helpful, Not helpful at all. That's why the
- 16:56Speaker 2: standard strongly recommends using a robust logging framework like Java,
- 17:01Speaker 2: dot util, dot Logging, Logger, or log forge. They're designed
- 17:06Speaker 2: to handle these errors gracefully, making sure your logs are
- 17:09Speaker 2: always available when you need them.
- 17:10Speaker 1: Right, So, even for something as seemingly basic as logging,
- 17:14Speaker 1: use the right tools for the job. Solid advice. Now
- 17:18Speaker 1: there was another rule that caught my eye. Do not
- 17:21Speaker 1: throw undeclared checked exceptions. Why is that such a big no? No?
- 17:25Speaker 2: Okay? So, in Java, checked exceptions are those that you're
- 17:29Speaker 2: supposed to declare in a method signature. You use that
- 17:32Speaker 2: throw as keyword, right, And that's so you're giving callers
- 17:34Speaker 2: a heads up about what exceptions they might need to handle.
- 17:37Speaker 1: It's like a warning label on a package. Handle with
- 17:39Speaker 1: care may throw.
- 17:40Speaker 2: Exceptions exactly, but there are sneaky ways to bypass this
- 17:44Speaker 2: whole mechanism, like using reflection or the class dot new
- 17:47Speaker 2: instance method. They can throw undeclared exceptions at runtime.
- 17:50Speaker 1: Ah. So it's like sneaking a dangerous item into a
- 17:53Speaker 1: package without putting that warning label on it. Someone's going
- 17:56Speaker 1: to get a nasty surprise.
- 17:57Speaker 2: That's a great way to put it, and it can
- 17:59Speaker 2: cause all sorts of chaos because the caller isn't prepared
- 18:02Speaker 2: to handle those unexpected exceptions. The standard is super clear
- 18:06Speaker 2: on this, don't break the contract of checked exceptions. It's
- 18:10Speaker 2: like a fundamental rule of the game.
- 18:11Speaker 1: All right, no surprise exceptions, got it? What other surprises
- 18:16Speaker 1: await us in the world of exceptional behavior?
- 18:18Speaker 2: Well, another important rule is to always handle exceptions and
- 18:21Speaker 2: finally blocks right.
- 18:23Speaker 1: Finally blocks, those are the code blocks that always execute
- 18:26Speaker 1: no matter what, even if an exception pops up. They're
- 18:29Speaker 1: essential for cleanup tasks like closing those resources.
- 18:32Speaker 2: We talked about exactly. But here's the catch. The standard
- 18:36Speaker 2: warns against putting any code in a finally block that
- 18:38Speaker 2: could itself throw an exception.
- 18:40Speaker 1: Wait, why is that a problem? Wouldn't the finally block
- 18:43Speaker 1: still execute even if it throws its own exception?
- 18:45Speaker 2: It would, But here's the thing. If a finally block
- 18:48Speaker 2: throws an exception, it masks any exceptions that were thrown
- 18:51Speaker 2: in the triblock I'm masking.
- 18:54Speaker 1: So it's like one loud sound drowning out another quieter,
- 18:57Speaker 1: but more important sound.
- 18:58Speaker 2: Precisely, you lose valuable information about the original exception that
- 19:03Speaker 2: caused the problem in the first place. The standard even
- 19:06Speaker 2: gives an example. Imagine a filestream that fails to close properly,
- 19:10Speaker 2: and that resulting io exception masks a more serious security
- 19:14Speaker 2: exception that happened earlier.
- 19:15Speaker 1: So it's like trying to diagnose a patient's illness, but
- 19:18Speaker 1: a minor symptom is hiding a much more serious underlying condition.
- 19:22Speaker 2: Exactly. The takeaway here, Handle those exceptions and finally blocks
- 19:26Speaker 2: for sure, but make absolutely certain that those blocks can't
- 19:29Speaker 2: throw any new exceptions. You don't want to mask the
- 19:32Speaker 2: real problem.
- 19:33Speaker 1: Got it handle with care and don't mask those critical exceptions.
- 19:38Speaker 1: This exception handling stuff is more nuanced than I realized.
- 19:41Speaker 1: What's next on the agenda.
- 19:42Speaker 2: Let's shift gears and talk about thread APIs. These are
- 19:46Speaker 2: the building blocks for creating and managing threads, and as
- 19:48Speaker 2: we talked about earlier, concurrency, when not handled carefully, can
- 19:53Speaker 2: be a breeding ground for security issues.
- 19:55Speaker 1: All right, bring it on, I'm ready to wrangle some
- 19:57Speaker 1: threads and make sure they don't go rogue. What kind
- 19:59Speaker 1: of trouble can these thread APIs get us into? Well?
- 20:02Speaker 2: One very common mistake is using the thread group class
- 20:05Speaker 2: for managing threads. The standard actually recommends against using.
- 20:09Speaker 1: It, really, but thread groups sounds like it was designed
- 20:12Speaker 1: specifically for managing threads. What's the issue with it?
- 20:16Speaker 2: It turns out thread group has some limitations that can
- 20:19Speaker 2: lead to unexpected behavior. For example, that active count method,
- 20:23Speaker 2: the one that's supposed to tell you how many threads
- 20:24Speaker 2: are running in a group right, Well, it can actually
- 20:27Speaker 2: be inaccurate because of timing issues and other subtleties.
- 20:31Speaker 1: So it's like relying on a faulty speedometer that might
- 20:33Speaker 1: give you the wrong speed. Not ideal when you're trying
- 20:36Speaker 1: to manage a fleet of threads, you need accurate.
- 20:38Speaker 2: Information exactly, and just to add to that, thread group
- 20:42Speaker 2: methods tend to operate on all the threads in a group,
- 20:44Speaker 2: which is often too broad. The standard recommends using more
- 20:48Speaker 2: modern concurrency utilities like the executor framework, which gives you
- 20:52Speaker 2: much finer control and more robust features.
- 20:54Speaker 1: Okay, so ditch the old school thread group and embrace
- 20:58Speaker 1: the modern executor framework. Got it? What other thread API
- 21:02Speaker 1: pitfalls should we watch out for?
- 21:03Speaker 2: Another one is misusing the notify and notifile methods.
- 21:07Speaker 1: Oh right, those are the methods you use to wake
- 21:10Speaker 1: up threads that are waiting on a condition like ringing
- 21:13Speaker 1: a doorbell to let them know it's their turn exactly.
- 21:15Speaker 2: But the standard cautions against using notify. It only wakes
- 21:20Speaker 2: up one arbitrary thread, and you have no guarantee which
- 21:23Speaker 2: thread it's going to be. It might not even be
- 21:25Speaker 2: the one you want to wake up, so.
- 21:26Speaker 1: It's like sending a vague text message to a group
- 21:29Speaker 1: chat and hoping the right person sees it.
- 21:31Speaker 2: And responds exactly. A much better approach is to use notifile,
- 21:35Speaker 2: which wakes up all the waiting threads and then they
- 21:38Speaker 2: can figure out amongst themselves who should go next.
- 21:41Speaker 1: So notifile is like making a clear announcement to the
- 21:44Speaker 1: entire team, making sure everyone's on the same page. Makes sense.
- 21:48Speaker 1: Is there anything else we need to know about managing
- 21:51Speaker 1: those waiting threads?
- 21:52Speaker 2: The standard highlights the importance of making sure waiting threads
- 21:55Speaker 2: can be terminated properly. You don't want any zombie threads
- 21:58Speaker 2: hanging around after they've their work.
- 22:00Speaker 1: Right, No zombie threads. How do we ensure a clean shutdown?
- 22:04Speaker 2: It's all about avoiding those operations that can leave a
- 22:07Speaker 2: thread hanging indefinitely, waiting for a network connection, for example,
- 22:11Speaker 2: or reading from a stream. If a thread gets stuck
- 22:14Speaker 2: in one of those blocking operations, it might never respond
- 22:17Speaker 2: to a termination request.
- 22:19Speaker 1: It's like being put on hold forever, never getting to
- 22:21Speaker 1: talk to a human. Incredibly frustrating.
- 22:24Speaker 2: Exactly. That's why the standard strongly recommends using timeouts or
- 22:27Speaker 2: other mechanisms to interrupt those blocking operations, give those threads
- 22:32Speaker 2: a chance to exit gracefully.
- 22:34Speaker 1: So it's all about giving those threads an escape route
- 22:36Speaker 1: just in case they get trapped in a blocking operation.
- 22:39Speaker 1: Good advice. Any other wisdom about thread APIs that we
- 22:43Speaker 1: should know.
- 22:43Speaker 2: One thing. The standard is very explicit about never use
- 22:47Speaker 2: the thread dot stop method to terminate a thread.
- 22:50Speaker 1: Oh really, but stop seems like the most straightforward way
- 22:53Speaker 1: to shut a thread down. What's wrong with it?
- 22:56Speaker 2: It turns out stop is like using a sledgehammer to
- 22:59Speaker 2: crack a nut. It's a very blunt instrument, and it
- 23:02Speaker 2: can have all sorts of unpredictable and potentially dangerous side effects. Okay,
- 23:06Speaker 2: when you call stop on a thread, it immediately throws
- 23:10Speaker 2: a thread death exception, which can interrupt those critical operations
- 23:13Speaker 2: and leave your application in an inconsistent state. It's not
- 23:17Speaker 2: a clean shutdown, so.
- 23:18Speaker 1: It's like yanking the power cord out of a server
- 23:20Speaker 1: in the middle of a critical operation. Not a good
- 23:22Speaker 1: idea exactly.
- 23:24Speaker 2: The standard recommends more graceful shut down techniques, like setting
- 23:28Speaker 2: a flag that the thread checks periodically. Then it can
- 23:31Speaker 2: exit cleanly when it sees that signal.
- 23:33Speaker 1: So a gentle nudge rather than a forceful shove.
- 23:36Speaker 2: Much better, much better, and that covers some critical ground
- 23:39Speaker 2: when it comes to thread APIs always remember concurrency is
- 23:43Speaker 2: a powerful tool, but handle it with care. Be aware
- 23:47Speaker 2: of those potential pitfalls, and you'll be much better off.
- 23:50Speaker 1: My head is spinning with all this knowledge. It's amazing
- 23:53Speaker 1: how many subtle things can go wrong when you're dealing
- 23:55Speaker 1: with multiple threads running around. This standard is definitely proving
- 23:59Speaker 1: its earth as a guide through this treacherous terrain.
- 24:02Speaker 2: Absolutely, and we're not finished yet. There's more to explore
- 24:06Speaker 2: in this world of secure Java coding. In the next part,
- 24:10Speaker 2: we'll dive into thread pools and how to manage those
- 24:12Speaker 2: groups of worker threads securely and efficiently. It's going to
- 24:16Speaker 2: be a fascinating discussion, so make sure you tune in
- 24:19Speaker 2: all right, ready to dive back in?
- 24:21Speaker 1: You bet, I'm eager to hear what other secure coding
- 24:24Speaker 1: wisdom this certain standard has in store for us.
- 24:27Speaker 2: Well. Next up, we're going to tackle thread pools. They're
- 24:29Speaker 2: a really fundamental tool for managing concurrency efficiently, especially in
- 24:34Speaker 2: server applications.
- 24:35Speaker 1: Right, thread pools, it's like having a whole crew of
- 24:37Speaker 1: worker threads just waiting around, ready to take on tasks
- 24:41Speaker 1: as they come in.
- 24:41Speaker 2: Exactly, instead of constantly creating and destroying threads, you've got
- 24:46Speaker 2: this pool of eager beavers ready to jump into action.
- 24:49Speaker 2: It's much more efficient.
- 24:51Speaker 1: I like that analogy. So what are the key things
- 24:53Speaker 1: we need to keep in mind when we're working with
- 24:55Speaker 1: threadpools to make sure we're doing things securely.
- 24:57Speaker 2: One of the biggest mistakes people make is using an
- 25:00Speaker 2: unbounded threadpool.
- 25:01Speaker 1: Unbounded yeah, meaning there's no limit to how many threads.
- 25:05Speaker 2: Can be in the pool exactly. It might seem convenient,
- 25:08Speaker 2: you know, like a workforce that never says no to
- 25:10Speaker 2: more work, but in reality that can lead to resource exhaustion.
- 25:14Speaker 2: Your application could just crash under heavy load.
- 25:16Speaker 1: So it's like throwing a party and inviting everyone, you know,
- 25:19Speaker 1: assuming you have unlimited food in space, things could get
- 25:23Speaker 1: out a hand pretty quickly.
- 25:24Speaker 2: That's a great way to put it. The standard strongly
- 25:27Speaker 2: recommends using a bounded threadpool instead. You set a limit
- 25:30Speaker 2: on the number of threads that can be active at
- 25:32Speaker 2: any given time. It prevents your application from getting overwhelmed
- 25:36Speaker 2: and crashing.
- 25:37Speaker 1: So it's like setting a reasonable guest list for your party,
- 25:39Speaker 1: making sure you don't run out of chips and dip.
- 25:42Speaker 1: Bounded threadpool, got it? What other threadpool traps should we
- 25:47Speaker 1: be aware of?
- 25:48Speaker 2: Another common pitfall is creating a threadpool with a custom
- 25:51Speaker 2: thread factory that doesn't set a proper thread group.
- 25:54Speaker 1: Okay, remind me what a thread group is. Again.
- 25:56Speaker 2: It's basically a way to group threads together for management purposes.
- 25:59Speaker 2: But if you're not careful, your threads might end up
- 26:02Speaker 2: getting created in unexpected places, making them hard to manage
- 26:05Speaker 2: and monitor.
- 26:06Speaker 1: So it's like having a bunch of construction workers show
- 26:08Speaker 1: up at your site but you don't know what team
- 26:11Speaker 1: they belong to or who their forman is. It's a
- 26:13Speaker 1: recipe for chaos and potential accidents.
- 26:16Speaker 2: Exactly the standard says you should either use the default
- 26:19Speaker 2: thread factory or create a custom one that explicitly assigns
- 26:23Speaker 2: a thread group. That way, you know where all your
- 26:25Speaker 2: threads belong and you can keep them under control.
- 26:28Speaker 1: Okay, thread organization check any other threadpool wisdom you want
- 26:32Speaker 1: to impart before we move on.
- 26:34Speaker 2: Yes, there's one more, really nasty problem called thread starvation deadlock.
- 26:38Speaker 2: It happens when the tasks in your threadpool actually depend
- 26:41Speaker 2: on each other to complete.
- 26:43Speaker 1: Wait, how can threads get stuck waiting for each other
- 26:45Speaker 1: if they're supposed to be working independently in a pool.
- 26:47Speaker 1: That doesn't sound right.
- 26:48Speaker 2: It's a tricky situation. Imagine Task A needs a result
- 26:51Speaker 2: from task B. To finish, but task B is stuck
- 26:54Speaker 2: waiting for task A to do something first. They're both
- 26:57Speaker 2: blocked waiting for each other, and the word just grinds
- 27:00Speaker 2: to a halt.
- 27:01Speaker 1: It's like a bureaucratic nightmare where you need approval from
- 27:04Speaker 1: Department A to get something from Department B, but Department
- 27:07Speaker 1: B needs a sign off from Department A first. Nothing
- 27:12Speaker 1: gets done.
- 27:12Speaker 2: A perfect analogy. The standard emphasizes designing your tasks to
- 27:17Speaker 2: be independent whenever possible, but if you absolutely must have
- 27:21Speaker 2: those dependencies, consider using techniques like fork join. It breaks
- 27:25Speaker 2: down those dependent tasks into smaller, more manageable chunks that
- 27:29Speaker 2: can be executed in parallel.
- 27:30Speaker 1: Fork join sounds a bit fancy. What's the basic idea
- 27:33Speaker 1: behind that?
- 27:34Speaker 2: It's basically a divide and conquer strategy. You split a
- 27:37Speaker 2: big task into smaller subtasks, you tackle those subtasks concurrently,
- 27:42Speaker 2: and then you combine the results at the end. Much
- 27:44Speaker 2: more efficient.
- 27:45Speaker 1: Ah. So it's like having a team of chefs working
- 27:47Speaker 1: on different parts of a meal at the same time
- 27:49Speaker 1: so you can get everything on the table faster. Yeah,
- 27:51Speaker 1: much more efficient than having one chef try to do
- 27:54Speaker 1: everything sequentially precisely.
- 27:56Speaker 2: Now, let's talk about thread local variables, right.
- 27:58Speaker 1: Thread local those are the very that are specific to
- 28:01Speaker 1: each thread. Right. It's like giving each worker their own
- 28:03Speaker 1: personal toolbox that nobody else can touch.
- 28:06Speaker 2: That's the idea. They can be really useful, but the
- 28:08Speaker 2: standard warns against reusing thread local variables in thread pools.
- 28:13Speaker 1: Really, if the threadpool is reusing threads, wouldn't the thread
- 28:17Speaker 1: local variables just get reused along with them? It seems logical.
- 28:20Speaker 2: The tricky part is when a thread finishes its task
- 28:24Speaker 2: and goes back to the pool, those thread local variables
- 28:27Speaker 2: aren't automatically cleared, so if that thread gets picked up
- 28:30Speaker 2: for a different task, the old values from the previous
- 28:34Speaker 2: task might still be hanging around, potentially causing weird and
- 28:37Speaker 2: unexpected behavior. Oh.
- 28:39Speaker 1: I see, so it's like getting a rental car and
- 28:40Speaker 1: finding someone else's stuff in the glove comparted. Yeah, not
- 28:43Speaker 1: a very pleasant surprise.
- 28:44Speaker 2: Exactly. The standard recommends clearing those thread local variables explicitly
- 28:49Speaker 2: before a thread goes back to the pool, or you
- 28:51Speaker 2: could just pass data as arguments to tasks, which is
- 28:54Speaker 2: often a cleaner solution.
- 28:56Speaker 1: Okay, thread local cleanup, got it. I'm really starting to
- 28:59Speaker 1: appreciate how many subtle details that are to consider when
- 29:01Speaker 1: it comes to multi threaded programming. This standard is incredibly
- 29:05Speaker 1: helpful for navigating this tricky terrain.
- 29:08Speaker 2: I agree, it's a real life saver. Now let's zoom
- 29:11Speaker 2: out a bit and consider some general thread safety principles
- 29:14Speaker 2: that apply beyond just specific APIs.
- 29:17Speaker 1: Thread safety making sure our code works correctly no matter
- 29:20Speaker 1: how many threads are running around doing their thing.
- 29:22Speaker 2: Ye.
- 29:23Speaker 1: What are some of those fundamental principles.
- 29:25Speaker 2: One of the most important rules is to avoid publishing
- 29:29Speaker 2: this reference during object construction.
- 29:32Speaker 1: Publishing that this reference that sounds a bit cryptic. What
- 29:35Speaker 1: does that actually mean? In practice?
- 29:37Speaker 2: It basically means exposing a reference to an object that's
- 29:41Speaker 2: still under construction before it's fully initialized. Okay, like assigning
- 29:45Speaker 2: this to a static variable or passing it as an
- 29:48Speaker 2: argument to another method during the constructor.
- 29:51Speaker 1: So it's like showing off a half built house before
- 29:53Speaker 1: the roof is even on. Yeah, people might get the
- 29:56Speaker 1: wrong impression exactly.
- 29:57Speaker 2: The problem is that another thread could see that partially
- 30:01Speaker 2: constructed object and try to use it before it's ready,
- 30:03Speaker 2: and that can lead to some really bad consequences.
- 30:06Speaker 1: Ouch. Yeah, it's like trying to move into a house
- 30:08Speaker 1: that's still a construction zone. Bad idea.
- 30:11Speaker 2: The standard's message is clear, finish building that object before
- 30:14Speaker 2: you give out any references to it. Don't let those
- 30:17Speaker 2: other threads peek behind the curtain before the show is ready.
- 30:20Speaker 1: Object construction safety check. What other thread safety gems does
- 30:24Speaker 1: this standard have for us?
- 30:26Speaker 2: Another crucial one is protecting shared variables. If you've got
- 30:29Speaker 2: multiple threads accessing the same variable without proper synchronization, things
- 30:34Speaker 2: can get messy fast.
- 30:35Speaker 1: It's like having multiple cooks all trying to use the
- 30:38Speaker 1: same mixing bowl at the same time. Without any coordination,
- 30:42Speaker 1: ingredients would be flying everywhere.
- 30:43Speaker 2: It's a great analogy. The standard says, use synchronization mechanisms
- 30:48Speaker 2: like locks or atomic variables to protect that shared data
- 30:51Speaker 2: from the chaos of multiple threads. Make sure everyone takes
- 30:54Speaker 2: turns and plays nicely.
- 30:55Speaker 1: Shared data protection check anything else we need to be
- 30:58Speaker 1: aware of.
- 30:59Speaker 2: When it comes to safety, the standard specifically addresses a
- 31:03Speaker 2: technique called double checked locking.
- 31:05Speaker 1: Double checked locking, I'm not familiar with that.
- 31:07Speaker 2: It's a way people try to optimize locking by checking
- 31:10Speaker 2: a condition twice, once without a lock, and then again
- 31:13Speaker 2: with a lock. If the first check passes. It's an
- 31:15Speaker 2: attempt to avoid the overhead of acquiring a lock unnecessarily.
- 31:19Speaker 1: So it's like checking if a door is locked twice
- 31:21Speaker 1: before you try to open. It seems a little excessive.
- 31:24Speaker 2: The intention is good, but the standard warns that double
- 31:28Speaker 2: checked locking is super tricky to get right. In Java,
- 31:31Speaker 2: there are these subtle memory visibility issues that can really
- 31:34Speaker 2: mess things up, can give you a false sense of
- 31:36Speaker 2: security and lead to those really hard to find bugs.
- 31:40Speaker 1: So double checked locking proceed with extreme caution. I'm starting
- 31:45Speaker 1: to notice a pattern here. It seems like multi threaded
- 31:47Speaker 1: programming is full of these seemingly clever optimizations that can
- 31:51Speaker 1: backfire spectacularly if you're not incredibly careful.
- 31:55Speaker 2: You're absolutely right, and that's exactly why this standard is
- 31:57Speaker 2: so valuable. It's a distillation of hard one experience guiding
- 32:01Speaker 2: us away from those tempting but often treacherous paths.
- 32:04Speaker 1: I completely agree. Now let's talk about fileio. It might
- 32:08Speaker 1: seem like a pretty straightforward area of programming, but I
- 32:11Speaker 1: suspect there are some security nuances we need to be
- 32:13Speaker 1: aware of.
- 32:14Speaker 2: You are absolutely right. Fileio can be surprisingly tricky when
- 32:18Speaker 2: it comes to security. One of the most basic rules
- 32:21Speaker 2: is to always work within what we call a secure directory.
- 32:24Speaker 1: A secure directory, so it's not enough to just check
- 32:27Speaker 1: file permissions. We need to worry about the directory itself.
- 32:30Speaker 2: Exactly. A secure directory is one where only authorized users
- 32:34Speaker 2: and of course the system administrator have control. This prevents
- 32:39Speaker 2: attackers from messing with files or creating new files where
- 32:42Speaker 2: they shouldn't, so it's.
- 32:43Speaker 1: Like having a vault where only certain people have the combination.
- 32:46Speaker 2: Precisely, the standard really stresses understanding this concept of secure
- 32:50Speaker 2: directories and making sure your application is playing within those boundaries.
- 32:54Speaker 1: Secure directories. Check what other file io pitfalls should we
- 32:59Speaker 1: watch out for.
- 33:00Speaker 2: A really common mistake is forgetting to clean up temporary files.
- 33:04Speaker 2: It's easy to create a temporary file for some operation
- 33:07Speaker 2: and then totally forget to delete it when you're done.
- 33:09Speaker 2: But over time those leftover temporary files they can clutter
- 33:13Speaker 2: up your system and potentially leak sensitive information.
- 33:17Speaker 1: It's like leaving your trash out on the curb and
- 33:19Speaker 1: never bothering to take it to the dump. Eventually, it's
- 33:22Speaker 1: going to attract some unwonted attention.
- 33:24Speaker 2: Exactly. The standard suggests a few different mechanisms. There's a
- 33:28Speaker 2: method called file dot delete on exit. And you can
- 33:31Speaker 2: also use those try with resources blocks to make sure
- 33:34Speaker 2: your temporary files get cleaned up even if something goes.
- 33:38Speaker 1: Wrong temporary file cleanup check. Yeah, what else do we
- 33:41Speaker 1: need to be mindful of when we're dealing with files?
- 33:43Speaker 2: Another area that can trip people up is file links.
- 33:46Speaker 2: You know those aliases or shortcuts that point to other files.
- 33:50Speaker 1: Right.
- 33:50Speaker 2: Attackers can actually exploit those links to trick your application
- 33:54Speaker 2: into accessing files that it shouldn't.
- 33:56Speaker 1: Oh, I see, So it's like clicking on a suspicious
- 33:58Speaker 1: link in an email that takes you to a militia
- 34:00Speaker 1: website disguised as something legitimate.
- 34:02Speaker 2: That's a great analogy. The standard recommends using methods like
- 34:06Speaker 2: file dot get canonical path, which will resolve those links
- 34:10Speaker 2: and make sure you're actually dealing with the file you
- 34:12Speaker 2: intended to and not some malicious substitute.
- 34:15Speaker 1: Following verification check any other file io wisdom you want
- 34:18Speaker 1: to share.
- 34:19Speaker 2: One more. When you're reading data from a file stream,
- 34:22Speaker 2: make sure you read the entire thing. Don't leave any
- 34:25Speaker 2: data behind.
- 34:25Speaker 1: Why is that important?
- 34:27Speaker 2: Well, if there's leftover data, an attacker won't be able
- 34:30Speaker 2: to get to it. So the standard says keep reading
- 34:33Speaker 2: until you hit the end of the file or an
- 34:36Speaker 2: exception occurs.
- 34:37Speaker 1: So it's like finishing your plate at a restaurant, even
- 34:40Speaker 1: if you're full. Make sure no one can sneak a itte.
- 34:42Speaker 2: Exactly, filestream, cleanup, check.
- 34:44Speaker 1: This file io. Stuff is trickier than I thought. Right, Okay,
- 34:48Speaker 1: what's next on the agenda.
- 34:49Speaker 2: Let's talk about platform security. This is all about understanding
- 34:52Speaker 2: the security features and limitations of the Java platform itself.
- 34:56Speaker 1: Platform security sounds like we're getting down to the nuts
- 34:59Speaker 1: and bolts the Java virtual machine. Now.
- 35:01Speaker 2: You could say that the standard highlights features like security
- 35:05Speaker 2: managers and security policies which you can use to control
- 35:08Speaker 2: what code can actually do. You can prevent those malicious
- 35:12Speaker 2: actions before they happen.
- 35:13Speaker 1: So it's like having security guards and access control systems
- 35:17Speaker 1: in place to protect a building from intruders exactly.
- 35:21Speaker 2: The standard points out common mistakes like putting security sensitive
- 35:25Speaker 2: code in multiple jrr files. It's like scattering your valuables
- 35:28Speaker 2: all over the place, making them easier to steal. The
- 35:31Speaker 2: recommendation is to consolidate that code into a single signed
- 35:35Speaker 2: jar file, making it much harder to compromise.
- 35:39Speaker 1: Consolidated security check what other platform security tips do we
- 35:43Speaker 1: need to keep in mind.
- 35:44Speaker 2: Another really important one is to use reflection carefully. It's
- 35:47Speaker 2: a powerful tool for accessing and manipulating code at runtime,
- 35:51Speaker 2: but it can also introduce security vulnerabilities if you're not careful.
- 35:55Speaker 1: Reflection that's the one that lets you do all sorts
- 35:57Speaker 1: of magical things with code, but also has the potential
- 35:59Speaker 1: to things if.
- 36:00Speaker 2: You're not careful exactly, and the standard specifically warns against
- 36:04Speaker 2: using reflection to increase the accessibility of classes, methods, or
- 36:08Speaker 2: fields that should be private or protected.
- 36:10Speaker 1: So it's like using a master key to unlock every
- 36:12Speaker 1: door in a building, including the ones that should.
- 36:14Speaker 2: Be off limits precisely. Reflection is a powerful tool, but
- 36:18Speaker 2: it should only be used when absolutely necessary, and you
- 36:21Speaker 2: need to take precautions to avoid those security risks.
- 36:24Speaker 1: Reflection security check anything else on the platform security front.
- 36:28Speaker 2: One more, the standard has a rule about deploying applications
- 36:33Speaker 2: that can be remotely monitored whoa while that can be
- 36:36Speaker 2: useful for things like debugging and performance tuning, it can
- 36:39Speaker 2: also be a security risk if you leave those features
- 36:42Speaker 2: enabled in a production environment, so it's.
- 36:44Speaker 1: Like leaving a surveillance camera running in your house even
- 36:48Speaker 1: when you're not home, anyone could be watching.
- 36:50Speaker 2: That's a great way to think about it. The recommendation
- 36:53Speaker 2: is to disable those remote monitoring features in production to
- 36:57Speaker 2: prevent attackers from gaining access or missing with your applications behavior.
- 37:02Speaker 1: Remote monitoring lockdown check. Okay, we've covered a lot of
- 37:06Speaker 1: ground with platform security. What's next on our deep dive itinerary.
- 37:10Speaker 2: We're down to the miscellaneous category. These are those rules
- 37:13Speaker 2: that don't fit neatly into other categories, but they're still
- 37:16Speaker 2: really important for writing secure Java code.
- 37:18Speaker 1: Bring on that grab bag of security tips. I'm ready
- 37:20Speaker 1: for anything, right.
- 37:22Speaker 2: One really important rule is to always generate strong random
- 37:25Speaker 2: numbers when you need them.
- 37:27Speaker 1: Random numbers they're used for all sorts of things, from
- 37:30Speaker 1: generating passwords to creating encryption keys exactly.
- 37:34Speaker 2: But the standard cautions against using the basic Java dot
- 37:37Speaker 2: util random class for security sensitive applications. Its output is
- 37:41Speaker 2: actually pretty predictable, especially if you use the same seed value, so.
- 37:45Speaker 1: It's like using a weak password that's easy for attackers
- 37:47Speaker 1: to guess precisely.
- 37:48Speaker 2: The recommendation is to use the Java dot security secure
- 37:52Speaker 2: random class instead. It produces much stronger random numbers that
- 37:56Speaker 2: are much harder to predict.
- 37:58Speaker 1: Secure randomness Check what other miscellaneous wisdom can you share?
- 38:02Speaker 2: Never hard code sensitive information directly into your code.
- 38:05Speaker 1: Hard coding sensitive information. That's like writing your passwords on
- 38:08Speaker 1: a sticky note and attaching it to your monitor. Anyone
- 38:11Speaker 1: could see them.
- 38:12Speaker 2: It's a surprisingly common practice, and it's a really bad idea.
- 38:16Speaker 2: Those secrets are just sitting there waiting to be discovered.
- 38:19Speaker 2: The standard recommends storing that sensitive information in external configuration
- 38:23Speaker 2: files or using environment variables. That way, you can keep
- 38:27Speaker 2: those secrets out of your code and much safer secret stores.
- 38:31Speaker 1: Check anything else Before we wrap up this deep dive.
- 38:35Speaker 2: Just a few quick points about memory management. The standard
- 38:38Speaker 2: emphasizes avoiding memory leaks, which can really slow down your
- 38:41Speaker 2: application and even cause it to crash.
- 38:43Speaker 1: Memory leaks those are like those tiny drips from a
- 38:47Speaker 1: leaky faucet. They seem small, but they can waste a
- 38:50Speaker 1: lot of water over time.
- 38:51Speaker 2: A perfect analogy. The standard recommends a few techniques, like
- 38:54Speaker 2: using weak references and being very diligent about cleaning up
- 38:58Speaker 2: resources when you're done with them. Plug those leaks.
- 39:01Speaker 1: Memory leak prevention check. Wow, we've covered a ton of
- 39:05Speaker 1: ground in this deep dive. Concurrency threadpools file io platform security,
- 39:10Speaker 1: even those little details like random numbers and memory management.
- 39:13Speaker 1: I feel like I've leveled up my secure coding skills significantly.
- 39:17Speaker 2: I'm glad to hear it. But remember, secure coding is
- 39:20Speaker 2: an ongoing process. There are always new threats and vulnerabilities
- 39:23Speaker 2: popping up, so it's important to stay up to date
- 39:25Speaker 2: with the latest best practices keep learning.
- 39:27Speaker 1: Absolutely, this cert Oracle Secure Coding Standard has been such
- 39:32Speaker 1: a valuable guide for us. I highly recommend it to
- 39:34Speaker 1: any Java developer out there who wants to write more secure,
- 39:37Speaker 1: robust code. Thanks so much for taking us on this
- 39:40Speaker 1: incredible journey.
- 39:41Speaker 2: It was my pleasure. And always remember everyone has a
- 39:44Speaker 2: role to play in security. By putting these principles into practice,
- 39:47Speaker 2: we can all contribute to making the digital world a
- 39:49Speaker 2: safer place.