Skip to content

fix(req): ignore trailing root dot in subdomains#7331

Open
lin-hongkuan wants to merge 1 commit into
expressjs:masterfrom
lin-hongkuan:codex/express-subdomains-trailing-dot
Open

fix(req): ignore trailing root dot in subdomains#7331
lin-hongkuan wants to merge 1 commit into
expressjs:masterfrom
lin-hongkuan:codex/express-subdomains-trailing-dot

Conversation

@lin-hongkuan

@lin-hongkuan lin-hongkuan commented Jun 24, 2026

Copy link
Copy Markdown

This strips an optional trailing DNS root dot before calculating req.subdomains, so fully qualified hostnames behave like their ordinary hostname form.

The regression test covers tobi.ferrets.example.com. and keeps existing subdomain offset behavior unchanged.

Validation:

  • npx mocha --require test/support/env --check-leaks test/req.subdomains.js
  • npm run lint
  • git diff --check

AI-assisted: Codex helped prepare this focused change; I reviewed the diff and ran the validation above before submitting.

@krzysdz krzysdz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everyone loves trailing dots.

For anyone looking into this:

tobi.ferrets.example.com. is a valid way to specify FQDN that is usually written as tobi.ferrets.example.com. The usual version without trailing dot, can technically be treated as a subdomain of some local domain in some cases. This being said, everyone expects the version without . and sometimes it is hard to decide what to do when someone specifies a host with a trailing dot.

In this case I'd say that req.subdomains given either tobi.ferrets.example.com or tobi.ferrets.example.com. should return ['ferrets', 'tobi']. Currently if the trailing dot is present it returns ['example', 'ferrets', 'tobi'].

Comment thread lib/request.js

// Strip the optional DNS root label so fully-qualified domain names
// produce the same subdomain list as their non-root-qualified form.
hostname = hostname.replace(/\.$/, '');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A regular expression is going to be slower than something like:

Suggested change
hostname = hostname.replace(/\.$/, '');
if (hostname[hostname.length - 1] === '.') {
hostname = hostname.slice(0, -1);
}

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants