Description
Check where each request value goes in HTML, a URL, JavaScript, or CSS. Using the wrong encoder can let the browser interpret input as code that reads page data or acts with the user’s privileges.
Recommended remediation
Keep the default escapeXml on JSTL’s <c:out>. In Thymeleaf, render text with th:text or [[...]]. Reserve escapeXml="false", th:utext, and [(...)] for HTML sanitized with a reviewed policy. For direct output, choose the encoder for the position where the value appears.
| Output context | Recommended handling | Caution |
|---|---|---|
| HTML text | Encode.forHtml or Encode.forHtmlContent |
Do not reuse it unchanged for attributes, JavaScript, CSS, or URLs. |
| Quoted ordinary HTML attribute | Encode.forHtmlAttribute |
Always quote values. Event handlers, URLs, style, and srcdoc need separate policies. |
| Unavoidable legacy unquoted ordinary attribute | Add quotes first; use Encode.forHtmlUnquotedAttribute only when conversion is not possible. |
The official API discourages this use; the application must control the delimiter following the value. |
| Path or query component of an application-owned URL | Encode.forUriComponent |
This does not validate an entire untrusted URL. Do not substitute deprecated forUri. |
| Entire untrusted URL | Parse and normalize it, allow-list the scheme (normally https) and required hosts, then apply forHtmlAttribute. |
Do not validate hosts with a string-prefix check alone. |
Quoted string inside <script> |
Encode.forJavaScriptBlock |
Do not insert data as code, identifiers, or template structure. Do not use forJavaScriptSource, which is unsafe within HTML. |
| Quoted string in an event handler | Encode.forJavaScriptAttribute |
Prefer removing inline event handlers. |
| Quoted CSS string | Encode.forCssString within a fixed property structure |
Quote the CSS string and do not let input control property names or stylesheet structure. |
CSS url(...) value |
Validate the URL or construct an application-owned URL, then use Encode.forCssUrl. |
forCssUrl does not validate the scheme or host. |
Apache Commons Text’s escapeHtml4 leaves apostrophes unchanged. Avoid it for single-quoted attributes. Check the output context before using Spring HtmlUtils.htmlEscape or a legacy encoder.
When rich HTML is required
If users need to write HTML, allow only the markup the feature requires. On the server, use OWASP Java HTML Sanitizer or a reviewed jsoup Safelist. Keep either library patched. Leave shared policies and sanitized output unchanged during use.
In the browser, use an up-to-date DOMPurify. Check browser support before using Element.setHTML(), and avoid relying on setHTMLUnsafe() alone. CSP, X-Content-Type-Options: nosniff, text/html;charset=UTF-8, and the HttpOnly, Secure, and SameSite cookie attributes can supplement output encoding.
Examples
Before
bio goes into the HTML body and next supplies the entire link destination.
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
public final class ProfileServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html;charset=UTF-8");
final PrintWriter out = response.getWriter();
String bio = request.getParameter("bio");
String next = request.getParameter("next");
out.println("<div class=\"bio\">" + bio + "</div>");
out.println("<a href=\"" + next + "\">계속</a>");
}
}
After
The servlet encodes bio as text and label as a quoted attribute value. For the link, it encodes profileId as a path component and appends it to the fixed /profiles/ path.
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.owasp.encoder.Encode;
public final class SafeProfileServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html;charset=UTF-8");
response.setHeader("X-Content-Type-Options", "nosniff");
final PrintWriter out = response.getWriter();
String bio = request.getParameter("bio");
String label = request.getParameter("label");
String profileId = request.getParameter("profileId");
out.println("<div class=\"bio\">" + Encode.forHtml(bio) + "</div>");
out.println("<span title=\"" + Encode.forHtmlAttribute(label) + "\">프로필</span>");
out.println("<a href=\"/profiles/" + Encode.forUriComponent(profileId) +
"\">계속</a>");
}
}
The policy combines FORMATTING and LINKS to retain permitted formatting and links.
import org.owasp.html.PolicyFactory;
import org.owasp.html.Sanitizers;
public final class RichHtmlSanitizer {
private static final PolicyFactory POLICY =
Sanitizers.FORMATTING.and(Sanitizers.LINKS);
public static String sanitize(String untrustedMarkup) {
return POLICY.sanitize(untrustedMarkup);
}
}
References
- OWASP Cross Site Scripting Prevention Cheat Sheet
- OWASP Java Encoder
- OWASP Java Encoder 1.4.0 API
- OWASP Java HTML Sanitizer releases
- jsoup releases
- DOMPurify guidance
- HTML Standard: safe and unsafe dynamic markup insertion
- MDN: Element.setHTML()
- Thymeleaf 3.1 tutorial
- Jakarta Tags 3.0 specification
- Jakarta Servlet 6.1
- OWASP ASVS 5.0.0
- CWE-79: Improper Neutralization of Input During Web Page Generation
- OWASP Top 10:2025 A05 Injection
- OWASP Top 10:2021 A03 Injection