Skip to content

Commit e8a3c7c

Browse files
committed
In LocalizationBundle#getStringWithParams, the key can refer a pattern in which the expected parameter(s) can be other than a String. Takes into account by applying the encoding for HTML only on String parameters.
1 parent 66a46a5 commit e8a3c7c

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

kernel-library/src/main/java/org/silverpeas/kernel/bundle/LocalizationBundle.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,11 @@ public String getStringWithParams(String resName, Object... params) {
217217
String msgPattern = getString(resName);
218218
// we encode the parameters as they can be provided by a user and they can have XSS statements
219219
return MessageFormat.format(msgPattern, Arrays.stream(params)
220-
.map(Object::toString)
221-
.map(Encode::forHtml)
222-
.toArray());
220+
.map(p -> Optional.of(p)
221+
.filter(String.class::isInstance)
222+
.map(o -> (Object) Encode.forHtml((String) o))
223+
.orElse(p)
224+
).toArray());
223225
}
224226

225227
@Override

kernel-library/src/test/java/org/silverpeas/kernel/bundle/LocalizationBundleTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,29 @@ void getParametrizedText() {
9393
assertThat(value2, is("'Bidule' est plus petit que 'Toto'"));
9494
}
9595

96+
@Test
97+
void getParametrizedTextWithXSS() {
98+
LocalizationBundle bundle = ResourceLocator.getLocalizationBundle(LOCALIZATION_BUNDLE, Locale.FRENCH);
99+
String value = bundle.getStringWithParams("cle.param.2", "Bidule",
100+
"<img src=x onerror=prompt(document.cookie)/>");
101+
assertThat(value, is("\"Bidule\" est plus petit que \"&lt;img src=x onerror=prompt(document" +
102+
".cookie)/&gt;\""));
103+
}
104+
105+
@Test
106+
void getParametrizedTextWithNumber() {
107+
LocalizationBundle bundle = ResourceLocator.getLocalizationBundle(LOCALIZATION_BUNDLE,
108+
Locale.ENGLISH);
109+
String value1 = bundle.getStringWithParams("cle.param.1", 0);
110+
assertThat(value1, is("just"));
111+
112+
String value2= bundle.getStringWithParams("cle.param.1", 1);
113+
assertThat(value2, is("1 hour"));
114+
115+
String value3= bundle.getStringWithParams("cle.param.1", 3);
116+
assertThat(value3, is("3 hours"));
117+
}
118+
96119
@Test
97120
void getMissingPropertyShouldThrowMissingResourceException() {
98121
LocalizationBundle bundle = ResourceLocator.getLocalizationBundle(LOCALIZATION_BUNDLE);

kernel-library/src/test/resources/org/silverpeas/test/multilang/l10n_en.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,7 @@ language_de = German
2727

2828
icon = ${sys.FOO}/en/logo-language.png
2929

30+
cle.param.1 = {0,choice, 0#just| 1#{0} hour| 1<{0} hours}
31+
3032
cle.param.2 = "{0}" is smaller than "{1}"
3133
cle.param.2bis = ''{0}'' is smaller than ''{1}''

kernel-library/src/test/resources/org/silverpeas/test/multilang/l10n_fr.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,7 @@ language_de = Allemand
2727

2828
icon = ${sys.FOO}/fr/logo-language.png
2929

30+
cle.param.1 = {0,choice, 0#juste| 1#{0} heure| 1<{0} heures}
31+
3032
cle.param.2 = "{0}" est plus petit que "{1}"
3133
cle.param.2bis = ''{0}'' est plus petit que ''{1}''

0 commit comments

Comments
 (0)