nodejs / nodejs/undici

Persist Cookies between redirects

Open
#3,784 6 comments 0 reactions 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

We have an Api that you need to call on endpoint A, it sets cookies and redirects to B (and you cannot call B directly) but undici looses the cookies so the Api call fails.

(Update: both A and B live on the same origins but diffent paths and the cookie path is set on /)

fetch(
        "https://example.com/api",
        {
            method: "POST",
            credentials: 'include', // this guy should probably make this assumption if I understood the spec correctly
            redirect: "follow", // together with him
            headers: {
                "Content-Type": "application/x-www-form-urlencoded",
            },
            body: urlencoded.toString(),
        }
    )

I also considered:

import { fetch as uFetch } from 'undici';
import fetchCookie from 'fetch-cookie';
const fetch = fetchCookie(uFetch);

However on Win11 I want to use SSL certs which is poorly implemented in windows so I have to use this syntax:

const ca = (await import('win-ca')).default;
const rootCAs = []
// Fetch all certificates in PEM format
ca({
    format: ca.der2.pem,
    ondata: buf => rootCAs.push(buf.toString())
})
    
async function uWinCaFetch(url) {
    const dispatcher = new Agent({
        connect: {
            ca: rootCAs, // Pass all root certificates
        }
    });
    return request(url, { dispatcher });
}

But trying to wrap that with fetchCookie like: const fetch = fetchCookie(uWinCaFetch); only becomes:

file:///C:/ws/projects/my-card/node_modules/fetch-cookie/esm/index.js:129
const cookieString = response.headers.get("set-cookie");
                                        ^

TypeError: response.headers.get is not a function
    at getCookiesFromResponse (file:///C:/ws/projects/my-card/node_modules/fetch-cookie/esm/index.js:129:41)
    at fetchCookieWrapper (file:///C:/ws/projects/my-card/node_modules/fetch-cookie/esm/index.js:145:21)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async GET (file:///C:/ws/projects/my-card/test.mjs:62:9)
    at async file:///C:/ws/projects/my-card/test.mjs:92:1

Additional context

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 fetch and request entry points and trace redirect handling, credentials, and response Set-Cookie processing. Reproduce the issue using an endpoint A that sets a cookie and redirects to endpoint B on the same origin; done means the cookie set by A is sent to B during the followed redirect.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
api, backend, networking
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.