Skip to content

Commit ea3486f

Browse files
committed
Fstab accepts non-absolute path as source
1 parent ed50135 commit ea3486f

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

libioc/Config/Jail/File/Fstab.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,16 @@ def __setitem__(
7676
value: typing.Union[str, libioc.Types.AbsolutePath]
7777
) -> None:
7878
"""Set an item of the FstabLine."""
79-
if (key == "source") or (key == "destination"):
79+
_type = None
80+
if key == "source":
81+
_type = libioc.Types.Path
82+
elif key == "destination":
83+
_type = libioc.Types.AbsolutePath
84+
85+
if _type is not None: # source or destination
8086
if isinstance(value, str) is True:
81-
absolute_path = libioc.Types.AbsolutePath(value)
82-
elif isinstance(value, libioc.Types.AbsolutePath) is True:
87+
absolute_path = _type(value)
88+
elif isinstance(value, _type) is True:
8389
absolute_path = value
8490
else:
8591
raise ValueError("String or AbsolutePath expected")
@@ -287,7 +293,7 @@ def parse_lines(
287293
])
288294

289295
new_line = FstabLine({
290-
"source": libioc.Types.AbsolutePath(source),
296+
"source": libioc.Types.Path(source),
291297
"destination": libioc.Types.AbsolutePath(destination),
292298
"type": fragments[2],
293299
"options": fragments[3],

libioc/Types.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,45 @@
2727
import re
2828

2929

30-
class AbsolutePath(str):
30+
class Path(str):
3131
"""Wrapper Type for ensuring a `str` matches a Unix Path."""
3232

33-
unix_path = re.compile(r"/([^/\0]+/*)+")
33+
blacklist = re.compile(
34+
r"(\/\/)|(\/\.\.)|(\.\.\/)|(\n)|(\r)|(^\.+$)",
35+
re.MULTILINE
36+
)
3437

3538
def __init__(
36-
self,
37-
sequence: str
39+
self,
40+
sequence: str
3841
) -> None:
39-
if self.unix_path.fullmatch(sequence) is None:
40-
raise TypeError(f"Invalid value for AbsolutePath: {sequence}")
42+
if isinstance(sequence, str) is False:
43+
raise TypeError("Path must be a string")
44+
45+
if len(self.blacklist.findall(sequence)) > 0:
46+
raise TypeError(f"Illegal path: {sequence}")
47+
4148
self = sequence # type: ignore
4249

4350

51+
class AbsolutePath(Path):
52+
"""Wrapper Type for ensuring a `str` matches an absolute Unix Path."""
53+
54+
def __init__(
55+
self,
56+
sequence: str
57+
) -> None:
58+
if isinstance(sequence, str) is False:
59+
raise TypeError("AbsolutePath must be a string or Path")
60+
61+
if str(sequence).startswith("/") is False:
62+
raise TypeError(
63+
f"Expected AbsolutePath to begin with /, but got: {sequence}"
64+
)
65+
66+
super().__init__(sequence)
67+
68+
4469
class UserInput:
4570
"""Any kind of user input data."""
4671

0 commit comments

Comments
 (0)