Skip to main content
Skip to Content
interlace
Plugin: secure-codingRules

no-unguarded-uri-decode

Disallow decodeURIComponent/decodeURI on request input outside a try/catch

Next step

no-unguarded-uri-decode ships in eslint-plugin-secure-coding, with 18 of its 36 rules in the recommended preset.

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

export default [configs.recommended];

Plugin pagenpmSource on GitHub

Detects a decodeURIComponent or decodeURI call on request or browser-location input that no try/catch surrounds. This rule is part of eslint-plugin-secure-coding and provides LLM-optimized error messages.

🚨 Security rule | Not in recommended; on in strict

Quick Summary

AspectDetails
CWE ReferenceCWE-248 (Uncaught Exception)
SeverityMedium (a 500 on demand; a crash outside an error boundary)
Auto-FixNone
CategorySecurity
Best ForHTTP servers and client routers that decode path, query or header text

Vulnerability and Risk

Vulnerability: decodeURIComponent and decodeURI throw URIError: URI malformed on a % that does not start a valid escape: p%ss, %E0%A4%A, a bare %. A request carries whatever bytes the client typed.

Risk: In a route handler, any client can turn the call into a 500. Outside a framework's error boundary (a timer, a socket or stream handler, startup code) the throw is an uncaught exception and the process exits. Node.js core's ProxyConfig decodes proxy credentials this way, and our own conventions/utm-taxonomy stopped ESLint on %E0%A4%A before it was guarded.

Rule Details

A call to the global decodeURIComponent or decodeURI is reported when its argument reads request or location input and no try block with a catch, in the same function, surrounds it.

Input is recognised by structure:

SourceShapes
A request parameterreq / request / ctx declared in scope, and its .url, .originalUrl, .path, .baseUrl, .params, .query, .headers, .cookies; req.get(name); Koa's ctx.request.*
The page locationthe global location, window.location, document.location; document.URL, document.cookie, document.referrer
Derived from eithera string method that copies text (split, slice, replace, …), ||, ??, ?:, +, a template, new URL(input).pathname, or a variable whose initializer or destructuring source is one of these

A string literal is checked at lint time: one that fails to decode is reported as malformedLiteral, since it throws on every run.

Examples

❌ Incorrect

app.get('/files/:name', (req, res) => {
  res.sendFile(decodeURIComponent(req.params.name)); // "/files/a%zz" -> 500
});

const tab = decodeURIComponent(location.hash.slice(1));

decodeURIComponent('100%'); // throws every time

✅ Correct

app.get('/files/:name', (req, res) => {
  let name;
  try {
    name = decodeURIComponent(req.params.name);
  } catch {
    return res.sendStatus(400);
  }
  res.sendFile(name);
});

// Already decoded by the URL parser.
const q = new URL(req.url, base).searchParams.get('q');

Not a finding

  • No request or location input. A configured URL (new URL(process.env.HTTP_PROXY).password) throws at startup, where the throw is the error report; an argument of unknown origin (a function parameter) is not judged.
  • searchParams.get() and URLSearchParams. Their values are already decoded.
  • A converted value (Number(req.params.id)) carries no escape.
  • A shadowed decodeURIComponent (a local function or import) is not the global.

A try in an outer function does not guard a callback defined inside it: the callback runs later, outside that try. try { … } finally { … } without a catch does not guard either.

Known False Negatives

Input that reaches the decode through a helper

// NOT DETECTED: the rule does not follow values across function calls
const name = readParam(req, 'name');
decodeURIComponent(name);

Mitigation: Guard the decode where the value is read, or inside the helper.

Further Reading

Use this rule

npm install -D eslint-plugin-secure-coding
eslint.config.js
import secureCoding from 'eslint-plugin-secure-coding';export default [  {    plugins: { 'secure-coding': secureCoding },    rules: {      'secure-coding/no-unguarded-uri-decode': 'error'    },  },];
npm install -D oxlint-plugin-secure-coding
.oxlintrc.json
{  "jsPlugins": ["oxlint-plugin-secure-coding"],  "rules": {    "secure-coding/no-unguarded-uri-decode": "error"  }}
vite.config.ts
import { defineConfig } from 'vite-plus';export default defineConfig({  lint: {    jsPlugins: ['oxlint-plugin-secure-coding'],    rules: {      'secure-coding/no-unguarded-uri-decode': 'error',    },  },});

oxlint-plugin-secure-coding re-exports eslint-plugin-secure-coding/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.