-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
missing out of parent path check on decryption
- Loading branch information
1 parent
dcf2304
commit da0644c
Showing
6 changed files
with
139 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
python3 setup.py sdist bdist_wheel | ||
python3 -m twine upload dist/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
""" Keep subpath handling consistent. | ||
As files should not be written outside of CWD, this is a security issue. | ||
Paths may be provided as absolute or relative, and they may be manipulated | ||
inside the storage files. So the check should happen every time a path is | ||
actually used. | ||
""" | ||
import os | ||
import pathlib | ||
from typing import TypeVar | ||
|
||
AnyPath = TypeVar("AnyPath", str, pathlib.Path) | ||
|
||
|
||
class SubPath: | ||
|
||
def __init__(self, relative_path: AnyPath): | ||
""" Wrapper for pathlib.Path that only allows relative paths without .. elements. """ | ||
self.relative_path = self.to_path(relative_path) | ||
if self.relative_path.is_absolute(): | ||
raise ValueError("only relative paths allowed here! (%s)" % relative_path) | ||
if '..' in self.relative_path.parts: | ||
raise ValueError("'..' not allowed in SubPath! (%s)" % relative_path) | ||
|
||
def __str__(self) -> str: | ||
""" relative string representation. """ | ||
return str(self.relative_path) | ||
|
||
@staticmethod | ||
def to_path(path: AnyPath) -> pathlib.Path: | ||
""" Pure conversion of string or Path to Path. """ | ||
print(path) | ||
if not isinstance(path, pathlib.Path): | ||
return pathlib.Path(path) | ||
return path | ||
|
||
def absolute_path(self, parent: AnyPath) -> pathlib.Path: | ||
""" Transform to absolute. """ | ||
return self.to_path(parent) / self.relative_path | ||
|
||
@classmethod | ||
def from_any_path(cls, path: AnyPath, parent: AnyPath) -> "SubPath": | ||
""" Create from absolute or relative path. """ | ||
abs_path = cls.to_path(os.path.abspath(path)) | ||
abs_parent = cls.to_path(os.path.abspath(parent)) | ||
return cls(abs_path.relative_to(abs_parent)) | ||
|
||
@property | ||
def slashed_string(self) -> str: | ||
""" '/'-separated string representation. | ||
Intended for platform-independent storage. | ||
""" | ||
return '/'.join(self.relative_path.parts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import pathlib | ||
import pytest | ||
from cryp_to_go import path_handler | ||
|
||
|
||
def test_init(): | ||
inst = path_handler.SubPath('foo/bar') | ||
assert str(inst.relative_path) == 'foo/bar' | ||
inst = path_handler.SubPath(pathlib.Path('foo/bar')) | ||
assert str(inst.relative_path) == 'foo/bar' | ||
with pytest.raises(ValueError, match="only relative"): | ||
path_handler.SubPath('/foo/bar') | ||
with pytest.raises(ValueError, match="not allowed"): | ||
path_handler.SubPath('foo/../bar') | ||
|
||
|
||
@pytest.mark.parametrize("input_path,target", [ | ||
('foo/bar', 'foo/bar'), | ||
(pathlib.Path('foo/bar'), 'foo/bar'), | ||
('/foo/bar', '/foo/bar'), | ||
(pathlib.Path('/foo/bar'), '/foo/bar'), | ||
]) | ||
def test_to_path(input_path, target): | ||
path = path_handler.SubPath.to_path(input_path) | ||
assert isinstance(path, pathlib.Path) | ||
assert str(path) == target | ||
|
||
|
||
def test_str(): | ||
path = path_handler.SubPath('foo/bar') | ||
assert str(path) == 'foo/bar' | ||
|
||
|
||
@pytest.mark.parametrize("path_parent", ['/foo', pathlib.Path('/foo')]) | ||
def test_absolute_path(path_parent): | ||
path_rel = path_handler.SubPath('bar/bar') | ||
path_abs = path_rel.absolute_path(path_parent) | ||
assert isinstance(path_abs, pathlib.Path) | ||
assert str(path_abs) == '/foo/bar/bar' | ||
|
||
|
||
@pytest.mark.parametrize("path", ['foo/bar', pathlib.Path('foo/bar')]) | ||
def test_from_any_path(path): | ||
subpath = path_handler.SubPath(path) | ||
assert isinstance(subpath, path_handler.SubPath) | ||
assert str(subpath) == 'foo/bar' | ||
|
||
|
||
def test_slashed_string(): | ||
subpath = path_handler.SubPath('foo') | ||
assert subpath.slashed_string == 'foo' | ||
# overwrite internal relative path with PurePath in different flavors | ||
subpath.relative_path = pathlib.PurePosixPath('foo/bar') | ||
assert subpath.slashed_string == 'foo/bar' | ||
subpath.relative_path = pathlib.PureWindowsPath(r'foo\bar') | ||
assert subpath.slashed_string == 'foo/bar' |