Navigating Web Security. Session Authentication and Cookies

Myra Jarenga
OSINT for all
Published in
5 min readAug 12, 2023

Introduction.

In the vast landscape of the internet, where websites hold the power to offer personalized experiences and protect sensitive information, the concepts of session authentication and cookies stand as crucial pillars. Now let me help you understand these concepts, from the basics to practical applications, while keeping it technical and also easy to understand to non-technical persons.

The Heart of Web Trust. Understanding Authentication

Imagine you’re at a party, and the bouncer asks to see your invitation card before allowing you in. This act of verifying your identity is akin to what we call “authentication” in the digital world. It’s the process of ensuring that only the right individuals have access to specific resources or information. When I talk about “session authentication,” think of it as ensuring you don’t need to show your invitation every time you move from one room to another.

Exploring Cookies.

Cookies are like your digital identity. Now, let me introduce you to “cookies.” No, I am not talking about the delicious treats. In the digital realm, a cookie is a small piece of information that a website sends to your web browser. I am sure you have seen this on websites you go to a pop telling you we use cookies, and you accepted without understanding what they are. It’s like an ID card for websites, allowing them to remember who you are, even when you move from one page to another. This is crucial because, just like you wouldn’t want to keep showing your invitation at every corner of a party, websites use cookies to remember your preferences and actions as you navigate their pages.

Sending Cookies: How It Works

Let me break down how sending and receiving cookies work for websites in simple terms. Imagine you’re visiting an online store. When you log in, the website generates a special code for your visit — let’s call it your “visitor tag.” This tag is sent to your browser as a cookie. Now, whenever you click on different items, the website knows it’s you because of that visitor tag. It’s like the store recognizing you as you move around, so you don’t have to introduce yourself again and again.

Here’s a real-world example using a café scenario:

  1. You enter the café (website) and order a latte (log in).
  2. The barista (server) gives you a unique table number (session ID) on a little card (cookie).
  3. You take the card and move to different tables (pages) within the café (website).
  4. Each time you order, the barista (server) looks at your table number (session ID) card (cookie) to serve you without asking your name again.

So let me use Python programing language and Flask framework to help developers understand how you can send a cookie to the client. If you don’t understand how to use flask you can check out my post here Understanding Flask web framework.

from flask import Flask, make_response

app = Flask(__name__)

@app.route('/')
def index():
session_id = 'unique_session_id' # Generate a unique session ID
response = make_response("Welcome to our website!")
response.set_cookie('session_id', session_id)
return response

if __name__ == '__main__':
app.run()

Here I am sending a cookie containing a unique session ID to the client’s browser. This is just the same as saying,

You (Browser): "Hey café, give me a table number."
Café (Server): "Here's your table number. Keep it handy!"

Reading the Room. Parsing Cookies

So, how does the café know which table you’re at? Similarly, how does a website know it’s you as you jump from one page to another? This is where “parsing cookies” comes into play. When you visit a page, your browser sends the cookie (your visitor tag or table number) to the website’s server. The server then “reads” this tag to recognize you and provide a personalized experience. Just like the café server looking at your table number to serve you your order without confusion.

Therefore, we can still use flask framework as developers to implement this. Feel free to use any other framework. I am using flask since it's the easiest framework to understand.

from flask import Flask, request

app = Flask(__name__)

@app.route('/profile')
def profile():
session_id = request.cookies.get('session_id')
if session_id:
# Perform actions based on the session ID
return f"Welcome back to your profile!"
else:
return "You need to log in first."

if __name__ == '__main__':
app.run()

The code above I show how to retrieve the session ID from the received cookie and use it to provide personalized content to the user.

This is the same as saying,

You (Browser): "Hi café, I'm at table number 5."
Café (Server): "Got it! Here's your favorite latte!"

These codes can be integrated in larger application using flask accordingly in order to get better experience of how these cookies will be able to serve your client.

Putting Security First

While cookies make browsing convenient, they also raise concerns about privacy and security. Imagine you’re at a social gathering, and everyone knows your name and preferences. Sounds uncomfortable, right? That’s why it’s important to use cookies wisely. Websites must handle cookies securely, ensuring they don’t store sensitive information like your passwords, just as you wouldn’t want to reveal personal details at a party. Implementing secure mechanisms for transmitting and handling cookies, such as HTTPS and secure flags.

In Conclusion.

In this journey through session authentication and cookies, we’ve explored the essence of user authentication and the magic of cookies. Just as you navigate a party with your ID card, websites use cookies to enhance your online experience. Remember, while cookies make the digital world smoother, we should prioritize security and privacy. As you continue to explore the wonders of the web, the concepts of session authentication and cookies will be your trusty companions in creating safe and tailored online interactions. So, go forth, explore, and enjoy your digital adventures!

NB: Just like in real life, protect your personal information and enjoy the online journey responsibly!

Below are references to the resources that helped me in understanding this concept. If you would like to connect with me you can do so on LinkedIn Myra Jarenga, you can also send me a DM on Twitter @myrajarenga for us to chat more on this topic. You can support me by following me on this blog. Thank you.

References

https://youtu.be/501dpx2IjGY

Cookie — HTTP | MDN (mozilla.org)

Flask | The Pallets Projects

Quickstart — Flask Documentation (1.1.x) (palletsprojects.com)

--

--

Myra Jarenga
OSINT for all

A Cybersecurity analyst with customer service experience and AI expert.