nodejs / nodejs/undici

Should fetch pass cookies when following redirects?

Open
#1,640 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
JavaScript
Stars
7.7k
Forks
880
Avg merge
2d 16h
Merged PRs (30d)
68

Description

Software like Postman (if not disabling the cookie jar in the settings) and browser redirects save cookies when they hit a redirect which has a set-cookie header and set them on requests following that redirect by adding a cookie header.

Should Node.js fetch do this too?

When I request the http://localhost:8080/redirect from this code with Postman, it logs the cookie header and in the Postman console you see the cookie was added to the following request. The native fetch that happens in the code doesn't pass the cookie to the /redirect_to url.

import express from 'express';

const app = express();

app.get('/redirect', (req, res, next) => {
	res.cookie('test', 'value');
	res.redirect('/redirect_to');
});

app.get('/redirect_to', (req, res, next) => {
	console.log(`Cookie: ${req.get('cookie')}`);
	res.status(200).end();
});

app.listen(8080, async () => {
	try {
		await fetch('http://localhost:8080/redirect');

		await fetch('http://localhost:8080/redirect', {
			credentials: 'include'
		});
	} catch (err) {
		console.error(err);
	}
});

Here is the minimal reproduction repository:
https://github.com/dsine-de/fetch-redirect-cookies

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal reproduction repository and the fetch calls to /redirect and /redirect_to. Compare the native fetch behavior with the cookie handling described for Postman, then determine the intended redirect-cookie semantics and the tests needed to establish when the behavior is complete.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.