The Real Fullstack

The Markdown XSS

We remove <script> tags. How did a comment still steal session cookies?

エンジニア8 分コード課題フローシミュレーター
angularreactmarkdowndompurify

この課題はまだ日本語に翻訳されていません。英語版を表示しています。

ミッション:Choose protections that keep legitimate formatting and links working while making injected HTML and scripts harmless.背景 · 学習目標 ▾

Fernpost lets users write comments in Markdown. The frontend converts it to HTML, strips <script> tags with a regular expression and assigns the result to innerHTML. Security found comments with onerror handlers, javascript: links and a fake login form.

ミッション: Choose protections that keep legitimate formatting and links working while making injected HTML and scripts harmless.

学習目標

  • Understand stored cross-site scripting (XSS).
  • Sanitize with an allow-list instead of deny-listing tags.
  • Restrict link schemes and add a Content Security Policy as defence in depth.
シミュレーションシミュレーション値
comment authorcomments APIcomments DBother readers' browsers
シミュレーションを実行
// comment preview (current)const html = marked.parse(comment.body).replace(/<script.*?<\/script>/gi, '');   // deny-listelement.innerHTML = html;                                                         // executes event handlers

簡略化

Seven comments with fixed properties. Real sanitizers handle many more vectors (mutation XSS, CSS, SVG). 教育用モデルです。数値は挙動を示すためのもので、ベンチマークではありません。

コンセプト

関連する課題

参考資料