Description
Passing a request parameter directly to res.redirect lets a caller send visitors from your service to an arbitrary external site.
Potential impact
A service link can lead to phishing or malicious downloads. An OAuth or SSO flow that trusts the return destination may expose codes or tokens.
Remediation
- Match normalized internal paths exactly against an allow-list. For external sites, accept only keys mapped to fixed URLs on the server. Use a default path if neither matches.
- In Next.js, apply the same checks before calling
NextResponse.redirect(new URL(..., request.url))or the App Router’sredirect(...).
Examples
Before
javascript
const express = require("express");
const app = express();
// Before: redirect directly to user input.
app.get("/go", (req, res) => {
const target = req.query.u; // User-controlled input.
return res.redirect(target); // No validation (open redirect).
});
app.listen(3000);
After
javascript
const express = require("express");
const app = express();
// After: allowlisted internal paths and keyed external URLs.
const allowedPaths = new Set(["/home", "/dashboard", "/help"]);
const externalMap = new Map([
["docs", "https://docs.example.com"],
["status", "https://status.example.com"],
]);
app.get("/go", (req, res) => {
const next = typeof req.query.next === "string" ? req.query.next.trim() : "";
const siteKey = typeof req.query.site === "string" ? req.query.site.trim() : "";
// 1) Allow only predefined internal paths.
if (allowedPaths.has(next)) {
return res.redirect(next);
}
// 2) Select external destinations only through the key-to-URL map.
const extUrl = externalMap.get(siteKey);
if (extUrl) {
return res.redirect(extUrl);
}
// 3) Use a safe default when validation fails.
return res.redirect("/");
});
app.listen(3000);
A string next value in allowedPaths takes priority. Otherwise the handler looks up the string site key in the Map and redirects to / if neither matches.