Overly permissive CORS regular expressions

Overly permissive CORS regular expressions with wildcards or unescaped dots

Description

In a CORS allow-list regex, an unescaped dot can match characters other than a literal dot. Scripts on unintended origins may then read responses, depending on the server's headers, the response content and whether the browser sends credentials. CORS does not block every cross-origin request.

Potential impact

An unintended site may read personal data in an API response, including account data when the browser sends credentials and the server allows credentialed reads.

Remediation

List the exact origins you trust. If you use a regex, match the whole origin, escape each literal dot as \., and narrow any wildcard or subdomain match. After a change, test origins that should pass and ones that should be rejected.

Examples

Before

tsx
const corsDomains = [
  /(.+\.)*example.com$/,
  /^(http|https):\/\/.+\.evil\.com$/,
  /^(http|https):\/\/(.+)\.my\.site.com$/,
];

// Example CORS configuration
app.use(cors({
  origin: corsDomains,
}));

After

tsx
const corsAllowedDomains = [
  /^https?:\/\/www\.example\.com$/,   // Allow only 'www.example.com'
  /^https?:\/\/service\.my\.site\.com$/,  // Allow only the service subdomain
];

app.use(cors({
  origin: corsAllowedDomains,
}));

The revised patterns match only HTTP or HTTPS on www.example.com and service.my.site.com. Check who controls these hosts and whether production should accept only HTTPS.

References