Skip to content

fix: harden torch.load, bare except, squeeze, and type checks#937

Open
haoyu-haoyu wants to merge 1 commit intosunlabuiuc:masterfrom
haoyu-haoyu:fix/security-and-correctness
Open

fix: harden torch.load, bare except, squeeze, and type checks#937
haoyu-haoyu wants to merge 1 commit intosunlabuiuc:masterfrom
haoyu-haoyu:fix/security-and-correctness

Conversation

@haoyu-haoyu
Copy link
Copy Markdown
Contributor

Summary

Fix 14 security and correctness issues across 12 files, found via codebase audit.

Security: unsafe torch.load() (5 files)

Added weights_only=True to prevent arbitrary code execution from untrusted pickle files:

File Line
pyhealth/medcode/.../train_kge_model.py 57
examples/lime_stagenet_mimic4.py 190
examples/interpretability/gim_transformer_mimic4.py 116
examples/interpretability/shap_stagenet_mimic4.py 189
examples/interpretability/gim_stagenet_mimic4.py 84

Correctness: bare except: (5 files)

Replaced with except Exception: or except ImportError: to avoid swallowing KeyboardInterrupt/SystemExit:

File Line Fix
pyhealth/metrics/ranking.py 36 except ImportError:
pyhealth/datasets/base_dataset.py 835, 900 except Exception:
pyhealth/calib/predictionset/favmac/core.py 119 except Exception:
pyhealth/calib/predictionset/scrib/quicksearch.py 12 except Exception:

Correctness: .squeeze() without dim (2 files)

Prevents batch dimension collapse when batch_size=1:

File Line Fix
pyhealth/interpret/methods/basic_gradient.py 133 .squeeze().squeeze(1)
pyhealth/tasks/temple_university_EEG_tasks.py 190-191 .squeeze().item()

Correctness: type() == (1 file)

File Line Fix
pyhealth/datasets/utils.py 143 type(i) == intisinstance(i, int)

All changes are mechanical, one-line fixes with no behavioral change for correct inputs.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant