Skip to content

Commit 4732198

Browse files
authored
Merge pull request #106 from saketjajoo/pull-custom-path
Removing the additional duplicate sub-directory from sanitize_path()
2 parents e6e936a + d9b8ccf commit 4732198

File tree

5 files changed

+49
-11
lines changed

5 files changed

+49
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ and **Merged pull requests**. Critical items to know are:
1414
The versions coincide with releases on pip. Only major versions will be released as tags on Github.
1515

1616
## [0.0.x](https://github.com/oras-project/oras-py/tree/main) (0.0.x)
17-
- patch fix for pulling artifacts by digest (0.1.23)
17+
- eliminate the additional subdirectory creation while pulling an image to a custom output directory (0.1.24)
18+
- updating the exclude string in the pyproject.toml file to match the [data type black expects](https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#configuration-format)
19+
- patch fix for pulling artifacts by digest (0.1.23)
1820
- patch fix to reject cookies as this could trigger registries into handling the lib as a web client
1921
- patch fix for proper validation and specification of the subject element
2022
- add tls_verify to provider class for optional disable tls verification (0.1.22)

oras/tests/test_provider.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import os
66
import sys
7+
from pathlib import Path
78

89
import pytest
910

@@ -116,3 +117,41 @@ def test_parse_manifest():
116117
ref, content_type = remote._parse_manifest_ref(testref)
117118
assert ref == "path/to/config.json"
118119
assert content_type == oras.defaults.unknown_config_media_type
120+
121+
122+
def test_sanitize_path():
123+
HOME_DIR = str(Path.home())
124+
assert str(oras.utils.sanitize_path(HOME_DIR, HOME_DIR)) == f"{HOME_DIR}"
125+
assert (
126+
str(oras.utils.sanitize_path(HOME_DIR, os.path.join(HOME_DIR, "username")))
127+
== f"{HOME_DIR}/username"
128+
)
129+
assert (
130+
str(oras.utils.sanitize_path(HOME_DIR, os.path.join(HOME_DIR, ".", "username")))
131+
== f"{HOME_DIR}/username"
132+
)
133+
134+
with pytest.raises(Exception) as e:
135+
assert oras.utils.sanitize_path(HOME_DIR, os.path.join(HOME_DIR, ".."))
136+
assert (
137+
str(e.value)
138+
== f"Filename {Path(os.path.join(HOME_DIR, '..')).resolve()} is not in {HOME_DIR} directory"
139+
)
140+
141+
assert oras.utils.sanitize_path("", "") == str(Path(".").resolve())
142+
assert oras.utils.sanitize_path("/opt", os.path.join("/opt", "image_name")) == str(
143+
Path("/opt/image_name").resolve()
144+
)
145+
assert oras.utils.sanitize_path("/../../", "/") == str(Path("/").resolve())
146+
assert oras.utils.sanitize_path(
147+
Path(os.getcwd()).parent.absolute(), os.path.join(os.getcwd(), "..")
148+
) == str(Path("..").resolve())
149+
150+
with pytest.raises(Exception) as e:
151+
assert oras.utils.sanitize_path(
152+
Path(os.getcwd()).parent.absolute(), os.path.join(os.getcwd(), "..", "..")
153+
) != str(Path("../..").resolve())
154+
assert (
155+
str(e.value)
156+
== f"Filename {Path(os.path.join(os.getcwd(), '..', '..')).resolve()} is not in {Path('../').resolve()} directory"
157+
)

oras/utils/fileio.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,11 @@ def sanitize_path(expected_dir, path):
4343
It can be directly there or a child, but not outside it.
4444
We raise an error if it does not - this should not happen
4545
"""
46-
# It's OK to pull to PWD exactly
47-
if os.path.abspath(expected_dir) == os.path.abspath(path):
48-
return os.path.abspath(expected_dir)
49-
base_dir = pathlib.Path(expected_dir)
50-
test_path = (base_dir / path).resolve()
51-
if not base_dir.resolve() in test_path.resolve().parents:
52-
raise Exception(f"Filename {test_path} is not in {base_dir} directory")
53-
return str(test_path.resolve())
46+
base_dir = pathlib.Path(expected_dir).expanduser().resolve()
47+
path = pathlib.Path(path).expanduser().resolve() # path = base_dir + file_name
48+
if not ((base_dir in path.parents) or (str(base_dir) == str(path))):
49+
raise Exception(f"Filename {path} is not in {base_dir} directory")
50+
return str(path)
5451

5552

5653
@contextmanager

oras/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
__copyright__ = "Copyright The ORAS Authors."
33
__license__ = "Apache-2.0"
44

5-
__version__ = "0.1.23"
5+
__version__ = "0.1.24"
66
AUTHOR = "Vanessa Sochat"
77
EMAIL = "vsoch@users.noreply.github.com"
88
NAME = "oras"

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.black]
22
profile = "black"
3-
exclude = ["^env/"]
3+
exclude = '^env/'
44

55
[tool.isort]
66
profile = "black" # needed for black/isort compatibility

0 commit comments

Comments
 (0)