Skip to content

SEC require a TypeNode per entry in dict key types - #570

Open
adrinjalali wants to merge 4 commits into
skops-dev:mainfrom
adrinjalali:sec/dictnode-key-types
Open

adrinjalali wants to merge 4 commits into
skops-dev:mainfrom
adrinjalali:sec/dictnode-key-types

Conversation

@adrinjalali

Copy link
Copy Markdown
Member

Reference Issues/PRs

Follow-up to #547 (#222).

What does this implement/fix? Explain your changes.

DictNode._construct calls each entry of the key-type list on its key. #547 only made sure the list itself is a ListNode, which accepts any node as an entry, so a file could put a callable such as a functools.partial there and have it called with a string from the file while the dict is built. This requires every entry to be a TypeNode, and the number of key types to equal the number of entries (zip silently truncated otherwise), in the current reader and in the protocol <= 2 reader in skops.io.old, which overrides __init__ and would otherwise skip the check.

On the save side, a dict entry whose value is a property is skipped, but its key type was still recorded, which paired the remaining key types with the wrong keys on load. Key types are now recorded per saved entry.

Any other comments?

The changelog amends the existing #547 entry.

DictNode calls each key type on its key when the dict is built, so the key
type list must hold only types, one per entry. Check this while the file is
read, for the current reader and the protocol <= 2 reader. Also stop saving
the key type of a dict entry whose value is a property, which paired the
remaining key types with the wrong keys on load.
@adrinjalali

Copy link
Copy Markdown
Member Author

@3urek4 would you mind having a look here as well?

@3urek4 3urek4 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me overall, nice catch on skipping the property key type and mirroring the check to _general_v2.py.

One small thing I noticed around TypeNode in DictNode._construct (non-blocking, could also be a follow-up): builtin_function_or_method also dispatches to TypeNode, and TypeNode._construct just calls _import_obj(module_name, class_name) without checking that the result is a type. So if a caller trusts a 1-arg function (for example for a FunctionTransformer, where FunctionNode only imports the function on load without calling it), a crafted dict can put a TypeNode naming that function in key_types, pass both isinstance(key_type, TypeNode) and the audit, and have k_type(key) call it on a file-chosen string during loads(). Checking isinstance(k_type, type) in DictNode._construct (current and v2) would close that since dict_get_state always records type(key).

…r types

Check the constructed key types before any is called, in the current and
protocol <= 2 readers, through shared helpers. Refuse dicts whose keys
collide as JSON keys at dump, give a ValueError for key types aliased to
a list still being loaded, and drop the get_tree overloads.

This branch has not been deployed

No deployments
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.

2 participants