feat(security): add OWASP Java Encoder and expose via XssWebAPI viewtool#35318
Conversation
…ool (fixes #24120) Integrates the OWASP Java Encoder (1.3.1) into dotCMS core as the standard context-aware output encoding library for XSS prevention. Changes: - bom/application/pom.xml, dotCMS/pom.xml: add org.owasp.encoder:encoder:1.3.1 - Xss.java: replace StringEscapeUtils.escapeHtml() with Encode.forHtml(); replace UtilMethods.encodeURL() with Encode.forUriComponent(); add new context-specific helpers: encodeForHTML, encodeForHTMLAttribute, encodeForJavaScript, encodeForCSS - VelocityRequestWrapper.java: replace htmlifyString() with Xss.encodeForHTML() in getParameter() for standards-compliant output encoding - XssWebAPI.java: expose all OWASP encoder contexts to Velocity templates via $xsstool — encodeForHTML, encodeForHTMLAttribute, encodeForJavaScript, encodeForURL, encodeForCSS; legacy strip/escape methods kept and deprecated Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…4120) Covers all 5 encoding contexts (HTML, HTML attribute, JavaScript, URL, CSS), null-safety, legacy methods, and XSS detection helpers. 22 tests, all passing. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Test ResultsTests covered
|
wezell
left a comment
There was a problem hiding this comment.
Also need to add the Viewtool to this pr - these files here:
and need to add the viewtool to the toolbox.xml so it can be used in velocity.
- Add $encode Velocity viewtool (OwaspEncoderTool) exposing full OWASP Java Encoder API: forHtml, forHtmlContent, forHtmlAttribute, forHtmlUnquotedAttribute, forCssString, forCssUrl, forUriComponent, forJavaScript, forJavaScriptAttribute, forJavaScriptBlock, forJavaScriptSource, forXml*, forCDATA, plus URL safety helpers (validateUrl, urlHasXSS, cleanUrl). Registered as $encode in toolbox.xml. - Wrap VelocityRequestWrapper XSS encoding in USE_OWASP_ENCODING_FOR_XSS_PARAMS config flag (default true) so it can be reverted to legacy htmlifyString if needed. Closes #24120 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Addressed both review comments in the latest commit: 1. Config flag for 2.
Registered in <p>$encode.forHtml($request.getParameter("name"))</p>
<a href="/search?q=$encode.forUriComponent($q)">Go</a>
<script>var msg = "$encode.forJavaScript($message)";</script> |
Covers every encoding context exposed by the viewtool: forHtml, forHtmlContent, forHtmlAttribute, forHtmlUnquotedAttribute, forCssString, forCssUrl, forUriComponent, forJavaScript and its block/attribute/source variants, forXml family, forCDATA, forJava, plus validateUrl / urlHasXSS / cleanUrl URL-safety helpers. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Added Coverage per context:
Two tests were corrected during development: |
|
@wezell Both changes from your review are now in place — config flag for the XSS encoding and the |
wezell
left a comment
There was a problem hiding this comment.
minor change needed - we should name the velocity tool $owasp
- toolbox.xml: rename velocity tool key from `encode` to `owasp` (per @wezell on toolbox.xml:211) - VelocityRequestWrapper: wrap USE_OWASP_ENCODING_FOR_XSS_PARAMS lookup in a Lazy<Boolean> instead of calling Config.getBooleanProperty on every request (per @jsanca on line 86) - Xss.java: remove dead `excludeList` field + `buildExcludeList()` method (per @jsanca on line 48; never read or called externally)
…er-24120 # Conflicts: # dotCMS/src/main/java/com/dotcms/rendering/velocity/viewtools/VelocityRequestWrapper.java # dotCMS/src/main/java/com/dotcms/rendering/velocity/viewtools/XssWebAPI.java # dotCMS/src/main/java/com/liferay/util/Xss.java # dotCMS/src/main/webapp/WEB-INF/toolbox.xml # dotCMS/src/test/java/com/dotcms/rendering/velocity/viewtools/OwaspEncoderToolTest.java
…nto feat/owasp-java-encoder-24120
|
Addressed the outstanding review feedback and merged latest @wezell
@jsanca
MergeResolved 5 merge conflicts with
Re-requesting review. |
|
Claude finished @mbiuki's task in 2m 25s —— View job PR Review — feat(security): add OWASP Java Encoder and expose via XssWebAPI viewtool
SummaryThe core integration (dependency, Issues1. Stale Javadoc in The class-level Javadoc still says // Line 20: "$encode" and lines 24-27 use $encode.forHtml(...) etc.
* <p>Registered in {@code toolbox.xml} under the key {@code encode}.
* <p>$encode.forHtml(...)After the toolbox key was changed to 2.
// Current (line 73):
final List<NameValuePair> params =
URLEncodedUtils.parse(url.getQuery(), StandardCharsets.UTF_8);
// Safer:
final String query = url.getQuery();
if (query == null) return false;
final List<NameValuePair> params = URLEncodedUtils.parse(query, StandardCharsets.UTF_8);3.
This matches the pattern requested by 4.
5. Line 74: Clean
|
Summary
Closes #24120
Integrates the OWASP Java Encoder (1.3.1) into dotCMS core as the standard context-aware output encoding library for XSS prevention, replacing legacy
StringEscapeUtilsandUtilMethods.encodeURL()calls.Changes
bom/application/pom.xml+dotCMS/pom.xml— addorg.owasp.encoder:encoder:1.3.1under the Security sectionXss.java— replaceStringEscapeUtils.escapeHtml()withEncode.forHtml(); replaceUtilMethods.encodeURL()withEncode.forUriComponent(); add new context-specific helpers:encodeForHTML(String)— HTML body contentencodeForHTMLAttribute(String)— quoted HTML attribute valuesencodeForJavaScript(String)— JavaScript string literalsencodeForCSS(String)— CSS strings/identifiersVelocityRequestWrapper.java— replaceUtilMethods.htmlifyString()withXss.encodeForHTML()ingetParameter()for standards-compliant encoding of XSS-detected parametersXssWebAPI.java— expose all OWASP encoder contexts to Velocity templates via$xsstool:$xsstool.encodeForHTML(value)$xsstool.encodeForHTMLAttribute(value)$xsstool.encodeForJavaScript(value)$xsstool.encodeForURL(value)$xsstool.encodeForCSS(value)strip,escape,escapeHTMLAttribmethods kept and marked@DeprecatedUsage in Velocity templates
Test plan
./mvnw install -pl :dotcms-core --am -DskipTests— BUILD SUCCESS$xsstool.escape()/$xsstool.strip()/$xsstool.hasXss()calls continue to work🤖 Generated with Claude Code