Skip to content

Redirect handler resolves a relative Location against a host read from a path starting with // #6184

Description

@dmonza

Bug Description

RedirectHandler builds the base URL for a redirect with new URL(this.opts.path, requestOrigin). A path such as //users/start is a path on origin, and the first request is sent there, but new URL() reads it as a network-path reference to a host called users. A relative Location is then resolved against that host instead of the origin.

The same expression feeds the redirect-loop history, so a visited URL can be recorded with the wrong host.

Reproduction

Standalone reproduction script:

'use strict'

const { test } = require('node:test')
const assert = require('node:assert')
const { createServer } = require('node:http')
const { once } = require('node:events')
const { Agent, interceptors } = require('undici')

test('a relative Location after a path starting with // stays on the origin', async (t) => {
  const seen = []
  const server = createServer((req, res) => {
    seen.push(req.url)
    if (req.url === '//users/start') {
      res.writeHead(302, { location: '/login' })
      return res.end()
    }
    res.end('ok')
  })
  t.after(() => server.close())

  server.listen(0, '127.0.0.1')
  await once(server, 'listening')

  const agent = new Agent().compose(interceptors.redirect({ maxRedirections: 3 }))
  t.after(() => agent.destroy())

  const { statusCode, body } = await agent.request({
    origin: `http://127.0.0.1:${server.address().port}`,
    path: '//users/start',
    method: 'GET'
  })
  await body.dump()

  assert.strictEqual(statusCode, 200)
  assert.deepStrictEqual(seen, ['//users/start', '/login'])
})

Steps to reproduce (if not using the script above):

Save the script as repro.js and run node --test repro.js.

Expected Behavior

Per RFC 3986, the relative Location resolves against the URL that was requested, so the second request reaches the same server at /login.

Actual Behavior

The second request tries to connect to a host named users and fails.

Logs & Screenshots

Error: getaddrinfo ENOTFOUND users

Environment

  • OS: Windows 11
  • Node.js version: v24.18.0
  • undici version: 8.12.3, and main at 8.12.4

Additional context

This also happens without unusual input: a base URL ending in a slash joined to a path starting with one produces //users.

I have a fix and a test ready, and can open a pull request.


_Note: Since SECURITY.md treats unsanitized input passed to request options as out of scope, I believe a regular issue is appropriate, but please let me know if you see it differently.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions