This is an automated email from the ASF dual-hosted git repository. lukaszlenart pushed a commit to branch WW-5639-property-mutually-exclusive-escaping in repository https://gitbox.apache.org/repos/asf/struts.git
commit 19a385a80678ad4c45a1aa954ee1df72ddc956e2 Author: Lukasz Lenart <[email protected]> AuthorDate: Thu Nov 26 17:38:44 2026 +0100 WW-5639 fix(tags): escapeXml replaces escapeHtml instead of double-escaping Co-Authored-By: Claude Opus 5.5 <[email protected]> --- .../org/apache/struts2/components/Property.java | 12 ++++++------ .../java/org/apache/struts2/components/Text.java | 12 ++++++------ .../struts2/components/escape/StrutsTagEscaper.java | 5 +++-- .../site/resources/tags/property-attributes.html | 6 +++--- core/src/site/resources/tags/text-attributes.html | 6 +++--- .../org/apache/struts2/components/TextTest.java | 10 ++++++++++ .../components/escape/StrutsTagEscaperTest.java | 21 +++++++++++++++++++-- 7 files changed, 50 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/org/apache/struts2/components/Property.java b/core/src/main/java/org/apache/struts2/components/Property.java index 43d06daff..f6d5f30a2 100644 --- a/core/src/main/java/org/apache/struts2/components/Property.java +++ b/core/src/main/java/org/apache/struts2/components/Property.java @@ -44,9 +44,9 @@ import java.io.Writer; * <ul> * <li>default (String) - The default value to be used if <u>value</u> attribute is null</li> * <li>escapeCsv (Boolean) - Escape CSV. Defaults to false</li> - * <li>escapeHtml (Boolean) - Escape HTML. Defaults to true</li> - * <li>escapeJavaScript (Boolean) - Escape JavaScript. Defaults to false</li> - * <li>escapeXml (Boolean) - Escape XML. Defaults to false</li> + * <li>escapeHtml (Boolean) - Escape HTML. Defaults to true. When false the value is emitted as raw HTML, an XSS risk unless the value is trusted</li> + * <li>escapeJavaScript (Boolean) - Escape for a JavaScript string literal, applied after HTML escaping unless escapeHtml is false. Escapes / as \/, so </b> becomes <\/b>. Defaults to false</li> + * <li>escapeXml (Boolean) - Escape XML, replacing HTML escaping. Defaults to false</li> * <li>value (Object) - value to be displayed</li> * </ul> * @@ -107,12 +107,12 @@ public class Property extends Component { this.defaultValue = defaultValue; } - @StrutsTagAttribute(description="Whether to escape HTML", type="Boolean", defaultValue="true") + @StrutsTagAttribute(description="Whether to escape HTML. When false the value is emitted as raw HTML, an XSS risk unless the value is trusted", type="Boolean", defaultValue="true") public void setEscapeHtml(boolean escape) { this.escapeHtml = escape; } - @StrutsTagAttribute(description="Whether to escape Javascript", type="Boolean", defaultValue="false") + @StrutsTagAttribute(description="Whether to escape for a JavaScript string literal, applied after HTML escaping unless escapeHtml is false. Escapes / as \\/, which breaks HTML markup in the value", type="Boolean", defaultValue="false") public void setEscapeJavaScript(boolean escapeJavaScript) { this.escapeJavaScript = escapeJavaScript; } @@ -131,7 +131,7 @@ public class Property extends Component { this.escapeCsv = escapeCsv; } - @StrutsTagAttribute(description="Whether to escape XML", type="Boolean", defaultValue="false") + @StrutsTagAttribute(description="Whether to escape XML, replacing HTML escaping", type="Boolean", defaultValue="false") public void setEscapeXml(boolean escapeXml) { this.escapeXml = escapeXml; } diff --git a/core/src/main/java/org/apache/struts2/components/Text.java b/core/src/main/java/org/apache/struts2/components/Text.java index fa47d68b4..3c268a74f 100644 --- a/core/src/main/java/org/apache/struts2/components/Text.java +++ b/core/src/main/java/org/apache/struts2/components/Text.java @@ -67,9 +67,9 @@ import java.util.List; * * <ul> * <li>name* (String) - the i18n message key</li> - * <li>escapeHtml (Boolean) - Escape HTML. Defaults to false</li> - * <li>escapeJavaScript (Boolean) - Escape JavaScript. Defaults to false</li> - * <li>escapeXml (Boolean) - Escape XML. Defaults to false</li> + * <li>escapeHtml (Boolean) - Escape HTML. Defaults to false. When false the value is emitted as raw HTML, an XSS risk unless the value is trusted</li> + * <li>escapeJavaScript (Boolean) - Escape for a JavaScript string literal, applied after HTML escaping unless escapeHtml is false. Escapes / as \/, so </b> becomes <\/b>. Defaults to false</li> + * <li>escapeXml (Boolean) - Escape XML, replacing HTML escaping. Defaults to false</li> * <li>escapeCsv (Boolean) - Escape CSV. Defaults to false</li> * </ul> * @@ -147,17 +147,17 @@ public class Text extends ContextBean implements Param.UnnamedParametric { this.name = name; } - @StrutsTagAttribute(description="Whether to escape HTML", type="Boolean", defaultValue="false") + @StrutsTagAttribute(description="Whether to escape HTML. When false the value is emitted as raw HTML, an XSS risk unless the value is trusted", type="Boolean", defaultValue="false") public void setEscapeHtml(boolean escape) { this.escapeHtml = escape; } - @StrutsTagAttribute(description="Whether to escape Javascript", type="Boolean", defaultValue="false") + @StrutsTagAttribute(description="Whether to escape for a JavaScript string literal, applied after HTML escaping unless escapeHtml is false. Escapes / as \\/, which breaks HTML markup in the value", type="Boolean", defaultValue="false") public void setEscapeJavaScript(boolean escapeJavaScript) { this.escapeJavaScript = escapeJavaScript; } - @StrutsTagAttribute(description="Whether to escape XML", type="Boolean", defaultValue="false") + @StrutsTagAttribute(description="Whether to escape XML, replacing HTML escaping", type="Boolean", defaultValue="false") public void setEscapeXml(boolean escapeXml) { this.escapeXml = escapeXml; } diff --git a/core/src/main/java/org/apache/struts2/components/escape/StrutsTagEscaper.java b/core/src/main/java/org/apache/struts2/components/escape/StrutsTagEscaper.java index 95d9ed767..4096ec90d 100644 --- a/core/src/main/java/org/apache/struts2/components/escape/StrutsTagEscaper.java +++ b/core/src/main/java/org/apache/struts2/components/escape/StrutsTagEscaper.java @@ -21,7 +21,8 @@ package org.apache.struts2.components.escape; import org.apache.commons.text.StringEscapeUtils; /** - * Default {@link TagEscaper}: applies the selected escapers in the order HTML, JavaScript, XML, CSV. + * Default {@link TagEscaper}: applies the selected escapers in the order HTML, JavaScript, XML, CSV. XML escaping + * replaces HTML escaping, as it already covers the HTML-sensitive characters. * <p> * Each step is a protected hook, so an application needing different handling for one context (e.g. letting a * safelist of HTML markup through) can extend this class and override that hook only. @@ -31,7 +32,7 @@ public class StrutsTagEscaper implements TagEscaper { @Override public String escape(EscapeConfig config, String value) { String result = value; - if (config.html()) { + if (config.html() && !config.xml()) { result = escapeHtml(result); } if (config.javaScript()) { diff --git a/core/src/site/resources/tags/property-attributes.html b/core/src/site/resources/tags/property-attributes.html index 1ff16ddae..883b2959c 100644 --- a/core/src/site/resources/tags/property-attributes.html +++ b/core/src/site/resources/tags/property-attributes.html @@ -35,7 +35,7 @@ <td class="tag-attribute">true</td> <td class="tag-attribute">false</td> <td class="tag-attribute">Boolean</td> - <td class="tag-attribute">Whether to escape HTML</td> + <td class="tag-attribute">Whether to escape HTML. When false the value is emitted as raw HTML, an XSS risk unless the value is trusted</td> </tr> <tr> <td class="tag-attribute">escapeJavaScript</td> @@ -43,7 +43,7 @@ <td class="tag-attribute">false</td> <td class="tag-attribute">false</td> <td class="tag-attribute">Boolean</td> - <td class="tag-attribute">Whether to escape Javascript</td> + <td class="tag-attribute">Whether to escape for a JavaScript string literal, applied after HTML escaping unless escapeHtml is false. Escapes / as \/, which breaks HTML markup in the value</td> </tr> <tr> <td class="tag-attribute">escapeXml</td> @@ -51,7 +51,7 @@ <td class="tag-attribute">false</td> <td class="tag-attribute">false</td> <td class="tag-attribute">Boolean</td> - <td class="tag-attribute">Whether to escape XML</td> + <td class="tag-attribute">Whether to escape XML, replacing HTML escaping</td> </tr> <tr> <td class="tag-attribute">performClearTagStateForTagPoolingServers</td> diff --git a/core/src/site/resources/tags/text-attributes.html b/core/src/site/resources/tags/text-attributes.html index 465bb52fd..fb3939239 100644 --- a/core/src/site/resources/tags/text-attributes.html +++ b/core/src/site/resources/tags/text-attributes.html @@ -27,7 +27,7 @@ <td class="tag-attribute">false</td> <td class="tag-attribute">false</td> <td class="tag-attribute">Boolean</td> - <td class="tag-attribute">Whether to escape HTML</td> + <td class="tag-attribute">Whether to escape HTML. When false the value is emitted as raw HTML, an XSS risk unless the value is trusted</td> </tr> <tr> <td class="tag-attribute">escapeJavaScript</td> @@ -35,7 +35,7 @@ <td class="tag-attribute">false</td> <td class="tag-attribute">false</td> <td class="tag-attribute">Boolean</td> - <td class="tag-attribute">Whether to escape Javascript</td> + <td class="tag-attribute">Whether to escape for a JavaScript string literal, applied after HTML escaping unless escapeHtml is false. Escapes / as \/, which breaks HTML markup in the value</td> </tr> <tr> <td class="tag-attribute">escapeXml</td> @@ -43,7 +43,7 @@ <td class="tag-attribute">false</td> <td class="tag-attribute">false</td> <td class="tag-attribute">Boolean</td> - <td class="tag-attribute">Whether to escape XML</td> + <td class="tag-attribute">Whether to escape XML, replacing HTML escaping</td> </tr> <tr> <td class="tag-attribute">name</td> diff --git a/core/src/test/java/org/apache/struts2/components/TextTest.java b/core/src/test/java/org/apache/struts2/components/TextTest.java index 74b723927..d771a6a19 100644 --- a/core/src/test/java/org/apache/struts2/components/TextTest.java +++ b/core/src/test/java/org/apache/struts2/components/TextTest.java @@ -20,6 +20,7 @@ package org.apache.struts2.components; import org.apache.struts2.ActionContext; import org.apache.struts2.StrutsInternalTestCase; +import org.apache.struts2.TestAction; import org.apache.struts2.components.escape.EscapeConfig; import org.apache.struts2.components.escape.TagOrigin; @@ -38,6 +39,15 @@ public class TextTest extends StrutsInternalTestCase { assertEquals("unknown.text.key", escaper.value); } + public void testTextXmlSupersedesHtml() { + ActionContext.getContext().getValueStack().push(new TestAction()); + Text text = newText("foo.escape.html"); + text.setEscapeHtml(true); + text.setEscapeXml(true); + + assertEquals("1 < 2", render(text, "")); + } + private Text newText(String name) { Text text = new Text(ActionContext.getContext().getValueStack()); container.inject(text); diff --git a/core/src/test/java/org/apache/struts2/components/escape/StrutsTagEscaperTest.java b/core/src/test/java/org/apache/struts2/components/escape/StrutsTagEscaperTest.java index a1ddf8d65..baba83499 100644 --- a/core/src/test/java/org/apache/struts2/components/escape/StrutsTagEscaperTest.java +++ b/core/src/test/java/org/apache/struts2/components/escape/StrutsTagEscaperTest.java @@ -19,6 +19,8 @@ package org.apache.struts2.components.escape; import org.apache.commons.text.StringEscapeUtils; +import org.apache.logging.log4j.Level; +import org.apache.struts2.test.LogCapture; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -61,9 +63,24 @@ public class StrutsTagEscaperTest { } @Test - public void htmlThenXmlDoubleEscapes() { + public void xmlSupersedesHtml() { assertThat(escaper.escape(config().html(true).xml(true).build(), VALUE)) - .isEqualTo("&quot;x' &amp; &lt;b&gt;&lt;/b&gt;"); + .isEqualTo(""x' & <b></b>"); + } + + @Test + public void xmlSupersedesHtmlAndStillChainsCsv() { + assertThat(escaper.escape(config().html(true).xml(true).csv(true).build(), VALUE)) + .isEqualTo(StringEscapeUtils.escapeCsv(StringEscapeUtils.escapeXml10(VALUE))); + } + + @Test + public void xmlSupersedingHtmlLogsNoWarning() { + try (LogCapture log = new LogCapture(StrutsTagEscaper.class)) { + escaper.escape(config().html(true).xml(true).build(), VALUE); + + assertThat(log.messages(Level.WARN)).isEmpty(); + } } @Test
