fix: harden torch.load, bare except, squeeze, and type checks#937
Open
haoyu-haoyu wants to merge 1 commit intosunlabuiuc:masterfrom
Open
fix: harden torch.load, bare except, squeeze, and type checks#937haoyu-haoyu wants to merge 1 commit intosunlabuiuc:masterfrom
haoyu-haoyu wants to merge 1 commit intosunlabuiuc:masterfrom
Conversation
Security: - Add weights_only=True to 5 torch.load() calls that were missing it, preventing arbitrary code execution from untrusted pickle files Correctness: - Replace 5 bare except: clauses with except Exception: or except ImportError: to avoid swallowing KeyboardInterrupt/SystemExit (metrics/ranking.py, datasets/base_dataset.py x2, calib/predictionset/favmac/core.py, calib/predictionset/scrib/quicksearch.py) - Replace .squeeze() with .squeeze(1) in interpret/basic_gradient.py to prevent batch dimension collapse when batch_size=1 - Replace .squeeze() with .item() in tasks/temple_university_EEG_tasks.py for scalar tensor extraction (clearer intent, no dimension ambiguity) - Replace type(i) == int with isinstance(i, int) in datasets/utils.py to correctly handle numpy integer types and bool subclasses
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fix 14 security and correctness issues across 12 files, found via codebase audit.
Security: unsafe
torch.load()(5 files)Added
weights_only=Trueto prevent arbitrary code execution from untrusted pickle files:pyhealth/medcode/.../train_kge_model.pyexamples/lime_stagenet_mimic4.pyexamples/interpretability/gim_transformer_mimic4.pyexamples/interpretability/shap_stagenet_mimic4.pyexamples/interpretability/gim_stagenet_mimic4.pyCorrectness: bare
except:(5 files)Replaced with
except Exception:orexcept ImportError:to avoid swallowingKeyboardInterrupt/SystemExit:pyhealth/metrics/ranking.pyexcept ImportError:pyhealth/datasets/base_dataset.pyexcept Exception:pyhealth/calib/predictionset/favmac/core.pyexcept Exception:pyhealth/calib/predictionset/scrib/quicksearch.pyexcept Exception:Correctness:
.squeeze()without dim (2 files)Prevents batch dimension collapse when
batch_size=1:pyhealth/interpret/methods/basic_gradient.py.squeeze()→.squeeze(1)pyhealth/tasks/temple_university_EEG_tasks.py.squeeze()→.item()Correctness:
type() ==(1 file)pyhealth/datasets/utils.pytype(i) == int→isinstance(i, int)All changes are mechanical, one-line fixes with no behavioral change for correct inputs.