Skip to content

VBC-070: use-innerhtml

error

Category: core
Analysis: Regex (whole file)
File types: .js, .ts, .tsx, .astro
Scope: source code only, ignoring anything inside strings and comments
Excluded paths: **/test/**, **/tests/**, **/__tests__/**, **/*.test.*, **/*.spec.*
Tags: security

What it reports

Use of 'innerHTML' detected at line {line}. Use 'textContent' or a sanitizer to avoid XSS.

Flagged

js
el.innerHTML = markup;
js
el.innerHTML = "<b>" + name + "</b>";
js
el.innerHTML = `<b>${name}</b>`;

Not flagged

js
el.textContent = text;
js
alert.innerHTML = '<i class="fa-check"></i> <span id="t"></span>';
js
el.innerHTML = "";
js
el.innerHTML =
  "Tip: try <code>/status</code>, " +
  "or <code>/help</code> for all commands.";
js
if (el.innerHTML === "") { init(); }

Pattern

regex
\.innerHTML\s*=(?!=)(?!\s*(?:(['"])(?:(?!\1)[^\\])*\1|\s|\+)+[;\n])

Released under the MIT License.