Skip to content
engineering workbench.by StudyPort Academy
All errors
browser · DEBUG LIBRARY

document.cookie did not do what the jar required

Locate the failed assumption, reproduce it, then repair the cause.

What actually caused it

document.cookie is not one string you replace. Assigning it sets or updates a single cookie. Reading it returns only the name=value pairs that script is allowed to see, joined together. HttpOnly cookies are missing from that string on purpose, so a session id set by the server may exist and still be invisible to your log.

Path, Domain, Secure, SameSite, and Expires change whether the cookie is stored and sent. The default path is the current directory, not always the whole site. Modern browsers treat a missing SameSite as Lax, so a cookie may not be sent on a cross-site post. A Secure cookie is not stored from an insecure page.

Putting a copy of the session in localStorage and also in a script-readable cookie gives script two places to steal it. The network panel shows the Cookie request header the browser actually sent, which can disagree with document.cookie.

Minimal reproduction

A second assignment does not clear the jar

document.cookie = "theme=dark";
document.cookie = "session=abc";
console.log(document.cookie);

Both cookies can remain. The second write adds one cookie.

The fix

Set one cookie per assignment and include the attributes you mean: Path=/, SameSite, and Secure on HTTPS. To remove a cookie, set it again with the same path and an expiry in the past. Do not expect an HttpOnly cookie to appear in document.cookie.

Let the server set session cookies with HttpOnly. Use script-readable cookies only for values script truly needs, and never for a password. When a request omits a cookie, compare the path, the site, and SameSite in the Network tab instead of logging the jar again.

Set the attributes you actually need

document.cookie = "theme=dark; Path=/; SameSite=Lax";
console.log(document.cookie);

HttpOnly cookies set by the server still will not appear here.

Expire the cookie you set

document.cookie = "theme=dark; Path=/; Max-Age=0";

Path must match the cookie you are trying to remove.