-
Notifications
You must be signed in to change notification settings - Fork 0
fix: remove unsafe pickle serialization #2
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
Conversation
Summary of ChangesHello @bniladridas, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the security and reduces the dependency load of the project by replacing an unsafe pickle-based serialization method with a more secure and explicit NumPy and JSON-based approach. The change removes PyTorch as a dependency and refactors model saving and loading across the codebase, while maintaining backward compatibility for existing models. The core functionality of model training, inference, and evaluation remains unchanged, ensuring a low-risk transition. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
c6dbf1e to
ca5baca
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request effectively addresses a critical security vulnerability by replacing unsafe pickle-based serialization with a secure combination of NumPy arrays for weights and JSON for metadata. The removal of the PyTorch dependency is also a great simplification. The changes are well-implemented across the training scripts, model definition, and utility scripts. However, I've found a critical issue where backward compatibility for loading old models re-introduces the same pickle deserialization vulnerability, undermining the primary goal of this PR. My review includes a comment with a suggestion to fully eliminate this risk.
harpertoken/models/model.py
Outdated
| # old format | ||
| old_path = os.path.join(model_id, "model.npy") | ||
| loaded = np.load(old_path, allow_pickle=True) | ||
| if isinstance(loaded, np.ndarray): | ||
| model.weights = loaded | ||
| model.env_name = "CartPole-v1" | ||
| model.env = gym.make(model.env_name) | ||
| model.observation_space = model.env.observation_space.shape[0] | ||
| model.action_type = "discrete" | ||
| model.num_actions = model.env.action_space.n | ||
| model.action_space = model.num_actions | ||
| else: | ||
| model_data = loaded.item() | ||
| model.weights = model_data["weights"] | ||
| model.observation_space = model_data["observation_space"] | ||
| model.action_type = model_data["action_type"] | ||
| model.env_name = model_data.get("env_name", "CartPole-v1") | ||
| model.env = gym.make(model.env_name) | ||
| if model.action_type == "discrete": | ||
| model.num_actions = model_data["num_actions"] | ||
| model.action_space = model.num_actions | ||
| elif model.action_type == "continuous": | ||
| model.action_dim = model_data["action_dim"] | ||
| model.action_bounds = model_data["action_bounds"] | ||
| model.action_space = model.action_dim |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This backward compatibility block re-introduces the critical pickle deserialization vulnerability that this pull request aims to fix. Using np.load with allow_pickle=True on a user-provided local path (model_id) allows for arbitrary code execution if a malicious model file is loaded. This undermines the security improvements made elsewhere.
To ensure the library is secure, I strongly recommend removing support for the old, insecure model format. Instead, you should raise an error to inform the user that the model format is outdated and unsafe to load.
raise ValueError(
"The model at the specified path is in an old, insecure format. "
"Loading of pickle-based models is disabled for security reasons. "
"Please convert the model to the new format (separate 'weights.npy' and 'metadata.json' files)."
)- Replace unsafe pickle-based serialization with safe NumPy arrays and JSON metadata - Update model loading/saving to avoid arbitrary code execution from untrusted sources - Remove unnecessary PyTorch dependency from training script - Fix bugs in main.py training implementation for proper linear policy - Update all related files: models, training, hub, tests, scripts
ca5baca to
7cc6190
Compare
Summary
Remove the PyTorch dependency and fix an unsafe serialization mechanism in
main.pyby replacingtorch.save()(pickle-based) with a safe NumPy-based approach.Why
torch.save(), which pickles the entire CMA-ES object.What changed
torch.save()withnp.save()to persist only the best weights array.Behavior
Before
After
Backward Compatibility
from_pretrained,get_action, andevaluatebehave exactly as documented.Testing
Manually verified loading and evaluation:
Output:
(8,)Risk
Low. Changes are internal, scoped to serialization logic, and covered by manual validation.