diff --git a/folium/features.py b/folium/features.py index 136f5e652b..72a127cd25 100644 --- a/folium/features.py +++ b/folium/features.py @@ -1149,6 +1149,10 @@ class GeoJsonDetail(MacroElement): return feature; } } + let escapeHTML = value => String(value).replace(/[&<>"']/g, char => { + const entities = {'&': '&', '<': '<', '>': '>', '"': '"', "'": '''}; + return entities[char]; + }); let fields = {{ this.fields | tojson | safe }}; let aliases = {{ this.aliases | tojson | safe }}; let table = '' + @@ -1156,9 +1160,9 @@ class GeoJsonDetail(MacroElement): fields.map( (v,i)=> `{% if this.labels %} - + {% endif %} - + `).join('')) +'
${aliases[i]{% if this.localize %}.toLocaleString(){% endif %}}${escapeHTML(aliases[i]{% if this.localize %}.toLocaleString(){% endif %})}${handleObject(layer.feature.properties[v]){% if this.localize %}.toLocaleString(){% endif %}}${escapeHTML(handleObject(layer.feature.properties[v]){% if this.localize %}.toLocaleString(){% endif %}}
'; div.innerHTML=table; diff --git a/tests/test_features.py b/tests/test_features.py index 5f2ae7b37c..83be65dd96 100644 --- a/tests/test_features.py +++ b/tests/test_features.py @@ -135,6 +135,24 @@ def test_geojson_single_feature_detail(detail_type, properties, fields): assert all(field in rendered for field in fields) +@pytest.mark.parametrize("detail_type", [folium.GeoJsonPopup, folium.GeoJsonTooltip]) +def test_geojson_detail_escapes_property_html(detail_type): + """GeoJson popup and tooltip property values are not rendered as HTML.""" + feature = { + "type": "Feature", + "geometry": {"type": "Point", "coordinates": [0, 0]}, + "properties": {"name": ''}, + } + + map_ = folium.Map() + geojson = folium.GeoJson(feature).add_to(map_) + detail_type(fields=["name"]).add_to(geojson) + + rendered = map_.get_root().render() + assert "escapeHTML" in rendered + assert "escapeHTML(handleObject(layer.feature.properties[v])" in rendered + + @pytest.mark.parametrize("detail_type", [folium.GeoJsonPopup, folium.GeoJsonTooltip]) def test_geojson_geometry_detail_unknown_field(detail_type): """A bare geometry has no properties, so any field is reported as missing."""