Alice logs into the Blogs app using cookie-based authentication. The server generates a session ID (sess_12345) and sends it to her browser in a cookie. Later, a hacker intercepts this cookie because the Blogs app isn’t using HTTPS. What could the hacker do with it?
sess_12345
Crack Alice’s password directly from the cookie
Steal the session ID but be unable to use it due to the HttpOnly flag
Use the session ID to log in as Alice and edit her drafts
Modify the cookie to gain admin access
Access Alice’s session only until the cookie expires
In the Blogs app, the login API route checks Alice’s credentials and sets a cookie like this: session=sess_12345; Secure; HttpOnly; SameSite=Strict. Which of these security measures prevents a malicious script on a fake Blogs page from stealing the cookie?
session=sess_12345; Secure; HttpOnly; SameSite=Strict
HttpOnly flag
Cookie encryption
Secure flag
Session ID expiration
SameSite=Strict flag
Alice uses token-based authentication with JWT in the Blogs app. Her session token expires in 15 minutes, and she sends a refresh token (rf_abc123xyz) to /api/refresh. The server checks the refresh token against its database. What happens if the refresh token was stolen and already used by a hacker?
rf_abc123xyz
/api/refresh
Alice’s browser sends both tokens, causing a conflict
The server ignores the refresh token since it’s not a JWT
The server issues a new session token to Alice anyway
The server rejects Alice’s request if it detects the token was revoked
The hacker’s session continues, and Alice is logged out
Here’s a snippet from the Blogs app’s Next.js API route for login:
import { setCookie } from 'cookies-next'; export default async function handler(req, res) { const { email, password } = req.body; const user = await verifyUser(email, password); // Hashed check if (user) { const sessionId = generateSessionId(); await storeSession(sessionId, user.id); setCookie('session', sessionId, { req, res, secure: true, httpOnly: true }); return res.status(200).json({ message: 'Logged in' }); } return res.status(401).json({ error: 'Invalid credentials' }); }
What does the httpOnly: true option in setCookie protect against?
httpOnly: true
setCookie
The cookie being sent to a different domain
The session expiring too quickly
JavaScript stealing the cookie from the browser
Hackers intercepting the cookie over HTTP
SQL injection in the database
Alice logs into Blogs via OAuth with her Google account. The Blogs server receives an access token and verifies it with Google. What could go wrong if Google’s authentication servers are down?
The Blogs server generates its own JWT instead
Alice’s browser stores the token indefinitely, causing a security risk
Alice can still log in using a cached token
The server uses SAML as a fallback
Blogs rejects her login until Google is back online
In the Blogs app, a company uses SAML for SSO. Alice logs into her company’s IdP (Okta), and Blogs receives this SAML assertion:
<saml:Assertion> <saml:Attribute Name="email" Value="alice@company.com"/> <saml:Attribute Name="role" Value="admin"/> </saml:Assertion>
What can Blogs do with this information?
Reject the assertion if it’s not signed by Okta
Store the assertion as a cookie for future requests
Log Alice in as a regular user, ignoring the role
Use the email to fetch her Google profile
Grant Alice admin access to edit posts
The Blogs app uses JWT with a refresh token workflow. Alice’s session token expires, and her browser sends this request:
POST /api/refresh Cookie: refresh_token=rf_abc123xyz
The server checks its database and finds rf_abc123xyz tied to userId: 1. What should the server do next?
userId: 1
Send Alice’s password back to the browser
Log Alice out automatically
Reject the request if the refresh token expired
Update the refresh token to a new value
Return a new session token if the refresh token is valid
Imagine a hacker tricks Alice into clicking a link that sends a request to Blogs with her cookie (session=sess_12345). The cookie has SameSite=Lax. What happens?
session=sess_12345
SameSite=Lax
Alice’s browser deletes the cookie automatically
The server ignores the cookie due to missing HTTPS
The hacker edits Alice’s drafts if the cookie is still valid
The request fails because SameSite=Lax blocks cross-site requests
The request succeeds because Lax allows GET requests
Comparing security for Blogs: A developer forgets to hash passwords in the database and stores them as plain text (e.g., supersecret). What’s the biggest risk?
supersecret
OAuth stops working with Google
Session tokens expire too quickly
A database leak exposes all user passwords
Hackers can steal cookies and impersonate users
SQL injection deletes the database
Alice uses Blogs on her phone with JWT. Her session token is stored in localStorage, and a malicious app steals it via XSS. What could Blogs have done to prevent this?
localStorage
Switched to SAML for mobile users
Made the session token expire in 5 minutes
Encrypted the token with Alice’s password
Stored the session token in memory instead of localStorage
Used an HTTP-only cookie for the session token
This content is only available to registered users.Please register at the the home page.