diff --git a/.gitignore b/.gitignore
index 5479765..de7c2d9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
env.test.sh
dist
-node_modules
\ No newline at end of file
+node_modules
+.idea
diff --git a/API.md b/API.md
index 69d258d..1e47292 100644
--- a/API.md
+++ b/API.md
@@ -214,7 +214,7 @@ Set the render function used in the results dropdown
#### Parameters
-* `fn` **[Function][85]** The function to use as a render function. This function accepts a single [extended GeoJSON][86] object as input and returns a string.
+* `fn` **[Function][85]** The function to use as a render function. This function accepts a single [extended GeoJSON][86] object as input and returns a string. If `fn` is not a function, the default render function is used.
Returns **[MapboxGeocoder][2]** this
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0f4103f..37e7629 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
## HEAD
+### Bug fixes 🐛
+
+- Fix potential XSS when the `render` option is not a function.
+
## 5.1.2
### Bug fixes 🐛
diff --git a/lib/index.js b/lib/index.js
index 23eabdb..21293e2 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -1121,13 +1121,12 @@ MapboxGeocoder.prototype = {
/**
* Set the render function used in the results dropdown
- * @param {Function} fn The function to use as a render function. This function accepts a single [extended GeoJSON](https://docs.mapbox.com/api/search/geocoding-v5/#geocoding-response-object) object as input and returns a string.
+ * @param {Function} fn The function to use as a render function. This function accepts a single [extended GeoJSON](https://docs.mapbox.com/api/search/geocoding-v5/#geocoding-response-object) object as input and returns a string. If `fn` is not a function, the default render function is used.
* @returns {MapboxGeocoder} this
*/
setRenderFunction: function(fn){
- if (fn && typeof(fn) == "function"){
- this._typeahead.render = fn;
- }
+ // Fall back to the escaping default: keeping the suggestions library's own renderer would inject place_name unescaped
+ this._typeahead.render = typeof fn === 'function' ? fn : MapboxGeocoder.prototype.options.render;
return this;
},
diff --git a/test/test.geocoder.js b/test/test.geocoder.js
index d2c5611..8fbc6c7 100644
--- a/test/test.geocoder.js
+++ b/test/test.geocoder.js
@@ -706,6 +706,30 @@ test('geocoder', function(tt) {
t.equals(geocoder._typeahead.render(fixture), 'feature id is abc123', 'render function is applied properly on the typeahead');
})
+ tt.test('default render function escapes place_name', function(t){
+ t.plan(2);
+ var html = MapboxGeocoder.prototype.options.render({ place_name: '
Cafe, Berlin' });
+ t.ok(html.indexOf('<img src=x>Cafe') > -1, 'place_name is escaped');
+ t.equals(html.indexOf('![]()