Skip to main content
ContentMedium PriorityPro

Dangerous JavaScript Functions

Updated November 2026·SecScanner Team

Dangerous JavaScript functions like eval(), document.write(), and innerHTML can introduce DOM-based XSS vulnerabilities when used with untrusted input.

Results in 1–3 minutes · Free · No signup required

Want the result for your own site? Free Website Vulnerability Scanner — paste your domain, get the report.

Why It Matters

DOM-based XSS through dangerous functions is harder to detect than reflected XSS and bypasses server-side sanitization. Attackers can execute arbitrary code in the user's browser context.

How We Check

We scan inline scripts and event handler attributes for calls to eval(), document.write(), innerHTML assignments, new Function(), and string-based setTimeout/setInterval.

Good vs Bad Values

Good and bad values for Dangerous JavaScript Functions
ValueVerdictWhy
el.textContent = userInputGoodThe browser treats the value as text, never as markup. This is the safe default for text.
const data = JSON.parse(text)GoodThe right replacement for eval when the input is JSON.
document.createElement + appendChildGoodBuilds nodes through the DOM, so no string is parsed as HTML.
el.innerHTML = DOMPurify.sanitize(html)WeakAcceptable when HTML is really needed, but the check still reports the assignment. The sanitizer is what makes it safe, so keep it up to date.
el.innerHTML = '<b>' + name + '</b>'BadConcatenating a value into markup is the classic DOM XSS sink. A name containing a script-bearing tag will run.
eval(response) or new Function(code)BadRuns any string as code. If an attacker controls part of the string, they control the page.
setTimeout("doThing()", 100)BadA string argument is compiled like eval. Pass a function instead.
document.write(...) or onclick="...eval(...)"BadBoth inline handlers and document.write are scanned. Handlers also stop a strict Content-Security-Policy from working.

How to Fix

Replace eval() with JSON.parse(). Use textContent instead of innerHTML. Use DOM APIs (createElement, appendChild) instead of document.write(). Implement Trusted Types via CSP.

Replace the pattern, not just the call.

// before
el.innerHTML = '<b>' + name + '</b>';
// after
const b = document.createElement('b');
b.textContent = name;
el.replaceChildren(b);

// before
setTimeout('refresh()', 500);
// after
setTimeout(refresh, 500);

When you must insert HTML, sanitize at the sink:

import DOMPurify from 'dompurify';
el.innerHTML = DOMPurify.sanitize(untrustedHtml);

Lint the whole codebase so the problem cannot return:

{
  "plugins": ["no-unsanitized"],
  "rules": {
    "no-eval": "error",
    "no-implied-eval": "error",
    "no-new-func": "error",
    "no-unsanitized/property": "error",
    "no-unsanitized/method": "error"
  }
}

Enforce it in the browser with Trusted Types. With this policy the browser refuses to pass a plain string to innerHTML or eval-like sinks:

add_header Content-Security-Policy "require-trusted-types-for 'script'; trusted-types default dompurify" always;
Header always set Content-Security-Policy "require-trusted-types-for 'script'; trusted-types default dompurify"

Roll it out as Content-Security-Policy-Report-Only first and watch the violation reports. React — avoid dangerouslySetInnerHTML, or pass only sanitized strings to it. Move inline scripts and onclick attributes to external files; that also lets your CSP drop 'unsafe-inline'.

Verify by running the scan again and by searching your templates for the same patterns: grep -rnE 'eval\(|document\.write\(|innerHTML\s*=' templates/ src/.

Common Mistakes

  • Assuming the data is safe because it came from your own API. A stored comment, a URL parameter or a third-party feed can carry markup into that field.
  • Sanitizing on the way in only. The right place is at the sink, where the value is placed into the page, because data is reused in other contexts.
  • Believing the check covers your whole app. It only reads inline scripts and inline event attributes in the HTML; bundled files are not scanned.
  • Using innerHTML for static markup out of habit. It is harmless there, but it adds noise and makes review harder; use template elements or textContent.
  • Silencing the lint rule with a comment instead of changing the code.
  • Writing innerHTML += ... and thinking it is a different thing. The sink is the same, and the pattern the check uses misses this form, so you must find it yourself.

Frequently Asked Questions

What is Dangerous JavaScript Functions?

Dangerous JavaScript functions like eval(), document.write(), and innerHTML can introduce DOM-based XSS vulnerabilities when used with untrusted input.

Why does Dangerous JavaScript Functions matter for website security?

DOM-based XSS through dangerous functions is harder to detect than reflected XSS and bypasses server-side sanitization. Attackers can execute arbitrary code in the user's browser context.

How do I fix Dangerous JavaScript Functions issues?

Replace eval() with JSON.parse(). Use textContent instead of innerHTML. Use DOM APIs (createElement, appendChild) instead of document.write(). Implement Trusted Types via CSP.

Related Security Checks

Related Tool

Website Vulnerability Scanner

Run all 7 related checks with our free website vulnerability scanner

Check Your Website Now

Run a free security scan to check for Dangerous JavaScript Functions issues and 61 other security checks.

Scan Your Website Free