Web Programming Security Web Programming 3 — Questions and Answers
Question 1: What does the `SameSite=Strict` cookie attribute do?
- Encrypts the cookie value
- Sends the cookie only over HTTPS
- Prevents the cookie from being sent on cross-site requests (Correct answer)
- Restricts the cookie to a single path
Correct answer: Prevents the cookie from being sent on cross-site requests
SameSite=Strict prevents the browser from sending the cookie with any cross-site request, providing strong CSRF protection.
Question 2: Which vulnerability is exploited when an attacker manipulates a URL parameter like `?file=../../../../etc/passwd`?
- Open Redirect
- Path Traversal (Correct answer)
- SSRF
- Clickjacking
Correct answer: Path Traversal
Path traversal (directory traversal) uses ../ sequences to escape the intended directory and access arbitrary files on the server.
Question 3: A web app forwards users to a URL from the query string without validation: `redirect?url=https://evil.com`. What is this?
- SSRF
- Open Redirect (Correct answer)
- Reflected XSS
- HTTP Response Splitting
Correct answer: Open Redirect
An open redirect allows attackers to redirect users to arbitrary external URLs, often used for phishing.
Question 4: Which of the following is the correct way to prevent SQL injection in most languages?
- Escape all quotes with backslashes
- Use parameterized queries or prepared statements (Correct answer)
- Limit query length to 256 characters
- Run queries as a read-only database user
Correct answer: Use parameterized queries or prepared statements
Parameterized queries separate SQL code from data, so user input is never interpreted as SQL regardless of its content.
Question 5: What is the role of a nonce in a Content Security Policy?
- It encrypts the script payload
- It allows specific inline scripts while blocking others (Correct answer)
- It replaces the need for HTTPS
- It rotates the session token on each request
Correct answer: It allows specific inline scripts while blocking others
A CSP nonce is a random per-request token added to allowed script tags, letting specific inline scripts run while blocking all others.
Question 6: Which attack involves a malicious website causing an authenticated user's browser to perform unintended actions on another site?
- XSS
- Clickjacking
- CSRF (Correct answer)
- Session Fixation
Correct answer: CSRF
Cross-Site Request Forgery (CSRF) tricks an authenticated user's browser into sending requests to a site where they're logged in, exploiting cookie-based auth.
Question 7: An API returns detailed database error messages to the client. Which security principle does this violate?
- Least privilege
- Defense in depth
- Fail securely / minimize information exposure (Correct answer)
- Separation of duties
Correct answer: Fail securely / minimize information exposure
Exposing internal error details violates the principle of minimizing information exposure, giving attackers insights into the database schema and query structure.
What does the `SameSite=Strict` cookie attribute do?