From ae785cda163e2a210bc4405ac2d23123b6951d98 Mon Sep 17 00:00:00 2001 From: KhadeerBasha1232 Date: Fri, 20 Nov 2026 08:49:00 +0530 Subject: [PATCH] Escape embedded JSON in TimestampedGeoJson, Vega and VegaLite These three put their data into the map's script block with plain json.dumps (or the raw file contents), which doesn't escape "<". A string value containing "" ended the script block early, and whatever followed was parsed as new HTML, so a value from the data could run its own script and the map was never drawn. Serialize the data with _to_escaped_json, the helper tojavascript already uses, so <, >, & and ' inside strings are written as \u escapes. File contents for TimestampedGeoJson are now parsed as JSON first, with a leading UTF-8 BOM stripped. A str is still passed through as-is, as documented. Co-Authored-By: Claude Opus 5.5 --- folium/features.py | 4 +- folium/plugins/timestamped_geo_json.py | 9 +++-- tests/plugins/test_timestamped_geo_json.py | 47 ++++++++++++++++++++++ tests/test_features.py | 36 +++++++++++++++++ 4 files changed, 91 insertions(+), 5 deletions(-) diff --git a/folium/features.py b/folium/features.py index 136f5e652b..7aa109fc88 100644 --- a/folium/features.py +++ b/folium/features.py @@ -185,7 +185,7 @@ def render(self, **kwargs): """Renders the HTML representation of the element.""" super().render(**kwargs) - self.json = json.dumps(self.data) + self.json = _to_escaped_json(self.data) self._parent.html.add_child( Element(Template(""" @@ -275,7 +275,7 @@ def __init__( if isinstance(self.data, str): self.data = json.loads(self.data) - self.json = json.dumps(self.data) + self.json = _to_escaped_json(self.data) # Size Parameters. self.width = _parse_size( diff --git a/folium/plugins/timestamped_geo_json.py b/folium/plugins/timestamped_geo_json.py index 11023bfca1..62a817474d 100644 --- a/folium/plugins/timestamped_geo_json.py +++ b/folium/plugins/timestamped_geo_json.py @@ -4,7 +4,7 @@ from folium.elements import JSCSSMixin from folium.folium import Map -from folium.template import Template +from folium.template import Template, _to_escaped_json from folium.utilities import get_bounds, remove_empty @@ -208,10 +208,13 @@ def __init__( if "read" in dir(data): self.embed = True - self.data = data.read() + content = data.read() + if isinstance(content, str): + content = content.lstrip("") + self.data = _to_escaped_json(json.loads(content)) elif type(data) is dict: self.embed = True - self.data = json.dumps(data) + self.data = _to_escaped_json(data) else: self.embed = False self.data = data diff --git a/tests/plugins/test_timestamped_geo_json.py b/tests/plugins/test_timestamped_geo_json.py index dbe4199be0..9df8ecbcd6 100644 --- a/tests/plugins/test_timestamped_geo_json.py +++ b/tests/plugins/test_timestamped_geo_json.py @@ -4,7 +4,10 @@ """ +import json + import numpy as np +import pytest import folium from folium import plugins @@ -208,3 +211,47 @@ def test_timestamped_geo_json(): bounds = m.get_bounds() assert bounds == [[-53.0, -158.0], [50.0, 158.0]], bounds + + +def test_timestamped_geo_json_escapes_script_tags(): + payload = "" + data = { + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "geometry": {"type": "Point", "coordinates": [0, 0]}, + "properties": {"times": ["2017-06-02"], "name": payload}, + } + ], + } + m = folium.Map() + tgj = plugins.TimestampedGeoJson(data).add_to(m) + out = m._parent.render() + + assert payload not in out + assert json.loads(tgj.data) == data + + +@pytest.mark.parametrize("encoding", ["utf-8", "utf-8-sig"]) +def test_timestamped_geo_json_from_file_escapes_script_tags(tmp_path, encoding): + payload = "" + data = { + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "geometry": {"type": "Point", "coordinates": [0, 0]}, + "properties": {"times": ["2017-06-02"], "name": payload}, + } + ], + } + path = tmp_path / "data.geojson" + path.write_text(json.dumps(data), encoding=encoding) + m = folium.Map() + with open(path, encoding=encoding) as f: + tgj = plugins.TimestampedGeoJson(f).add_to(m) + out = m._parent.render() + + assert payload not in out + assert json.loads(tgj.data) == data diff --git a/tests/test_features.py b/tests/test_features.py index 5f2ae7b37c..9e847746eb 100644 --- a/tests/test_features.py +++ b/tests/test_features.py @@ -560,3 +560,39 @@ def test_choropleth_get_by_key(): # Test with combined string path and numerical index in key_on assert Choropleth._get_by_key(geojson_data, "geometry.coordinates.0.0") == [1, 2] + + +VEGA_SCRIPT_PAYLOAD = "" + + +def test_vegalite_escapes_script_tags(): + spec = { + "$schema": "https://vega.github.io/schema/vega-lite/v5.json", + "data": {"values": [{"a": VEGA_SCRIPT_PAYLOAD, "b": 1}]}, + "mark": "bar", + "encoding": { + "x": {"field": "a", "type": "nominal"}, + "y": {"field": "b", "type": "quantitative"}, + }, + } + m = Map() + popup = Popup().add_to(folium.Marker([0, 0]).add_to(m)) + vegalite = folium.VegaLite(spec).add_to(popup) + out = m._parent.render() + + assert VEGA_SCRIPT_PAYLOAD not in out + assert json.loads(vegalite.json) == spec + + +def test_vega_escapes_script_tags(): + spec = { + "data": [{"name": "table", "values": [{"x": VEGA_SCRIPT_PAYLOAD}]}], + "marks": [], + } + m = Map() + popup = Popup().add_to(folium.Marker([0, 0]).add_to(m)) + vega = folium.Vega(spec).add_to(popup) + out = m._parent.render() + + assert VEGA_SCRIPT_PAYLOAD not in out + assert json.loads(vega.json) == spec