Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add protection against invalid image path declarations for add #247

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstring does say relative so I guess it is good. I just wanted to raise a comment that on general we are ok with providing absolute paths everywhere and I think we (or git) normalize them into relative if needed... so it would've been more consistent to just normalize absolute path to correct relative here

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())