If you've been working as a web developer for a while, chances are, you've faced either one of these two error codes, or both. Error codes 401 and 403 are related to each other, and because of that, sometimes they tend to be mixed up together. However, they don't reference the same type of error, and consequently, they're not solved through the same means. In this article, we're going to review what each of these error codes mean and what you can do to solve them.
First things first, we need to cover what HTTP error codes are and how to interpret them. Thankfully, the HTTP standard provides some rules to help us understand what we're seeing.
HTTP response codes are separated into five different categories:
With this in mind, we can figure out that both error codes 401 and 403 are related to us, as users, and our request data. This means we have the power to fix both errors—or at least to understand how to stop them from happening.
Officially, the HTTP error code 401 means "Unauthorized"; however, in practice, this error code generally means "Unauthenticated". In other words, the client (or user) needs to authenticate before even trying to access the resource.
Some example scenarios where a 401 would arise include:
In both situations, the problem lies within the actual request—hence the 4XX type of error. It's not a problem with the server as the actual request hasn't been authenticated before trying to access the requested resource (whatever it might be).
The first thing to understand is if the error is due to a problem on the system's side (something you as a developer can fix) or on the user side, which is the first place we tend to look. So, when you see the 401 error on your logs, the first thing is to understand the source of the problem.
If the problem is due to a client or user that hasn't been properly authenticated, the solution is simple: you don't do anything, as this is not a mistake. Indeed, this is your platform properly enforcing the security standards you programmed and configured. Good job!
If that's not it, however, then you're looking for potential sources for this response code.
The meaning of the 403 error code is "Forbidden". In this case, the identity of the client is known to the server (so the authentication step happened), however the client doesn't have permission to access the requested resource. Unlike with the 401 error—where you might have permissions but just haven't properly authenticated yourself—with 403 you have authenticated yourself, but you're simply not allowed to access it.
The difference is subtle but significant. In fact, we have a blog post just about the differences between authentication and authorization which you should definitely check out.
As with the 401 code, the first step to fixing your 403 error code, is to understand the source of the error. Be careful! It could very well be a valid response from your system for requests trying to access resources that simply can't be accessed.
If you're looking for potential sources of the 403 response code, consider the following:
In conclusion, remember that both HTTP 401 and 403 error codes are valid responses that your platform might send back. Just because your users (or you) are experiencing either of these errors, it doesn't mean it's an automatic problem on the platform's side. To solve these problems, start by trying to understand the source of the problem. Then, using the information about both response codes, figure out if there is actually a problem. If there is, you now know where to look for the solution.
Book a free Policy Workshop to discuss your requirements and get your first policy written by the Cerbos team
Join thousands of developers | Features and updates | 1x per month | No spam, just goodies.