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.
import noUnsanitized from 'eslint-plugin-no-unsanitized';
export default [noUnsanitized.configs.recommended];npm install --save-dev eslint-plugin-no-unsanitizedTypeScript 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.
| Statement | no-unsanitized | browser-security (ours) |
|---|---|---|
el.innerHTML = userInput | reports | reports |
el.outerHTML = userInput | reports | reports |
el.innerHTML = `<b>${userInput}</b>` | reports | reports |
el.insertAdjacentHTML('beforeend', userInput) | reports | reports |
document.write(userInput) | reports | reports |
range.createContextualFragment(userInput) | reports | reports |
el['innerHTML'] = userInput | silent | reports |
iframe.srcdoc = userInput | silent | reports |
Object.assign(el, { innerHTML: userInput }) | silent | reports |
el.setHTMLUnsafe(userInput) | reports | silent |
import(userInput) (code loading, not DOM) | reports | silent |
el.innerHTML = DOMPurify.sanitize(userInput) | reports | silent |
el.innerHTML = escapeHTML`<b>${userInput}</b>` | silent | reports |
message handler writing e.data to innerHTML | reports | reports (no-postmessage-innerhtml) |
$(el).html(userInput) | silent | silent |
el.innerHTML = '<p>static</p>' | silent | silent |
el.textContent = userInput | silent | silent |
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:
| ESLint | xss/no-location-href-assign | xss/no-mixed-html |
|---|---|---|
| 9.39.4 | reported location.href = getNextUrl() | threw: Rule must be an object with a create method |
| 10.15.0 | threw: context.getSourceCode is not a function | threw: 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:
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 toinnerHTML,outerHTML,srcdoc,insertAdjacentHTML,document.writeandwriteln, andcreateContextualFragment, including computed keys andObject.assign. This list comes from the rule source onmain(commit80e7983); the rule's own page lists onlyinnerHTMLandouterHTML.no-postmessage-innerhtml,no-websocket-innerhtml,no-worker-message-innerhtmlandno-filereader-innerhtml: the same sinks when the value arrives from a message event, a WebSocket, a Worker or aFileReader.no-innerhtmlstands down on a value one of these rules can attribute, so a statement is reported once.no-eval:eval,new Functionand string arguments tosetTimeoutandsetInterval.require-postmessage-origin-check,no-incomplete-url-sanitizationandno-unescaped-url-parameter: adjacent checks on message origins, URL host and scheme tests, and URLs built from unescaped parameters.no-unescaped-url-parameteris not inrecommendedinsrc/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-innerhtmlaccepts calls to functions namedDOMPurify.sanitize,sanitize,sanitizeHtml,xss,purify,escapeHtml,escapeHTMLandescapeby default. In the run above,el.innerHTML = other.sanitize(u)was silent. SettrustedSanitizersto the names you actually use. - Mozilla's tagged-template form is reported.
el.innerHTML = escapeHTML`<b>${userInput}</b>`was reported byno-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-dangerfromeslint-plugin-react7.37.5 reporteddangerouslySetInnerHTMLin a run on ESLint 10.13.0. - Requirements. The package declares Node
>=22.13.0inengines, whicheslint-plugin-no-unsanitizeddoes 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.
Related
- Security plugins in one flat config shows
no-unsanitizedandbrowser-securitynext toeslint-plugin-securityand the Node plugins. eslint-plugin-browser-securitylists every rule in the plugin.- Flat config migration covers moving off
.eslintrc.
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.
Migrate from eslint-plugin-security-node
Rule-by-rule mapping from eslint-plugin-security-node (last published Jan 2024) to maintained Interlace equivalents
Security plugins in one flat config
One eslint.config.js that runs eslint-plugin-security, no-unsanitized and the Interlace security plugins together on ESLint 9 and 10, with overlapping rules turned off.