Authorization
If you haven't already, first read the previous post about Cookies.
Authorization refers to what a user can do once they're authenticated. It also includes what a user sees (and what a visitor doesn't see). We've already seen the simplest version: the navbar checks if @current_user and shows your name and Log out, or Sign up and Log in.
That's what the app shows. Let's look at what it allows.
Someone else's account
Log in, and click your name in the navbar. That's your account page, /users/3 (or whatever your id is), with your name and your email.
Now change the number in the address bar to /users/2.
That's Brian's account page, and his email. You're not Brian. Try /users/1. Try them logged out. Anyone who can count can read every user's email in our app.
Here's the show action in app/controllers/users_controller.rb:
def show
# find the User whose id is in the address
@user = User.find_by({ "id" => params["id"] })
# render users/show view
render({ :template => "users/show" })
end
It finds whichever user is in the address and shows them, no questions asked. Nothing is wrong with the code; nobody wrote down the rule. This hole has a name: broken access control, and it's number one on the OWASP Top 10, the industry's list of the most common serious flaws in web applications. Not because it's clever. Because it's easy to forget.
The rule is: only the user themself gets to see this page. Now that we have @current_user, that's one if:
def show
# find the User whose id is in the address
@user = User.find_by({ "id" => params["id"] })
# only the user themself gets to see this page: the user in the address has to be
# the user who is logged in, or the request is turned away
if @current_user.nil? || @user != @current_user
flash["notice"] = "That isn't your account."
redirect_to("/posts")
return
end
# render users/show view
render({ :template => "users/show" })
end
If nobody is logged in, or the user in the address isn't the user who's logged in, we send them away with a message. The return stops the action there, so the page never renders. Try /users/2 again: "That isn't your account." Your own page still works.
Notice where the rule lives: in the controller, not the view. Every request arrives at the controller, whether it came from a link on our page or from someone typing an address. Hiding a link is a courtesy. The controller is the lock.
If you fell behind, code-along-complete/3-access/ in your copy has this file finished.
Tacostagram
Now the posts. Right now, anyone can post a taco under any name they type, and posts don't belong to anyone. Here's who should be able to do what:
- A visitor can read the feed, but can't post to it.
- A logged-in user's posts carry their name, and nobody can post as someone else.
- A logged-in user can delete their own post.
- And nobody can delete your post.
These are the four tickets in your copy's Issues tab, each with a definition of done. Build them first: point your agent at each issue, one at a time, and read the tests it writes against the sentences. Then compare with the full walkthrough, The four Tacostagram tickets.
Without giving the whole thing away, here are the rules as they end up in the code.
Only members. A second method in the application controller, used as a before_action on the actions that need a login:
before_action(:require_login, { :only => ["new", "create", "destroy"] })
index isn't on the list, so the feed stays public. And create is on it, not just new: hiding the form doesn't stop someone from sending a POST to /posts without it.
The author comes from the session, never the form. We could put a hidden user_id field in the post form with the current user's id. But anything in the browser is the user's to change: open the developer tools, change the number, submit, and you've posted as someone else. Instead, the backend assigns it, in code only we can touch:
post["user_id"] = @current_user["id"]
Your posts, and only yours. To delete a post, don't find it and then check whose it is. Only ever look among the current user's posts:
post = Post.find_by({ "id" => params["id"], "user_id" => @current_user["id"] })
Someone else's post is never found, so it can never be destroyed, even by a DELETE request that didn't come from our page.
Abuse stories
The fourth ticket is a different kind of user story: "As another user, I want to delete your post." It's written from the attacker's chair, and it describes something that must never work. These are sometimes called abuse stories.
Write them down next to the user stories. What would a malicious user try in your app? Each one becomes a ticket with a definition of done, and each sentence gets a test that proves the refusal. Then the rule isn't true by accident, a side effect of how one line happens to be written. It's true on purpose, and the day someone (or some agent) rewrites that line and loses it, a test fails.
Just conditionals
Besides the concept of @current_user and how it gets assigned in a before_action, you may have noticed that the rest of this authorization code is no different from anything we've done before. Authorization at its core is conditional logic, SQL queries and column assignment:
if @current_user@user != @current_userpost["user_id"] = @current_user["id"]Post.find_by({ "id" => params["id"], "user_id" => @current_user["id"] })
With those tools, you have almost infinite control over the authorization logic in your applications. The hard part isn't the code. It's deciding who may do what, and writing it down - which, with an agent writing the code, is the product owner's job.