Skip to content

Commit 08031d4

Browse files
authored
Fix to_json escape (#2310)
* Fix to_json escape * Fix flake8
1 parent f10f9c2 commit 08031d4

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

pygeoapi/util.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,9 @@ def to_json(dict_: dict, pretty: bool = False) -> str:
265265
json_dump = json.dumps(dict_, default=json_serial, indent=indent,
266266
separators=(',', ':'))
267267

268-
LOGGER.debug('Removing < and >')
269-
json_dump = json_dump.replace('<', '&lt').replace('>', '&gt')
268+
LOGGER.debug('Escaping < and >')
269+
json_dump = json_dump.replace('<', '&lt;')
270+
json_dump = json_dump.replace('>', '&gt;')
270271

271272
return json_dump
272273

tests/other/test_util.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from io import StringIO
3434
from unittest import mock
3535
import uuid
36+
from xml.sax.saxutils import unescape
3637

3738
import pytest
3839

@@ -77,13 +78,20 @@ def test_get_typed_value():
7778
@pytest.mark.parametrize('data,minified,pretty_printed', [
7879
[{'foo': 'bar'}, '{"foo":"bar"}', '{\n "foo":"bar"\n}'],
7980
[{'foo<script>alert("hi")</script>': 'bar'},
80-
'{"foo&ltscript&gtalert(\\"hi\\")&lt/script&gt":"bar"}',
81-
'{\n "foo&ltscript&gtalert(\\"hi\\")&lt/script&gt":"bar"\n}']
81+
'{"foo&lt;script&gt;alert(\\"hi\\")&lt;/script&gt;":"bar"}',
82+
'{\n "foo&lt;script&gt;alert(\\"hi\\")&lt;/script&gt;":"bar"\n}']
8283
])
8384
def test_to_json(data, minified, pretty_printed):
84-
assert util.to_json(data) == minified
85+
output = util.to_json(data)
86+
assert output == minified
8587
assert util.to_json(data, pretty=True) == pretty_printed
8688

89+
unescaped_output = unescape(output)
90+
if '&lt;' in output:
91+
assert '<' in unescaped_output
92+
if '&gt;' in output:
93+
assert '>' in unescaped_output
94+
8795

8896
def test_yaml_load(config):
8997
assert isinstance(config, dict)

0 commit comments

Comments
 (0)