This is an automated email from the ASF dual-hosted git repository. dashorst pushed a commit to branch wicket-8.x in repository https://gitbox.apache.org/repos/asf/wicket.git
commit e4cf638decf1018f68bbe9e18c5b8d663bfd4a61 Author: Pedro Santos <[email protected]> AuthorDate: Fri Oct 9 17:47:05 2026 -0300 Escape the css class the form writes into its hidden fields Form#writeHiddenFields builds the div that holds its hidden fields with String.format, and the css class it reads from the resource bundles went in as it stands. It now goes through Strings#escapeMarkup, as the css class the filter form writes into its hidden fields already does. A value the framework writes into markup is escaped by the code that writes it, whichever of them the value came from. Co-Authored-By: Claude Mythos 5.1 <[email protected]> --- .../org/apache/wicket/markup/html/form/Form.java | 2 +- .../markup/html/form/FormHiddenFieldsTest.java | 87 ++++++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java index bc5364822c..68cb18c0f5 100644 --- a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java +++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java @@ -1758,7 +1758,7 @@ public class Form<T> extends WebMarkupContainer { String cssClass = getString(CssUtils.key(Form.class, "hidden-fields")); - getResponse().write(String.format("<div id=\"%s\" style=\"width:0px;height:0px;position:absolute;left:-100px;top:-100px;overflow:hidden\" class=\"%s\">", getHiddenFieldsId(), cssClass)); + getResponse().write(String.format("<div id=\"%s\" style=\"width:0px;height:0px;position:absolute;left:-100px;top:-100px;overflow:hidden\" class=\"%s\">", getHiddenFieldsId(), Strings.escapeMarkup(cssClass))); AppendingStringBuffer buffer = new AppendingStringBuffer(); diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormHiddenFieldsTest.java b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormHiddenFieldsTest.java new file mode 100644 index 0000000000..d438303651 --- /dev/null +++ b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormHiddenFieldsTest.java @@ -0,0 +1,87 @@ +/* + * 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.wicket.markup.html.form; + +import java.util.Locale; + +import org.apache.wicket.Component; +import org.apache.wicket.MarkupContainer; +import org.apache.wicket.core.util.string.CssUtils; +import org.apache.wicket.markup.IMarkupResourceStreamProvider; +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.resource.loader.IStringResourceLoader; +import org.apache.wicket.util.resource.IResourceStream; +import org.apache.wicket.util.resource.StringResourceStream; +import org.apache.wicket.util.tester.WicketTestCase; +import org.junit.Test; + +/** + * {@link Form#writeHiddenFields()} writes its markup as a string, so it has to escape what it puts + * into the attributes itself. + */ +public class FormHiddenFieldsTest extends WicketTestCase +{ + /** + * The css class of the hidden div comes from the resource bundles. + */ + @Test + public void cssClassIsEscaped() + { + final String key = CssUtils.key(Form.class, "hidden-fields"); + tester.getApplication().getResourceSettings().getStringResourceLoaders().add(0, + new IStringResourceLoader() + { + @Override + public String loadStringResource(Class<?> clazz, String k, Locale locale, + String style, String variation) + { + return key.equals(k) ? "a\"b" : null; + } + + @Override + public String loadStringResource(Component component, String k, Locale locale, + String style, String variation) + { + return loadStringResource((Class<?>)null, k, locale, style, variation); + } + }); + + tester.startPage(new TestPage()); + + tester.assertContains("class=\"a"b\""); + } + + /** a form submitted with GET, which is when the hidden fields are written */ + public static class TestPage extends WebPage implements IMarkupResourceStreamProvider + { + private static final long serialVersionUID = 1L; + + /** */ + public TestPage() + { + add(new Form<Void>("form")); + } + + @Override + public IResourceStream getMarkupResourceStream(MarkupContainer container, + Class<?> containerClass) + { + return new StringResourceStream( + "<html><body><form wicket:id=\"form\" method=\"get\"></form></body></html>"); + } + } +}
