Latest / CyberCode Academy / Course 37 - Building Web Apps with Ruby On Rails | Episode 7: From RSS Feeds to User Authentication and Recovery
Transcript
- 0:00Speaker 1: So imagine you're building a brand new digital news stand,
- 0:05Speaker 1: like an exclusive app that automatically pulls in fresh headlines.
- 0:09Speaker 2: Right using something like Google's RSS news feed Exactly.
- 0:12Speaker 1: It pulls in the feed formats, everything into these beautiful
- 0:15Speaker 1: little article cards on your homepage. You want anyone to
- 0:18Speaker 1: be able to browse the basic headlines, But the real
- 0:22Speaker 1: premium investigative content.
- 0:24Speaker 2: Yeah, that needs to be locked down.
- 0:25Speaker 1: Lockdown tight, strictly behind a paywall, reserved only for verified,
- 0:31Speaker 1: paying users. And today we are walking you step by
- 0:35Speaker 1: step through how to build the security architecture for that
- 0:39Speaker 1: exact application.
- 0:40Speaker 2: And we're going to be using Ruby on rails for this.
- 0:42Speaker 2: But really our goal here is to completely demystify how
- 0:46Speaker 2: web apps actually keep your data safe behind the scenes.
- 0:49Speaker 1: Yeah, from how they mathematically lock away your passwords to
- 0:52Speaker 1: how they handle password recovery without accidentally handing the keys
- 0:56Speaker 1: over to a hacker.
- 0:57Speaker 2: Because we aren't just going to talk about the code today,
- 0:59Speaker 2: We're going to explore the actual logic of security. Will
- 1:02Speaker 2: map out exactly what is happening inside the server, inside
- 1:05Speaker 2: the computer at each step of a user's journey.
- 1:08Speaker 1: Right, because code syntex changes, right, frameworks, update every year,
- 1:12Speaker 1: but the fundamental mechanics of access control and cryptography, those concepts.
- 1:16Speaker 2: Stay the same exactly. If you understand the why behind
- 1:19Speaker 2: these mechanisms, you can apply this security mindset to literally
- 1:23Speaker 2: any tech stack you're working with.
- 1:24Speaker 1: Absolutely. So before we can lock the doors, we kind
- 1:28Speaker 1: of have to build a house first.
- 1:30Speaker 2: Yeah, you need a foundation, right, and standing.
- 1:32Speaker 1: Up a functional prototype in a framework like Rails is
- 1:35Speaker 1: notoriously fast.
- 1:37Speaker 2: Yeah.
- 1:37Speaker 1: You literally just open your terminal and run a command
- 1:40Speaker 1: like Rail's newsap and.
- 1:43Speaker 2: Boom, it generates the entire directory structure for you.
- 1:45Speaker 1: It really does. It's wild. And then we just pull
- 1:47Speaker 1: in an rssgem, which is just Ruby's term for a
- 1:51Speaker 1: plug in or a software package to fetch and parts
- 1:53Speaker 1: that raw on news feed, which.
- 1:54Speaker 2: Is great because it offloads all the heavy lifting of
- 1:57Speaker 2: data extraction. You don't have to write the code to
- 1:59Speaker 2: parse raw.
- 2:00Speaker 1: Right, And then you just type rail server and suddenly
- 2:02Speaker 1: your application is spinning up locally on port three thousand.
- 2:06Speaker 1: It all happens so fast. I always compare using a
- 2:09Speaker 1: framework like this to building a prefabricated house.
- 2:12Speaker 2: Oh, that's a good way to put it.
- 2:13Speaker 1: Yeah, Like instead of going out to the forest chopping
- 2:15Speaker 1: down trees and milling your own lumber. You just run
- 2:18Speaker 1: one command like rails generate controller and it automatically sets
- 2:21Speaker 1: up the routing, the logic, the display files all at once.
- 2:25Speaker 2: And the reason frameworks exist is exactly that, so developers
- 2:29Speaker 2: don't have to reinvent the wheel for basic web structure.
- 2:32Speaker 2: If you built this in raw code, you'd have to
- 2:34Speaker 2: manually open a network socket, listen for incoming traffic, wrap
- 2:39Speaker 2: the request. It's exhausting, right, you'd.
- 2:41Speaker 1: Be exhausted before you even displayed a single headline.
- 2:44Speaker 2: Exactly. Frameworks let you focus entirely on your custom features,
- 2:48Speaker 2: like our paywall. But there's a catch here.
- 2:51Speaker 1: There's always a catch, right.
- 2:53Speaker 2: A house with no locks isn't safe. And right now,
- 2:56Speaker 2: in this default state, guests and anonymous users have full
- 2:59Speaker 2: access to everything on that port three thousand server.
- 3:03Speaker 1: The doors are completely off the hinges.
- 3:04Speaker 2: Yeah, anyone who sends a request gets the premium articles.
- 3:08Speaker 2: There is no concept of a verified user yet.
- 3:11Speaker 1: So okay, the structure is up, the news is flowing
- 3:14Speaker 1: in from the RSS feed, But how do we start
- 3:17Speaker 1: checking IDs at the door. How do we actually protect
- 3:20Speaker 1: that premium content?
- 3:22Speaker 2: Well, we have to create a user blueprint in the database,
- 3:25Speaker 2: and that means we have to deal with passwords.
- 3:28Speaker 1: Okay, so we're building the user model. But and this
- 3:31Speaker 1: is a crucial twist for anyone following along. When we
- 3:34Speaker 1: set up the database fields, we explicitly do not use
- 3:38Speaker 1: a field call password.
- 3:40Speaker 2: No, definitely not.
- 3:40Speaker 1: We create a field called password digest And okay, let's
- 3:44Speaker 1: unpack this. Why the extra complexity here? Yeah, I know,
- 3:47Speaker 1: early developers often just make a password column and save
- 3:50Speaker 1: whatever the user types in, right.
- 3:51Speaker 2: Yeah, it's a very common early mistake.
- 3:53Speaker 1: But saving a plaintext password is well. I like to
- 3:57Speaker 1: use the welcome mat analogy. Storing a plaintext password is
- 4:00Speaker 1: like leaving your house key right under your welcome matt
- 4:02Speaker 1: with your home address written on the key and.
- 4:04Speaker 2: Sharp that is exactly what it's like.
- 4:06Speaker 1: If someone finds them at you're entirely compromised.
- 4:08Speaker 2: They have the master list, right, because if an attacker
- 4:11Speaker 2: dumps your database, they now have all those plaintext credentials.
- 4:14Speaker 2: And people reuse passwords, so now the attacker can try
- 4:17Speaker 2: to compromise those users' bank accounts, their emails, everything.
- 4:21Speaker 1: Yeah, it's a disaster. But wait, why can't we just
- 4:23Speaker 1: use normal two way encryption? Like entrip to going into
- 4:26Speaker 1: the database and decryptic coming out.
- 4:28Speaker 2: Because if you use two way encryption, your app has
- 4:32Speaker 2: to hold the decryption key. Ah right, and if a
- 4:34Speaker 2: hacker has enough access to steal your database, they probably
- 4:38Speaker 2: have enough access to steal your environment variables and grab
- 4:41Speaker 2: that decryption key too. You have to assume the server
- 4:44Speaker 2: will eventually be compromised, so encryption is out right. This
- 4:48Speaker 2: is where cryptographic hashing and salting come in. It's the
- 4:51Speaker 2: gold standard. Rails handles this really elegantly with a library
- 4:55Speaker 2: called bcrypt and a simple little flag you add called
- 4:58Speaker 2: hassecure password.
- 5:00Speaker 1: So hashing isn't encryption. It's a one way.
- 5:02Speaker 2: Street, exactly one way.
- 5:03Speaker 1: It takes the user's password and scrambles it into this unrecognizable,
- 5:08Speaker 1: mathematically complex string. That's the digest. It's like taking a
- 5:12Speaker 1: full novel and running it through an algorithm that spits
- 5:15Speaker 1: out a sixty character fingerprint.
- 5:17Speaker 2: Yes, and if you change even one comma in that novel,
- 5:20Speaker 2: the whole fingerprint changes.
- 5:22Speaker 1: Right. You can never reconstruct the original text from the fingerprint.
- 5:25Speaker 1: You just save the fingerprint. Then when they log in,
- 5:28Speaker 1: the authenticate function just checks if the new scramble matches
- 5:32Speaker 1: the save scramble, and.
- 5:33Speaker 2: B crypt goes a step further by adding a salt.
- 5:35Speaker 2: It generates a random spring of characters, adds it to
- 5:38Speaker 2: the user's password, and then hashes them together.
- 5:40Speaker 1: Which stops rainbow tables right where hackers use lists of
- 5:43Speaker 1: pre computed hashes.
- 5:44Speaker 2: Exactly because of the unique salt, even if ten users
- 5:48Speaker 2: have the terrible password one, two, three, their hashes in
- 5:51Speaker 2: your database will all look completely different.
- 5:54Speaker 1: Okay, so our database fault is mathematically secure. But when
- 5:58Speaker 1: we actually go to bill the h TML log in
- 6:01Speaker 1: form for the user, the system throws an error. It
- 6:03Speaker 1: says missing authenticity token.
- 6:05Speaker 2: Ah. Yes, that single line of code is a massive
- 6:08Speaker 2: defense mechanism.
- 6:09Speaker 1: Because right now the server rejects the naked form submission.
- 6:13Speaker 2: Right. It comes down to how browsers handle sessions. The
- 6:16Speaker 2: web has no memory, so to keep you logged in,
- 6:19Speaker 2: the server drops a cookie in your browser. But browsers
- 6:23Speaker 2: will attach that cookie to any request sent to your domain,
- 6:26Speaker 2: even if it comes from a malicious third party site.
- 6:29Speaker 1: Wait. So if I'm log into our newsgand and I
- 6:31Speaker 1: open a bad link in.
- 6:32Speaker 2: Another tab, that bad site could secretly send a request
- 6:36Speaker 2: to our app to say, delete your account, and your
- 6:39Speaker 2: browser would happily attach your valid session cookie to it.
- 6:41Speaker 1: Oh wow, so the server sees my cookie and just
- 6:44Speaker 1: assumes I wanted to delete my account exactly.
- 6:46Speaker 2: That's a cross site request forgery CSRF.
- 6:50Speaker 1: So to fix this, we put a hidden authenticity token
- 6:53Speaker 1: inside our official log in form YEP.
- 6:55Speaker 2: The server generates it dynamically. When the form is submitted,
- 6:58Speaker 2: the server checks for your cookie and that specific token.
- 7:02Speaker 2: A malicious site can't read that token, so the forgery fails.
- 7:05Speaker 1: Oh okay, I love that. So we've successfully locked the user's
- 7:08Speaker 1: secrets in a mathematical bault, and we've secured the forms.
- 7:11Speaker 2: We're solid, the perimeter is locked.
- 7:13Speaker 1: But humans are forgetful.
- 7:15Speaker 2: We certainly are right.
- 7:16Speaker 1: Like, what happens when a user forgets their password and
- 7:19Speaker 1: we need to let them back in. Password recovery is
- 7:21Speaker 1: notoriously tricky.
- 7:22Speaker 2: It really is. You're basically building a secure bypass to
- 7:25Speaker 2: your own fault.
- 7:26Speaker 1: Okay, let's trace the logic. The user clicks forgot password,
- 7:30Speaker 1: they get a form prompting them for their email if
- 7:33Speaker 1: they exist in our database. We generate a secure ten
- 7:36Speaker 1: digit token using a tool like secure random dot x ten.
- 7:41Speaker 2: Right, a high inter by temporary key.
- 7:43Speaker 1: But wait, I see a massive real world vulnerability in
- 7:47Speaker 1: the initial logic here.
- 7:48Speaker 2: Oh, walk me through it.
- 7:49Speaker 1: Okay, So the intuitive thing to do is query the database.
- 7:52Speaker 1: If the email doesn't exist, you throw a four h
- 7:54Speaker 1: four error. Right, you tell the user, hey, no account found.
- 7:57Speaker 2: It feels like good customer service.
- 7:59Speaker 1: Right. But if our system returns an error when an
- 8:02Speaker 1: email doesn't exist and a success message when it does,
- 8:05Speaker 1: we've inadvertently built an enumeration oracle.
- 8:08Speaker 2: Yes. What's fascinating here is that politeness is actually a
- 8:11Speaker 2: catastrophic security hole.
- 8:13Speaker 1: Yeah, because a hacker could just plug in half a
- 8:15Speaker 1: million leaked emails from some other breach into our form,
- 8:18Speaker 1: and based on whether they get a four h four
- 8:20Speaker 1: error or a success message, they can harvest a massive
- 8:24Speaker 1: list of exactly who is using our premium app.
- 8:27Speaker 2: Which is a huge privacy breach, especially for a newsstand
- 8:30Speaker 2: that might cover sensitive topic exactly.
- 8:32Speaker 1: So how do we fix it?
- 8:33Speaker 2: Best practice dictates that the system must always return the
- 8:36Speaker 2: exact same generic message blank stare.
- 8:39Speaker 1: Something like a link to reset your password has been
- 8:42Speaker 1: sent to that email if it.
- 8:44Speaker 2: Exists, exactly if it exists, whether the email is valid
- 8:48Speaker 2: or a total fake, they get the exact same HTTP
- 8:51Speaker 2: status and the exact same visual text. It provides zero
- 8:54Speaker 2: intelligence to a would be attacker. You're essentially blinding them.
- 8:57Speaker 1: Okay, so we've patched the data leak. We sayefly generated
- 9:00Speaker 1: our secure token without giving away who our users are.
- 9:03Speaker 1: But how do we actually deliver this ten digit token
- 9:07Speaker 1: to the user without letting it fall into the wrong hands?
- 9:10Speaker 2: Right? Delivery is the next big hurdle.
- 9:12Speaker 1: So normally we use a mailer template. Right, We generate
- 9:14Speaker 1: an email, and that email contains a link, and we
- 9:17Speaker 1: attach the token to that link as a URL query parameter.
- 9:21Speaker 2: Yeah, so the URL looks something like dot token A
- 9:23Speaker 2: one B two C three eighty four.
- 9:25Speaker 1: But sending a raw security token in a URL, I
- 9:29Speaker 1: mean that feels inherently risky. What if someone is intercepting
- 9:32Speaker 1: the network traffic?
- 9:33Speaker 2: Well, this relies entirely on HTTPS and SSL transport layer security.
- 9:38Speaker 2: It's encrypted, very heavily encrypted. When you click that link,
- 9:41Speaker 2: your browser establishes a secure encrypted tunnel with the server
- 9:45Speaker 2: before any HTTP data is even sent. So anyone eavesdropping
- 9:49Speaker 2: on the network, they just see a stream of mathematical garbage.
- 9:52Speaker 2: They can't read the URL path or your token.
- 9:55Speaker 1: Okay, that makes sense, So the secure tunnel gets them
- 9:57Speaker 1: safely back to our app. They land on the ternew
- 9:59Speaker 1: password page. Our server reads the token from the URL,
- 10:03Speaker 1: verifies it, and shows them the form.
- 10:05Speaker 2: Right, But there's a gap in the logic here that
- 10:07Speaker 2: developers miss.
- 10:08Speaker 1: Oh because HTTP is stateless exactly.
- 10:10Speaker 2: HTTP has no memory, So we verified.
- 10:13Speaker 1: The token when they loaded the page, But when they
- 10:15Speaker 1: actually type in their new password and hit the submit button,
- 10:18Speaker 1: that's a brand new post request. The server has already
- 10:21Speaker 1: forgotten who they are.
- 10:22Speaker 2: Yes, the server gets a new password, but has no
- 10:25Speaker 2: idea which account it belongs to, so we have to.
- 10:27Speaker 1: Carry that token forward. We have to add the reset
- 10:29Speaker 1: token as a hidden field inside the actual HTML of
- 10:33Speaker 1: the password reset form.
- 10:34Speaker 2: Exactly. It renders invisibly, but when the user hits submit,
- 10:38Speaker 2: the browser package is that hidden token right alongside the
- 10:42Speaker 2: new password.
- 10:42Speaker 1: And that tracks the token across the entire process, so
- 10:45Speaker 1: users can't spoof request to reset other people's passwords.
- 10:49Speaker 2: Spot on. The server does one final verification of that
- 10:52Speaker 2: hidden token against the database, hashes the new password with
- 10:56Speaker 2: b crypt and saves it. The loop is closed.
- 10:59Speaker 1: Okay, critical wrap up quotion here. What happens to that
- 11:03Speaker 1: secure ten digit token in our database after the user
- 11:06Speaker 1: successfully types in their new password, can it just like
- 11:09Speaker 1: sit there?
- 11:10Speaker 2: Absolutely not emphatically.
- 11:11Speaker 1: No why, I mean, they've already used it.
- 11:13Speaker 2: Leaving that token in the database turns a temporary bypass
- 11:16Speaker 2: into a permanent skeleton key. The absolute final step in
- 11:20Speaker 2: the recovery flow must be to delete the reset token
- 11:24Speaker 2: from the user's file.
- 11:25Speaker 1: Oh I see, because if you don't remove it, and
- 11:28Speaker 1: someone accidentally gets their hands on that old email, like
- 11:31Speaker 1: if the user forwards it or leaves their inbox open.
- 11:33Speaker 2: Anyone who clicks that old link could reset the password
- 11:36Speaker 2: again because the server would see the token in the link,
- 11:39Speaker 2: check the database and say, yep, that still matches.
- 11:42Speaker 1: Wow. So you have to destroy the bridge the second
- 11:45Speaker 1: the user crosses it.
- 11:46Speaker 2: Exactly. Tying it all together and closing that loop is
- 11:49Speaker 2: the final piece of creating a fully secure architecture from
- 11:52Speaker 2: the ground up.
- 11:53Speaker 1: Incredible. Okay, let's recap this journey really quick. We went
- 11:56Speaker 1: step by step through spinning up a RAILS application. We
- 11:59Speaker 1: lock down user passwords with the b crypts and digests.
- 12:02Speaker 1: We talked about utilizing authenticity tokens to stop forged.
- 12:05Speaker 2: Requests, and we carefully navigated the security mind fields of
- 12:08Speaker 2: password recovery.
- 12:10Speaker 1: Right, standardizing our error messages to prevent numeration, safely transporting
- 12:14Speaker 1: tokens over SSL, and completely wiping our tracking tokens at
- 12:17Speaker 1: the end.
- 12:18Speaker 2: It's a lot of sequential logic.
- 12:19Speaker 1: It really is. So before we go, here is a
- 12:22Speaker 1: quick mental exercise for you to test what you've learned today.
- 12:25Speaker 1: If you were building this app, how would you securely
- 12:28Speaker 1: verify a user's identity during a password reset if they
- 12:32Speaker 1: suddenly lost access to their email address.
- 12:34Speaker 2: That's a great real world scenario.
- 12:36Speaker 1: Yeah, like, what other delivery methods could you use to
- 12:39Speaker 1: send that secure token? Take a minute and think about
- 12:43Speaker 1: how you might adapt the architecture to use, say SMS,
- 12:46Speaker 1: text messages or even the two factor authentication app. The
- 12:50Speaker 1: delivery changes, but the core logic of generation, verification, and
- 12:55Speaker 1: destruction that stays exactly the same.
- 12:58Speaker 2: And to leave you with a final thought upon on
- 13:00Speaker 2: all this, we talked about how be a crypt scrambles
- 13:02Speaker 2: passwords so well that current computers can't realistically guess them.
- 13:07Speaker 1: Right, the intentional computational drag exactly.
- 13:09Speaker 2: It makes brute forcing mathematically unfeasible, but As computing power grows,
- 13:14Speaker 2: especially with the rise of quantum computing. What happens when
- 13:17Speaker 2: those complex scrambles can be guessed in seconds?
- 13:20Speaker 1: Oh man, that's terrifying.
- 13:22Speaker 2: It is Processors operating on quantum entanglement could drastically alter
- 13:26Speaker 2: how long it takes to reverse a cryptographic hash. Digital
- 13:29Speaker 2: security is never finished. It is a constantly evolving arms race.
- 13:33Speaker 2: So let that marinate, Let that marinate.
- 13:35Speaker 1: Indeed, keep auditing your endpoints, keep tracking your state, and always,
- 13:40Speaker 1: always destroy your temporary tokens.