nodejs / nodejs/undici

Pool and Client don't follow redirects to a different host

Open
#1,744 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Description

When a request is made for a URL that returns a redirect to another host, Pool and Client do follow the redirect but do not change the destination host.
The request goes to the same base host as the Pool, but with the path changed.

Reproducible By

import http from 'node:http';
import { promisify } from 'node:util'
import { Pool, request } from "undici";

// Server that does the work
const requestListener1 = function (req, res) {
  res.writeHead(200);
  res.end('Hello, World!');
}
const server1 = http.createServer(requestListener1);
const listen1 = promisify(server1.listen.bind(server1))
await listen1(5100)

// Server that redirects to server1
const requestListener2 = function (req, res) {
  if (req.url === "/redirect") {
    res.writeHeader(303, {
      'location': 'http://localhost:5100/newpath'
    })
    res.end();
  } else {
    console.log("Path", req.url)
    res.writeHead(200);
    res.end('Should never happen');
  }
}
const server2 = http.createServer(requestListener2);
const listen2 = promisify(server2.listen.bind(server2))
await listen2(5101)

// Using request everything works as expected
const { body: bodyRequest } = await request("http://localhost:5101/redirect", { method: "GET", maxRedirections: 1 })
const textFromRequest = await bodyRequest.text()

// This line prints "Hello, World!"
console.log(`Request: ${textFromRequest}`)

// Using Pool or Client it does not update the host
const pool = new Pool("http://localhost:5101")
const { body: bodyPool } = await pool.request({ method: "GET", path: "/redirect", maxRedirections: 1 })
const textFromPool = await bodyPool.text()

// This line prints "Should never happen"
console.log(`Pool: ${textFromPool}`)

server1.close()
server2.close()  

Expected Behavior

Calls made with Pool or Client returns the same result as request.

Environment

macOS - node v18.12.0

Additional context

I don't know if it's a bug or this behavior is expected.
My scenario is that I have an API that returns a redirect to an AWS S3 url, and I use maxRedirections to directly receive the file stream.
If this is an expected behavior, is there any way to use a Pool in cases like this?

Thanks!

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 by comparing redirect handling in Pool and Client with the working request path, using the supplied two-server reproduction to observe the destination host. Done means Pool and Client follow a redirect to a different host and return the same result as request when maxRedirections is set.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.