HTML Escape is the process of converting special HTML characters (like <, >, &, ") into HTML entities (like <, >, &, ").
Unescape is the reverse process, converting HTML entities back to their original characters.
Why Do We Need HTML Escaping?
- Prevent XSS (Cross-Site Scripting): Escaping is the first line of defense against XSS attacks. It prevents malicious HTML/JavaScript injection and is essential when displaying user-generated content.
- Display HTML Code as Text: Show HTML examples in documentation, render code snippets on web pages, or debug template engines.
- Data Serialization: Safely embed HTML in JSON/XML, prepare content for API responses, or store HTML in databases.
- Email & Header Safety: Escape special characters in email subjects, prepare data for HTTP headers, or handle form data correctly.
Entity Format Comparison
| Character | Named Entity | Decimal Entity | Hex Entity |
|---|---|---|---|
| < | < | < | < |
| > | > | > | > |
| & | & | & | & |
| " | " | " | " |
| ' | ' | ' | ' |
Code Examples
JavaScript (Browser)
// Escape HTML
function escapeHtml(text) {
const map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g, m => map[m]);
}
// Unescape HTML
function unescapeHtml(html) {
const doc = new DOMParser().parseFromString(html, 'text/html');
return doc.documentElement.textContent;
}
// Usage
const escaped = escapeHtml('<script>alert("XSS")</script>');
console.log(escaped);
// <script>alert("XSS")</script>Node.js (using 'he' library)
const he = require('he');
// Encode
const encoded = he.encode('<div class="test">Hello & goodbye</div>');
console.log(encoded);
// <div class="test">Hello & goodbye</div>
// Decode
const decoded = he.decode('<div>Hello</div>');
console.log(decoded); // <div>Hello</div>
// Different formats
he.encode('<tag>', { useNamedReferences: true }); // <tag>
he.encode('<tag>', { decimal: true }); // <tag>
he.encode('<tag>', { hexadecimal: true }); // <tag>Python
import html
# Escape
escaped = html.escape('<script>alert("XSS")</script>')
print(escaped)
# <script>alert("XSS")</script>
# Unescape
unescaped = html.unescape('<div>Hello</div>')
print(unescaped) # <div>Hello</div>Security Best Practices
⚠️ Important Security Notes:
- Escaping is NOT Enough: Combine with Content Security Policy (CSP) and backend validation.
- Context Matters: Use appropriate escaping for HTML content, JavaScript strings, URLs, and SQL.
- Don't Trust User Input: Always validate and sanitize on both client and server side.
- Use Framework Built-ins: React, Vue, and Angular escape by default—leverage these features.
Common Use Cases
- XSS Prevention: Escape user input before displaying:
<script>alert('XSS')</script>→<script>alert('XSS')</script> - Displaying Code: Show HTML code examples on web pages by escaping tags.
- JSON with HTML: Prepare HTML fragments for JSON serialization.
- Email Content: Escape special characters in email subjects and headers.
- Template Debugging: Unescape rendered template output to see actual HTML.
การ escape HTML เป็นสิ่งสำคัญพื้นฐานสำหรับความปลอดภัยของเว็บและการแสดงข้อมูล ผมสร้างเครื่องมือนี้หลังจากจัดการกับช่องโหว่ XSS นับไม่ถ้วนระหว่างการตรวจสอบความปลอดภัยและความจำเป็นในการแสดงเนื้อหาที่ผู้ใช้สร้างอย่างปลอดภัย ไม่ว่าคุณจะทำความสะอาดอินพุตฟอร์ม, ดีบั๊กเทมเพลตเอนจิน, เตรียมข้อมูลสำหรับการ serialize JSON/XML หรือสอนแนวคิดความปลอดภัยของเว็บ การเข้ารหัส HTML entity ที่เหมาะสมเป็นสิ่งจำเป็น เครื่องมือนี้รองรับรูปแบบ entity สามแบบ: named entities (<, >), decimal entities (<, >) และ hexadecimal entities (<, >)—แต่ละแบบมีประโยชน์ในบริบทที่แตกต่างกัน การประมวลผลทั้งหมดเกิดขึ้นฝั่งไคลเอนต์ในเบราว์เซอร์ของคุณ ทำให้ปลอดภัยในการจัดการเนื้อหาที่ละเอียดอ่อนโดยไม่ส่งข้อมูลไปยังเซิร์ฟเวอร์ใดๆ
How to Use
สำหรับการ escape: วางข้อความ HTML หรือข้อความที่มีอักขระพิเศษลงในแผงอินพุต เครื่องมือจะแปลงอักขระอย่าง <, >, &, \", และ ' เป็น HTML entity เทียบเท่าโดยอัตโนมัติแบบเรียลไทม์ เลือกรูปแบบ entity ที่ต้องการ: Named (อ่านง่ายที่สุด เช่น &), Decimal (รองรับกันอย่างกว้างขวาง เช่น &) หรือ Hexadecimal (กระชับ เช่น &) สลับ 'เก็บการขึ้นบรรทัดใหม่' เพื่อรักษาหรือลบการขึ้นบรรทัดใหม่ สำหรับการ unescape: วาง HTML ที่มี entities และเครื่องมือจะถอดรหัสกลับเป็นอักขระเดิม ตัวเลือก 'เข้ารหัสอักขระทั้งหมด' จะ escape แม้แต่อักขระที่ไม่ใช่พิเศษ ซึ่งมีประโยชน์สำหรับความเข้ากันได้สูงสุดในกรณีพิเศษ
Common Use Cases
การป้องกัน XSS
Escape อินพุตของผู้ใช้ก่อนแสดง: '<script>alert(XSS)</script>' กลายเป็น '<script>alert(XSS)</script>'
การดีบั๊กเทมเพลต
Unescape เอาต์พุตเทมเพลตที่เรนเดอร์เพื่อดู HTML ที่สร้างจริงๆ
การ Escape สตริง JSON
เตรียมส่วนย่อย HTML สำหรับการ serialize JSON โดยการ escape เครื่องหมายคำพูดและอักขระพิเศษ
การแสดงโค้ด
แสดงตัวอย่างโค้ด HTML บนหน้าเว็บโดยการ escape แท็กเพื่อให้ปรากฏเป็นข้อความ
อีเมล HTML
Escape อักขระพิเศษในหัวเรื่องหรือส่วนหัวของอีเมล
การจัดเก็บฐานข้อมูล
Escape HTML ก่อนเก็บในฟิลด์ข้อความเพื่อป้องกันปัญหาการแสดงผล
Limitations & Important Notes
เครื่องมือนี้จัดการ HTML entities ที่กำหนดไว้ในข้อกำหนด HTML5 มันไม่ได้ลบหรือทำความสะอาดแอตทริบิวต์ HTML ที่อาจเป็นอันตราย (เช่น onclick, onerror) หรือโค้ด JavaScript—มันแค่ escape การแสดงอักขระเท่านั้น สำหรับการป้องกัน XSS ที่แท้จริง ให้รวมการ escape กับ Content Security Policy (CSP) และการตรวจสอบอินพุต เครื่องมือสันนิษฐานว่าเป็นการเข้ารหัส UTF-8 การเข้ารหัสอื่นๆ อาจให้ผลลัพธ์ที่ไม่คาดคิด สำหรับเอกสารขนาดใหญ่มาก (>5MB) ข้อจำกัดของหน่วยความจำเบราว์เซอร์อาจทำให้ช้าลง การเข้ารหัส entity เพิ่มขนาดข้อความ ไม่เหมาะสำหรับสถานการณ์ที่มีแบนด์วิดท์จำกัด