<!-- element content: escape & and < -->
<p>Tickets < $20 & free entry</p>
<!-- double-quoted attribute: escape & and " -->
<input value="say "hi" & wave">
<!-- UNQUOTED attribute: breakout with none of the five chars -->
<input value=x onmouseover=alert(1)>
<!-- entity encoding does NOT help here -->
<a href="javascript:alert(1)"> <!-- decoded before URL resolution -->
Named エンティティと数値エンティティ — そして XML の罠
WHATWG の HTML Living Standard はちょうど 2,231 個の named character reference を定義しています — & — のような常用組から ∳ のような珍種まで。あらゆる Unicode code point は数値でも書けます。10 進の → と 16 進の → はどちらも右向き矢印になり、16 進形式は Unicode チャートで見る U+2192 表記にそのまま対応するので検証が楽です。
罠は、この 2,231 個の名前が HTML 専用の語彙だという点です。XML が事前定義するのは 5 つだけ: & < > " '。 を XML パーサーに渡すと — application/xhtml+xml で配信される XHTML ページ、RSS/Atom フィード、単体の XML として処理される SVG ファイル — 警告ではなく致命的なパースエラーになります。移植可能な書き方は   です。逆方向では、' は XML と HTML5 には定義されていますが HTML 4 には存在しなかったため、古いパーサーや一部のメールレンダラーはそのまま表示してしまいます。どこでも通用するアポストロフィは ' で、PHP の htmlspecialchars のようなシリアライザーが ' ではなく ' を出力するのはこのためです。
手でデコードするときは、named reference が大文字小文字を区別することを忘れずに。Ä は Ä、ä は ä で、& は存在しますが &AMp; はありません。テーブル参照の前に入力を小文字化するデコーダーはテキストを静かに壊します。
// safe: never parsed as markup
el.textContent = userInput;
// XSS sink: parsed and executed
el.innerHTML = userInput;
// frameworks escape by default; escape hatches opt out
// Svelte: {comment.text} vs {@html post.trustedHtml}
// React: {comment.text} vs dangerouslySetInnerHTML
// Vue: interpolation vs v-html
// rich text: sanitize, don't encode
import DOMPurify from 'dompurify';
el.innerHTML = DOMPurify.sanitize(userHtml);
最終更新:
ツールについて
HTML エンティティエンコーダは <、>、&、" など HTML で特別な意味を持つ文字を <、>、&、" などのエンティティ参照に変換します。ユーザー入力を画面に出すときは必ずエンコードして、テキストとして扱わせるのが XSS 対策の基本です。
使い方
Pick Encode to convert raw characters into HTML entities, or Decode to do the reverse.
Paste the text or HTML snippet into the input box.
Read the encoded or decoded output on the right.
Click Copy to grab the result for use in your template, email, or doc.
Use the entity reference card at the bottom as a quick lookup for common characters.
主な使用例
Sanitising user-generated content before injecting it into a server-rendered page.
Embedding code snippets that contain < and > inside a Markdown or HTML document.
Preparing email HTML so apostrophes and ampersands render correctly across clients.
Decoding scraped HTML where entities like &quot; were double-encoded.
Verifying that an output escapes correctly before flagging an XSS bug.
Showing literal HTML markup in a tutorial or technical doc.
よくある質問
Q. Do I still need to encode if I use a templating engine?
A. Most modern engines (Svelte, React, Jinja, ERB) auto-escape by default. But once you reach for a "raw" or "unsafe" helper you take responsibility for encoding yourself.
Q. What is the difference between &#39; and &apos;?
A. Both encode an apostrophe. ' is technically XHTML/XML; some legacy HTML parsers do not recognise it, so ' is the safer choice for HTML output.
Q. Does encoding fix all XSS issues?
A. No. Context matters — JavaScript strings, URL attributes, and CSS each need their own escaping. Use a battle-tested sanitizer for HTML you want to render with markup intact.
Q. Why does decoding a non-encoded string return it unchanged?
A. There is nothing to decode. Decoding only swaps recognised entities back to characters; raw text passes through untouched.