Skip to content

Commit

Permalink
Add protection against invalid image path declarations for add
Browse files Browse the repository at this point in the history
This is a bit painful, because datalad-next parameter validation
is not available, but worth adding given the issue.

Closes datalad#150
  • Loading branch information
mih committed Oct 10, 2023
1 parent c505e52 commit 2d245c2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
10 changes: 10 additions & 0 deletions datalad_container/containers_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ def __call__(name, url=None, dataset=None, call_fmt=None, image=None,
purpose='add container')
runner = WitlessRunner()

if image is not None:
if op.isabs(image):
raise ValueError(
f'image must be a relative path, got {image!r}')
if ds.pathobj.resolve() \
not in (ds.pathobj / image).resolve().parents:
raise ValueError(
'image must be a relative path pointing inside'
f'the dataset, got {image!r}')

# prevent madness in the config file
if not re.match(r'^[0-9a-zA-Z-]+$', name):
raise ValueError(
Expand Down
25 changes: 22 additions & 3 deletions datalad_container/tests/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,27 @@

from datalad_container.containers_add import _ensure_datalad_remote

# NOTE: At the moment, testing of the containers-add itself happens implicitly
# via use in other tests.
# NOTE: At the moment, most testing of the containers-add itself happens implicitly
# this via use in other tests.

common_kwargs = {'result_renderer': 'disabled'}


def test_add_invalid_imgpath(tmp_path):
ds = Dataset(tmp_path).create(**common_kwargs)
# path spec must be relative
with pytest.raises(ValueError):
ds.containers_add(
'dummy',
image=tmp_path,
**common_kwargs
)
with pytest.raises(ValueError):
ds.containers_add(
'dummy',
image=Path('..', 'sneaky'),
**common_kwargs
)


@with_tempfile
Expand All @@ -41,4 +60,4 @@ def test_ensure_datalad_remote_maybe_enable(path=None, *, autoenable):
if not autoenable:
assert_not_in("datalad", repo.get_remotes())
_ensure_datalad_remote(repo)
assert_in("datalad", repo.get_remotes())
assert_in("datalad", repo.get_remotes())

0 comments on commit 2d245c2

Please sign in to comment.