diff --git a/CHANGELOG.md b/CHANGELOG.md index aec1538c..052a11fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## HEAD +### Bug fixes 🐛 + +- Fix potential XSS when rendering place name [#547](https://github.com/mapbox/mapbox-gl-geocoder/pull/547) + ## 5.1.1 ### Dependency update diff --git a/lib/index.js b/lib/index.js index 971eea1d..23eabdbd 100644 --- a/lib/index.js +++ b/lib/index.js @@ -93,6 +93,16 @@ function MapboxGeocoder(options) { this.geolocation = new Geolocation(); } +function escapeHtml(str) { + if (!str) return ''; + return String(str) + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/'/g, '''); +} + MapboxGeocoder.prototype = { options: { zoom: 16, @@ -116,7 +126,7 @@ MapboxGeocoder.prototype = { return item.place_name }, render: function(item) { - var placeName = item.place_name.split(','); + var placeName = escapeHtml(item.place_name).split(','); return '