Repository navigation
SEC tie a loaded sparse matrix to its declared type - #572
Open
adrinjalali wants to merge 2 commits into
Open
adrinjalali wants to merge 2 commits into
adrinjalali wants to merge 2 commits into
Conversation
SparseMatrixNode trusted the abstract spmatrix/sparray bases by default while load_npz builds whatever concrete format the npz names, so a file could pass the audit under a base or unrelated type and materialise an unaudited sparse object. Stop trusting the bases, refuse declarations that are not a concrete scipy.sparse type, and check the built object against the declared format.
Member
Author
|
@3urek4 would you mind having a look here? |
3urek4
approved these changes
Nov 29, 2026
3urek4
left a comment
There was a problem hiding this comment.
Thanks @adrinjalali, looks good to me. Rejecting non-scipy.sparse.* modules and spmatrix/sparray in __init__ before reading the .npz member, and checking isinstance(obj, sparse_bases) with type(obj).__name__ == self.class_name in _construct, covers both the base-class bypass and the trusted-type spoofing case without breaking across scipy module moves.
Leaving index validation to a separate PR makes sense too (check_format(full_check=True) only exists on csr/csc/bsr, while coo and dia don't have check_format and would need separate bounds checks).
This branch has not been deployed
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.
Reference Issues/PRs
What does this implement/fix? Explain your changes.
SparseMatrixNodetrusted the abstract basesspmatrixandsparrayby default, while_constructignores the declared type and letsload_npzbuild whatever concrete format the npz member names. A file could declare one of the bases, pass the audit withtrusted=None, and still have an unaudited concrete sparse object materialised; or declare any type the caller trusts and get a sparse object instead.This stops trusting the bases (concrete sparse types were never trusted by default, since the trust check is an exact name match, so genuine files behave as before), refuses a declaration that is not a concrete
scipy.sparsetype while the file is read, and checks that the built object is a sparse object of the declared class. The class name, not scipy's internal module path, is compared, since that path has moved between scipy versions.Any other comments?
The index arrays of a trusted sparse file are still not validated (
check_format(full_check=True)); that is the same class of issue as #567 and is left for a separate PR.