Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions folium/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,16 +1149,20 @@ class GeoJsonDetail(MacroElement):
return feature;
}
}
let escapeHTML = value => String(value).replace(/[&<>"']/g, char => {
const entities = {'&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;'};
return entities[char];
});
let fields = {{ this.fields | tojson | safe }};
let aliases = {{ this.aliases | tojson | safe }};
let table = '<table>' +
String(
fields.map(
(v,i)=>
`<tr>{% if this.labels %}
<th>${aliases[i]{% if this.localize %}.toLocaleString(){% endif %}}</th>
<th>${escapeHTML(aliases[i]{% if this.localize %}.toLocaleString(){% endif %})}</th>
{% endif %}
<td>${handleObject(layer.feature.properties[v]){% if this.localize %}.toLocaleString(){% endif %}}</td>
<td>${escapeHTML(handleObject(layer.feature.properties[v]){% if this.localize %}.toLocaleString(){% endif %}}</td>
</tr>`).join(''))
+'</table>';
div.innerHTML=table;
Expand Down
18 changes: 18 additions & 0 deletions tests/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": '<img src=x onerror="alert(1)">'},
}

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."""
Expand Down