fix(req): ignore trailing root dot in subdomains#7331
Conversation
krzysdz
left a comment
There was a problem hiding this comment.
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'].
|
|
||
| // 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(/\.$/, ''); |
There was a problem hiding this comment.
A regular expression is going to be slower than something like:
| hostname = hostname.replace(/\.$/, ''); | |
| if (hostname[hostname.length - 1] === '.') { | |
| hostname = hostname.slice(0, -1); | |
| } |
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.jsnpm run lintgit diff --checkAI-assisted: Codex helped prepare this focused change; I reviewed the diff and ran the validation above before submitting.