Skip to content

spring-form JSP tags should escape HTML value based on response character encoding #33023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected final int doStartTagInternal() throws Exception {
* as required. This version is <strong>not</strong> {@link PropertyEditor}-aware.
*/
protected String getDisplayString(@Nullable Object value) {
return ValueFormatter.getDisplayString(value, isHtmlEscape());
return ValueFormatter.getDisplayString(value, this::htmlEscape);
}

/**
Expand All @@ -102,7 +102,7 @@ protected String getDisplayString(@Nullable Object value) {
* to obtain the display value.
*/
protected String getDisplayString(@Nullable Object value, @Nullable PropertyEditor propertyEditor) {
return ValueFormatter.getDisplayString(value, propertyEditor, isHtmlEscape());
return ValueFormatter.getDisplayString(value, propertyEditor, this::htmlEscape);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.web.servlet.tags.form;

import java.beans.PropertyEditor;
import java.util.function.UnaryOperator;

import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;
Expand Down Expand Up @@ -49,6 +50,16 @@ public static String getDisplayString(@Nullable Object value, boolean htmlEscape
return (htmlEscape ? HtmlUtils.htmlEscape(displayValue) : displayValue);
}

/**
* Build the display value of the supplied {@code Object}, HTML escaped
* as required. This version is <strong>not</strong> {@link PropertyEditor}-aware.
* @see #getDisplayString(Object, java.beans.PropertyEditor, UnaryOperator)
*/
public static String getDisplayString(@Nullable Object value, UnaryOperator<String> htmlEscape) {
String displayValue = ObjectUtils.getDisplayString(value);
return htmlEscape.apply(displayValue);
}

/**
* Build the display value of the supplied {@code Object}, HTML escaped
* as required. If the supplied value is not a {@link String} and the supplied
Expand All @@ -74,4 +85,29 @@ public static String getDisplayString(
return getDisplayString(value, htmlEscape);
}

/**
* Build the display value of the supplied {@code Object}, HTML escaped
* as required. If the supplied value is not a {@link String} and the supplied
* {@link PropertyEditor} is not null then the {@link PropertyEditor} is used
* to obtain the display value.
* @see #getDisplayString(Object, UnaryOperator)
*/
public static String getDisplayString(
@Nullable Object value, @Nullable PropertyEditor propertyEditor, UnaryOperator<String> htmlEscape) {

if (propertyEditor != null && !(value instanceof String)) {
try {
propertyEditor.setValue(value);
String text = propertyEditor.getAsText();
if (text != null) {
return getDisplayString(text, htmlEscape);
}
}
catch (Throwable ex) {
// The PropertyEditor might not support this value... pass through.
}
}
return getDisplayString(value, htmlEscape);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ void simpleBindTagWithinForm() throws Exception {

@Test
void simpleBindWithHtmlEscaping() throws Exception {
final String NAME = "Rob \"I Love Mangos\" Harrop";
final String HTML_ESCAPED_NAME = "Rob &quot;I Love Mangos&quot; Harrop";
final String NAME = "Rob \"I Love Café\" Harrop";
final String HTML_ESCAPED_NAME = "Rob &quot;I Love Caf&eacute;&quot; Harrop";

this.tag.setPath("name");
this.rob.setName(NAME);
Expand All @@ -112,6 +112,26 @@ void simpleBindWithHtmlEscaping() throws Exception {
assertValueAttribute(output, HTML_ESCAPED_NAME);
}

@Test
void simpleBindWithHtmlEscapingAndCharacterEncoding() throws Exception {
final String NAME = "Rob \"I Love Café\" Harrop";
final String HTML_ESCAPED_NAME = "Rob &quot;I Love Café&quot; Harrop";

this.getPageContext().getResponse().setCharacterEncoding("UTF-8");
this.tag.setPath("name");
this.rob.setName(NAME);

assertThat(this.tag.doStartTag()).isEqualTo(Tag.SKIP_BODY);

String output = getOutput();
assertTagOpened(output);
assertTagClosed(output);

assertContainsAttribute(output, "type", getType());
assertValueAttribute(output, HTML_ESCAPED_NAME);
}


protected void assertValueAttribute(String output, String expectedValue) {
assertContainsAttribute(output, "value", expectedValue);
}
Expand Down