Use framework-provided escaping facilities where available. Avoid building HTML strings with untrusted input and assuming that a single generic escaping function is safe for every context.
Common Mistakes
- Assuming HTML validation prevents malicious requests.
- Inserting untrusted content with unsafe HTML rendering.
- Giving embedded content more sandbox permissions than necessary.
- Trusting hidden form fields for authorization or pricing.
- Assuming target="_blank" alone is a complete security strategy.
Practice Exercises
- Create an external link with target="_blank" and explicit rel attributes.
- Build a sandboxed iframe and explain its restrictions.
- Explain why textContent is safer than interpreting untrusted input as HTML.
- List three server-side protections that an HTML form cannot provide by itself.
Revision Quiz
- What is XSS?
- What does noopener prevent?
- What does iframe sandbox do?
- Should untrusted user input be rendered as executable HTML?
- Are hidden inputs trustworthy for access control?
- Can HTML validation replace server-side validation?
Answer Key and Explanations
- Cross-site scripting — It involves unsafe content being interpreted as executable script or markup.
- Access through window.opener — It helps isolate a newly opened browsing context from its opener.
- Restricts embedded content capabilities — Permissions can be granted selectively.
- No. Render untrusted input as text or sanitize rich HTML with an appropriate sanitizer.
- No. Users can inspect and change client-submitted values.
- No. The server must validate and authorize requests independently.