Skip to content

Commit 31bf0df

Browse files
committed
Do not allow to provide absolute paths as filesystem will join it silently (fixes #211).
1 parent aefb175 commit 31bf0df

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

bonobo/nodes/io/base.py

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
from bonobo.config import Configurable, ContextProcessor, Option, Service
22

33

4+
def filesystem_path(path: str):
5+
if path.startswith("/"):
6+
raise ValueError(
7+
"File path should not be absolute. If you really need to provide absolute paths, then you must pass a "
8+
"filesystem instance that is bound to your filesystem root and provide a relative path from there."
9+
)
10+
return str(path)
11+
12+
413
class FileHandler(Configurable):
514
"""Abstract component factory for file-related components.
615
@@ -13,39 +22,12 @@ class FileHandler(Configurable):
1322
"""
1423

1524
path = Option(
16-
str,
17-
required=True,
18-
positional=True,
19-
__doc__="""
20-
Path to use within the provided filesystem.
21-
""",
22-
) # type: str
23-
eol = Option(
24-
str,
25-
default="\n",
26-
__doc__="""
27-
Character to use as line separator.
28-
""",
29-
) # type: str
30-
mode = Option(
31-
str,
32-
__doc__="""
33-
What mode to use for open() call.
34-
""",
35-
) # type: str
36-
encoding = Option(
37-
str,
38-
default="utf-8",
39-
__doc__="""
40-
Encoding.
41-
""",
42-
) # type: str
43-
fs = Service(
44-
"fs",
45-
__doc__="""
46-
The filesystem instance to use.
47-
""",
25+
filesystem_path, required=True, positional=True, __doc__="Path to use within the provided filesystem."
4826
) # type: str
27+
eol = Option(str, default="\n", __doc__="Character to use as line separator.") # type: str
28+
mode = Option(str, __doc__="What mode to use for open() call.") # type: str
29+
encoding = Option(str, default="utf-8", __doc__="Encoding.") # type: str
30+
fs = Service("fs", __doc__="The filesystem instance to use.") # type: str
4931

5032
@ContextProcessor
5133
def file(self, context, *, fs):

tests/nodes/io/test_io_base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
3+
from bonobo.nodes.io.base import filesystem_path
4+
5+
6+
def test_filesystem_path_absolute():
7+
with pytest.raises(ValueError):
8+
filesystem_path("/this/is/absolute")
9+
10+
11+
def test_filesystem_path_relative():
12+
assert filesystem_path("this/is/relative") == "this/is/relative"

0 commit comments

Comments
 (0)