Skip to content

Fix invalid array index validation to reject indices with leading zeros #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion jsonpointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class JsonPointer(object):

# Array indices must not contain:
# leading zeros, signs, spaces, decimals, etc
_RE_ARRAY_INDEX = re.compile('0|[1-9][0-9]*$')
_RE_ARRAY_INDEX = re.compile('^(?:0|[1-9][0-9]*)$')
_RE_INVALID_ESCAPE = re.compile('(~[^01]|~$)')

def __init__(self, pointer):
Expand Down
7 changes: 7 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,13 @@ def test_invalid_index(self):
doc = [0, 1, 2]
self.assertRaises(JsonPointerException, resolve_pointer, doc, '/a')

def test_invalid_index_leading_zeros(self):
# RFC 6901 specifies that array indices must not have leading zeros
doc = [0, 1, 2]
self.assertRaises(JsonPointerException, resolve_pointer, doc, '/01')
self.assertRaises(JsonPointerException, resolve_pointer, doc, '/00')
self.assertRaises(JsonPointerException, resolve_pointer, doc, '/001')

def test_oob(self):
# this list does not have 10 members
doc = [0, 1, 2]
Expand Down