flawopen.com/Cross-Site Scripting/Javascript

Cross-Site Scripting in Javascript

Élevée CWE-79 Draft — pending review
Language: English Português (Brasil) Español Français Deutsch Русский 简体中文 日本語 हिन्दी 한국어 Bahasa Indonesia
Explication simple

Imagine a guestbook where a visitor writes a note containing hidden script tags that execute in the browser of the next person who opens the page.

Termes clés sur cette page
output encoding
Converting special markup characters into safe entities so browsers display them as text rather than script.

Ce qui se passe

Cross-site scripting happens when untrusted user input is inserted into the DOM without proper HTML output encoding.

Impact dans le monde réel

In 2005, the Samy XSS worm infected over 1 million user profiles on MySpace in under 20 hours, forcing the platform offline.

Documented historical AppSec case study.

Vulnérable vs corrigé

VULNERABLE
// comment text parsed directly as HTML
function renderComment(el, comment) {
  el.innerHTML =
    `

${comment}

`; }
FIXED
// comment text inserted as safe plain text
function renderComment(el, comment) {
  const p = document.createElement('p');
  p.textContent = comment;
  el.appendChild(p);
}

Pourquoi la correction fonctionne

textContent inserts values strictly as plain text, preventing the browser from parsing markup into executable elements.

Pièges spécifiques au langage

React dangerouslySetInnerHTML

Explicitly disables React auto-escaping. Must be paired with DOMPurify if used.

Idées reçues courantes

"Frameworks prevent all XSS"

Template escape hatches and direct DOM APIs (innerHTML, outerHTML) bypass framework defenses.

Comment vérifier si vous êtes concerné

grep -rn "\.innerHTML\s*=" --include="*.js" --include="*.ts" . grep -rn "dangerouslySetInnerHTML" --include="*.jsx" --include="*.tsx" .
Configure ESLint rule react/no-danger and DOMPurify for cases where HTML formatting is genuinely required.

Liste de contrôle de prévention

Foire aux questions

Does Content Security Policy (CSP) replace escaping?

No. CSP provides defense-in-depth, but output encoding remains the primary defense.

Références

Voir aussi : SQL Injection Command InjectionPath Traversal