← Blog

Claude MCP skill CORS misconfiguration via regex and wildcard edge cases — SA-SKL-CORS16

Cross-Origin Resource Sharing (CORS) is one of the most frequently misconfigured web security controls — not because it is conceptually hard, but because the implementation details hide multiple failure modes that look correct at first glance. Dynamic origin validation using regex is especially prone to subtle bugs: an unescaped dot, a missing anchor, or a startsWith check instead of an exact match can open the allowlist to attacker-controlled origins. The other failure modes are equally subtle: returning CORS headers before authentication leaks resource information, inconsistent preflight and actual-request policies can confuse browser caches, and missing Vary: Origin causes CDNs to serve the wrong ACAO header to callers from unauthorized origins. Four SA-SKL-CORS16 patterns cover the most dangerous CORS edge cases found in MCP skill infrastructure.

By SkillAudit  |  9 Nov 2026
SA-SKL-CORS16 findings at a glance
CRITICALCORS16-001 — Origin allowlist regex built from a domain list by joining with |; domain skillaudit.dev becomes regex skillaudit.dev where the dot is unescaped and matches any character; attacker registers skillauditXdev.evil.com and their origin is accepted as valid; allows cross-origin credential theft via fetch with credentials: 'include'
HIGHCORS16-002 — CORS middleware runs before authentication middleware; a 401 Unauthorized response from an unauthenticated cross-origin request includes Access-Control-Allow-Origin: attacker.com in the response; this confirms the resource exists and leaks any structured error body the application returns in 401 responses
HIGHCORS16-003 — OPTIONS preflight returns Access-Control-Allow-Origin: * but the actual GET/POST returns Access-Control-Allow-Origin: app.skillaudit.dev; some older browser implementations cache the wildcard from the preflight and apply it to subsequent requests, treating responses from other origins as allowed
HIGHCORS16-004 — CDN configured with Cache-Control: public, max-age=300 for API responses; CORS response for app.skillaudit.dev is cached with ACAO: app.skillaudit.dev; next request from docs.skillaudit.dev receives the cached response with the wrong origin in ACAO, causing CORS failures or silently returning data to an unexpected origin

CORS16-001 — Unescaped dot in dynamically-built origin regex

When building a CORS origin allowlist dynamically from a configuration list, developers often construct a regex by joining domain names with |. The problem is that . in regex matches any character, not just a literal dot. So the pattern skillaudit.dev matches not only https://skillaudit.dev but also https://skillauditXdev.attacker.com (where X can be any character including a dot). An attacker who registers a domain matching the unescaped pattern can make cross-origin requests that include session cookies and read the response.

// VULNERABLE — CORS16-001
// Origin allowlist built by joining domain strings with | to form a regex.
// Dots in domain names are not escaped — they match any character.
// skillaudit.dev matches: skillauditXdev, skillaudit-dev, skillauditYdev, etc.

const allowedOrigins = ['skillaudit.dev', 'app.skillaudit.dev', 'docs.skillaudit.dev'];

// VULNERABLE: dots unescaped in regex — joins: skillaudit.dev|app.skillaudit.dev|...
const originRegex = new RegExp(`^https?://(${allowedOrigins.join('|')})$`);

app.use((req, res, next) => {
  const origin = req.headers.origin;
  if (origin && originRegex.test(origin)) {
    // VULNERABLE: origin "https://skillauditXdev.evil.com" passes this test
    res.setHeader('Access-Control-Allow-Origin', origin);
    res.setHeader('Access-Control-Allow-Credentials', 'true');
  }
  next();
});
// FIXED — CORS16-001
// Option A: Use a Set for exact string comparison — no regex needed.
// Option B: If regex is required, escape dots with \\.

// Option A: exact Set comparison (recommended — no regex parsing risk)
const allowedOrigins = new Set([
  'https://skillaudit.dev',
  'https://app.skillaudit.dev',
  'https://docs.skillaudit.dev',
]);

app.use((req, res, next) => {
  const origin = req.headers.origin;
  if (origin && allowedOrigins.has(origin)) {
    // FIX: exact match — no regex, no dot-escape bug possible
    res.setHeader('Access-Control-Allow-Origin', origin);
    res.setHeader('Access-Control-Allow-Credentials', 'true');
    res.setHeader('Vary', 'Origin');  // required when ACAO varies by origin
  }
  next();
});

// Option B: escaped regex (use only if dynamic suffix matching is truly needed)
function escapeRegexDomain(domain) {
  return domain.replace(/\./g, '\\.').replace(/\*/g, '[a-z0-9-]+');
}
const escaped = allowedOrigins.map(escapeRegexDomain).join('|');
const safeRegex = new RegExp(`^https://(${escaped})$`);

Detection: SkillAudit's static analyzer searches for CORS middleware that constructs a regex from a domain list and flags any .join('|') pattern on domain strings where dots are not escaped. It also sends a probe from a crafted origin matching the unescaped pattern and checks whether ACAO is reflected.

CORS16-002 — CORS handler before auth leaks resource existence

CORS middleware is often registered before authentication middleware in Express application setup because it needs to handle OPTIONS preflight requests that arrive without credentials. However, applying CORS to ALL responses — including 401 Unauthorized — means that unauthenticated cross-origin requests receive a full CORS response with Access-Control-Allow-Origin set to their origin. This allows any attacker site to make a cross-origin request, read the 401 response body (which often contains structured error information), and confirm that the resource endpoint exists and what authentication scheme is required.

// VULNERABLE — CORS16-002
// CORS middleware registered before auth middleware.
// A 401 response to an unauthenticated cross-origin request includes ACAO.
// Attacker can read the 401 body cross-origin and confirm the endpoint exists.

app.use(cors({ origin: allowedOrigins, credentials: true }));  // CORS applied first
app.use(requireAuth);  // Auth applied second — 401 responses already have CORS headers

app.get('/api/audit/:id/report', (req, res) => {
  // If unauthenticated: returns 401 {"error":"Unauthorized","resource":"audit_report"}
  // Cross-origin attacker reads this body because ACAO was set before the 401.
  res.json(getAuditReport(req.params.id, req.user));
});
// FIXED — CORS16-002
// Two fixes:
// Fix 1: Apply CORS only after auth for authenticated endpoints — OPTIONS preflights
//         can be handled separately without reflecting ACAO for non-OPTIONS requests
//         that fail auth.
// Fix 2: For unauthenticated endpoints that must accept CORS (public API), ensure
//         the 401 body does not contain information that shouldn't be revealed to
//         cross-origin callers.

// Handle OPTIONS preflight separately
app.options('*', cors({ origin: checkOriginAllowed, credentials: true }));

// For authenticated API routes — apply auth before CORS reflection
app.get('/api/audit/:id/report',
  requireAuth,   // FIX: auth first — returns 401 without ACAO if not authenticated
  cors({ origin: checkOriginAllowed, credentials: true }),  // CORS only for authed requests
  (req, res) => {
    res.json(getAuditReport(req.params.id, req.user));
  }
);

// Alternative: use a smarter CORS handler that only reflects ACAO for 2xx responses
function conditionalCors(req, res, next) {
  res.on('finish', () => {
    // Only set ACAO on successful responses — prevents leaking on 401/403
    if (res.statusCode < 400 && checkOriginAllowed(req.headers.origin)) {
      res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
      res.setHeader('Vary', 'Origin');
    }
  });
  next();
}

CORS16-003 — Inconsistent ACAO on preflight vs actual request

The HTTP spec says the Access-Control-Allow-Origin header on the preflight OPTIONS response and the actual response must be consistent for the CORS check to work. When a server returns ACAO: * on OPTIONS but ACAO: app.skillaudit.dev on the actual GET, browsers implement the CORS check using the ACAO from the actual response — so the GET fails for origins other than app.skillaudit.dev. However, some older browser implementations and certain CDN preflight caching behaviors apply the preflight's wildcard ACAO to subsequent same-origin-matching requests. This creates an inconsistency that is hard to debug and can in edge cases allow unintended cross-origin reads.

// VULNERABLE — CORS16-003
// OPTIONS preflight returns ACAO: * (permissive)
// Actual GET returns ACAO: specific origin (restrictive)
// These are inconsistent — creates browser caching and policy confusion.

// Preflight handler
app.options('/api/audit/*', (req, res) => {
  res.setHeader('Access-Control-Allow-Origin', '*');   // VULNERABLE: wildcard
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, DELETE');
  res.setHeader('Access-Control-Allow-Headers', 'Authorization, Content-Type');
  res.setHeader('Access-Control-Max-Age', '86400');
  res.status(204).end();
});

// Actual request handler
app.get('/api/audit/:id', requireAuth, (req, res) => {
  res.setHeader('Access-Control-Allow-Origin', 'https://app.skillaudit.dev');  // specific
  res.setHeader('Access-Control-Allow-Credentials', 'true');
  res.json(getAudit(req.params.id));
});
// FIXED — CORS16-003
// Use a single CORS middleware for both OPTIONS and actual requests.
// The same origin validation logic applies to both — no inconsistency.
// Use a cors() middleware package or write one that handles both cases identically.

import cors from 'cors';

const corsOptions = {
  origin: (origin, callback) => {
    if (!origin || allowedOrigins.has(origin)) {
      callback(null, origin || false);
    } else {
      callback(new Error('CORS: origin not allowed'));
    }
  },
  credentials: true,
  methods: ['GET', 'POST', 'DELETE', 'OPTIONS'],
  allowedHeaders: ['Authorization', 'Content-Type'],
  maxAge: 86400,
};

// FIX: Same cors() instance handles both OPTIONS and actual requests — consistent policy.
app.use(cors(corsOptions));  // handles OPTIONS preflights automatically

app.get('/api/audit/:id', requireAuth, (req, res) => {
  // cors() middleware already set ACAO consistently — no need to set it here again
  res.json(getAudit(req.params.id));
});

CORS16-004 — CDN caches CORS response without Vary: Origin

HTTP caching and CORS interact in a subtle way. When a server returns a CORS response with Access-Control-Allow-Origin: https://app.skillaudit.dev and no Vary: Origin header, CDNs and intermediate caches treat the response as cacheable for all callers, regardless of the Origin request header. The next caller from https://docs.skillaudit.dev receives the cached response — which has the wrong ACAO value. The browser rejects the CORS check for docs.skillaudit.dev, causing CORS errors for legitimate callers. In the reverse scenario (wildcard ACAO cached), the next caller from an unauthorized origin receives a wildcard ACAO and can read the response.

// VULNERABLE — CORS16-004
// CORS response with specific ACAO sent without Vary: Origin.
// CDN caches the response for all callers.
// Second caller from docs.skillaudit.dev receives ACAO: app.skillaudit.dev — CORS error.
// Or: if attacker poisons the cache with their ACAO, legitimate callers may get wrong ACAO.

app.get('/api/public/skills', (req, res) => {
  const origin = req.headers.origin;
  if (allowedOrigins.has(origin)) {
    res.setHeader('Access-Control-Allow-Origin', origin);  // specific origin
    res.setHeader('Access-Control-Allow-Credentials', 'true');
    // VULNERABLE: no Vary: Origin — CDN caches this response for ALL callers
    res.setHeader('Cache-Control', 'public, max-age=300');
  }
  res.json(getPublicSkills());
});
// FIXED — CORS16-004
// Always add Vary: Origin when ACAO varies by request origin.
// This tells CDNs and browser caches to use separate cache entries per Origin.
// Without Vary: Origin, cached responses are served to wrong callers.

app.get('/api/public/skills', (req, res) => {
  const origin = req.headers.origin;
  if (allowedOrigins.has(origin)) {
    res.setHeader('Access-Control-Allow-Origin', origin);
    res.setHeader('Access-Control-Allow-Credentials', 'true');
  }

  // FIX: Vary: Origin tells CDN to cache separately per Origin value
  res.setHeader('Vary', 'Origin');
  res.setHeader('Cache-Control', 'public, max-age=300');
  res.json(getPublicSkills());
});

// Also: if using ACAO: *, you don't need Vary: Origin because * applies to everyone.
// Vary: Origin is only needed when ACAO varies (reflects specific origins) per request.

CORS testing methodology: Use curl -H "Origin: https://attacker.com" https://skillaudit.dev/api/audits and check whether ACAO: https://attacker.com is returned. Then test with a crafted origin matching your domain with an unescaped character: curl -H "Origin: https://skillauditXdev.attacker.com". Check all responses for Vary: Origin when ACAO is not *. Run this against preflight and actual requests separately to catch CORS16-003.

Summary — SA-SKL-CORS16 at a glance

PatternRoot causeFixSeverity
CORS16-001Dynamically built origin regex with unescaped dots; skillaudit.dev matches skillauditXdev.evil.comUse exact Set comparison; if regex required, escape dots with \\.Critical
CORS16-002CORS middleware runs before auth; 401 responses include ACAO, leaking resource existence to cross-origin callersApply auth before CORS reflection; handle OPTIONS separately; only set ACAO on 2xx responsesHigh
CORS16-003Preflight returns ACAO: * but actual response returns specific origin; inconsistent policy confuses browser/CDN cachesUse single CORS middleware instance for both OPTIONS and actual requestsHigh
CORS16-004ACAO varies by origin but Vary: Origin missing; CDN serves cached response with wrong ACAO to other originsAdd Vary: Origin to all responses where ACAO is dynamically set per originHigh
SkillAudit

Does your MCP skill's CORS implementation have regex or caching edge cases?

SkillAudit scans for unescaped dot patterns in CORS origin validators, missing Vary: Origin headers, and inconsistent preflight vs actual ACAO policies — before attackers exploit the edge cases.

Audit your MCP skill →