Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/node/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Agent extends AgentBase {
const cookies = res.headers['set-cookie'];
if (cookies) {
const url = new URL(res.request?.url || '');
this.jar.setCookies(cookies, url.hostname, url.pathname);
this.jar.setCookies(cookies, url.hostname, null);
}
}

Expand Down Expand Up @@ -82,7 +82,7 @@ for (const name of methods) {
const request_ = new request.Request(method, url);

request_.on('response', this._saveCookies.bind(this));
request_.on('redirect', this._saveCookies.bind(this));
request_.on('pre-redirect', this._saveCookies.bind(this));
request_.on('redirect', this._attachCookies.bind(this, request_));
this._setDefaults(request_);
this._attachCookies(request_);
Expand Down
9 changes: 9 additions & 0 deletions src/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ Request.prototype._redirect = function (res) {
// this is required for Node v0.10+
res.resume();

this._emitPreRedirect(res);

let headers = this.req.getHeaders ? this.req.getHeaders() : this.req._headers;

const changesOrigin = new URL(url).host !== new URL(this.url).host;
Expand Down Expand Up @@ -956,6 +958,13 @@ Request.prototype._emitRedirect = function () {
this.emit('redirect', response);
};

Request.prototype._emitPreRedirect = function (res) {
this.res = res;
const response = new Response(this);
response.redirects = this._redirectList;
this.emit('pre-redirect', response);
};

Request.prototype.end = function (fn) {
this.request();
debug('%s %s', this.method, this.url);
Expand Down
20 changes: 20 additions & 0 deletions test/agent-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,24 @@ describe('Agent', () => {
assert.deepEqual({ hello: 'world' }, res.body);
});
});

it('should assign cookies without domains correctly when following redirects', () => {
const agent = request.agent();

const firstUrl = new URL(base)
firstUrl.hostname = 'first.local'
const first = firstUrl.toString().slice(0, -1)

const secondUrl = new URL(base)
secondUrl.hostname = "second.local"
const second = secondUrl.toString().slice(0, -1)

return agent
.get(`${first}/cookie-cross-domain-redirect`)
.query({ first, second })
.connect('127.0.0.1')
.then((res) => {
assert.equal(res.text, 'first.local=true')
})
})
});
25 changes: 25 additions & 0 deletions test/support/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,31 @@ app.put('/redirect-308', (request, res) => {
res.redirect(308, '/reply-method');
});

app.get('/cookie-cross-domain-redirect', (request, res) => {
const { first, second } = request.query
const hostname = new URL(`http://${request.headers.host ?? request.headers[':authority']}`).hostname
res.cookie(hostname, 'true', {
path: '/',
sameSite: 'lax',
secure: false,
maxAge: 1000
});

res.redirect(303, `${second}/cookie-cross-domain-second-redirect?first=${encodeURIComponent(first)}`);
});

app.get('/cookie-cross-domain-second-redirect', (request, res) => {
const first = request.query.first
const hostname = new URL(`http://${request.headers.host ?? request.headers[':authority']}`).hostname
res.cookie(hostname, 'true', {
path: '/',
sameSite: 'lax',
secure: false,
maxAge: 1000
});
res.redirect(303, `${first}/show-cookies`);
});

app.all('/reply-method', (request, res) => {
res.send(`method=${request.method.toLowerCase()}`);
});
Expand Down