Skip to main content
interlace
Plugin: express-securityRules

no-user-controlled-redirect

Disallow res.redirect() with values directly from req.query / req.body / req.params

Next step

no-user-controlled-redirect ships in eslint-plugin-express-security, with 25 of its 28 rules in the recommended preset.

Install
npm install -D eslint-plugin-express-security
eslint.config.js
import { configs } from 'eslint-plugin-express-security';

export default [configs.recommended];

Plugin pagenpmSource on GitHub

Disallow res.redirect() with values taken directly from req.query / req.body / req.params

Severity: 🔴 High CWE: CWE-601

Rule Details

res.redirect(req.query.url) is an open redirect: the attacker chooses the destination, and the victim sees your trusted domain in the link they clicked. Open redirects are a standard phishing primitive and an OAuth token-leak vector.

The rule flags a redirect whose argument comes from a user-controlled request object (req.query, req.body, req.params). Validation only counts when it guards the same value being redirected — checking req.query.a and then redirecting req.query.b is still flagged, because the checked value and the redirected value are different sources.

A handler that is defined in one module and registered in another is covered too. A file with no express import and no app.get('/x', …) of its own is read as Express when it exports a (req, res) function that calls res.redirect, res.render, res.sendStatus or res.sendFile (module.exports.redirect = function (req, res) { … }, exports.x = …, export function x(req, res)). Fastify (request, reply), Koa (ctx), an unexported helper, a node:http listener that only calls res.end or res.writeHead, and a default export (the shape of a Next.js API route) do not open the gate.

Examples

❌ Incorrect

app.get('/go', (req, res) => {
  res.redirect(req.query.returnTo);
  res.redirect(302, req.query.returnTo); // status-first form: same sink
});
// Validating one key and redirecting another is NOT validation.
app.get('/go', (req, res) => {
  if (new URL(req.query['a']).host !== 'example.com') return res.sendStatus(400);
  res.redirect(req.query['b']);
});

✅ Correct

res.redirect('/dashboard');
res.redirect(301, '/login');
// Allowlist the target, then redirect a value you constructed.
const ALLOWED = new Map([['docs', '/docs'], ['home', '/']]);
app.get('/go', (req, res) => {
  res.redirect(ALLOWED.get(req.query.to) ?? '/');
});

Configuration Examples

Basic Usage

// eslint.config.js
{
  rules: {
    'express-security/no-user-controlled-redirect': 'error',
  },
}

Options

OptionTypeDefaultDescription
responseObjectsstring[]—Additional response object names (e.g. ["reply"] for Fastify)
requestObjectsstring[]—Additional request object names

When Not To Use It

An internal admin tool where every user already holds full trust in the redirect targets. Even then, prefer the allowlist — it costs three lines.

Use this rule

npm install -D eslint-plugin-express-security
eslint.config.js
import expressSecurity from 'eslint-plugin-express-security';export default [  {    plugins: { 'express-security': expressSecurity },    rules: {      'express-security/no-user-controlled-redirect': 'error'    },  },];
npm install -D oxlint-plugin-express-security
.oxlintrc.json
{  "jsPlugins": ["oxlint-plugin-express-security"],  "rules": {    "express-security/no-user-controlled-redirect": "error"  }}
vite.config.ts
import { defineConfig } from 'vite-plus';export default defineConfig({  lint: {    jsPlugins: ['oxlint-plugin-express-security'],    rules: {      'express-security/no-user-controlled-redirect': 'error',    },  },});

oxlint-plugin-express-security re-exports eslint-plugin-express-security/oxlint: the same rules under the same IDs, and no ESLint install. Why oxlint.

Did this rule catch something? Star the repo to get new CWE coverage as we ship it — or follow the AI-code-security benchmarks behind these rules.