Skip to content

GeoJsonTooltip and GeoJsonPopup render property values as raw HTML #2365

Open

Description

@KhadeerBasha1232

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:

Hovering the polygon shows a tooltip with a broken image, and a red 'Injected script ran' banner appears at the top of the page

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:

Tooltip shows 'site: Depot' with '<unknown>' missing

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

  1. Opt-in escaping. Add a parameter to GeoJsonTooltip and GeoJsonPopup, for example escape=False by default. With escape=True, values are inserted as text, while aliases and labels keep working as now. No existing map changes, and the docs can recommend escape=True for untrusted data.
  2. Escape by default, with an opt-out. The same parameter with escape=True by default, plus a changelog note for users who rely on HTML in values. Safer by default, but a breaking change.
  3. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions