Secure cookies are a server-side setting that instructs browsers to encrypt and restrict cookie data, protecting user sessions from hijacking and unauthorized cross-site access.
Securing Cookies
Another thing that we're going to want to do on our application, if we're using some sort of cookies, is secure those cookies.
Let's first discuss what cookies even are. If a user makes it to a website, probably what's happening is they might log into the system, they might enter some information, they might add something to a shopping cart — there are different things that they would do at this site. Now, let's say in this example their browser accidentally gets shut down and they go back to that site. Now they've lost everything. That would be frustrating. Maybe they're going through lessons, and then they come back and it didn't track how far they were on one of the lessons. That would be frustrating.
One of the ways that we can track where they're at, what they've done, and whether they've logged in or not, is through the use of cookies. What will happen is when you visit a site, it will create one of these cookies, and it will track different things about your interaction, like whether you've logged in or not; if you've logged in, what account you have logged in; and if you've logged in, do you have anything added to the shopping cart. There are other mechanisms that can do that as well, but one of those mechanisms is these cookies. And it's stored on the individual's machine — whoever is visiting that website, it's on their machine that those are stored.
What do we use it for? Well, session management: when they log in, we can track that kind of stuff. Personalization, so that way you're not resetting up — maybe you set up dark mode on a site, and you come back and it's light mode again. We kind of like that stuff to be captured, so that way when we come back it's going to be dark mode again. Or some sort of tracking, so that way they can track what products you have seen on our site, so that way we can market those products to you.
But of course this introduces a big problem with privacy. That is, if you go to a site and they say, "Okay, well, I'm going to retrieve information from your cookies," so that way I know what you were doing on our site last and where you left off at — but at the same time, I'm also going to see what else you went and did. So for instance, what Google searches did you end up doing? And if I see what Google searches that you ended up doing, or Yahoo or YouTube or Facebook or whatever searches that you did, now what I can do is I can start marketing towards you. This is a big privacy concern, because now they are reading exactly where we've been, and what data has been collected on us — they see all of that.
Another issue with this is session hijacking. Session hijacking would be that these cookies sometimes store the state of where we're at at a single site. And so if somebody wants to take over that site, they can steal the cookie, which has information, and then they can use that cookie to access that site.
So one thing that we can do is we can use secure cookies. Cookies have a setting on them of whether they're secure or not. And so we as programmers, as the one that is in charge of the applications, would be the one that would switch that on. So we'd switch it on on the server side. Then what would happen is when that cookie is created, it's created on the client side, and the client browser really decides what it's going to do with that cookie and how it's going to secure that cookie. But essentially Google Chrome or Safari or Edge or Firefox — any one of those — is going to do what it can do to secure these cookies, to encrypt it, to make sure it's locked down to certain sites and make sure that other sites then can't access it.
TechKnowSurge builds IT and cybersecurity professionals through hands-on, concept-first training built around real understanding — not memorization. Free interactive tools, structured programs, and 25+ years of real-world experience, all in one place.
Explore free tools and programs →