Incomplete hostname regular expression: unescaped dot

Incomplete hostname regular expression with an unescaped dot

Description

In a regular expression, an unescaped . can match characters other than a dot. A hostname check that uses it for a domain separator can accept unlisted hosts.

Potential impact

A redirect can send a user to the wrong site. The same check on a server request or integration can allow connections to an unintended host.

Remediation

  • Parse the URL and compare its hostname, which excludes the port, against an exact allow-list. If every subdomain is trusted, check for the base domain or the dot-prefixed suffix .example.com.
  • If you need a regular expression, use ^ and $ around the complete hostname and \. for its separators. Limit label characters and lengths, and avoid broad or nested repetitions.

Examples

URL redirects

Before

javascript
const express = require("express");
const app = express();

// /jump?next=https://example.com
app.get("/jump", (req, res) => {
  const next = req.query.next;
  if (!next) return res.status(400).send("missing");

  let host;
  try {
    host = new URL(next).hostname; // Hostname of the user-supplied URL
  } catch {
    return res.status(400).send("bad url");
  }

  // Unescaped '.' matches more than intended
  // For example, staticXexampleYcom and static-exampleZcom may pass
  const allowed = /^((static|media).)?example.com$/i.test(host);
  if (allowed) return res.redirect(next);
  return res.status(400).send("blocked");
});

After

javascript
const express = require("express");
const app = express();

// Strict allow-list validation
const ALLOW_HOSTS = new Set([
  "example.com",
  "static.example.com",
  "media.example.com",
]);

app.get("/jump", (req, res) => {
  const next = req.query.next;
  if (typeof next !== "string") return res.status(400).send("missing");

  let parsed;
  try {
    parsed = new URL(next);
  } catch {
    return res.status(400).send("bad url");
  }

  const host = parsed.hostname.toLowerCase();
  const safeTransport =
    parsed.protocol === "https:" &&
    parsed.port === "" &&
    parsed.username === "" &&
    parsed.password === "";

  // Allow only exact hostnames and the default HTTPS port
  if (!safeTransport || !ALLOW_HOSTS.has(host)) {
    return res.status(400).send("blocked");
  }
  return res.redirect(parsed.toString());
});

// If a regular expression is required, escape the dots as follows.
// const ok = /^((static|media)\.)?example\.com$/i.test(host);

The redirect handler accepts only the three listed hostnames on the default HTTPS port and rejects URLs containing a username or password.

Browser hostname

Before

javascript
function checkLocation(document) {
  return /^example.com$/.test(globalThis.location.hostname);
}

After

javascript
function checkLocation(document) {
  return /^example\.com$/.test(globalThis.location.hostname);
}

The browser example checks the complete globalThis.location.hostname. The escaped dot matches only a domain separator.

References