Skip to content

Commit

Permalink
Suppress pickle security issues in pytorch materializer
Browse files Browse the repository at this point in the history
  • Loading branch information
stefannica committed Sep 25, 2024
1 parent c6f14b0 commit 1362cda
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ def load(self, data_type: Type[Any]) -> Any:
The loaded PyTorch object.
"""
with fileio.open(os.path.join(self.uri, self.FILENAME), "rb") as f:
return torch.load(f)
# NOTE (security): The `torch.load` function uses `pickle` as
# the default unpickler, which is NOT secure. This materializer
# is intended for use with trusted data sources.
return torch.load(f) # nosec

def save(self, obj: Any) -> None:
"""Uses `torch.save` to save a PyTorch object.
Expand All @@ -50,7 +53,10 @@ def save(self, obj: Any) -> None:
obj: The PyTorch object to save.
"""
with fileio.open(os.path.join(self.uri, self.FILENAME), "wb") as f:
torch.save(obj, f, pickle_module=cloudpickle)
# NOTE (security): The `torch.save` function uses `cloudpickle` as
# the default unpickler, which is NOT secure. This materializer
# is intended for use with trusted data sources.
torch.save(obj, f, pickle_module=cloudpickle) # nosec


# Alias for the BasePyTorchMaterializer class, allowing users that have already used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ def save(self, model: Module) -> None:
with fileio.open(
os.path.join(self.uri, CHECKPOINT_FILENAME), "wb"
) as f:
torch.save(model.state_dict(), f, pickle_module=cloudpickle)
# NOTE (security): The `torch.save` function uses `cloudpickle` as
# the default unpickler, which is NOT secure. This materializer
# is intended for use with trusted data sources.
torch.save(model.state_dict(), f, pickle_module=cloudpickle) # nosec

def extract_metadata(self, model: Module) -> Dict[str, "MetadataType"]:
"""Extract metadata from the given `Model` object.
Expand Down

0 comments on commit 1362cda

Please sign in to comment.