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
4 changes: 2 additions & 2 deletions folium/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("""
Expand Down Expand Up @@ -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(
Expand Down
9 changes: 6 additions & 3 deletions folium/plugins/timestamped_geo_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions tests/plugins/test_timestamped_geo_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

"""

import json

import numpy as np
import pytest

import folium
from folium import plugins
Expand Down Expand Up @@ -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 = "</script><script>alert(1)</script>"
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 = "</script><script>alert(1)</script>"
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
36 changes: 36 additions & 0 deletions tests/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "</script><script>alert(1)</script>"


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
Loading