CAS-003 · Question #232
A web developer has implemented HTML5 optimizations into a legacy web application. One of the modifications the web developer made was the following client side optimization…
The correct answer is C. Cookies should be scoped to a relevant domain/path. Storing session cookies in localStorage exposes them to persistent JavaScript access; scoping cookies to the correct domain and path limits which cookie values appear in document.cookie and therefore what is exposed in client-side storage.
Question
A web developer has implemented HTML5 optimizations into a legacy web application. One of the modifications the web developer made was the following client side optimization:
localStorage.setItem("session-cookie", document.cookie); Which of the following should the security engineer recommend?
Options
- ASessionStorage should be used so authorized cookies expire after the session ends
- BCookies should be marked as "secure" and "HttpOnly"
- CCookies should be scoped to a relevant domain/path
- DClient-side cookies should be replaced by server-side mechanisms
How the community answered
(32 responses)- A3% (1)
- B9% (3)
- C72% (23)
- D16% (5)
Why each option
Storing session cookies in localStorage exposes them to persistent JavaScript access; scoping cookies to the correct domain and path limits which cookie values appear in document.cookie and therefore what is exposed in client-side storage.
Switching to sessionStorage would cause the copied cookie data to expire when the browser tab closes, but the session cookie value is still readable by JavaScript during the session and remains fully vulnerable to XSS-based exfiltration within that window.
Marking cookies HttpOnly and Secure are valuable hardening measures - HttpOnly prevents JavaScript from reading the cookie via document.cookie and Secure restricts transmission to HTTPS - but neither attribute directly addresses the scoping problem of which cookies are exposed to the current page context and stored in localStorage.
The Domain and Path attributes on the Set-Cookie header control which cookies the browser makes visible to a given page through document.cookie - by scoping cookies tightly to only the relevant domain and path, the developer ensures that the minimum necessary cookies are readable by JavaScript, directly limiting what sensitive data ends up persisted in localStorage and reducing the cross-context exposure risk.
Migrating to server-side session mechanisms is a comprehensive architectural change that would eliminate client-side cookie exposure entirely, but the recommendation requested here is scoped to the specific client-side optimization already implemented, not a full re-architecture of the session model.
Concept tested: Cookie domain and path scoping to limit client-side exposure
Source: https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html
Topics
Community Discussion
No community discussion yet for this question.