Repository navigation
fix: confine datastore write paths #1915
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
Open
garciadias
wants to merge
9
commits into
main
Choose a base branch
from
fix/confine-datastore-write-paths
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5d2f672
fix: confine datastore write paths to prevent path traversal
garciadias caa5bfe
fix: handle cross-drive ValueError in path confinement check
garciadias a77ff17
fix(ci): add SimpleITK to dev requirements for unit tests
garciadias 6d655d7
fix(ci): resolve pre-existing CI failures
garciadias 5a9f93c
fix(test): remove double unlink in test_binary_to_image
garciadias 3da00b7
fix: restore compatibility with MONAI 1.6 in dataset download and NIf…
garciadias 5021250
test: xfail graphcut inferer tests on MONAI >= 1.6 (Spacingd regression)
garciadias 28ab201
test: rename datastore path tests and tidy wording
garciadias 78ae7d2
fix(packaging): declare SimpleITK, pyOpenSSL and huggingface_hub in i…
garciadias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
71 changes: 71 additions & 0 deletions
71
tests/unit/datastore/test_local_datastore_path_confinement.py
This file contains hidden or 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,71 @@ | ||
| # Copyright (c) MONAI Consortium | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import os | ||
| import shutil | ||
| import tempfile | ||
| import unittest | ||
|
|
||
| from monailabel.datastore.local import LocalDatastore | ||
|
|
||
|
|
||
| class TestLocalDatastorePathConfinement(unittest.TestCase): | ||
| """Image/label IDs and label tags must resolve inside the datastore image/labels directories.""" | ||
|
|
||
| def setUp(self): | ||
| self.base = tempfile.mkdtemp(prefix="monailabel_ds_confine_") | ||
| self.studies = os.path.join(self.base, "studies") | ||
| os.makedirs(self.studies, exist_ok=True) | ||
| self.datastore = LocalDatastore(self.studies) | ||
|
|
||
| self.image_file = os.path.join(self.base, "image.nii.gz") | ||
| with open(self.image_file, "wb") as f: | ||
| f.write(b"image") | ||
|
|
||
| self.image_id = "image_1" | ||
| self.datastore.add_image(self.image_id, self.image_file, {}) | ||
|
|
||
| def tearDown(self): | ||
| shutil.rmtree(self.base, ignore_errors=True) | ||
|
|
||
| def test_add_image_rejects_id_outside_image_dir(self): | ||
| outside = os.path.realpath(os.path.join(self.base, "other.nii.gz")) | ||
| with self.assertRaises(ValueError): | ||
| self.datastore.add_image("../../other", self.image_file, {}) | ||
| self.assertFalse(os.path.exists(outside)) | ||
|
|
||
| def test_add_image_accepts_plain_id(self): | ||
| image_id = self.datastore.add_image("image_2", self.image_file, {}) | ||
| self.assertEqual(image_id, "image_2") | ||
| self.assertTrue(os.path.exists(os.path.join(self.studies, "image_2.nii.gz"))) | ||
|
|
||
| def test_save_label_rejects_tag_outside_labels_dir(self): | ||
| outside = os.path.realpath(os.path.join(self.base, "labels", "final", "other.nii.gz")) | ||
| with self.assertRaises(ValueError): | ||
| self.datastore.save_label(self.image_id, self.image_file, "../../labels/final/other", {}) | ||
| self.assertFalse(os.path.exists(outside)) | ||
|
|
||
| def test_save_label_rejects_image_id_outside_labels_dir(self): | ||
| # an existing datastore entry whose ID does not resolve inside the labels directory | ||
| self.datastore._datastore.objects["../../other"] = self.datastore._datastore.objects[self.image_id] | ||
| outside = os.path.realpath(os.path.join(self.base, "other.nii.gz")) | ||
| with self.assertRaises(ValueError): | ||
| self.datastore.save_label("../../other", self.image_file, "final", {}) | ||
| self.assertFalse(os.path.exists(outside)) | ||
|
|
||
| def test_save_label_accepts_plain_input(self): | ||
| label_id = self.datastore.save_label(self.image_id, self.image_file, "final", {}) | ||
| self.assertEqual(label_id, self.image_id) | ||
| self.assertTrue(os.path.exists(os.path.join(self.studies, "labels", "final", "image_1.nii.gz"))) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.