Skip to content

Commit ab44e37

Browse files
committed
Perform input validation in JsonPoiner
1 parent ba2af26 commit ab44e37

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

jsonpointer.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,16 @@ class JsonPointer(object):
167167
# Array indices must not contain:
168168
# leading zeros, signs, spaces, decimals, etc
169169
_RE_ARRAY_INDEX = re.compile('0|[1-9][0-9]*$')
170+
_RE_INVALID_ESCAPE = re.compile('(~[^01]|~$)')
170171

171172
def __init__(self, pointer):
173+
174+
# validate escapes
175+
invalid_escape = self._RE_INVALID_ESCAPE.search(pointer)
176+
if invalid_escape:
177+
raise JsonPointerException('Found invalid escape {0}'.format(
178+
invalid_escape.group()))
179+
172180
parts = pointer.split('/')
173181
if parts.pop(0) != '':
174182
raise JsonPointerException('location must starts with /')

tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ def test_oob(self):
126126
doc = [0, 1, 2]
127127
self.assertRaises(JsonPointerException, resolve_pointer, doc, '/10')
128128

129+
def test_training_escape(self):
130+
self.assertRaises(JsonPointerException, JsonPointer, '/foo/bar~')
131+
132+
def test_invalid_escape(self):
133+
self.assertRaises(JsonPointerException, JsonPointer, '/foo/bar~2')
134+
129135

130136
class ToLastTests(unittest.TestCase):
131137

0 commit comments

Comments
 (0)