Repository navigation
Description
Problem
GeoJsonTooltip and GeoJsonPopup put each property value into the tooltip/popup as raw HTML. GeoJsonDetail.base_template in folium/features.py builds a <table> from the values inside a template literal and assigns it to div.innerHTML.
So any HTML in the data is parsed, including event handlers that run JavaScript.
import folium
data = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [[[-20, -10], [30, -10], [30, 30], [-20, 30], [-20, -10]]],
},
"properties": {"name": '<img src=x onerror="alert(document.domain)">'},
}],
}
m = folium.Map(location=[10, 5], zoom_start=2)
folium.GeoJson(
data,
tooltip=folium.GeoJsonTooltip(fields=["name"]),
popup=folium.GeoJsonPopup(fields=["name"]),
).add_to(m)
m.save("map.html")Open map.html and hover over the polygon: the onerror handler runs. Clicking it does the same through the popup. I checked this in a browser on current main.
In the screenshot below, the handler adds a red "Injected script ran" banner instead of an alert, so the effect is visible. The tooltip shows a broken image where the text should be, and the banner appears as soon as the polygon is hovered:
This matters when the GeoJSON or GeoDataFrame comes from data you don't control, such as scraped data, user uploads or public datasets. Anyone who opens the shared map runs that code.
It is also a plain display bug: text that looks like a tag disappears. Here "site": "Depot <unknown>" is shown as just Depot:
Why it isn't a one-line fix
Some users put HTML in property values on purpose (links, <b>, images, iframes), see #1362. Escaping every value by default would break their maps, so this needs a decision on the behaviour.
Possible approaches
- Opt-in escaping. Add a parameter to
GeoJsonTooltipandGeoJsonPopup, for exampleescape=Falseby default. Withescape=True, values are inserted as text, whilealiasesand labels keep working as now. No existing map changes, and the docs can recommendescape=Truefor untrusted data. - Escape by default, with an opt-out. The same parameter with
escape=Trueby default, plus a changelog note for users who rely on HTML in values. Safer by default, but a breaking change. - Docs only. Document that values are rendered as HTML and must be trusted or escaped by the user beforehand.
Option 1 seems the least disruptive, and option 2 could follow later in a major version.
I'm open to fixing this too. If you let me know which approach you prefer, I'll open a PR with tests and a docs note.


Activity