From 91047880e266b8cdd2fa16051ff4f3664cbb5eaa Mon Sep 17 00:00:00 2001 From: KhadeerBasha1232 Date: Thu, 19 Nov 2026 22:56:10 +0530 Subject: [PATCH] Escape strings in VectorGrid, TimestampedGeoJson and FloatImage Three plugins put user-supplied strings into their templates without escaping: - VectorGridProtobuf put `url` inside single quotes, so a URL with an apostrophe ended the JS string and the layer was never created. - TimestampedGeoJson put `date_options` inside double quotes, so a format with a double quote broke the script and no time control appeared. - FloatImage put `image` into the img src attribute as-is, so a double quote ended the attribute and the rest was parsed as new HTML attributes, such as an onerror handler that runs code. Use tojson for the two JS strings and HTML-escape the src attribute. Co-Authored-By: Claude Opus 5.5 --- folium/plugins/float_image.py | 2 +- folium/plugins/timestamped_geo_json.py | 2 +- folium/plugins/vectorgrid_protobuf.py | 2 +- tests/plugins/test_float_image.py | 9 +++++++++ tests/plugins/test_timestamped_geo_json.py | 10 +++++++++- tests/plugins/test_vectorgrid_protobuf.py | 8 ++++++++ 6 files changed, 29 insertions(+), 4 deletions(-) diff --git a/folium/plugins/float_image.py b/folium/plugins/float_image.py index 5cf8c1f7a4..30b45c112c 100644 --- a/folium/plugins/float_image.py +++ b/folium/plugins/float_image.py @@ -37,7 +37,7 @@ class FloatImage(MacroElement): {% macro html(this,kwargs) %} float_image {% endmacro %} diff --git a/folium/plugins/timestamped_geo_json.py b/folium/plugins/timestamped_geo_json.py index 11023bfca1..84a72aa695 100644 --- a/folium/plugins/timestamped_geo_json.py +++ b/folium/plugins/timestamped_geo_json.py @@ -84,7 +84,7 @@ class TimestampedGeoJson(JSCSSMixin, MacroElement): _getDisplayDateFormat: function(date){ var newdate = new moment(date); console.log(newdate) - return newdate.format("{{this.date_options}}"); + return newdate.format({{ this.date_options|tojson }}); } }); {{this._parent.get_name()}}.timeDimension = L.timeDimension( diff --git a/folium/plugins/vectorgrid_protobuf.py b/folium/plugins/vectorgrid_protobuf.py index 3438541a38..3ed8c10560 100644 --- a/folium/plugins/vectorgrid_protobuf.py +++ b/folium/plugins/vectorgrid_protobuf.py @@ -104,7 +104,7 @@ class VectorGridProtobuf(JSCSSMixin, Layer): _template = Template(""" {% macro script(this, kwargs) -%} var {{ this.get_name() }} = L.vectorGrid.protobuf( - '{{ this.url }}', + {{ this.url|tojson }}, {%- if this.options is defined %} {{ this.options if this.options is string else this.options|tojavascript }} {%- endif %} diff --git a/tests/plugins/test_float_image.py b/tests/plugins/test_float_image.py index 365e8ae68a..289857c08c 100644 --- a/tests/plugins/test_float_image.py +++ b/tests/plugins/test_float_image.py @@ -42,3 +42,12 @@ def test_float_image(): bounds = m.get_bounds() assert bounds == [[None, None], [None, None]], bounds + + +def test_float_image_escapes_src(): + m = folium.Map() + image = 'https://example.com/a.png?x=1&y="2"' + plugins.FloatImage(image).add_to(m) + out = m.get_root().render() + assert 'src="https://example.com/a.png?x=1&y="2""' in out + assert 'y="2"' not in out diff --git a/tests/plugins/test_timestamped_geo_json.py b/tests/plugins/test_timestamped_geo_json.py index dbe4199be0..23cd4a1a03 100644 --- a/tests/plugins/test_timestamped_geo_json.py +++ b/tests/plugins/test_timestamped_geo_json.py @@ -136,7 +136,7 @@ def test_timestamped_geo_json(): _getDisplayDateFormat: function(date){ var newdate = new moment(date); console.log(newdate) - return newdate.format("{{this.date_options}}"); + return newdate.format({{ this.date_options|tojson }}); } }); {{this._parent.get_name()}}.timeDimension = L.timeDimension( @@ -208,3 +208,11 @@ 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_date_options_with_quotes(): + m = folium.Map() + data = {"type": "FeatureCollection", "features": []} + plugins.TimestampedGeoJson(data, date_options='YYYY "Q"Q').add_to(m) + out = m.get_root().render() + assert 'newdate.format("YYYY \\"Q\\"Q");' in out diff --git a/tests/plugins/test_vectorgrid_protobuf.py b/tests/plugins/test_vectorgrid_protobuf.py index 99b5ddc247..117e3f5728 100644 --- a/tests/plugins/test_vectorgrid_protobuf.py +++ b/tests/plugins/test_vectorgrid_protobuf.py @@ -107,3 +107,11 @@ def test_vectorgrid_dict_options(): continue assert f'"{k}": {v}' in out + + +def test_vectorgrid_url_with_quote(): + m = folium.Map() + url = "https://example.com/{z}/{x}/{y}.pbf?name=O'Neil" + VectorGridProtobuf(url, "test").add_to(m) + out = m.get_root().render() + assert r'"https://example.com/{z}/{x}/{y}.pbf?name=O\u0027Neil"' in out