Skip to lesson

Practice: security basics

A review of this module. Skip it if you feel confident; the next module does not need it.

Fix the mistakes

The slips that cost marks in this module. Spot each one, then check the fix.

Four slips cause most beginner security bugs: the wrong status code, the wrong tool for the data, trusting what the client sends, and a vague explanation in review. Try each one here.

Mistake 1: mixing up 401 and 403

Five ShopLite requests are refused. Quick test for each: would logging in again change the answer? If yes, it is 401; if no, it is 403.

Example 1 · Five ShopLite requests, five status codes

Text
1  GET    /api/v1/me/orders        no Authorization header2  GET    /api/v1/me/orders        token that expired 10 minutes ago3  DELETE /api/v1/products/3       Asha's valid customer token4  GET    /api/v1/orders/14        Asha's valid token; order 14 is Ravi's5  POST   /api/v1/auth/login       correct email, wrong password
The right answers
1  401  nobody is known yet; WWW-Authenticate: Bearer
2  401  an expired token is not a valid credential; logging in again fixes it
3  403  Asha is known, but deleting products needs ADMIN
4  404  owner check fails; 404 hides that order 14 exists
5  401  the login itself failed: "Email or password is wrong."

What this code does

  • Requests 1, 2 and 5 have no valid credential, so they are 401; a new login would fix each of them. Request 3 has a valid credential, and no login would give Asha the ADMIN role, so it is 403.
  • Request 4 is an access-control decision too, but ShopLite answers 404 for other customers' orders so the response does not confirm that order 14 exists. The Roles & Permissions module builds this owner check.

The mix-up has real effects. A mobile app usually opens the login screen on 401, so a wrong 401 sends a signed-in user back to log in for no reason, and a wrong 403 tells a signed-out user "not allowed" when they only needed to log in. For another customer's order, many APIs answer 404, so they do not even confirm that the order exists.

Mistake 2: the wrong tool for the data

A teammate pasted an "encrypted" key from a config file. The question that decides the tool: what will I do with this data later?

Example 2 · An "encrypted" key that was only encoded

Terminal
# A config file pushed to a public repository says the key is "encrypted":#   payment.apiKey=c2hvcGxpdGUtZGVtby1rZXktMjAyNg==printf 'c2hvcGxpdGUtZGVtby1rZXktMjAyNg==' | base64 -d
Output
shoplite-demo-key-2026

What this code does

  • The trailing == is a strong sign of Base64. One command turns it back into the real key, so anyone who finds the file now holds it.
  • The fix has two parts: never commit keys at all (read them from environment variables or an uncommitted file), and replace this key at once, because it has already been exposed.

If you must read it back, encrypt it, with the key kept out of the code and the database. If you only need to check it matches, hash it, slow and salted for passwords. If you only need a safe format, encode it, and never treat that as protection. The most common wrong choice is Base64 used to "hide" a password, an API key or an email inside a reset link: it has no key, so the first person who looks can decode it.

Mistake 3: trusting values the client controls

Anything in a request can be changed by the caller. In each request below, find the value the server wrongly trusts.

Example 3 · Four requests, four trusted values

Text
a)  GET /api/v1/orders/15    Authorization: Bearer <Asha's token>          server returns any order by idb)  GET /api/v1/products?sort=price desc    server builds: "... ORDER BY " + sort         text joined into SQLc)  POST /api/v1/orders    {"customerId": 2, "addressId": 3, "items": [...]}   sent with Asha's tokend)  POST /api/v1/orders    {"items": [{"productId": 3, "quantity": 1, "unitPrice": 1.00}]}
The trusted value and the fix
a)  A01 broken access control: no owner check -> load only Asha's orders, else 404
b)  A05 injection: client text in SQL -> map allowed sort names to fixed columns
c)  A01: customerId from the body -> take the customer from the token (/api/v1/me/orders)
d)  trusting a client price -> ignore it; read the price from the products table

What this code does

  • In each request one value came from the client and was used as if the server had decided it. The fixes all move that decision back to the server: the owner from the token, sort columns from an allow-list, prices from the database.
  • Request d is not a famous attack name, but it is the same mistake. A real ShopLite order already ignores client prices, because unitPrice is copied from the product row when the order is placed.

A hidden form field, a disabled button or a mobile app that "never sends that" protects nothing, because tools like curl can send any request. Look for an id the server does not owner-check, text it joins into SQL, a customerId or role it uses instead of the token, or a price it accepts instead of reading its own.

Interview drill: two minutes per idea

A good two-minute answer has four parts, in this order: what happens, why it is dangerous, how to stop it, and one short example.

Example 4 · A two-minute answer: "What happens when a password database leaks?"

Text
WHAT     Attackers copy the users table and try to recover the passwords.WHY      People reuse passwords, so one leak opens their other accounts too         (credential stuffing).HOW      Never store passwords, only slow salted hashes (BCrypt or Argon2).         Salts stop precomputed tables; slowness makes each guess expensive.         Also: refuse common passwords, limit login attempts, offer a second factor.EXAMPLE  Two users with campus123 get two different BCrypt values, and each         guess takes about half a second, so cracking the table is very slow.
Spoken answer (about 120 words)
"If a password table leaks, attackers try to turn the stored values back into passwords, and because many people reuse passwords, one leak can open their email and UPI accounts too. That is why a server never stores the password itself. It stores a slow, salted hash such as BCrypt. The salt is a random value per user, so two people with the same password get different hashes and precomputed tables are useless. The slowness means each guess costs a fraction of a second, so testing millions of guesses per user takes far too long. On top of that, I would refuse very common passwords at sign-up, limit failed logins, and offer a second factor."

What this code does

  • The frame keeps the answer in a fixed order, so you never jump to tools before explaining the danger. Interviewers listen for the why as much as for the names BCrypt and salt.
  • Practice items 7 and 8 ask for the same kind of answer about SQL injection and HTTPS. Say them out loud: written answers always sound shorter than spoken ones.

Practise three answers out loud with a timer: a password leak, SQL injection and HTTPS. Keep each to about 150 to 250 spoken words, with plain words first and the technical name second, such as "the input becomes part of the query, which is called SQL injection".

Module quiz

Questions from the whole module. Your first answer is your score.

Problems

Graded easy to hard, using everything from this module.

Try each one yourself first. Use the hint if you get stuck. Mark it as done when it works.

Exercise 1· Easy

Write the status code for each ShopLite request and a short reason:

  • (a) GET /api/v1/products/3 with no token
  • (b) PATCH /api/v1/products/3 with no token
  • (c) PATCH /api/v1/products/3 with Asha's customer token
  • (d) GET /api/v1/me/orders with a token whose last character was changed
  • (e) GET /api/v1/customers with the admin's token
Expected output
(a) 200: reading the catalogue is public
(b) 401: changing a product needs a login first
(c) 403: Asha is known, but only admins may change products
(d) 401: an edited token is not a valid credential
(e) 200: admins may list customers

Exercise 2· Easy

A teammate pasted payment.secret=YWRtaW46YWRtaW4= from a config file. Decode it and write what it reveals and what to do.

Expected output
admin:admin
It is a user name and password, only Base64-encoded, and the default admin:admin as well. Change the password at once, remove it from the file and from Git history, and read secrets from environment variables.

Exercise 3· Medium

Choose encryption, a slow password hash, a fast hash, an HMAC or signature, encoding, or "do not store" for each, with a reason:

  • (a) a student's Aadhaar number that must be shown masked and verified later
  • (b) the hostel Wi-Fi password users choose
  • (c) an email-verification token sent in a link
  • (d) a file attached inside a JSON body
  • (e) a token that says "customer 1, role CUSTOMER" given to the app
  • (f) the CVV of a debit card
Expected output
(a) encryption: it must be read back later
(b) slow password hash: people choose it, and it is only checked
(c) fast hash of a long random token: only compared; random values cannot be guessed from a list
(d) encoding (Base64): format only
(e) HMAC or signature: the server must detect edits
(f) do not store: card rules forbid keeping the CVV after the payment

Exercise 4· Medium

A ShopLite reviewer found these status codes. Find the two wrong ones, correct them and explain what each mistake does to a mobile app.

Text
No token on DELETE /api/v1/products/3           -> 403Expired token on GET /api/v1/me/orders           -> 401Asha's token on GET /api/v1/customers            -> 401Admin token on DELETE /api/v1/products/3         -> 204
Expected output
Wrong: no token on DELETE → should be 401. With 403 the app shows "not allowed" instead of opening the login screen, so the user never gets a chance to sign in.
Wrong: Asha's token on GET /customers → should be 403. With 401 the app throws a signed-in customer back to the login screen, and logging in again still fails.
The other two are correct.

Exercise 5· Medium

For each request or query, name the value the server wrongly trusts, the risk, and the fix.

Text
a) POST /api/v1/auth/register  {"email": "kabir@example.com", "password": "...", "role": "ADMIN"}b) GET  /api/v1/me/orders?customerId=4c) SELECT * FROM orders WHERE status = '" + status + "'d) GET  /api/v1/orders/9   with Asha's token (order 9 is Divya's)
Expected output
a) role in the body: anyone can sign up as an admin (A01) → ignore it; every sign-up gets CUSTOMER, admins are made by an admin.
b) customerId in the query: a /me URL must use the token's customer (A01) → drop the parameter.
c) status joined into SQL: injection (A05) → a parameter, and check status is one of the four values.
d) the order id without an owner check: IDOR (A01) → load the order only if it is Asha's, else 404.

Exercise 6· Medium

A college fee-payment app stores passwords with MD5, sends logins over http://, puts the token in the URL and prints all headers to the log. List each problem with its OWASP 2025 category and the fix, most serious first.

Expected output
1. Logins over plain HTTP: passwords and tokens readable on the network (A04) → HTTPS everywhere, redirect HTTP, HSTS.
2. MD5 password hashes: fast and broken, leaked hashes are cracked quickly (A04) → BCrypt or Argon2 with a salt.
3. Token in the URL: leaks through history and logs (A07) → Authorization header only, short expiry.
4. Headers in the log: tokens sit in log files (A09) → mask Authorization and never log secrets.

Exercise 7· Hard

Interview drill: write a two-minute answer (about 150 words) to "What is SQL injection and how do you prevent it?" using the what, why, how, example frame.

Expected output
What: SQL injection happens when text from a user is joined into a SQL query, so the text can change what the query does instead of staying a plain value.
Why: the database runs whatever query it receives, so an attacker can read other users' data, skip a login check or change rows. It is one of the most damaging web risks (A05 Injection).
How: use parameterised queries everywhere: the SQL text is fixed with ? slots and the values travel separately, so they can never become SQL. For parts that cannot be parameters, such as a sort column, map the client's choice to a fixed allow-list. Give the database user only the permissions it needs.
Example: a product search that builds "... WHERE name LIKE '%" + q + "%'" is vulnerable; the same search with "WHERE name LIKE ?" and the value set as a parameter is safe, whatever the user types.

Exercise 8· Hard

Interview drill: write a two-minute answer (about 150 words) to "Why does an API need HTTPS, and what does it protect?" using the what, why, how, example frame.

Expected output
What: HTTPS is HTTP inside TLS, which encrypts the connection, detects any change to it and proves the server's identity with a certificate signed by a trusted authority.
Why: over plain HTTP every machine on the path, such as a public Wi-Fi or a router, can read and change the traffic. An API sends a password at login and a bearer token on every request, and a bearer token works for whoever holds it.
How: serve the API only over HTTPS, redirect plain HTTP, send HSTS so browsers never use HTTP, and keep tokens in the Authorization header, short-lived and out of logs. TLS does not hide which server you call and does not fix app bugs, so access checks are still needed.
Example: on station Wi-Fi, a plain HTTP login shows the email, password and token in readable text; over HTTPS the same eavesdropper sees only the server name and encrypted bytes.

Interview questions

Questions interviewers ask on this topic.

Finish up

Mark complete & continue

Next: Mini project: ShopLite threat model