DEV Community

Cover image for πŸ” Stop Writing Weak Auth! Build Rock-Solid Authentication for Your Next Project
Ajinkya Anil Kahane
Ajinkya Anil Kahane

Posted on

2 1 1 1 1

πŸ” Stop Writing Weak Auth! Build Rock-Solid Authentication for Your Next Project

First, let's talk about IAM (Identity and Access Management) πŸ”

It basically means signing up or logging into a platform (Authentication), maintaining the session (Session Management) for subsequent requests or activities, and doing this while keeping the user restricted to only the actions they are allowed to perform (Access Control).


πŸ”‘ Authentication

Authentication is the process where a user either signs up or logs into their account on your platform. If the user is signing up, you store their information in the database. If you are opting for password-based authentication, you store that password in the database after hashing it.

Now comes the later partβ€”verifying the email ID, phone number, and other such necessary steps. After this, the process depends on what mode of authentication you've chosen. There are plenty of options, for instance:

  • Cookie-based authentication πŸͺ
  • Header-based authentication πŸ“œ

πŸ•’ Session Management

After authentication, you've either got a cookie from the server or a token. One crucial aspect here is, if you chose cookie-based authentication, make sure to configure its options properly:

  • Secure (Enables sending cookies only over HTTPS connections)
  • HttpOnly (Prevents JavaScript from accessing cookies, a great prevention against XSS attacks)
  • SameSite (Specifies whether to send cookies with cross-site requests)
  • Domain (Defines which domain can access the cookie)
  • Path (Specifies on which path the cookies are accessible)

So, session management ensures that for every subsequent request after authentication, you verify whether the user is logged in. You refer to the cookie or headers sent from the frontend and check if they are intact or expired. You can also use Redis to keep track of sessions efficiently. πŸ—„οΈ


🚦 Access Control

Now that you've implemented a layer to verify whether a request is authenticated, the next step is to ensure that the user making the request has permission to perform the requested action.

Access control means restricting users to only those actions that they are allowed to perform.

This was a brief introduction to IAM. Of course, there's much more to exploreβ€”Access Control itself is a vast topic, but we'll save that for another time. πŸ˜‰


⚠️ Horizontal Privilege Escalation

It's intuitive that a user can access their own data using their session. But what if, by some "holy magic," they manage to access another user's data? This is called Horizontal Privilege Escalation (HPE). 😱

Example πŸ”

Consider a request where a user wants to access their own information:

https://api.some-random-server.com/user/123
Enter fullscreen mode Exit fullscreen mode

Now, the user opens the network tab in their browser, analyzes the API request, and decides to do something sneaky 🌚. They copy their cookie or authorization header, go to Postman, and make this request:

https://api.some-random-server.com/user/124
Enter fullscreen mode Exit fullscreen mode

If proper checks are missing, the user might actually fetch another user's data! That's some unholy business, but pretty cool from a hacker's perspective, right? πŸ˜†

A real-world example would be an e-commerce platform where users fetch their invoices:

https://this-is-ecommerce.com/orders/123456.pdf
Enter fullscreen mode Exit fullscreen mode

If someone changes 123456 to 123457, they might fetch someone else's invoice. That's a serious security flaw. πŸ˜΅β€πŸ’«

This is known as Insecure Direct Object Reference (IDOR), which we’ll discuss shortly.

πŸ›‘οΈ How to prevent HPE?

  1. Use UUIDs instead of simple IDs: Using predictable IDs like 123456 makes guessing easy. UUIDs add a layer of difficulty.
  2. Perform a proper check at the code level: Ensure the user requesting the resource actually has permission.
  3. Securely manage sessions: Validate request origins, check user agents, and block suspicious requests (e.g., from Postman or cURL). 🚫
  4. There’s more, but these basics will get you started.

πŸ” Vertical Privilege Escalation

Imagine an organization with roles like Admin, Manager, Employee. If an Employee gains access to another Employee's data, that's HPE. But what if the Employee accesses data meant for a Manager? That’s Vertical Privilege Escalation (VPE)! πŸš€

πŸ›‘οΈ How to prevent VPE?

  1. Implement the Principle of Least Privilege (PoLP): Users should only have the permissions necessary for their tasks.
  2. Secure authentication channels: Ensure robust authentication to prevent unauthorized access to sensitive records.

πŸ“ˆ Horizontal to Vertical Privilege Escalation

Now, imagine a user (Employee) exploits an HPE vulnerability to fetch an Admin’s data, including their password. If they successfully log into the Admin account, they now have escalated their privileges both horizontally and verticallyβ€”this is Horizontal to Vertical Privilege Escalation. 😨


πŸ”“ What is IDOR (Insecure Direct Object Reference)?

We saw an example where users could fetch another person’s invoice just by changing an ID in the URL. That’s IDORβ€”one of the most common web security issues. πŸ›‘

πŸ›‘οΈ How to prevent IDOR?

  1. Implement proper authorization checks at the code level:
   if (logged_in_user.id != resource.user.id) {
       return null; // Or throw an "Unauthorized action" error
   }
Enter fullscreen mode Exit fullscreen mode
  1. Use UUIDs instead of predictable IDs.
  2. Implement granular access control: Restrict user access based on need.
  3. Use strong authentication measures: Verify JWT tokens, cookies, headers, or OAuth scopes before granting access.

This was just a basic guide to what a developer should keep in mind while implementing Authentication in their projects. πŸš€

Thanks for reading! See you next time. 😊

Ajinkya ❀️

Top comments (5)

Collapse
 
0xajinkya profile image
Ajinkya Anil Kahane β€’

Share your thoughts, did you like it?

Next time we'll discuss access controls in great detail.

Collapse
 
suchit_chaudhari_11e3469a profile image
suchit chaudhari β€’

This is gold, Ajinkya! Developers often overlook IAM until it becomes a serious issue. The breakdown of HPE, VPE, and IDOR was really well explained very informative! Using UUIDs and proper authorization checks should be a standard practice. Security isn’t something to think about later, it needs to be built in from the start. Keep sharing such great content!

Collapse
 
0xajinkya profile image
Ajinkya Anil Kahane β€’ β€’ Edited

Glad you liked it, Suchit!

Collapse
 
nazim_akkal_a6c14939d5955 profile image
nazim akkal β€’

Great post! I was curious why NextAuth wasn't mentioned and noticed a few session security details might need more clarity. Thanks for sharing your insights!

Collapse
 
0xajinkya profile image
Ajinkya Anil Kahane β€’

Hey, thanks for the comment, actually this was a code/framework agnostic post. I'll be covering the remaining session security details in my upcoming posts :)