Skip to content

req.subdomains splits an IPv6 host that contains dots #7532

Description

@johnnyhuirilef

Hi! 🙂 req.subdomains splits an IPv6 host that contains dots.

Environment information

Version: express 5.3.0 (4.22.3 gives the same result)

Platform: Darwin 27.0.0

Node.js version: v24.21.0

Any other relevant information: None.

What steps will reproduce the bug?

req.subdomains does not split a host that is an IP address. This works for [::1]. An IPv6 address can also contain dots, for example the IPv4-mapped address [::ffff:127.0.0.1]. req.subdomains splits that host on the dots:

const express = require('express')
const app = express()

app.get('/', (req, res) => res.json(req.subdomains))
app.listen(3000)
curl -H 'Host: [::1]' http://localhost:3000/
# []

curl -H 'Host: [::ffff:127.0.0.1]' http://localhost:3000/
# ["0","[::ffff:127"]

Full repro, with npm run repro: https://github.com/johnnyhuirilef/expressjs-repros/tree/main/subdomains-ipv6-dotted-host

Note: fetch() rewrites the host to [::ffff:7f00:1], so it does not show the bug. curl, http.get() and X-Forwarded-Host (with trust proxy) send the host as written.

What is the expected behavior?

[] for [::ffff:127.0.0.1], the same as for [::1]. With subdomain offset set to 0, the expected value is ["[::ffff:127.0.0.1]"].

What do you see instead?

["0","[::ffff:127"]. With subdomain offset set to 0: ["1]","0","0","[::ffff:127"].

Additional information

req.hostname keeps the brackets of an IPv6 literal. req.subdomains passes that value to net.isIP() in lib/request.js, and isIP('[::ffff:127.0.0.1]') returns 0. The existing [::1] test passes only because that address has no dots.

I have a fix with tests. Happy to open a PR 🙌

Activity

  1. added theissue type on Oct 20, 2026
  2. hamarshehmhmd commented on Nov 7, 2026

    @hamarshehmhmd

    Confirmed on current master — Host: [::ffff:127.0.0.1] makes req.subdomains return ["0","[::ffff:127"] instead of [].

    Root cause: req.hostname keeps the brackets for IPv6 literals, and net.isIP() returns 0 for bracketed forms. The dotted IPv4-mapped address then goes through hostname.split('.') like a DNS name. Bare ::ffff:127.0.0.1 is fine with isIP; [::1] happens to survive because slice(offset) empties a one-segment split.

    Fix in lib/request.js — strip [] before the IP check (still return the original hostname string for IP hosts):

      var hostForIp = hostname[0] === '[' && hostname[hostname.length - 1] === ']'
        ? hostname.slice(1, -1)
        : hostname;
      var subdomains = !isIP(hostForIp)
        ? hostname.split('.').reverse()
        : [hostname];

    Verified (Node 20, mocha):

    • Added should work with IPv4-mapped IPv6 address expecting [] for [::ffff:127.0.0.1]
    • Existing IPv4 / [::1] / DNS cases unchanged
    • npx mocha --exit test/req.subdomains.js → 12 passing

    Happy to open a PR.

  3. johnnyhuirilef commented on Nov 11, 2026

    @johnnyhuirilef
    Author

    Thanks for confirming, @hamarshehmhmd. A fix is already open in #7533. It strips the brackets before the isIP check, the same approach you describe, and adds tests for [::ffff:127.0.0.1]. It is waiting for maintainer review, so another PR should not be needed. Reviews or testing on #7533 are very welcome.

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

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions