…
…
In a producer cooperative's store database, write the revenue rule into views, give each team only the permissions its job needs, then reproduce the SQL injection in the order tracking screen and close it with a parameterized query.
A producer cooperative sells honey, cheese, olive oil and pantry goods from its online store. Until now everyone has used the database with full permissions: three people quote three different revenue figures, the web application can do anything on every table, and someone who typed a strange text into the order tracking screen was noticed seeing other people's orders. In this project you will first write the revenue rule into views, then give the reporting team, customer service and the web application only as much permission as their jobs need, and finally reproduce the hole and close it.
Data: five tables, fictional sample data (September–November 2026).
sales_rep: id, first_name, last_name, region (4 sales reps)customer: id, first_name, last_name, city, email, phone (40 customers)product: id, name, category, price (today's selling price), cost (12 products)customer_order: id, customer_id, sales_rep_id (empty for an order that came straight from the website), order_date, status ('delivered', 'canceled' or 'preparing'; 110 orders)order_item: order_id, product_id, quantity, unit_price (the price at the time of sale; 207 items)Lessons used: grouping and aggregation (11–13), joins (14–18), views (38), permissions and SQL injection (40). No new concept is taught; the helper pieces in the cells (DROP OWNED BY, CREATE TEMP TABLE ... AS EXECUTE) come ready and are explained next to them.
Project cells are permanent: the views and roles you create here stay in the database; they are not rolled back as in lesson exercises. That is why each cell is written to be rebuilt from scratch: a cell that creates a view first drops the old one with DROP VIEW IF EXISTS, and a cell that creates a role first returns the session to your own user (RESET ROLE) and drops the old role together with its permissions. You can re-run a cell as many times as you like; if you re-run an earlier cell, run the later ones again in order. Do not change the -- Check query parts or the ready-made lines in the cells. The first comment of some cells asks you for one sentence; it is not checked, it just makes thinking easier.
What you'll do
sales.sqlDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
Three people at the cooperative quote three different revenue figures: one counts canceled orders too, one multiplies the units sold by today's product prices, one adds orders that are still being prepared. Management settled the rule: revenue is computed only from delivered orders, at the price written on the invoice at the time of sale. On November 1 the cheese and olive oil prices went up; the price in the product table is today's price, not the price of old orders.
You will write this rule once, in the sales_detail view that all reports will read: every item of a delivered order is one row. The columns are in the comment in the cell; use the names and order exactly, because the later views will depend on these names. The ready-made line at the start of the cell drops the view and the views built on it; if you re-run this step, re-run the following steps in order too.
This step's code cell is for members.Sign up free
Management looks at the categories every week: how many orders did each category appear in, how many units were sold, what is the revenue? You will build this report from the sales_detail view, not from the tables; the delivered and price rule is not rewritten here, it comes from the view. Write the category_summary view, then list it by revenue from largest to smallest.
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
An order can contain more than one product from the same category: the number of orders is not the number of items.
This step's code cell is for members.Sign up free
The sales manager wants a scorecard for the four sales reps: each rep's number of delivered orders and revenue. The rep newly assigned to the North region has no delivered orders yet; they must still appear on the list with 0 orders and 0 revenue, not empty (NULL). Write the rep_performance view and list it.
After you get the list, add up the reps' revenues and compare the total with the total revenue from step 1. Where does the difference come from? Write your answer in the comment line in the cell; it will be the footnote of the report you take to the manager.
This step's code cell is for members.Sign up free
Management also wants to see the average amount per order in the category summary: avg_order = revenue / number of orders, rounded to two decimals. The weekly dashboard reads the category_summary view and refers to its columns by name. A friend tried to change the view with CREATE OR REPLACE VIEW, but the cell fails.
Run it, read the error message and fix the definition. Do not drop the view: the columns the dashboard reads must stay with the same names and in the same places.
This step's code cell is for members.Sign up free
The reporting team will use these three views. It does not need access to the tables: customer emails, phone numbers and product costs are not the subject of the reports. Following the principle of least privilege, create the reporter role and give it read-only permission on the three views and nothing else.
The check query puts itself in the reporting team's place (SET ROLE): can it read the views, and what permissions does it have on which objects? If a permission is missing, the query fails with permission denied; that is not a malfunction, it is a sign that the key card did not open that door. The ready-made lines at the start of the cell return the session to your own user and drop the role with its old permissions; you can re-run the cell as many times as you like.
This step's code cell is for members.Sign up free
Customer service tells a customer who calls about the status of their orders. The agent finds the customer by phone number, greets them by name and looks at their orders. The agent does not need to see the customer's email or city.
Create the support role and give it the permissions this job needs: only the necessary columns on the customer table, and read on the customer_order table. The check query lists, from the agent's point of view, the orders of the customer who calls from 555 0133 39. When you decide which columns are needed, look not only at the columns the query shows but also at the ones it filters and joins on.
This step's code cell is for members.Sign up free
The shop's web application connects to the database with the web_shop role. To finish the job quickly, whoever did the setup gave this role all permissions on four tables (ready-made in the cell). What the application really does:
Take back the extra permissions; only what these jobs need should remain. You choose the way: revoke them one by one, or revoke everything and grant back what is needed. The check query lists the table-level permissions and the column permissions on the product table. When you finish, write in the comment line: if this role's password leaks, what can an attacker no longer do?
This step's code cell is for members.Sign up free
The order tracking screen on the site has two boxes: the order number and the email. The program pastes whatever is typed into the boxes into the query text; the template is in the cell as a comment. The ready-made query is the text produced by an honest user's input and it returns one row.
An attacker types 1 in the number box and ' OR '1'='1 in the email box. The program pastes this text into the same template. Write the query the program would produce, letter for letter, and run it: how many orders show on the screen, under whose names? Then write the reason in the comment line: in the condition, which binds first, AND or OR, and why does that help the attacker?
This step's code cell is for members.Sign up free
The root of the hole is that the text in the box mixes into the query text. The fix is to send the query and the values separately: a parameterized query. With PREPARE, the query is prepared once with placeholders ($1, $2); with EXECUTE, the values are given separately and the database always uses them as values, never as part of the query.
Write the prepared statement named order_lookup: the first parameter is the order number (integer), the second is the email (text); the columns and the condition are the same as in the template. The ready-made lines run the statement first with the honest user's input, then with the attacker's, and put the results in temporary tables (CREATE TEMP TABLE ... AS EXECUTE); because a quote is written as two quotes in SQL text, the attack input appears as ''' OR ''1''=''1'. The DEALLOCATE ALL at the top drops earlier prepared statements: a prepared statement stays for the whole session, and without this line the cell would fail with already exists on a second run.
One last question, for the comment line: if the program still concatenated text and the attacker typed '; DELETE FROM order_item; -- into the email box, what would happen while the application is connected with the web_shop role? The work you did in step 7 is the second line of defense here.
This step's code cell is for members.Sign up free