Skip to content
POLARISAI ACADEMY
R04-201 · Navigator

Web Security, OWASP and Supply-Chain Defence

You can require the session CSRF token on a write, accept or reject a note with a character allow-list, bind that note so an apostrophe stays inside one value, encode five HTML characters in one left-to-right pass, return an entry only when owner_id matches the verified sub, and open a file only when the normalized path stays under that account's root.

Level
Navigator
Time
24 h (estimate)
Prerequisites
R04-101, R03-101
Course 2 of 4
Jump to

After this course you can

The student can patch a vulnerable web application to close 10 seeded OWASP vulnerabilities while preserving 100% of authorised functionality and generating a valid SBOM and tamper-evident audit log.

An allow-list and a bound query

Table entries has three rows. Row 11 has owner_id u17, amount_cents 2500, and note rent. Row 12 has owner_id u18, amount_cents 9000, and note rent. Row 13 has owner_id u19, amount_cents 400, and note tax. A new note is a string the caller typed. The allow-list accepts it only when the length runs from 1 through 40 and every character sits in this set: the letters A through Z, the letters a through z, the digits 0 through 9, a space, a comma, a period, or a hyphen. May rent has length 8 and uses letters plus one space, so it passes. A string of 40 a's passes the length check. A string of 41 a's fails it. Call the attack string S. Its eleven characters are an apostrophe, a space, O, R, a space, an apostrophe, 1, an apostrophe, an equals sign, an apostrophe, and 1. The length 11 sits inside the window. S still fails, because an apostrophe and an equals sign sit outside the set. An empty note fails because its length is 0.

SQL injection, SQLi, is what you get when the typed string is glued into the query text and the database reads part of it as syntax. The glued lookup is the fixed text SELECT id, amount_cents FROM entries WHERE owner_id = 'u17' AND note = ' followed by the typed string and then one closing apostrophe from the template. With S the finished text is SELECT id, amount_cents FROM entries WHERE owner_id = 'u17' AND note = '' OR '1'='1'. In SQL, AND binds tighter than OR, so the condition is (owner_id = 'u17' AND note = '') OR ('1'='1'). The comparison '1'='1' is true, so every row passes. The result has 3 rows. The amount_cents values 2500, 9000, and 400 add to 11900.

A bound query keeps the typed characters in value slots beside the query text. The text is SELECT id, amount_cents FROM entries WHERE owner_id = ? AND note = ?, and the two values are u17 and S. The apostrophes inside S are compared with the note column. They do not close the statement. The stored notes are rent, rent, and tax, so the match count is 0 and the amount sum is 0. The write uses UPDATE entries SET note = ? WHERE id = ? AND owner_id = ? with the three values May rent, 11, and u17. Row 11 has that owner, so the write changes 1 row. The note becomes May rent. amount_cents stays 2500. Rows 12 and 13 stay rent and tax. The same write with S stops at the allow-list, and the number of rows changed is 0.

Five characters, one header, one age

Cross-site scripting, XSS, is a browser treating note characters as markup. Output encoding for a text node, and for a double-quoted attribute, walks the string once from the left and replaces five characters: & becomes &amp;, < becomes &lt;, > becomes &gt;, a double quote becomes &quot;, and an apostrophe becomes &#39;. The ampersand is replaced in that same single pass, so the ampersands that the entities introduce are left as they stand. A second pass would turn &amp; into &amp;amp;, and the page would show the entity text instead of an ampersand.

Take the 17-character note Tom & <b>"ok"</b>. One pass produces Tom &amp; &lt;b&gt;&quot;ok&quot;&lt;/b&gt;. The original length is 17. One & grows from 1 character to 5, which adds 4. Two < each grow from 1 to 4, which adds 6. Two > each grow from 1 to 4, which adds 6. Two double quotes each grow from 1 to 6, which adds 10. The added length is 4 + 6 + 6 + 10 = 26, and 17 + 26 = 43. The text node shows the original 17 characters, and the parser creates 0 elements from it. S has four apostrophes and no ampersand, less-than, or greater-than. Each apostrophe grows from 1 to 5, which adds 4, and four of them add 16. Then 11 + 16 = 27. The encoded text is &#39; OR &#39;1&#39;=&#39;1.

Content-Security-Policy, the CSP header, gates what the browser will load. This service sends Content-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'. The page origin is https://ledger.example. script-src 'self' allows a script whose scheme, host, and port match that origin, and it has no 'unsafe-inline', so an inline script is refused. https://ledger.example/app.js is allowed. https://cdn.example/app.js has another host and is refused. Of those three attempts, 1 is allowed and 2 are refused. default-src 'self' covers a stylesheet, so https://ledger.example/app.css is allowed and https://cdn.example/app.css is refused. object-src 'none' refuses every object. base-uri 'self' keeps the base URL on this origin. Strict-Transport-Security, the HSTS header, is Strict-Transport-Security: max-age=31536000. A day is 86400 seconds, and 31536000 / 86400 = 365. After an HTTPS response brings that header, the browser keeps the host ledger.example on HTTPS for 365 days. The header names no includeSubDomains flag.

The object id and the owner column

Insecure direct object reference, IDOR, is the failure you get when the path carries an object id and the handler uses that id while skipping the owner column. Broken object level authorization, BOLA, is the name the API security list uses for that same gap. The request already carries a verified sub and a role. A role cell can allow a clerk to read an entry and still say nothing about which row. The object rule sits beside that cell. An admin may read or write any id. Anyone else may read or write entries.id = N only when that row's owner_id equals sub. A denied read returns an empty body and leaves amount_cents out of the response. A denied write leaves the stored amount_cents as it was.

u17 is a clerk. GET /entries/11 reads row 11 because owner_id u17 equals sub u17, and the response includes amount_cents 2500. GET /entries/12 meets owner_id u18. u18 differs from u17, so the body is empty and 9000 is withheld. u20 is an admin. GET /entries/12 by u20 returns amount_cents 9000. PATCH /entries/12 with amount_cents 1 and csrf 9c2e, sent by u17, fails the object rule, and row 12 stays 9000. The same PATCH on /entries/11, with csrf 9c2e and amount_cents 2600, matches owner_id u17, so the stored amount becomes 2600. The difference 2600 - 2500 = 100 is the only change that write makes.

A root, a walk, and a prefix

Path traversal is a file name that climbs out of the directory the account may use. The root for sub u17 is the 15-character string /var/ledger/u17. The root for u18 is /var/ledger/u18. The check has five steps. Step 1 rejects the input when it is empty, when it starts with /, when it contains a backslash, or when it contains the two-character sequence dot-dot. That last test rejects a..b.txt as well as ../u18/notes.txt. Step 2 runs only after step 1 passes, and it sets path to the root, a slash, and the input. Step 3 splits path on /, drops empty pieces, and walks from the left. A piece that is a single dot leaves the stack unchanged. A piece dot-dot pops one piece from a non-empty stack, and an empty stack stays empty. Any other piece is pushed. Step 4 joins the stack with / and puts / at the front. That string is the normalized path. Step 5 allows the file only when the normalized path equals the root, or starts with the root followed by /.

The input may/rent.txt passes step 1. The path is /var/ledger/u17/may/rent.txt. The walk pushes var, ledger, u17, may, and rent.txt. The normalized path is /var/ledger/u17/may/rent.txt. It starts with /var/ledger/u17/, so step 5 allows it. The input ../u18/notes.txt contains dot-dot, so step 1 rejects it and the later steps stay idle. The input may/../../u18/secret.txt also contains dot-dot, so step 1 rejects it. If step 1 were removed, the walk on /var/ledger/u17/may/../../u18/secret.txt would push var, then ledger, then u17, then may, then pop may, then pop u17, then push u18, then push secret.txt. The stack would finish as var, ledger, u18, secret.txt, and the normalized path would be /var/ledger/u18/secret.txt. After the shared prefix /var/ledger/u the next character is 8 on that path and 1 on the root, so the path neither equals /var/ledger/u17 nor starts with /var/ledger/u17/. Step 5 would deny it. The input may/./rent.txt has no dot-dot and no leading slash. The single dot leaves the stack unchanged, and the normalized path is again /var/ledger/u17/may/rent.txt, which step 5 allows. The input /etc/passwd starts with /, so step 1 rejects it.

The token on a write

A session stores one CSRF token. CSRF, cross-site request forgery, is a write that arrives with the caller's session and without a token the caller placed on that write. The cookie may already be present, for example on a same-site POST. The compare still runs, and it asks only whether the field csrf equals the token stored for this session. A token stored for a different session fails the compare. u17 stores 9c2e. u18 stores ab10. POST, PATCH, and DELETE must bring the field. GET skips it. A failed compare stops the request before the allow-list and the owner rule, and the row stays as it was. A match lets those later checks run.

POST /entries/11/note from u17 with csrf 9c2e and note May rent matches the token, passes the allow-list, and matches owner_id u17, so 1 row changes. The same POST with csrf 9c2f differs in the last character and changes 0 rows. The same POST with the field missing changes 0 rows. POST /entries/12/note from u18 with csrf 9c2e fails the compare, because u18 stores ab10, and 9c2e is u17's token. POST /entries/12/note from u18 with csrf ab10 matches the token. The note still has to pass the allow-list, and row 12 has owner_id u18, so that write can change the note. GET skips the token and still runs the owner rule.

An example

Entry 11, checked by hand

Start from the three rows (11, u17, 2500, rent), (12, u18, 9000, rent), and (13, u19, 400, tax). The caller is u17, a clerk, the session token is 9c2e, and the root is /var/ledger/u17. POST /entries/11/note carries csrf 9c2e and the note May rent. The token matches. The length is 8 and the characters pass the allow-list. The bound write UPDATE entries SET note = ? WHERE id = ? AND owner_id = ? runs with May rent, 11, and u17. It changes 1 row. Row 11 is now (11, u17, 2500, May rent). Rows 12 and 13 stay put, so the three amount_cents values still add to 11900.

A second POST carries the same csrf 9c2e and the note S, the 11-character attack string. The apostrophe fails the allow-list, and the write changes 0 rows. Row 11 stays May rent. The glued lookup, written out so the row count can be recomputed, would read WHERE owner_id = 'u17' AND note = '' OR '1'='1', which is true for all 3 rows and would sum the amounts to 11900. The bound lookup with the values u17 and S matches 0 stored notes. Rendering S as a text node encodes four apostrophes and yields &#39; OR &#39;1&#39;=&#39;1, length 27, and the node creates 0 elements.

GET /entries/11 as u17 returns amount_cents 2500 because owner_id matches sub. GET /entries/12 as u17 returns an empty body, and 9000 stays unsent. GET /entries/12 as admin u20 returns 9000. PATCH /entries/12 to amount_cents 1, with csrf 9c2e, from u17, is denied by the owner rule, and row 12 stays 9000. The file may/rent.txt normalizes to /var/ledger/u17/may/rent.txt and is allowed. The file ../u18/notes.txt is rejected at step 1. Among https://ledger.example/app.js, https://cdn.example/app.js, and an inline script, the counts are 1 allowed and 2 refused. The HSTS max-age 31536000 is 365 days.

Exercise

Score a fresh batch on paper

Start each decision from the original three rows, with notes rent, rent, and tax. Caller u18 is a clerk, the session token is ab10, and the root is /var/ledger/u18. For POST /entries/12/note with csrf ab10 and note May-rent, 2024., count the note length, say whether the allow-list passes, and say how many rows change and what note and amount_cents row 12 then has. For POST /entries/11/note with csrf ab10 and note May rent, say how many rows change. For POST /entries/12/note with csrf 9c2e and note May rent, say how many rows change. Encode Tom & <b>"ok"</b> in one pass and give the encoded length and the element count. Decide GET /entries/11 as u18, GET /entries/12 as u18, and PATCH /entries/13 setting amount_cents to 50 with csrf ab10 as u18. Say what amount_cents remains on row 13. Under the u18 root, decide may/./rent.txt, a..b.txt, and /etc/passwd, and write the normalized path for the one step 5 can allow. A note of 41 hyphens arrives with a matching token. Say what the length check does. Of the three script attempts in the worked header, how many are refused, and how many days is max-age 31536000?

May-rent, 2024. has length 15: May-rent is 8, the comma is 1, the space is 1, 2024 is 4, and the period is 1. Every character is in the set, the token ab10 matches u18, and owner_id on row 12 equals u18, so the allow-list passes and the write changes 1 row. The note on row 12 becomes May-rent, 2024. and amount_cents stays 9000. The POST to id 11 matches the token and the allow-list, then the owner u17 differs from sub u18, so 0 rows change and row 11 stays rent. The POST with csrf 9c2e fails the compare against ab10 and changes 0 rows. One pass on the 17-character note produces Tom &amp; &lt;b&gt;&quot;ok&quot;&lt;/b&gt;, length 43, and the text node creates 0 elements. GET /entries/11 as u18 is empty because owner u17 differs from u18, so 2500 is withheld. GET /entries/12 returns 9000. The PATCH matches ab10 and then fails the owner rule, because owner u19 differs from u18, so row 13 stays 400. may/./rent.txt is allowed and normalizes to /var/ledger/u18/may/rent.txt. a..b.txt is rejected at step 1 because it contains dot-dot. /etc/passwd is rejected at step 1 because it starts with /. A note of 41 hyphens uses an allowed character and still fails the length check, which accepts 1 through 40. The header refuses 2 of the 3 script attempts, and the max-age is 365 days.

Check questions

Answer on your own first. Then open a question to see the answer.

The glued lookup places S between the template apostrophes. How many rows come back, and what do their amount_cents add to? How many rows does the bound lookup return for S when the stored notes are rent, rent, and tax?

3 rows, and the amounts add to 11900. The bound lookup returns 0.

One pass encodes S and the 17-character note Tom & <b>"ok"</b>. What are the two encoded lengths, how many elements does that text node create, and what does a second pass turn &amp; into?

S encodes to length 27. The 17-character note encodes to length 43 and creates 0 elements. A second pass turns &amp; into &amp;amp;.

Clerk u17 requests GET /entries/12, and admin u20 requests the same path. What does each response do with the amount 9000? After u17 sends PATCH /entries/12 with csrf 9c2e and amount_cents 1, what is stored on row 12?

The clerk response is empty and withholds 9000. The admin response returns 9000. The stored amount stays 9000.

Under the root /var/ledger/u17, what is the normalized path of may/./rent.txt? Which step rejects a..b.txt, which step rejects /etc/passwd, and how many days is the HSTS max-age 31536000?

The normalized path is /var/ledger/u17/may/rent.txt. Step 1 rejects both inputs. The max-age is 365 days.

Take with you

A write changes a row only when the CSRF field matches that session's token and the allow-list accepts the note. The bound update then needs owner_id equal to sub, while an admin may name any id. A page shows the note after one encoding pass, a read sends amount_cents only when the owner rule passes, and a file opens only when step 5 accepts the normalized path.

Final task

Submit the patched application and security header configuration; the test runner executes 40 automated exploit payloads (SQLi, XSS, CSRF, SSRF, IDOR, path traversal) verifying 100% are blocked while all 20 functional tests pass.

The final task is marked against fixed answer keys and automated tests once the tests open.

Terms in this course

8 terms in the glossary belong to this course.

More lessons coming

The first lesson is open to read. The rest of the course is being written, and tests and diplomas come later. Join the waitlist