Skip to main content
interlace

Detect XSS in JavaScript with ESLint flat config

A copy-paste eslint.config.js that flags DOM XSS sinks on ESLint 9 and 10, and what eslint-plugin-xss, no-unsanitized and browser-security each miss.

To detect XSS in JavaScript with ESLint flat config, enable a plugin that reports non-literal values reaching DOM sinks such as innerHTML, insertAdjacentHTML and document.write. Two maintained plugins do this: Mozilla's eslint-plugin-no-unsanitized and eslint-plugin-browser-security, which is ours. Pick one of these configs.

Covers ESLint 9 and 10 (flat config). Last updated 2026-11-19. ESLint 8 is out of scope: no-unsanitized 4.x declares eslint: ^9 || ^10 as its peer range.

eslint.config.js
import noUnsanitized from 'eslint-plugin-no-unsanitized';

export default [noUnsanitized.configs.recommended];
npm install --save-dev eslint-plugin-no-unsanitized

TypeScript and JSX files need a parser such as typescript-eslint in the same config. Neither plugin configures one.

What each plugin reports

The table comes from a run on 2026-11-19 with ESLint 9.39.4 and 10.15.0 (the results were identical), eslint-plugin-no-unsanitized 4.1.5 and eslint-plugin-browser-security 3.0.2. The latest browser-security on npm is 3.1.0, published the same day; the run has not been repeated on it. Each row is one statement where userInput is an unknown identifier.

Statementno-unsanitizedbrowser-security (ours)
el.innerHTML = userInputreportsreports
el.outerHTML = userInputreportsreports
el.innerHTML = `<b>${userInput}</b>` reportsreports
el.insertAdjacentHTML('beforeend', userInput)reportsreports
document.write(userInput)reportsreports
range.createContextualFragment(userInput)reportsreports
el['innerHTML'] = userInputsilentreports
iframe.srcdoc = userInputsilentreports
Object.assign(el, { innerHTML: userInput })silentreports
el.setHTMLUnsafe(userInput)reportssilent
import(userInput) (code loading, not DOM)reportssilent
el.innerHTML = DOMPurify.sanitize(userInput)reportssilent
el.innerHTML = escapeHTML`<b>${userInput}</b>` silentreports
message handler writing e.data to innerHTMLreportsreports (no-postmessage-innerhtml)
$(el).html(userInput)silentsilent
el.innerHTML = '<p>static</p>'silentsilent
el.textContent = userInputsilentsilent

eslint-plugin-security 4.2.1 reported none of the DOM rows in a separate run on ESLint 10.13.0. None of its fifteen rules targets DOM sinks; detect-disable-mustache-escape, for template escaping, is the closest. See alternatives to eslint-plugin-security.

eslint-plugin-xss

eslint-plugin-xss has two rules. xss/no-mixed-html warns when HTML strings are concatenated with values that were not passed through an encoder, and it decides what counts as HTML partly from variable and function names (html, asHtml, htmlEncoded). xss/no-location-href-assign warns on assignments to location.href that are not wrapped in an escape function.

Its latest npm release is 0.1.12, published 2022-06-27. The GitHub repository's last push was 2023-08-29 (GitHub API, 2026-11-19). The README documents only .eslintrc configuration and the package ships no flat config.

On 2026-11-19, with the package files loaded directly and one dependency (requireindex) replaced by a three-line stand-in:

ESLintxss/no-location-href-assignxss/no-mixed-html
9.39.4reported location.href = getNextUrl()threw: Rule must be an object with a create method
10.15.0threw: context.getSourceCode is not a functionthrew: Rule must be an object with a create method

no-mixed-html is written as a bare function rather than an object with create, and both ESLint versions rejected it. no-location-href-assign calls context.getSourceCode(), which does not exist on ESLint 10.15.0. A project still on eslint-plugin-xss will lose both rules when it moves to ESLint 10.

There is no one-to-one replacement. For the location.href rule, the closest rule in browser-security is no-insecure-redirects. It reports a request-controlled value written to location.href as an open redirect (CWE-601), and it does not report every assignment. We did not diff it against the xss rule. Nothing maintained matches no-mixed-html, which works from naming conventions.

eslint-plugin-no-unsanitized

Mozilla builds and uses this plugin for its own products. It has two rules. no-unsanitized/property covers innerHTML and outerHTML assignments. no-unsanitized/method covers insertAdjacentHTML, createContextualFragment, setHTMLUnsafe, import(), and write and writeln on a receiver whose name matches document. Version 4.1.5 was published 2026-02-19, it has no runtime dependencies, and it is licensed MPL-2.0.

By default a value passes if it is a literal, or if it is wrapped in Sanitizer.escapeHTML or escapeHTML used as a tagged template, or if it goes through .setHTML(). A call to DOMPurify.sanitize(...) is reported until you list it:

eslint.config.js
import noUnsanitized from 'eslint-plugin-no-unsanitized';

export default [
  {
    ...noUnsanitized.configs.recommended,
    rules: {
      'no-unsanitized/property': [
        'error',
        { escape: { methods: ['DOMPurify.sanitize'] } },
      ],
      'no-unsanitized/method': [
        'error',
        { escape: { methods: ['DOMPurify.sanitize'] } },
      ],
    },
  },
];

With that config, el.innerHTML = DOMPurify.sanitize(u) was silent and el.innerHTML = other.sanitize(u) was reported, on both ESLint versions above.

What it misses, from the table: computed keys such as el['innerHTML'], srcdoc, Object.assign with an innerHTML key, and jQuery's .html().

eslint-plugin-browser-security (ours)

We maintain this plugin, so weigh this section accordingly. The XSS rules, all linked to their pages:

  • no-innerhtml: non-literal values written to innerHTML, outerHTML, srcdoc, insertAdjacentHTML, document.write and writeln, and createContextualFragment, including computed keys and Object.assign. This list comes from the rule source on main (commit 80e7983); the rule's own page lists only innerHTML and outerHTML.
  • no-postmessage-innerhtml, no-websocket-innerhtml, no-worker-message-innerhtml and no-filereader-innerhtml: the same sinks when the value arrives from a message event, a WebSocket, a Worker or a FileReader. no-innerhtml stands down on a value one of these rules can attribute, so a statement is reported once.
  • no-eval: eval, new Function and string arguments to setTimeout and setInterval.
  • require-postmessage-origin-check, no-incomplete-url-sanitization and no-unescaped-url-parameter: adjacent checks on message origins, URL host and scheme tests, and URLs built from unescaped parameters. no-unescaped-url-parameter is not in recommended in src/index.ts, although its page says it warns there. We did not run it for this page.

What it misses or gets wrong, and how we know:

  • Sanitizers are trusted by name. no-innerhtml accepts calls to functions named DOMPurify.sanitize, sanitize, sanitizeHtml, xss, purify, escapeHtml, escapeHTML and escape by default. In the run above, el.innerHTML = other.sanitize(u) was silent. Set trustedSanitizers to the names you actually use.
  • Mozilla's tagged-template form is reported. el.innerHTML = escapeHTML`<b>${userInput}</b>` was reported by no-innerhtml, because the tag is not a call it recognizes.
  • Frameworks. The rule page lists jQuery .html() and Angular [innerHTML] as not detected, and the table above confirms jQuery. For React, react/no-danger from eslint-plugin-react 7.37.5 reported dangerouslySetInnerHTML in a run on ESLint 10.13.0.
  • Requirements. The package declares Node >=22.13.0 in engines, which eslint-plugin-no-unsanitized does not.

The xss preset has a gap

browserSecurity.configs.xss enables only no-innerhtml and no-eval. Because no-innerhtml stands down on values it can attribute to a message event, a handler that writes e.data to innerHTML was reported by nothing under that preset in the run above, and reported by no-postmessage-innerhtml under recommended. Use recommended, or enable the four source-specific rules next to xss.

Where they overlap

On the first six rows of the table both plugins report the same statement, so with both enabled in full a single el.innerHTML = userInput produces two messages. The third config above removes that by turning off no-unsanitized/property and limiting no-unsanitized/method to the two sinks ours does not report. Running only one of them also works. Which one to keep depends on what you already write: if your code uses Mozilla's escapeHTML template convention, keep no-unsanitized; if it calls DOMPurify and you would rather not list it, keep browser-security.

The published CWE corpus run (2026-11-15, ESLint 10.14.0) has three vulnerable CWE-79 samples. no-unsanitized found two, Interlace's plugins together found two, eslint-plugin-security found one, and eslint-plugin-sonarjs found none. Three samples cannot rank plugins, and we wrote the corpus. Method and caveats are on Use alongside eslint-plugin-security.

What none of them catch

All of these rules read one file at a time and do not follow a value through function calls across files. A value that travels from a request to a template in another module needs taint analysis: CodeQL ships js/xss-family queries and Semgrep Code runs cross-file analysis, as covered in alternatives to eslint-plugin-security. Server-side reflected XSS, such as res.send(userInput), was not tested for this page.

Building secure JavaScript with Interlace? Star the repo to get new rules and CWE coverage as we ship them — or follow the AI-code-security benchmarks behind them.