Skip to main content
HighClient-sideOWASP A03:2021CWE-829

How to fix: Dynamic import() Built From a URL-Derived Value

A dynamic import() call's module specifier is built from location.hash, location.search, location.href, or a URLSearchParams query lookup, either directly inline or via a variable assigned one hop earlier, with no allowlist check visible between the two.

Why it matters

Dynamic import() fetches and executes whatever module the resulting specifier resolves to. If an attacker can influence the specifier through a crafted URL fragment or query string, they can potentially redirect it to an unexpected path or, if the value reaches a bare or protocol-qualified specifier, an attacker-hosted script entirely, resulting in cross-origin JavaScript execution equivalent to eval() of attacker-controlled code.

import() is a legitimate code-splitting/lazy-loading API, but its argument is a live module specifier: the same rules that make eval(userInput) dangerous apply when the module path itself is attacker-influenced. A common but unsafe pattern is building a locale/theme/plugin file path directly from a query parameter without checking it against a fixed list of valid values first.

How to fix it

  1. 1Validate the derived value against a fixed allowlist (e.g. an array of supported locale codes) before it reaches import().
  2. 2Never interpolate a URL-derived value directly into the import() specifier: look it up in a static map instead (import(LOCALE_MODULES[lang])).
  3. 3Reject any value containing '/', '..', or a protocol (http:, https:, //) before it reaches import().

Code examples

Validate against an allowlist first

JavaScript
// Bad
const lang = new URLSearchParams(location.search).get('lang');
import(`./locales/${lang}.json`);

// Good
const SUPPORTED = { en: () => import('./locales/en.json'), fr: () => import('./locales/fr.json') };
const lang = new URLSearchParams(location.search).get('lang');
const load = SUPPORTED[lang] ?? SUPPORTED.en;
load();

References

Detected automatically by VulnRadar. The check id is cs-dynamic-import-untrusted-specifier, stable across every scan so you can filter or diff on it.

See every client-side check

Questions

How severe is Dynamic import() Built From a URL-Derived Value?
VulnRadar rates this high severity in the client-side category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
Does VulnRadar check for this automatically?
Yes. It runs as part of every scan under the check id cs-dynamic-import-untrusted-specifier, which stays the same between runs so you can filter, diff, or gate CI on it.
Which standards does Dynamic import() Built From a URL-Derived Value map to?
It maps to CWE-829. In the OWASP Top 10 it falls under A03:2021.

Missing a check?

The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.

Propose a checkHow a check is built

Scan your own site for dynamic import() built from a url-derived value

VulnRadar runs this check and hundreds more against a URL in seconds. No agent to install, just paste a URL.

Or scan this site as a demoRead the docs

Dynamic import() Built From a URL-Derived Value | VulnRadar