Fix #1119: avoid unsafe approach to deserializing data #1141
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.
CodeQL security scan report #567 flagged a data loading operation in
src/openfermion/utils/operator_utils.py
as being usafe due because it uses a user-provided value. The warning is about lines 282-283, involving the codeThe problem here is that loading serialized Python objects using
marshal
is inherently unsafe, because the format is not designed to be secure against malicious or corrupted data. Unfortunately, we probably can't simply switch to a safer serialization method (e.g., JSON or protobufs) because users may already have saved files in this format. We should maintain backward compatibility with people's existing files.This PR adds more checks on the data read by
load_operator()
, including checks on maximum file size.Note: the maximum file size values are hardwired as constants near the top of this file. I don't have much evidence for what would be reasonable max values, so made some guesses based on other files found in the repository. Someone with longer experiencing using OpenFermion should double-check the values.