Skip to content
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

[GEOS-11153] Improve handling special characters in the WMS OpenLayers Format #7174

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static org.geoserver.template.TemplateUtils.FM_VERSION;

import freemarker.core.HTMLOutputFormat;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.Configuration;
import freemarker.template.Template;
Expand Down Expand Up @@ -107,6 +108,7 @@ public abstract class AbstractOpenLayersMapOutputFormat implements GetMapOutputF
BeansWrapper bw = new BeansWrapper(FM_VERSION);
bw.setExposureLevel(BeansWrapper.EXPOSE_PROPERTIES_ONLY);
cfg.setObjectWrapper(bw);
cfg.setOutputFormat(HTMLOutputFormat.INSTANCE);
}

/** wms configuration */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<script src="${relBaseUrl}/openlayers/OpenLayers.js" type="text/javascript">
</script>
<script defer="defer" type="text/javascript">
<#outputformat "JavaScript">
var map;
var untiled;
var tiled;
Expand Down Expand Up @@ -149,7 +150,7 @@

// setup tiled layer
tiled = new OpenLayers.Layer.WMS(
"${layerName} - Tiled", "${baseUrl}/${servicePath}",
"${layerName?js_string} - Tiled", "${baseUrl}/${servicePath?js_string}",
{
<#list parameters as param>
"${param.name?js_string}": '${param.value?js_string}',
Expand All @@ -168,7 +169,7 @@

// setup single tiled layer
untiled = new OpenLayers.Layer.WMS(
"${layerName} - Untiled", "${baseUrl}/${servicePath}",
"${layerName?js_string} - Untiled", "${baseUrl}/${servicePath?js_string}",
{
<#list parameters as param>
"${param.name?js_string}": '${param.value?js_string}',
Expand Down Expand Up @@ -242,7 +243,7 @@
if(map.layers[0].params.FEATUREID) {
params.featureid = map.layers[0].params.FEATUREID;
}
OpenLayers.loadURL("${baseUrl}/${servicePath}", params, this, setHTML, setHTML);
OpenLayers.loadURL("${baseUrl}/${servicePath?js_string}", params, this, setHTML, setHTML);
OpenLayers.Event.stop(e);
});
}
Expand Down Expand Up @@ -447,6 +448,7 @@
tiled.mergeNewParams(params);
untiled.mergeNewParams(params);
}
</#outputformat>
</script>
</head>
<body onload="init()">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
<em>Click on the map to get feature info</em>
</div>
<script type="text/javascript">
<#outputformat "JavaScript">
var pureCoverage = ${pureCoverage?string};
// if this is just a coverage or a group of them, disable a few items,
// and default to jpeg format
Expand Down Expand Up @@ -257,7 +258,7 @@
var untiled = new ol.layer.Image({
source: new ol.source.ImageWMS({
ratio: 1,
url: '${baseUrl}/${servicePath}',
url: '${baseUrl}/${servicePath?js_string}',
params: {'FORMAT': format,
'VERSION': '1.1.1',
<#list parameters as param>
Expand All @@ -269,7 +270,7 @@
var tiled = new ol.layer.Tile({
visible: false,
source: new ol.source.TileWMS({
url: '${baseUrl}/${servicePath}',
url: '${baseUrl}/${servicePath?js_string}',
params: {'FORMAT': format,
'VERSION': '1.1.1',
tiled: true,
Expand Down Expand Up @@ -450,7 +451,7 @@
}
map.updateSize()
}

</#outputformat>
</script>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,28 @@ public void testXssFix() throws Exception {
StyleInfo styleByName = catalog.getStyleByName("Default");
Style basicStyle = styleByName.getStyle();
FeatureLayer layer = new FeatureLayer(fs, basicStyle);
layer.setTitle("Title");
layer.setTitle("Title</foo");
map.addLayer(layer);
request.setFormat("application/openlayers");
String htmlDoc = getAsHTML(map);
// check that weird param is correctly encoded to avoid js code execution
int index =
htmlDoc.replace("\\n", "")
.replace("\\r", "")
.indexOf(
"\"<\\/script><script>alert(\\'x-scripted\\');<\\/script><script>\": 'foo'");
assertTrue(index > -1);
index =
htmlDoc.replace("\\n", "")
.replace("\\r", "")
.indexOf("\"25064;ALERT(1)//419\": '1'");
assertTrue(index > -1);

StyleInfo otherStyle = new StyleInfoImpl(null);
otherStyle.setName("style<>");
try {
request.getLayers().get(0).getLayerInfo().getStyles().add(otherStyle);
String htmlDoc = getAsHTML(map);
// check that weird param is correctly encoded to avoid js code execution
assertThat(
htmlDoc,
containsString(
"\"<\\/script><script>alert(\\'x-scripted\\');<\\/script><script>\": 'foo'"));
assertThat(htmlDoc, containsString("\"25064;ALERT(1)//419\": '1'"));
assertThat(htmlDoc, not(containsString(layer.getTitle())));
assertThat(htmlDoc, containsString("Title<\\/foo"));
assertThat(htmlDoc, not(containsString(otherStyle.getName())));
assertThat(htmlDoc, containsString("style&lt;&gt;"));
} finally {
request.getLayers().get(0).getLayerInfo().getStyles().remove(otherStyle);
}
}

@Test
Expand Down Expand Up @@ -472,19 +478,23 @@ public void testXssOL3() throws Exception {
layer.setTitle("Title");
map.addLayer(layer);
request.setFormat("application/openlayers3");
String htmlDoc = getAsHTMLOL3(map);
// check that weird param is correctly encoded to avoid js code execution
int index =
htmlDoc.replace("\\n", "")
.replace("\\r", "")
.indexOf(
"\"<\\/script><script>alert(\\'x-scripted\\');<\\/script><script>\": 'foo'");
assertTrue(index > -1);
index =
htmlDoc.replace("\\n", "")
.replace("\\r", "")
.indexOf("\"25064;ALERT(1)//419\": '1'");
assertTrue(index > -1);

StyleInfo otherStyle = new StyleInfoImpl(null);
otherStyle.setName("style<>");
try {
request.getLayers().get(0).getLayerInfo().getStyles().add(otherStyle);
String htmlDoc = getAsHTMLOL3(map);
// check that weird param is correctly encoded to avoid js code execution
assertThat(
htmlDoc,
containsString(
"\"<\\/script><script>alert(\\'x-scripted\\');<\\/script><script>\": 'foo'"));
assertThat(htmlDoc, containsString("\"25064;ALERT(1)//419\": '1'"));
assertThat(htmlDoc, not(containsString(otherStyle.getName())));
assertThat(htmlDoc, containsString("style&lt;&gt;"));
} finally {
request.getLayers().get(0).getLayerInfo().getStyles().remove(otherStyle);
}
}

@Test
Expand Down