Since 15.20.0, a cookie rotated by cy.request() is overwritten by the stale server-side cookie jar value on the next AUT navigation (session lost)
Current behavior
Since Cypress 15.20.0, a cookie that the server rotates in response to cy.request() is not the cookie the browser sends on the next AUT-frame navigation. The stale value stored in Cypress's server-side cookie jar wins.
cy.visit('/'): the document response setssid=anon-1; Path=/; HttpOnly; SameSite=Lax.cy.request('POST', '/login'): the server rotates the session and repliesSet-Cookie: sid=auth-1. Cypress writes it to the browser, andcy.getCookie('sid')reportsauth-1.- Any AUT-frame navigation that follows (
cy.reload(), clicking a link, submitting a form, a download link,win.location.href = ...) reaches the server withCookie: sid=anon-1. The user is anonymous again and gets redirected to the login page.
fetch/XHR issued by the page still carry auth-1; only AUT-frame document requests are affected (captured with cy.intercept req.headers.cookie).
The result is the same in Chrome on the native browser network (16.x), in Chrome with forceHttp1: true, and in Electron, so it is independent of the 16.0 network change.
In our suite (Shoptet e-commerce platform, ~650 e2e tests) every test that logs in via cy.request() and then navigates started failing on 15.20.0+ with "redirected to login". Wrapping the login in cy.session() avoids it, because cy.session() clears the jar.
Desired behavior
The cookie the browser holds after cy.request() (the one cy.getCookie() reports) is what the server receives on the next navigation. Either cy.request() should update the server-side jar together with the browser, or jar cookies should not overwrite same-named cookies the browser already sent for same-origin AUT-frame requests.
Test code to reproduce
Self-contained, no dependencies. Run node server.js, then cypress run -b chrome in the project dir.
server.js:
// Session cookie rotates on POST /login; GET /account requires the rotated one.
const http = require('http');
const sessions = new Set();
const parseCookies = (header = '') =>
Object.fromEntries(header.split(';').map((s) => s.trim()).filter(Boolean).map((s) => [s.slice(0, s.indexOf('=')), s.slice(s.indexOf('=') + 1)]));
http.createServer((req, res) => {
const cookies = parseCookies(req.headers.cookie);
if (req.method === 'GET' && req.url === '/') {
const headers = { 'Content-Type': 'text/html' };
if (!cookies.sid) headers['Set-Cookie'] = `sid=anon-${Date.now()}; Path=/; HttpOnly; SameSite=Lax`;
res.writeHead(200, headers);
return res.end('<a id="account" href="/account">account</a>');
}
if (req.method === 'POST' && req.url === '/login') {
const sid = `auth-${Date.now()}`;
sessions.add(sid);
res.writeHead(302, { 'Set-Cookie': `sid=${sid}; Path=/; HttpOnly; SameSite=Lax`, Location: '/account' });
return res.end();
}
if (req.url === '/account') {
if (sessions.has(cookies.sid)) { res.writeHead(200, { 'Content-Type': 'text/html' }); return res.end('<h1 id="ok">logged in</h1>'); }
res.writeHead(302, { Location: '/login' });
return res.end();
}
if (req.url === '/login') { res.writeHead(200, { 'Content-Type': 'text/html' }); return res.end('<h1 id="login">login page</h1>'); }
res.writeHead(404); res.end();
}).listen(4567);cypress.config.js:
module.exports = {
e2e: { baseUrl: 'http://localhost:4567', supportFile: false, video: false, screenshotOnRunFailure: false },
};cypress/e2e/rotated_cookie.cy.js:
describe('cookie rotated by cy.request', () => {
it('is what the browser sends on the next navigation', () => {
cy.visit('/');
cy.request('POST', '/login');
cy.getCookie('sid').its('value').should('match', /^auth-/); // passes: Cypress believes the browser has the rotated cookie
cy.reload();
cy.get('#account').click(); // AUT-frame navigation
cy.location('pathname').should('eq', '/account'); // fails on >= 15.20.0: the server received the stale anon-* cookie
cy.get('#ok');
});
});Results with the exact files above, Chrome 153, Node 24.14.0:
| Cypress | Result |
|---|---|
| 15.10.0, 15.16.0, 15.17.0, 15.19.0 | pass |
| 15.20.0, 15.21.0, 16.0.0, 16.1.0 | fail (expected '/login' to equal '/account') |
Cypress Version
16.1.0 (also 16.0.0, 15.21.0, 15.20.0). Last good: 15.19.0.
Node version
v24.14.0
Operating System
macOS 26.5 (local), Linux cypress/browsers:node-24.21.0-chrome-153.0.8010.36-1 (CI). Browsers: Chrome 153 headless, Electron 146, Chrome 153 with forceHttp1: true.
Debug Logs
No response
Other
Bisect points at #34287 (15.20.0, closes #29719). It changes shouldAttachAndSetCookies() in packages/proxy/lib/http/util/cookies.ts to return !!isAutFrame when there is no AUT URL yet, so the first visit's Set-Cookie now lands in the server-side tough-cookie jar.
From there:
doesTopNeedToBeSimulated()(packages/proxy/lib/http/util/top-simulation.ts) is true for every AUT-frame request because of|| ctx.req.isAUTFrame, soattachCrossOriginCookiesruns for same-origin navigations too.addCookieJarCookiesToRequest()inpackages/proxy/lib/http/util/cookies.tsapplies "Always have cookies in the jar overwrite cookies in the request if they are the same", so the jar's stale value replaces the browser's rotated one.cy.request()(packages/server/lib/request.ts,setCookiesOnBrowser) syncs the response cookies through theset:cookieautomation only.packages/server/lib/automation/automation.tstouchescookieJaronclear:cookie/clear:cookiesbut not onset:cookie, so the jar never learns about the rotation.
Workaround we use in the meantime: overwrite cy.request so that, for every cookie name the response set, it calls Cypress.automation('clear:cookie', { name, domain, path }) (removes the cookie from browser and jar) followed by Cypress.automation('set:cookie', cookieAsReadFromBrowser).
Source: cypress-io/cypress