Claude skill HTTP Host header injection security — SA-SKL-HOSTINJ
The HTTP Host header is set by the client, not the server — it is an attacker-controlled input like any other request header. MCP skills that use req.headers.host or req.headers['x-forwarded-host'] to construct URLs without validating them against a known-good allowlist create four attack surfaces: password reset links built from the host header deliver reset tokens to an attacker-controlled domain, CDN cache poisoning via a crafted forwarded-host embeds attacker URLs in cached responses, proxied host headers reach internal services that use the Host header for access control, and canonical/OG URL generation builds SEO metadata with attacker-controlled host values. SA-SKL-HOSTINJ-001 through HOSTINJ-004 cover each with the injection point and fix.
Why the Host header is an attacker-controlled input
When a user's browser makes a request, it sets the Host header to the domain in the URL bar. This is benign for direct requests. But in a proxied architecture — a CDN or load balancer in front of a Node.js origin — the CDN may forward the original Host as an X-Forwarded-Host header, or the CDN's configuration may allow the client to override it. For requests that bypass the CDN and reach the origin directly (common in security testing and misconfigured infrastructure), the attacker controls the Host header entirely. The Node.js application cannot reliably distinguish a legitimate proxied host from an attacker-injected one — the defense is to never trust the host header as a source of truth for the application's own base URL.
SA-SKL-HOSTINJ in scope: HOSTINJ-001 (password reset link from Host header), HOSTINJ-002 (CDN cache poisoning via X-Forwarded-Host), HOSTINJ-003 (SSRF via proxied Host to internal services), HOSTINJ-004 (canonical URL built from X-Forwarded-Host). The common fix for all four: derive base URL from a trusted environment variable (APP_ORIGIN), never from request headers.
HOSTINJ-001 — Password reset link built from Host header
Password reset flows generate a time-limited token and send the user an email containing a link like https://skillaudit.dev/reset?token=abc123. If the base URL for this link is constructed from req.headers.host, an attacker can trigger a password reset for any user and set the Host header to their own domain. The user receives an email with a link to https://evil.com/reset?token=abc123. If the user clicks the link (phishing-style), the token is delivered to the attacker's server. The attacker can then use the token to complete the password reset on the legitimate site, taking over the account. This attack is feasible whenever the application generates transactional email links from the request's Host header rather than a configured base URL.
// VULNERABLE: password reset link built from req.headers.host
app.post('/auth/forgot-password', async (req, res) => {
const user = await db.users.findOne({ email: req.body.email });
if (!user) return res.json({ ok: true }); // Don't reveal existence
const token = crypto.randomBytes(32).toString('hex');
await db.passwordResets.create({ userId: user.id, token, expiresAt: Date.now() + 3600000 });
// VULNERABLE: host derived from request header — attacker sets Host: evil.com
const host = req.headers.host; // → 'evil.com' for attacker-crafted request
const resetUrl = `https://${host}/reset-password?token=${token}`;
await email.send({
to: user.email,
subject: 'Reset your SkillAudit password',
html: `Reset password`,
// ← User clicks link to evil.com; attacker receives token
});
res.json({ ok: true });
});
// FIXED: base URL from trusted environment variable — never from request headers
const APP_ORIGIN = process.env.APP_ORIGIN; // 'https://skillaudit.dev' in .env
app.post('/auth/forgot-password', async (req, res) => {
const user = await db.users.findOne({ email: req.body.email });
if (!user) return res.json({ ok: true });
const token = crypto.randomBytes(32).toString('hex');
await db.passwordResets.create({ userId: user.id, token, expiresAt: Date.now() + 3600000 });
// FIXED: host from env — not from any request header
const resetUrl = `${APP_ORIGIN}/reset-password?token=${token}`;
await email.send({
to: user.email,
subject: 'Reset your SkillAudit password',
html: `Reset password`,
});
res.json({ ok: true });
});
req.headers.host or req.headers['x-forwarded-host'] as the base URL; an attacker who triggers a password reset for any user with a crafted Host: evil.com header causes the user to receive an email with a link pointing to the attacker's domain, delivering the reset token on click. Fix: derive the application base URL from a trusted environment variable (APP_ORIGIN); never use req.headers.host in URL generation for transactional emails. Detection: SkillAudit flags email.send() calls in password reset or invitation handlers where the link URL is constructed from req.headers.host or req.headers['x-forwarded-host'].HOSTINJ-002 — CDN cache poisoning via X-Forwarded-Host
When a Caddy or Nginx reverse proxy forwards requests to a Node.js backend, it typically includes the original client Host as an X-Forwarded-Host header. The Node.js application uses this header to construct absolute URLs embedded in the response — navigation links, Open Graph og:url tags, schema.org mainEntityOfPage JSON-LD, sitemap URLs. If the CDN caches this response (the response is public and cacheable), and if the X-Forwarded-Host header is included in the CDN's cache key (which many CDNs do not do by default, treating it as an unkeyed header), then an attacker who sends a request with X-Forwarded-Host: evil.com before any legitimate user causes the CDN to cache a response with evil.com embedded in all absolute URLs. Subsequent users receive a cached page with malicious links.
// VULNERABLE: X-Forwarded-Host used in OG metadata for public cacheable pages
app.get('/blog/:slug', async (req, res) => {
const post = await db.posts.findOne({ slug: req.params.slug });
if (!post) return res.status(404).end();
// VULNERABLE: forwarded host not validated — CDN doesn't key on X-Forwarded-Host
const host = req.headers['x-forwarded-host'] || req.headers.host;
const canonicalUrl = `https://${host}/blog/${req.params.slug}`;
res.set('Cache-Control', 'public, max-age=3600');
res.send(`
${post.content}
`);
// Attacker requests with X-Forwarded-Host: evil.com before any user
// CDN caches page with evil.com in OG and canonical tags
// All subsequent users see evil.com in social preview and search engine canonical
});
// FIXED: canonical URL from APP_ORIGIN env — never from forwarded headers
const APP_ORIGIN = process.env.APP_ORIGIN || 'https://skillaudit.dev';
app.get('/blog/:slug', async (req, res) => {
const post = await db.posts.findOne({ slug: req.params.slug });
if (!post) return res.status(404).end();
// FIXED: canonical URL built from trusted env variable
const canonicalUrl = `${APP_ORIGIN}/blog/${post.slug}`;
res.set('Cache-Control', 'public, max-age=3600, stale-while-revalidate=300');
res.send(`
${post.content}
`);
});
req.headers['x-forwarded-host']; since CDNs typically treat X-Forwarded-Host as an unkeyed header, an attacker who sends a request with a crafted forwarded-host before the cache is populated causes the CDN to cache a response with attacker-controlled host values in all embedded URLs, poisoning social previews and search engine canonical attribution. Fix: build all absolute URLs from APP_ORIGIN environment variable; never use forwarded host headers in page metadata generation. Detection: SkillAudit flags server-rendered templates or response strings that interpolate req.headers['x-forwarded-host'] into <link rel="canonical">, og:url, or JSON-LD @id fields.HOSTINJ-003 — SSRF via proxied Host header to internal services
In some Caddy configurations, the reverse proxy forwards the client's Host header to the upstream Node.js backend without replacing it with the internal hostname. If the application or any internal service it calls uses the Host header to route requests — a common pattern in multi-tenant platforms and microservice meshes — an attacker can craft a Host header that routes to an internal admin interface. For example, if the internal admin dashboard runs on admin.internal:8080 and the main Caddy proxy does not rewrite the Host header, an attacker who sends Host: admin.internal may cause the backend to treat the request as an internal admin request, bypassing network-level access controls that only check the Host header.
// VULNERABLE: Caddy config forwards client Host to Node.js (implicit in some setups)
// Caddyfile — MISSING: header_up Host {upstream_hostport} rewrite
# skillaudit.dev {
# reverse_proxy localhost:3000 # Host header not rewritten — attacker controls it
# }
// Node.js admin check relying on Host header (found in some internal tooling)
app.use('/admin', (req, res, next) => {
// VULNERABLE: trusts Host header for internal access control
const host = req.headers.host;
if (host === 'admin.internal' || host === 'localhost') {
req.isInternalRequest = true; // ← Attacker sets Host: admin.internal
}
next();
});
// FIXED: Caddy rewrites Host to internal upstream address; Node.js uses env for internal check
# Caddyfile — FIXED: rewrite Host header before proxying
# skillaudit.dev {
# reverse_proxy localhost:3000 {
# header_up Host {upstream_hostport} # Rewrites Host to 'localhost:3000'
# }
# }
// Node.js: never use Host header for internal access decisions
app.use('/admin', (req, res, next) => {
// FIXED: internal access determined by environment variable or IP range check
// Never by the Host header which is attacker-controlled
const clientIp = req.socket.remoteAddress;
const internalSubnet = process.env.INTERNAL_SUBNET || '127.0.0.1';
if (!clientIp.startsWith(internalSubnet)) {
return res.status(403).json({ error: 'Forbidden' });
}
next();
});
Host header as an access control check for internal/admin routes without validating the host against a trusted list; an attacker who can reach the Node.js backend directly (bypassing Caddy) or whose request is forwarded with the original Host intact can set Host: admin.internal to trigger admin-only code paths. Fix: add header_up Host {upstream_hostport} in Caddy to rewrite the Host header before proxying; use IP range checks or a shared secret header for internal service access control — never the Host header. Detection: SkillAudit flags route middleware that reads req.headers.host and uses it as an access control gate.HOSTINJ-004 — Canonical and OG URL built from X-Forwarded-Host
Beyond cache poisoning (HOSTINJ-002), generating canonical and Open Graph URLs from unvalidated forwarded-host headers has a second consequence: search engine canonical attribution. If a search engine crawler (Googlebot, Bingbot) crawls the site while a poisoned cache entry is active — or if the application dynamically serves canonical URLs from the forwarded-host — the crawler may index the page with a canonical pointing to an attacker-controlled domain. The original site loses canonical authority for its own content. For MCP skills where SEO value is accumulated from blog posts and programmatic pages, canonical hijacking can destroy months of indexing work. The fix is the same as HOSTINJ-002: use APP_ORIGIN for all URL generation.
// VULNERABLE: sitemap generated dynamically from X-Forwarded-Host
app.get('/sitemap.xml', async (req, res) => {
const urls = await db.posts.findAllSlugs();
// VULNERABLE: host in sitemap URLs from forwarded header
const host = req.headers['x-forwarded-host'] || req.headers.host;
const sitemap = urls.map(slug =>
`https://${host}/blog/${slug} `
).join('');
res.type('application/xml');
res.send(`${sitemap} `);
});
// FIXED: sitemap uses APP_ORIGIN — static file preferred over dynamic generation
// Best: serve a pre-generated static sitemap.xml (no host injection surface)
// If dynamic generation is required:
const APP_ORIGIN = process.env.APP_ORIGIN || 'https://skillaudit.dev';
app.get('/sitemap.xml', async (req, res) => {
const urls = await db.posts.findAllSlugs();
// FIXED: host from env — never from request headers
const sitemap = urls.map(slug =>
`${APP_ORIGIN}/blog/${slug} weekly `
).join('');
res.set('Cache-Control', 'public, max-age=86400');
res.type('application/xml');
res.send(`${sitemap} `);
});
req.headers['x-forwarded-host']; a search engine crawler that processes a poisoned response or a dynamically generated response with an injected host will index the page with a canonical pointing to an attacker-controlled domain, destroying the site's canonical authority for its own content. Fix: use APP_ORIGIN environment variable for all URL generation; prefer pre-generated static sitemap.xml over dynamic generation to eliminate the host injection surface entirely. Detection: SkillAudit flags sitemap, canonical, and OG URL generation that reads from req.headers.host or req.headers['x-forwarded-host'].SkillAudit detection summary — SA-SKL-HOSTINJ
| Pattern ID | Severity | What SkillAudit flags | Fix |
|---|---|---|---|
| HOSTINJ-001 | CRITICAL | email.send() calls where link URL is constructed from req.headers.host in password reset or invitation handlers | APP_ORIGIN env variable for all email link generation |
| HOSTINJ-002 | HIGH | Server-rendered templates that interpolate req.headers['x-forwarded-host'] into canonical, OG, or JSON-LD URL fields in cacheable responses | APP_ORIGIN for all public page URL generation |
| HOSTINJ-003 | HIGH | Route middleware that reads req.headers.host as an access control gate for internal or admin routes | IP range check or shared secret header; Caddy header_up Host rewrite |
| HOSTINJ-004 | MEDIUM | Sitemap, canonical, or OG URL generation that reads from req.headers.host or forwarded-host in dynamic route handlers | APP_ORIGIN for all URL generation; prefer static sitemap.xml |
Related patterns: For cache poisoning via unkeyed query parameters and missing Cache-Control: private on authenticated responses, see SA-SKL-CACHEPOISON. For SSRF via user-supplied URLs in MCP skill audit submission, see SA-SKL-SSRF.