In the Blog application, a guest visits the site, and the server sets a cookie with visitor_id=guest456 using the code below:
visitor_id=guest456
cookies().set('visitor_id', 'guest456', { path: '/', maxAge: 60 * 60 });
Later, the guest refreshes the page. What happens to the visitor_id cookie?
visitor_id
The server overwrites it with a new value, like guest789.
guest789
The browser ignores it since it’s a guest cookie.
The browser sends visitor_id=guest456 back to the server in the request.
The server can’t see it because it lacks HttpOnly.
HttpOnly
The cookie is deleted because the page refreshed.
An admin logs into the Blog application, and the server sets a cookie:
cookies().set('session_id', 'abc789xyz', { httpOnly: true, secure: true, sameSite: 'strict' });
A hacker tries to steal this cookie using a malicious script injected into the page (XSS attack). What prevents the hacker from succeeding?
The sameSite: 'strict' setting stops the cookie from being sent to other sites.
sameSite: 'strict'
The secure flag ensures the cookie is encrypted.
secure
The httpOnly flag blocks JavaScript from accessing the cookie.
httpOnly
The cookie’s value abc789xyz is random and meaningless to the hacker.
abc789xyz
A user sets a preference cookie client-side in the Blog application:
documentcookie='prefs=tag:tech; path=/; max-age=86400';
Later, the server needs to check this cookie. Which of these could go wrong?
The server misreads it because max-age is in seconds, not minutes.
max-age
The cookie might be missing if the user cleared their browser data.
The server can’t see the cookie because it was set client-side.
The cookie exceeds the 4KB size limit and gets truncated.
Look at this server-side code to verify an admin session:
const sessionId = cookieStore.get('session_id')?.value; if (sessionId === 'abc789xyz') { return new Response(JSON.stringify({ message: 'Admin access granted' })); }
What’s a potential problem with this approach in a real application?
Hardcoding abc789xyz means all admins share the same session ID.
It lacks HttpOnly, making the cookie vulnerable.
The server should use document.cookie instead of cookieStore.
document.cookie
cookieStore
It assumes session_id is always present, which might crash the code.
session_id
In the Blog application, a developer decides to store a user’s email directly in a cookie: user_email=jane@example.com. Why might this be a bad idea? (Select all that apply.)
user_email=jane@example.com
The email could be intercepted if not sent over HTTPS.
Cookies have a 4KB size limit, and emails could exceed it.
It’s better to store a random ID and link it to the email server-side.
If the cookie isn’t HttpOnly, scripts could steal the email.
The server can’t read emails stored in cookies.
A student modifies the guest cookie code:
cookies().set('visitor_id', 'guest456', { path: '/blog', maxAge: 60 * 60 });
What changes when the user navigates the Blog application?
The cookie lasts longer than the original 1-hour setting.
The server rejects the cookie due to the path restriction.
path
The cookie is only sent for requests under /blog, not the whole site.
/blog
The browser overwrites it with a site-wide cookie on the next request.
This content is only available to registered users.Please register at the the home page.