Skip to content

Lesson 7 of 7

Function level checks

Listen to an AI summary of Function level checks

0:00 / 0:33

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. 1.Admin page

    Hides the Delete button from a customer

  2. 2.Sam's request

    A request for /admin/users/42/delete

  3. 3.Route

    No check on the role. The check belongs here

  4. 4.Database

    User 42 is deleted

The only check is in the page.

The route with no check

Look for the line that checks the role in each of the two Python routes.

  1. @app.route("/admin/users")# @app.route gives the path that the route answers
  2. def list_users():# the first route
  3. if session["role"] != "admin":# the check on the role
  4. return "Not allowed", 403# sends back the words Not allowed and stops. 403 is the code for a request that is not allowed
  5. return show_user_list()# shows the page to an admin
  6. @app.route("/admin/users/<id>/delete", methods=["POST"])# the path of the delete action. <id> is the user's number. A POST request sends data
  7. def delete_user(id):# the second route
  8. database.delete_user(id)# deletes the user, with no check on the role
  9. return "Deleted"
Two routes of the sports shop.

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

  1. /admin/userschecks the role
  2. /admin/orderschecks the role
  3. /admin/orders/1042/refundchecks the role
  4. /admin/products/newchecks the role
  5. /admin/users/42/deleteno check
  6. /admin/users/42/rolechecks the role
Five routes check the role and one route does not.

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.

  1. PUBLIC = ["/", "/sign-in", "/search"]# the paths anyone may use
  2. @app.before_request# runs the function before every route
  3. def check_request():# the middleware
  4. if request.path in PUBLIC:# request.path is the path of the request
  5. return# a public path carries on to the route
  6. if "role" not in session:# nobody is signed in
  7. return "Sign in first", 401# refuses the request. 401 is the code for not signed in
  8. if request.path.startswith("/admin") and session["role"] != "admin":# an admin path, and the person is not an admin
  9. return "Not allowed", 403# refuses the request
  10. return# any other request carries on to the route
Middleware in the sports shop that refuses a request by default.

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 check requireAdmin before every admin route.
Rails, in Ruby
before_action runs 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. 1.Start

    checks the role, then shows the confirm page

  2. 2.Confirm

    no check on the role, and pays the refund

The check is on the first step only.

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 Referer header to decide who may use a route.
  • Code or a setting that reads X-Original-URL or X-Rewrite-URL to choose the path.
  • Code that changes a person's role and writes nothing to a log.

Check your understanding

  1. State what default deny means.

    Show the answer

    Every route refuses a request unless a rule allows the request.

  2. Which route in the sports shop example has no function level check?

    Show the answer

    The route delete_user.

  3. 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.