Allow relative imports with two dots at start of path. Update unit tests.
This commit is contained in:
parent
e5429d6cda
commit
4395d4001c
2 changed files with 9 additions and 10 deletions
|
|
@ -140,11 +140,11 @@ def make_path_safe(unsafe_path_string: str) -> str:
|
||||||
for i, component in enumerate(components):
|
for i, component in enumerate(components):
|
||||||
if component in ("/", "\\"):
|
if component in ("/", "\\"):
|
||||||
sanitised_components.append(component)
|
sanitised_components.append(component)
|
||||||
elif component == ".":
|
elif component in (".", ".."):
|
||||||
# Only allow '.' if its the very first
|
# Only allow '.' and '..' if they are the very first
|
||||||
# element and the next element is a separator.
|
# elements and the next element is a separator.
|
||||||
# This allows for relative paths like "./file"
|
# This allows for relative paths like "./file" or "../file"
|
||||||
# but not "file./file"
|
# but not "file./file" or "file/../file"
|
||||||
is_first = i == 0
|
is_first = i == 0
|
||||||
next_to_sep = (i + 1 < len(components)) and (
|
next_to_sep = (i + 1 < len(components)) and (
|
||||||
components[i + 1] in ("/", "\\")
|
components[i + 1] in ("/", "\\")
|
||||||
|
|
|
||||||
|
|
@ -95,13 +95,12 @@ def test_make_path_safe_separators():
|
||||||
|
|
||||||
|
|
||||||
def test_make_path_safe_relative():
|
def test_make_path_safe_relative():
|
||||||
"""Test that relative path components are preserved.
|
"""Test that relative path components are preserved."""
|
||||||
We only allow relative paths with one dot, and at the beginning of the path,
|
# We only allow relative paths with one dot, and at the beginning of the path,
|
||||||
to avoid issues with paths like "file./file" or "file../file
|
# to avoid issues with paths like "file./file" or "file../file.
|
||||||
"""
|
|
||||||
assert make_path_safe("./openflexure/data/") == "./openflexure/data/"
|
assert make_path_safe("./openflexure/data/") == "./openflexure/data/"
|
||||||
|
assert make_path_safe("../openflexure/data/") == "../openflexure/data/"
|
||||||
|
|
||||||
assert make_path_safe("../openflexure/data/") == "_/openflexure/data/"
|
|
||||||
assert make_path_safe("path/./to/file") == "path/_/to/file"
|
assert make_path_safe("path/./to/file") == "path/_/to/file"
|
||||||
assert make_path_safe("path/../to/file") == "path/_/to/file"
|
assert make_path_safe("path/../to/file") == "path/_/to/file"
|
||||||
assert make_path_safe(".") == "_"
|
assert make_path_safe(".") == "_"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue