Lesson 6 of 7
Object references
Listen to an AI summary of Object references
A locker that opens for any pass
A sports centre has a front desk, lockers and a staff room. Every member shows a pass at the front desk. Each locker has a number and belongs to one member.
In the sports shop, showing the pass is signing in. A locker is one order, and the locker number is the order number. A locker that opens for any member with a pass is the first weakness.
One order in the sports shop
Sam, a customer, signs in to the sports shop and opens an order. The URL is shop.example/order?id=1042. The part id=1042 is a query parameter.
An identifier, or id, is a value that names one record, such as order number 1042. A record is one row of a table in a database.
Sam changes 1042 to 1043 in the address bar and presses Enter. The browser sends a new request.
GET is the word for a request that reads a page.Sam now knows Priya's name and address, and can try every other number.
The code that fetches the order
The server runs five lines of Python for the page. Look at where line 2 gets the id, and at what line 3 asks the database.
The question mark on line 3 marks where the id goes. Line 4 sends the id separately, so the database never reads the id as SQL. Sending the id separately prevents SQL injection.
The WHERE part of line 3 tests the id of the order and nothing else. No line checks whether the order belongs to Sam.
What is an insecure direct object reference
An insecure direct object reference (CWE-639), also called IDOR, is an id from a request used to fetch a record with no ownership check. An ownership check is code that confirms a record belongs to the signed-in person before the code uses the record. In the sports centre, checking whose locker is being opened is the ownership check.
The sports shop did check who Sam is, which is called authentication. The missing check is authorisation.
The ownership check
Each row of the orders table has a column named customer_id, the id of the customer who placed the order. The safer code adds line 3 and changes lines 4 and 5. Look at where line 3 gets the customer id.
def show_order(): order_id = request.args["id"] customer_id = session["customer_id"] query = "SELECT * FROM orders WHERE id = ? AND customer_id = ?" rows = database.execute(query, [order_id, customer_id]) return rows
The variable session holds what the server remembers about the signed-in customer (Sessions). The server stored Sam's customer id in session at sign-in, and Sam cannot change session. Line 3 reads that customer id.
Line 4 has a second question mark, and line 5 sends both ids.
SELECT * FROM orders WHERE id = ? AND customer_id = ?
- SELECT * FROM orders WHERE id = ?the query before
- AND customer_id = ?the ownership check
Sam asks for order 1043 again. No row has the id 1043 and Sam's customer id, so the query returns no rows. Sam gets the same empty answer as for an order number that does not exist.
Where the owner comes from
A customer id in a request is an id as well. Code that reads customer_id from the request lets Sam type Priya's customer id. The sender writes every part of a request, so the id of the owner comes from the session.
The ownership check belongs on every route that uses an id. A route that changes or deletes an order needs the check as much as the route that reads an order.
The sports shop's phone app sends requests to an API. An API is a set of routes that other programs send requests to, with no page for a person to look at. A person can send the same requests without the app, so every route of an API needs the check.
Examples of defences
Some applications send every query on a table through one function that adds the owner. A new query then cannot leave the owner out.
Row-level security is a database setting that lets each person reach only the records that belong to that person. The database then hides Priya's order from Sam even when a query leaves the owner out. A second defence that catches a mistake in the first is called defence in depth (Risk based review).
A field the form never showed
The second weakness is in code that changes a record. Sam's profile page has a form with two boxes, called form fields, for a name and an email address. Sam's record in the customers table has a third column, role, which holds admin for staff who run the shop.
The form sends the two form fields in the body, the last part of a request. Sam sends the request from a program in place of the form, and adds a third field.
The server runs five lines of Python to save the profile. Look at the loop on lines 3 and 4.
def update_profile(): customer = find_customer(session["customer_id"]) for field in request.form: customer[field] = request.form[field] save(customer)
Line 2 fetches Sam's record. On line 3, request.form holds the form fields from the body, and the loop goes through the name of every field. Line 4 copies the value of each field onto the record under the same name. Line 5 saves the record, and Sam's record now has role set to admin.
What is mass assignment
Mass assignment (CWE-915) is copying every value in a request body onto a record, including values the person should not set. A balance and a price are two more values a customer must not set.
The safer code changes line 3. The loop goes through a list of names that the programmer wrote:
for field in ["name", "email"]:The list is an allowlist, a fixed list of permitted values. The loop never reads role from the body.
Some frameworks copy a whole body onto a record in one line, with no loop to see. Each of those frameworks has a way to list the fields a request may set, such as permit in the framework Rails.
Examples of what to look for
- A query that finds a record by an id from the request, with no owner in the
WHEREpart. - A customer id or an account id read from the request and used as the owner.
- An ownership check on the route that reads a record, and none on the route that changes the record.
- A new table that holds the data of customers, with no column for the owner.
- A loop or a single line that copies every field of the body onto a record.
- A new column that a customer must not set, such as
role, with no change to the allowlist.
Check your understanding
State what an ownership check is.
Show the answer
An ownership check is code that confirms a record belongs to the signed-in person before the code uses the record.
Sam is signed in and changes
id=1042toid=1043in the URL. What does the first version ofshow_orderreturn?Show the answer
The first version returns Priya's order, number 1043. The query finds an order by id alone.
Give one reason why the safer code reads the customer id from the session and not from the request.
Show the answer
The sender writes every part of a request, so Sam could type Priya's customer id. Sam cannot change the session.
Practise it
Challenges with a weakness this lesson teaches.
- Share a list with a linkGiftwrap, BeginnerOpen
- Secret Santa drawGiftwrap, BeginnerOpen
- Checkout rewriteSwiftcart, IntermediateOpen
- Order lookupSwiftcart, IntermediateOpen
- Budgets and shared goalsBrightledger, IntermediateOpen
- Statement exportBrightledger, IntermediateOpen
- Joint accountsBrightledger, IntermediateOpen
- Payday remindersBrightledger, IntermediateOpen
- Year in reviewBrightledger, IntermediateOpen
- Hand in homeworkGradebook, BeginnerOpen
- Marking and feedbackGradebook, BeginnerOpen