[
https://issues.apache.org/jira/browse/WW-5830?focusedWorklogId=1056194&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-1056194
]
ASF GitHub Bot logged work on WW-5830:
--------------------------------------
Author: ASF GitHub Bot
Created on: 20/Nov/26 11:15
Start Date: 20/Nov/26 11:15
Worklog Time Spent: 10m
Work Description: lukaszlenart commented on code in PR #2088:
URL: https://github.com/apache/struts/pull/2088#discussion_r4641765775
##########
core/src/main/resources/template/simple/form-common.ftl:
##########
@@ -33,10 +33,10 @@
name="${attributes.name}"<#rt/>
</#if>
<#if attributes.onsubmit?has_content>
- onsubmit="<#outputformat
'JavaScript'>${attributes.onsubmit}</#outputformat>"<#rt/>
+ onsubmit="${attributes.onsubmit}"<#rt/>
</#if>
<#if attributes.onreset?has_content>
- onreset="<#outputformat
'JavaScript'>${attributes.onreset}</#outputformat>"<#rt/>
+ onreset="${attributes.onreset}"<#rt/>
Review Comment:
`onreset` ends up twice on the `<form>` element: once here and again in
`simple/form.ftl:23`. The same happens in `html5/form.ftl` (lines 36 and 66)
and in `xhtml/form.ftl` (lines 24 and 27, where the default
`clearErrorMessages(this);clearErrorLabels(this);` copy and the user-supplied
one are both written). A duplicate attribute is a parse error and the browser
keeps only the first one.
This predates the PR, but since each of these lines is being edited here,
please leave a single `onreset` per theme. That also removes the cause of the
vacuous assertion in `testFormEventsAreEscapedInEveryTheme`.
##########
core/src/test/java/org/apache/struts2/views/jsp/ui/EventHandlerEscapeTest.java:
##########
@@ -0,0 +1,213 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.struts2.views.jsp.ui;
+
+import org.apache.struts2.views.jsp.AbstractUITagTest;
+
+import jakarta.servlet.jsp.JspException;
+import jakarta.servlet.jsp.tagext.Tag;
+
+public class EventHandlerEscapeTest extends AbstractUITagTest {
+
+ private static final String PAYLOAD = "x\" autofocus onfocus=\"alert(1)";
+ private static final String ESCAPED = "x" autofocus
onfocus="alert(1)";
+ private static final String[] THEMES = {"simple", "xhtml", "css_xhtml",
"html5"};
+
+ public void testScriptingEventsAreEscapedInEveryTheme() throws Exception {
+ for (String theme : THEMES) {
+ TextFieldTag tag = new TextFieldTag();
+ tag.setTheme(theme);
+ tag.setName("foo");
+ tag.setTitle(PAYLOAD);
+ tag.setOnclick(PAYLOAD);
+ tag.setOnchange("a && b < c");
+
+ String out = render(tag);
+
+ assertTrue(theme + ": " + out, out.contains("title=\"" + ESCAPED +
"\""));
+ assertTrue(theme + ": " + out, out.contains("onclick=\"" + ESCAPED
+ "\""));
+ assertTrue(theme + ": " + out, out.contains("onchange=\"a
&& b < c\""));
+ }
+ }
+
+ public void testLegitimateScriptingEventIsRendered() throws Exception {
+ for (String theme : THEMES) {
+ TextFieldTag tag = new TextFieldTag();
+ tag.setTheme(theme);
+ tag.setName("foo");
+ tag.setOnclick("doIt();");
+
+ assertTrue(theme, render(tag).contains("onclick=\"doIt();\""));
+ }
+ }
+
+ public void testFormEventsAreEscapedInEveryTheme() throws Exception {
+ for (String theme : THEMES) {
+ FormTag tag = new FormTag();
+ tag.setTheme(theme);
+ tag.setOnsubmit(PAYLOAD);
+ tag.setOnreset(PAYLOAD);
+
+ String out = render(tag);
+
+ assertTrue(theme + ": " + out, out.contains("onsubmit=\"" +
ESCAPED + "\""));
+ assertTrue(theme + ": " + out, out.contains("onreset=\"" + ESCAPED
+ "\""));
+ }
+ }
+
+ public void testDoubleSelectEventsAreEscaped() throws Exception {
+ DoubleSelectTag tag = new DoubleSelectTag();
+ tag.setName("foo");
+ tag.setDoubleName("bar");
+ tag.setList("{'a'}");
+ tag.setDoubleList("{'b'}");
+ tag.setFormName("inputForm");
+ tag.setDoubleOnclick(PAYLOAD);
+
+ assertTrue(render(tag).contains("onclick=\"" + ESCAPED + "\""));
+ }
+
+ public void testOptionTransferSelectEventsAreEscaped() throws Exception {
+ OptionTransferSelectTag tag = new OptionTransferSelectTag();
+ tag.setName("foo");
+ tag.setList("{'a'}");
+ tag.setDoubleName("bar");
+ tag.setDoubleList("{'b'}");
+ tag.setDoubleOnclick(PAYLOAD);
+ tag.setDoubleAccesskey(PAYLOAD);
+
+ String out = render(tag);
+
+ assertTrue(out, out.contains("onclick=\"" + ESCAPED + "\""));
+ assertTrue(out, out.contains("accesskey=\"" + ESCAPED + "\""));
+ }
+
+ public void testJsTooltipIsEscaped() throws Exception {
+ FormTag formTag = new FormTag();
+ formTag.setTheme("xhtml");
+ formTag.setPageContext(pageContext);
+ formTag.setAction("testAction");
+
+ TextFieldTag tag = new TextFieldTag();
+ tag.setTheme("xhtml");
+ tag.setPageContext(pageContext);
+ tag.setName("foo");
+ tag.setLabel("foo");
+ tag.setTooltip(PAYLOAD);
+ tag.setTooltipConfig("#{'jsTooltipEnabled':'true'}");
+
+ formTag.doStartTag();
+ tag.doStartTag();
+ tag.doEndTag();
+ formTag.doEndTag();
+
+ String out = writer.toString();
+ // ?js_string backslash-escapes the quote before the HTML entity
encoding applies on top.
+ assertTrue(out, out.contains("'content', 'x\\" autofocus
onfocus=\\"alert(1)'"));
+ }
+
+ public void testJsTooltipApostropheCannotBreakOutOfTheJsString() throws
Exception {
+ String payload = "it's <script>alert(1)</script>";
+
+ FormTag formTag = new FormTag();
+ formTag.setTheme("xhtml");
+ formTag.setPageContext(pageContext);
+ formTag.setAction("testAction");
+
+ TextFieldTag tag = new TextFieldTag();
+ tag.setTheme("xhtml");
+ tag.setPageContext(pageContext);
+ tag.setName("foo");
+ tag.setLabel("foo");
+ tag.setTooltip(payload);
+ tag.setTooltipConfig("#{'jsTooltipEnabled':'true'}");
+
+ formTag.doStartTag();
+ tag.doStartTag();
+ tag.doEndTag();
+ formTag.doEndTag();
+
+ String out = writer.toString();
+ // The raw apostrophe must not reach the markup unescaped: that would
close the
+ // JS string literal early and let the rest run as script on mouseover.
+ assertFalse(out, out.contains("'it's"));
+ assertTrue(out, out.contains("'content', 'it\\'s
<script>alert(1)<\\/script>'"));
+ }
+
+ public void testJsTooltipDelayApostropheCannotBreakOutOfTheJsString()
throws Exception {
+ String payload = "1000'+alert(1)+'";
+
+ FormTag formTag = new FormTag();
+ formTag.setTheme("xhtml");
+ formTag.setPageContext(pageContext);
+ formTag.setAction("testAction");
+
+ TextFieldTag tag = new TextFieldTag();
+ tag.setTheme("xhtml");
+ tag.setPageContext(pageContext);
+ tag.setName("foo");
+ tag.setLabel("foo");
+ tag.setTooltip("foo");
+ tag.setTooltipConfig("#{'jsTooltipEnabled':'true'}");
+ tag.setTooltipDelay(payload);
+
+ formTag.doStartTag();
+ tag.doStartTag();
+ tag.doEndTag();
+ formTag.doEndTag();
+
+ String out = writer.toString();
+ assertFalse(out, out.contains("'1000'+"));
+ assertTrue(out, out.contains("'delay',
'1000\\'+alert(1)+\\''"));
+ }
+
+ public void testJsTooltipCssClassApostropheCannotBreakOutOfTheJsString()
throws Exception {
+ String payload = "x'+alert(1)+'";
+
+ FormTag formTag = new FormTag();
+ formTag.setTheme("xhtml");
+ formTag.setPageContext(pageContext);
+ formTag.setAction("testAction");
+
+ TextFieldTag tag = new TextFieldTag();
+ tag.setTheme("xhtml");
+ tag.setPageContext(pageContext);
+ tag.setName("foo");
+ tag.setLabel("foo");
+ tag.setTooltip("foo");
+ tag.setTooltipConfig("#{'jsTooltipEnabled':'true'}");
+ tag.setTooltipCssClass(payload);
+
+ formTag.doStartTag();
+ tag.doStartTag();
+ tag.doEndTag();
+ formTag.doEndTag();
+
+ String out = writer.toString();
+ assertFalse(out, out.contains("'x'+"));
+ assertTrue(out, out.contains("'styleClass',
'x\\'+alert(1)+\\''"));
+ }
+
+ private String render(Tag tag) throws JspException {
+ tag.setPageContext(pageContext);
+ tag.doStartTag();
+ tag.doEndTag();
+ return writer.toString();
Review Comment:
`writer` is created once in `AbstractTagTest.setUp()` and never cleared, so
`render()` returns the accumulated output of every earlier iteration. In the
`THEMES` loops the `contains()` assertions for `xhtml`, `css_xhtml` and `html5`
are satisfied by what `simple` already wrote. Reverting
`html5/scripting-events.ftl` alone leaves
`testScriptingEventsAreEscapedInEveryTheme`,
`testLegitimateScriptingEventIsRendered` and
`testFormEventsAreEscapedInEveryTheme` green.
Please reset the buffer before rendering
(`writer.getBuffer().setLength(0)`), and confirm each theme's assertion fails
with only that theme's template reverted.
##########
core/src/main/resources/template/simple/optiontransferselect.ftl:
##########
@@ -143,49 +143,49 @@
style="${attributes.doubleStyle}"
</#if><#t/>
<#if attributes.doubleOnclick??><#t/>
- onclick="<#outputformat
'JavaScript'>${attributes.doubleOnclick}</#outputformat>"
+ onclick="${attributes.doubleOnclick}"
</#if><#t/>
<#if attributes.doubleOndblclick??><#t/>
- ondblclick="<#outputformat
'JavaScript'>${attributes.doubleOndblclick}</#outputformat>"
+ ondblclick="${attributes.doubleOndblclick}"
</#if><#t/>
<#if attributes.doubleOnmousedown??><#t/>
- onmousedown="<#outputformat
'JavaScript'>${attributes.doubleOnmousedown}</#outputformat>"
+ onmousedown="${attributes.doubleOnmousedown}"
</#if><#t/>
<#if attributes.doubleOnmouseup??><#t/>
- onmouseup="<#outputformat
'JavaScript'>${attributes.doubleOnmouseup}</#outputformat>"
+ onmouseup="${attributes.doubleOnmouseup}"
</#if><#t/>
<#if attributes.doubleOnmousemove??><#t/>
- onmousemove="<#outputformat
'JavaScript'>${attributes.doubleOnmousemove}</#outputformat>"
+ onmousemove="${attributes.doubleOnmousemove}"
</#if><#t/>
<#if attributes.doubleOnmouseover??><#t/>
- onmouseover="<#outputformat
'JavaScript'>${attributes.doubleOnmouseover}</#outputformat>"
+ onmouseover="${attributes.doubleOnmouseover}"
</#if><#t/>
<#if attributes.doubleOnmouseout??><#t/>
- onmouseout="<#outputformat
'JavaScript'>${attributes.doubleOnmouseout}</#outputformat>"
+ onmouseout="${attributes.doubleOnmouseout}"
</#if><#t/>
<#if attributes.doubleOnfocus??><#t/>
- onfocus="<#outputformat
'JavaScript'>${attributes.doubleOnfocus}</#outputformat>"
+ onfocus="${attributes.doubleOnfocus}"
</#if><#t/>
<#if attributes.doubleOnblur??><#t/>
- onblur="<#outputformat
'JavaScript'>${attributes.doubleOnblur}</#outputformat>"
+ onblur="${attributes.doubleOnblur}"
</#if><#t/>
<#if attributes.doubleOnkeypress??><#t/>
- onkeypress="<#outputformat
'JavaScript'>${attributes.doubleOnkeypress}</#outputformat>"
+ onkeypress="${attributes.doubleOnkeypress}"
</#if><#t/>
<#if attributes.doubleOnKeydown??><#t/>
Review Comment:
The guard reads `attributes.doubleOnKeydown` (capital `K`) while the next
line prints `attributes.doubleOnkeydown`. The guard key is never set, so
`onkeydown` is never rendered on the right-hand select and the line changed
below is dead.
Please change the guard to `attributes.doubleOnkeydown??` and add a
`doubleOnkeydown` assertion to `testOptionTransferSelectEventsAreEscaped`.
##########
core/src/test/java/org/apache/struts2/views/jsp/ui/EventHandlerEscapeTest.java:
##########
@@ -0,0 +1,213 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.struts2.views.jsp.ui;
+
+import org.apache.struts2.views.jsp.AbstractUITagTest;
+
+import jakarta.servlet.jsp.JspException;
+import jakarta.servlet.jsp.tagext.Tag;
+
+public class EventHandlerEscapeTest extends AbstractUITagTest {
+
+ private static final String PAYLOAD = "x\" autofocus onfocus=\"alert(1)";
+ private static final String ESCAPED = "x" autofocus
onfocus="alert(1)";
+ private static final String[] THEMES = {"simple", "xhtml", "css_xhtml",
"html5"};
+
+ public void testScriptingEventsAreEscapedInEveryTheme() throws Exception {
+ for (String theme : THEMES) {
+ TextFieldTag tag = new TextFieldTag();
+ tag.setTheme(theme);
+ tag.setName("foo");
+ tag.setTitle(PAYLOAD);
+ tag.setOnclick(PAYLOAD);
+ tag.setOnchange("a && b < c");
+
+ String out = render(tag);
+
+ assertTrue(theme + ": " + out, out.contains("title=\"" + ESCAPED +
"\""));
+ assertTrue(theme + ": " + out, out.contains("onclick=\"" + ESCAPED
+ "\""));
+ assertTrue(theme + ": " + out, out.contains("onchange=\"a
&& b < c\""));
+ }
+ }
+
+ public void testLegitimateScriptingEventIsRendered() throws Exception {
+ for (String theme : THEMES) {
+ TextFieldTag tag = new TextFieldTag();
+ tag.setTheme(theme);
+ tag.setName("foo");
+ tag.setOnclick("doIt();");
+
+ assertTrue(theme, render(tag).contains("onclick=\"doIt();\""));
+ }
+ }
+
+ public void testFormEventsAreEscapedInEveryTheme() throws Exception {
+ for (String theme : THEMES) {
+ FormTag tag = new FormTag();
+ tag.setTheme(theme);
+ tag.setOnsubmit(PAYLOAD);
+ tag.setOnreset(PAYLOAD);
+
+ String out = render(tag);
+
+ assertTrue(theme + ": " + out, out.contains("onsubmit=\"" +
ESCAPED + "\""));
+ assertTrue(theme + ": " + out, out.contains("onreset=\"" + ESCAPED
+ "\""));
Review Comment:
This passes without the template change. `simple/form.ftl:23` and
`html5/form.ftl:66` already emit a second `onreset="${attributes.onreset}"`
that was always auto-escaped, and `contains()` matches that copy. With the
wrapper restored in `form-common.ftl` the first attribute is written as-is and
the test still passes.
Please also assert that the unencoded form is absent
(`assertFalse(out.contains("onreset=\"" + PAYLOAD + "\""))`), or assert the
number of `onreset=` occurrences. The same negative assertion is worth adding
next to the other `contains(ESCAPED)` checks.
##########
core/src/main/resources/template/simple/scripting-events.ftl:
##########
@@ -19,44 +19,44 @@
*/
-->
<#if attributes.onclick??>
- onclick="<#outputformat
'JavaScript'>${attributes.onclick}</#outputformat>"<#rt/>
+ onclick="${attributes.onclick}"<#rt/>
Review Comment:
This output has been written as-is since WW-4972, so applications may
already pass values that contain character references, e.g. `onclick="if (a
&& b) go()"` or `onclick="show('O'Brien')"`. Those are now encoded
a second time (`&amp;&amp;`, `O&#39;Brien`) and the handler breaks
or shows the literal entity.
The break is accepted, but please:
- add a test that pins the output for an already-encoded value (one per
affected template family is enough), so the behaviour is deliberate rather than
incidental;
- move the note from the *Compatibility* paragraph into an explicit
**Breaking change** section of the PR description, with a before/after example
and the migration step (pass the handler unencoded and let the tag encode it).
That text is what goes into the Version Notes.
Issue Time Tracking
-------------------
Worklog Id: (was: 1056194)
Time Spent: 50m (was: 40m)
> FreeMarker UI templates write event-handler attributes (onclick, onsubmit,
> ...) without HTML escaping
> ------------------------------------------------------------------------------------------------------
>
> Key: WW-5830
> URL: https://issues.apache.org/jira/browse/WW-5830
> Project: Struts 2
> Issue Type: Bug
> Components: Core
> Affects Versions: 6.12.0, 7.0.0, 7.3.0, 7.4.0
> Reporter: Gouri Sankar A
> Priority: Minor
> Fix For: 7.6.0
>
> Time Spent: 50m
> Remaining Estimate: 0h
>
> The simple, xhtml, css_xhtml and html5 templates write the values of
> event-handler attributes (on*, doubleOn*, doubleAccesskey, the form
> onsubmit/onreset and the xhtml tooltip onmouseover) inside <#outputformat
> 'JavaScript'>${...}</#outputformat>. With FreeMarker 2.3.35 that output
> format does not escape anything and also turns off the surrounding HTML
> auto-escape, so these values are written raw. Every other attribute, for
> example title, is escaped, and the javatemplates engine escapes onclick.
> Reproduction (main, against a running Struts Boot app): <@s.textfield
> name="f" onclick="%\{payload}" title="%\{payload}"/> with the payload x"
> autofocus onfocus="alert(1) renders, in all four themes:
> title="x" autofocus onfocus="alert(1)" onclick="x" autofocus
> onfocus="alert(1)"
> The title value stays inside its attribute; the onclick value closes the
> attribute and adds new ones.
> THREAT_MODEL 8.8 says a value a UI tag renders into an attribute is
> HTML-escaped "under every bundled theme and rendering engine", with
> exceptions only for escape/escapeHtmlBody and URL-valued attributes; section
> 9 says a value the application hands to a framework tag is covered by 8.8.
> Scope: HTML escaping closes the attribute breakout. It does not make request
> data safe inside JavaScript code or strings in a handler (a browser decodes
> ' before the script runs). No default framework path feeding request data
> to these attributes was found; exposure needs an application binding data
> into one, for example onclick="%\{#parameters.x}".
> Affected versions: the wrappers (14 in simple/scripting-events.ftl) are
> present in the 6.12.0, 7.0.0, 7.3.0 and 7.4.0 tags, by source inspection. The
> rendering was only reproduced on main. The wrappers date from the WW-4972
> work (3d20e8095, 35497649b).
> Fix: remove the outputformat wrappers on those attributes (8 templates).
> Behaviour change: handlers containing ' & < > or " change byte-for-byte
> (browser-equivalent); applications that hand-escaped quotes (for example
> ") to work around the raw output would be double-escaped. Suggested
> release note: "Event-handler attributes (onclick, onsubmit, ...) rendered by
> the bundled templates are now HTML-escaped; remove any manual pre-escaping of
> quotes in values passed to them."
> Tests: new EventHandlerEscapeTest (5 of its 6 tests fail without the template
> change); 17 existing fixtures updated (' becomes ').
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)