Base64 Encoding: When and Why to Use It
Learn what Base64 encoding is, how it works, and when to use it. Understand the difference between encoding and encryption, common use cases, and best practices for web development.
Learn how HTML entity encoding protects your web applications from Cross-Site Scripting (XSS) attacks. Understand character encoding, when to encode user input, framework auto-escaping, and common vulnerabilities developers miss.
I'll never forget the first time I saw XSS exploited on a site I built. A comment field. Someone posted what looked like innocent text, but hidden inside was a script that stole session cookies. The client called me at 11 PM because users were getting logged out randomly. Turned out someone was hijacking sessions.
Cross-Site Scripting lets attackers inject malicious JavaScript into pages viewed by other users. When the browser renders that page, the injected code runs with full privileges - reading cookies, stealing tokens, modifying the page. It's been in the OWASP Top 10 forever, and I still catch it in code reviews regularly.
Not all XSS is created equal. Understanding the types helps you know where to look and how to defend.
HTML entity encoding is your first line of defense. It converts dangerous characters into their HTML entity equivalents. The browser displays them as text instead of interpreting them as code.
When someone tries to inject <script>alert('gotcha')</script>, encoding turns it into visible text. The browser shows the literal characters instead of executing the script.
Here's where I see people mess up: HTML encoding isn't a silver bullet. WHERE you're inserting user data determines WHAT encoding you need.
Modern frameworks escape output by default, which is amazing. But I've seen developers assume they're protected when they're not. Know your framework's limits.
Even with frameworks, certain patterns keep causing XSS. These slip through code reviews because they don't look obviously dangerous.
I get asked all the time: 'Can't I just filter out script tags?' No. These are different tools for different jobs.
Don't write your own encoding function. Seriously. Use what's battle-tested.
CSP is like a seatbelt for XSS. Even if your encoding fails somewhere, CSP limits what attackers can do. I implement it on everything now.
Every project at Šikulovi s.r.o. gets XSS testing. Manual plus automated. Here's my process:
After enough XSS incidents, these patterns became muscle memory. I don't even think about them anymore.
No single technique stops all XSS. I use HTML encoding as the foundation, but it's part of a system: context-aware encoding, framework auto-escaping, Content Security Policy, and safe coding patterns.
Know where user data flows through your app. Encode appropriately at every output point. Don't bypass framework protections without sanitization. Add CSP. Test regularly. XSS is one of those vulnerabilities that never fully goes away - it just waits for the one place you forgot to encode.
HTML entity encoding prevents XSS in HTML body context but is insufficient alone. Different contexts (JavaScript, URLs, CSS, HTML attributes) require context-specific encoding. Additionally, some XSS vectors like javascript: URLs in href attributes execute despite HTML encoding. Use a combination of context-aware encoding, framework auto-escaping, Content Security Policy, and safe coding patterns for full protection.
React escapes all values by default, but sometimes you genuinely need to render HTML—for example, content from a CMS, markdown rendering, or rich text editors. The deliberately alarming name reminds developers to sanitize content with a library like DOMPurify before using it. The name is a feature, not a bug—it forces conscious decisions about bypassing XSS protection.
Encoding converts special characters to safe representations (< becomes <) and preserves all input—the user sees exactly what they typed. Sanitization removes or modifies dangerous content (strips script tags, removes event handlers) and may alter the input. Use encoding when displaying plain text; use sanitization when you need to allow some HTML formatting while blocking dangerous elements.
No, CSP is defense in depth, not a replacement for encoding. CSP limits what scripts can execute if XSS occurs but does not prevent all XSS impacts—attackers can still modify page content, steal form data via CSS, or exfiltrate data through allowed endpoints. Some applications cannot use strict CSP due to legacy code. Always encode output properly and use CSP as an additional layer.
Use a sanitization library like DOMPurify (JavaScript), Bleach (Python), or HTML Purifier (PHP). These libraries parse HTML and remove dangerous elements (script, iframe) and attributes (onclick, onerror) while preserving safe formatting. Configure the sanitizer with an allowlist of permitted tags and attributes. Never build your own HTML sanitizer—the edge cases are numerous and subtle.
Modern frameworks provide strong defaults, but developers bypass them: using dangerouslySetInnerHTML/v-html without sanitization, inserting user data into href or src attributes, building DOM with innerHTML, embedding user data in inline scripts, or misunderstanding which contexts the framework protects. Framework protection only works when developers understand and follow the secure patterns.
Encode on display (output), not on storage (input). Store data in its original form and encode when rendering. This allows the same data to be safely rendered in different contexts (HTML, JSON, CSV) with appropriate encoding for each. Encoding on input causes double-encoding issues and makes it impossible to properly encode for contexts you did not anticipate at storage time.
The most exploited patterns include innerHTML assignments with user data, href attributes with javascript: URLs, event handler attributes containing user data, JSON embedded in script tags without encoding, URL fragments processed unsafely in JavaScript, and markdown or rich text rendering without sanitization. Code review should specifically check for these patterns.
Founder of CodeUtil. Web developer building tools I actually use. When I'm not coding, I experiment with productivity techniques (with mixed success).
Learn what Base64 encoding is, how it works, and when to use it. Understand the difference between encoding and encryption, common use cases, and best practices for web development.
SQL injection is still the #1 web attack. Learn the real difference between escaping and parameterized queries — with code examples in Python, PHP, and Node.js — and why one method leaves you exposed.
Learn how JWT claims work, explore registered, public, and private claims, and discover security best practices for implementing JSON Web Tokens in your applications.