-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix: restrict algo_from_json _target_ to Algo subclasses (GHSA-2wx3) #9115
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
3
commits into
Project-MONAI:dev
Choose a base branch
from
garciadias:secfix/ghsa-2wx3-algo-target
base: dev
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.
+126
−17
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # 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. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import os | ||
| import tempfile | ||
| import unittest | ||
|
|
||
| from monai.auto3dseg.utils import _reject_non_algo_target, algo_from_json | ||
|
|
||
|
|
||
| class TestAlgoTargetAllowlist(unittest.TestCase): | ||
| """Regression tests for GHSA-2wx3-8x3w-r8qv. | ||
|
|
||
| ``algo_from_json`` resolves the file's ``_target_`` through ``ConfigParser``, which imports the | ||
| dotted path and calls it. ``algo_object.json`` has exactly one legitimate target type, so a | ||
| ``_target_`` that is not an ``Algo`` subclass is rejected before it is instantiated. | ||
| """ | ||
|
|
||
| def test_code_execution_targets_are_rejected(self): | ||
| for target in ("subprocess.call", "os.system", "builtins.eval", "builtins.exec", "shutil.rmtree"): | ||
| with self.subTest(target=target): | ||
| with self.assertRaises(ValueError) as ctx: | ||
| _reject_non_algo_target(target, "algo_object.json") | ||
| self.assertIn("GHSA-2wx3-8x3w-r8qv", str(ctx.exception)) | ||
|
|
||
| def test_non_class_target_is_rejected(self): | ||
| """A module or plain function is not an Algo subclass.""" | ||
| with self.assertRaisesRegex(ValueError, r"GHSA-2wx3-8x3w-r8qv"): | ||
| _reject_non_algo_target("json.dumps", "algo_object.json") | ||
|
|
||
| def test_unresolvable_target_raises_module_not_found(self): | ||
| """Unresolvable names raise ModuleNotFoundError so the caller can try the next path.""" | ||
| with self.assertRaises(ModuleNotFoundError): | ||
| _reject_non_algo_target("no_such_module.NoSuchAlgo", "algo_object.json") | ||
|
|
||
| def test_algo_subclass_is_accepted(self): | ||
| """A real Algo subclass passes the check.""" | ||
| _reject_non_algo_target("monai.apps.auto3dseg.BundleAlgo", "algo_object.json") | ||
|
|
||
| def test_non_string_target_is_rejected(self): | ||
| """A JSON ``null`` ``_target_`` raises the documented ``ValueError``, not ``AttributeError``.""" | ||
| with self.assertRaisesRegex(ValueError, r"expected a string"): | ||
| _reject_non_algo_target(None, "algo_object.json") | ||
|
|
||
| def test_bare_algo_base_class_is_rejected(self): | ||
| """The abstract ``Algo`` base class is not itself a valid instantiation target.""" | ||
| with self.assertRaisesRegex(ValueError, r"GHSA-2wx3-8x3w-r8qv"): | ||
| _reject_non_algo_target("monai.auto3dseg.algo_gen.Algo", "algo_object.json") | ||
|
|
||
| def test_algo_from_json_rejects_payload_target(self): | ||
| """End to end: a malicious algo_object.json never reaches instantiation.""" | ||
| with tempfile.TemporaryDirectory() as tempdir: | ||
| path = os.path.join(tempdir, "algo_object.json") | ||
| marker = os.path.join(tempdir, "PWNED") | ||
| with open(path, "w") as f: | ||
| json.dump({"_target_": "subprocess.call", "args": ["/bin/sh", "-c", f"touch {marker}"]}, f) | ||
|
|
||
| with self.assertRaisesRegex(ValueError, r"GHSA-2wx3-8x3w-r8qv"): | ||
| algo_from_json(path) | ||
| self.assertFalse(os.path.exists(marker), "the algo_object.json payload executed") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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.