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 7 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
3 changes: 0 additions & 3 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,3 @@ exclude_lines =

# No need to test __repr__
def __repr__

# Python 2/3 compatibility
except ImportError
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [ "3.7", "3.8", "3.9", "3.10", "3.11", "3.12" ]
python-version: [ "3.9", "3.10", "3.11", "3.12" ]

steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ python-json-pointer
===================

*python-json-pointer* is a Python library for resolving JSON pointers (`RFC
6901 <http://tools.ietf.org/html/rfc6901>`_). Python 2.7, 3.4+
6901 <http://tools.ietf.org/html/rfc6901>`_). Python 3.9+
and PyPy are supported.

**Contents**
Expand Down
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
4 changes: 1 addition & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
Expand All @@ -62,5 +60,5 @@
py_modules=MODULES,
scripts=['bin/jsonpointer'],
classifiers=CLASSIFIERS,
python_requires='>=3.7',
python_requires='>=3.9',
)
56 changes: 24 additions & 32 deletions tests.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import copy
import doctest
import sys
import unittest

import jsonpointer
Expand Down Expand Up @@ -78,45 +75,33 @@ def test_round_trip(self):

def test_str_and_repr(self):
paths = [
("", "", "JsonPointer({u}'')"),
("/foo", "/foo", "JsonPointer({u}'/foo')"),
("/foo/0", "/foo/0", "JsonPointer({u}'/foo/0')"),
("/", "/", "JsonPointer({u}'/')"),
("/a~1b", "/a~1b", "JsonPointer({u}'/a~1b')"),
("/c%d", "/c%d", "JsonPointer({u}'/c%d')"),
("/e^f", "/e^f", "JsonPointer({u}'/e^f')"),
("/g|h", "/g|h", "JsonPointer({u}'/g|h')"),
("/i\\j", "/i\\j", "JsonPointer({u}'/i\\\\j')"),
("/k\"l", "/k\"l", "JsonPointer({u}'/k\"l')"),
("/ ", "/ ", "JsonPointer({u}'/ ')"),
("/m~0n", "/m~0n", "JsonPointer({u}'/m~0n')"),
("", "", "JsonPointer('')"),
("/foo", "/foo", "JsonPointer('/foo')"),
("/foo/0", "/foo/0", "JsonPointer('/foo/0')"),
("/", "/", "JsonPointer('/')"),
("/a~1b", "/a~1b", "JsonPointer('/a~1b')"),
("/c%d", "/c%d", "JsonPointer('/c%d')"),
("/e^f", "/e^f", "JsonPointer('/e^f')"),
("/g|h", "/g|h", "JsonPointer('/g|h')"),
("/i\\j", "/i\\j", "JsonPointer('/i\\\\j')"),
("/k\"l", "/k\"l", "JsonPointer('/k\"l')"),
("/ ", "/ ", "JsonPointer('/ ')"),
("/m~0n", "/m~0n", "JsonPointer('/m~0n')"),
]
for path, ptr_str, ptr_repr in paths:
ptr = JsonPointer(path)
self.assertEqual(path, ptr.path)

if sys.version_info[0] == 2:
u_str = "u"
else:
u_str = ""
self.assertEqual(ptr_str, str(ptr))
self.assertEqual(ptr_repr.format(u=u_str), repr(ptr))

if sys.version_info[0] == 2:
path = "/\xee"
ptr_str = b"/\xee"
ptr_repr = "JsonPointer(u'/\\xee')"
else:
path = "/\xee"
ptr_str = "/\xee"
ptr_repr = "JsonPointer('/\xee')"
self.assertEqual(ptr_repr, repr(ptr))

path = "/\xee"
ptr_str = "/\xee"
ptr_repr = "JsonPointer('/\xee')"
ptr = JsonPointer(path)
self.assertEqual(path, ptr.path)

self.assertEqual(ptr_str, str(ptr))
self.assertEqual(ptr_repr, repr(ptr))

# should not be unicode in Python 2
self.assertIsInstance(str(ptr), str)
self.assertIsInstance(repr(ptr), str)

Expand Down Expand Up @@ -220,6 +205,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