Start learning free

LESSON 47 OF 56 · SECURITY: SECURING AI APPS

The Model Is Not an Access Check in Python

Practice

When a support bot uses tools, the model writes each tool call: which tool, and with what input. It writes that input from what the customer typed, so a customer can talk it into asking for anything, even someone else's order. A line in the system prompt like "Only show customers their own orders" is a request to the model, not a lock.

The access check belongs in the tool's own code. Your app knows who is signed in. Pass that into the tool from your app, never from the model's input, and compare it with what the database says. Here is the idea on another tool, one that reads invoices:

def read_invoice(tool_input, account_id):
    invoice = invoices.get(tool_input["invoice_id"])
    if invoice is None or invoice["account_id"] != account_id:
        return {"error": "Invoice not found"}
    return invoice

Give the same answer for "not yours" as for "doesn't exist", so nobody learns which numbers are real. And print a line to the log, so the shop can see who tried.

In this lesson, Ana (customer 1) asks for order 7, which belongs to Chloe Ng (customer 3). Your script must print:

Customer 1: Show me order 7.
Refused: customer 1 asked for order 7
Tool: lookup_order {'order_id': 7} -> {'error': 'Order not found'}
Bot: I couldn't find order 7 in your account. Please check the number in your confirmation email.

Chloe, signed in as customer 3, still gets her own order:

Customer 3: Has my order 7 shipped?
Tool: lookup_order {'order_id': 7} -> {'id': 7, 'customer_id': 3, 'name': 'Chloe Ng', 'ordered': '2026-05-30', 'status': 'paid'}
Bot: Your order 7, from May 30, is paid but hasn't shipped yet.

Your task: in lookup_order, refuse any order whose customer_id is not the signed-in customer_id: print a Refused line and return the same {"error": "Order not found"} as for a missing order.


Assignment
Follow the Coding Tutorial to add the owner check to lookup_order.


Hint
Compare row["customer_id"] with the customer_id argument, never with anything in tool_input: the model can put any number there.


STEP 1 OF 3
    Ctrl + Enter to check

    Secx Ai Access Check in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free