Lesson 7 of 7
Function level checks
Listen to an AI summary of Function level checks
The staff room door
A sports centre has a front desk, lockers and a staff room. A sign on the staff room door that says Staff only stops nobody. A lock that reads a staff pass stops a member.
The staff room stands for an action that only some people may use, such as deleting a user. The sign stands for a web page that hides a button. The lock stands for a check in the code on the server.
A function level check is code that confirms a signed-in person may use an action. In the name, function means an action of the website. A function level check is one kind of authorisation.
What is a role
A server cannot see who sends a request. The server keeps a record of what each person may do.
A role is a name for the set of actions a person may use. The sports shop has two roles. Sam, a customer, has the role customer. Ms Patel in the shop office has the role admin, short for administrator.
The hidden button
The admin page of the sports shop lists every user, with a Delete button beside each name. The page shows the Delete button only to an admin.
Pressing Delete sends a request for the path /admin/users/42/delete. In the path, 42 is the number of one user. The server answers each path with a route, the code for that one path.
The sender writes every line of a request. Sam can write the same request in a program, with no button.
1.Admin page
Hides the Delete button from a customer
2.Sam's request
A request for /admin/users/42/delete
3.Route
No check on the role. The check belongs here
4.Database
User 42 is deleted
The route with no check
Look for the line that checks the role in each of the two Python routes.
The server remembers each signed-in person in a session (Sessions), and session["role"] reads that person's role. The route delete_user never reads session, so the route deletes a user for any sender.
A route with no check on who may use the route has missing authorisation (CWE-862). A check that is present and wrong, such as a check on the wrong role, is incorrect authorisation (CWE-863).
Where the check goes
The safer route checks the role on the server, before the route does any work. Look at lines 3 and 4:
@app.route("/admin/users/<id>/delete", methods=["POST"]) def delete_user(id): if session["role"] != "admin": return "Not allowed", 403 database.delete_user(id) return "Deleted"
Lines 3 and 4 refuse Sam's request before line 5 deletes the user. With a check after line 5, the sender sees an error page and the user is already deleted.
One missed route
A route that no page links to still answers a request, such as a route left over from testing.
What is default deny
Default deny means that every route refuses a request unless a rule allows the request. A programmer who forgets a rule then gets a refused request, not a route that anyone can use.
Middleware is code that a framework runs on every request before the code for the route. Look at the list on line 1 and the three if lines.
Every path outside PUBLIC needs a signed-in person, and every /admin path needs the role admin. A new admin route is covered with no new code. A path added to PUBLIC lets anyone use the route, so a reviewer checks each addition.
Every framework has its own name for middleware.
- Express, in JavaScript
admin.use(requireAdmin)runs the checkrequireAdminbefore every admin route.- Rails, in Ruby
before_actionruns a check before every route.
A check on the first step only
A refund in the sports shop takes two requests. The first request, for /admin/refund/start, checks the role and shows a page that asks the admin to confirm. The second request, for /admin/refund/confirm, pays the money back.
1.Start
checks the role, then shows the confirm page
2.Confirm
no check on the role, and pays the refund
A sender can skip the first request and send the second. Every step that does work needs a check of its own.
A check that trusts a header
The Referer header names the page a request came from. Some code lets a request through when Referer names an admin page. The sender writes every header, so the sender can write Referer too.
# Weakness: the sender writes the Referer header if "/admin" in request.headers.get("Referer", ""): return show_refunds()
Some servers and frameworks read a header such as X-Original-URL or X-Rewrite-URL and answer for the path in the header. A check on the path of the request then runs for one path, and the route runs for another. Safer code checks the role inside the route, and the server removes both headers from every request.
Where a role comes from
The server sets the role when a person signs in, and keeps the role in the session or the database. A role must never come from the request, since the sender writes the request. Look at where each line reads the role.
role = request.form["role"] # Weakness: the sender chooses the role role = session["role"] # Safer: the server set the role at sign-in
A hidden field is a box in a form on a web page that the page does not show. The value still goes in the request, and the sender can change the value. The first line reads a hidden field named role.
A role in a cookie, a header or a query parameter is also the sender's choice. With the first line, a customer who sends role=admin gains the abilities of an admin. The gain is called elevation of privilege (Threat modelling).
Safer code also writes each change of a role to a log, a list of what happened and when.
Examples of what to look for
- A new route with no check on the role, such as
@app.route("/admin/refund"). - A check on the role only in the code that shows a page or hides a button.
- A role read from the request, such as
request.form["role"]. - A new path added to a list of public paths, such as
PUBLIC. - A new admin route that the middleware does not cover.
- A route that no page links to, with no check on the role.
- A process in several steps where only the first step checks the role.
- A check that reads the
Refererheader to decide who may use a route. - Code or a setting that reads
X-Original-URLorX-Rewrite-URLto choose the path. - Code that changes a person's role and writes nothing to a log.
Check your understanding
State what default deny means.
Show the answer
Every route refuses a request unless a rule allows the request.
Which route in the sports shop example has no function level check?
Show the answer
The route
delete_user.Give one reason why hiding the Delete button does not protect the delete action.
Show the answer
The sender writes every line of a request, so Sam can send the delete request with no button.
Practise it
Challenges with a weakness this lesson teaches.
- Share a list with a linkGiftwrap, BeginnerOpen
- Budgets and shared goalsBrightledger, IntermediateOpen
- Joint accountsBrightledger, IntermediateOpen
- Follow and unfollowChirp, IntermediateOpen
- Delete a postChirp, IntermediateOpen
- Comments on homeworkGradebook, BeginnerOpen
- Marking and feedbackGradebook, BeginnerOpen
- Parent portalGradebook, BeginnerOpen
- Next.js middleware bypassReal CVEs, IntermediateOpen