Skip to content

FIX validate OperatorFuncNode.attrs node type in protocol <= 2 - #568

Open
3urek4 wants to merge 3 commits into
skops-dev:mainfrom
3urek4:fix-operator-v2-allowed-types
Open

3urek4 wants to merge 3 commits into
skops-dev:mainfrom
3urek4:fix-operator-v2-allowed-types

Conversation

@3urek4

@3urek4 3urek4 commented Nov 28, 2026

Copy link
Copy Markdown

Reference Issues/PRs

Follow-up to #547 and #551.

What does this implement/fix? Explain your changes.

#547 restricted OperatorFuncNode.attrs to TupleNode via allowed_types=(TupleNode,) in skops.io._general, and #551 bumped the persistence protocol to 3 and moved the protocol <= 2 reader to skops.io.old._general_v2.OperatorFuncNode.

The protocol <= 2 reader still called get_tree(state["attrs"], ...) without allowed_types, allowing a file declaring protocol <= 2 to bypass the TupleNode child-kind check noted in skops.io._protocol. This PR passes allowed_types=(_general.TupleNode,) in skops/io/old/_general_v2.py and adds a regression test in test_persist_old.py across protocols 0, 1, and 2.

Any other comments?

No changelog entry added since both #547 and #551 are part of the unreleased v0.17 cycle and #547 already covers child-kind validation in docs/changes.rst.

@adrinjalali adrinjalali left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @3urek4, good catch. The fix is right: with allowed_types=(_general.TupleNode,), a file declaring protocol 0–2 now goes through the same check as the current OperatorFuncNode. That includes a node coming back from the memo through __id__, since get_tree checks those too. I also checked that the new test fails for protocols 0, 1 and 2 without the change to _general_v2.py, and that the other old readers (dict/list/set in _general_v2.py, the generator in _numpy_v1) already check the same node types as their current versions. I agree no changelog entry is needed, since the #547 entry covers this.

I've left a few suggestions inline. They're about keeping the old and current readers from drifting apart again, since that's how this gap opened after #547/#551.

state["attrs"],
load_context,
trusted=trusted,
allowed_types=(_general.TupleNode,),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This now repeats the current node's check by hand, and nothing keeps the two in step. A later PR that tightens allowed_types in skops.io._general.OperatorFuncNode, or moves another node into skops.io.old, could leave the old reader behind again.

It doesn't have to be fixed here, but one of these would help:

  • a shared module-level constant, e.g. _OPERATOR_ATTRS_TYPES = (TupleNode,) in _general.py, used by both classes, or
  • a parametrized test that runs each node-type rejection test from test_audit.py against every protocol that has its own reader.

The second one would also catch the same mistake in other nodes, so I'd lean towards it. Happy for that to be a follow-up.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Agreed — happy to open a follow-up PR for the cross-protocol test!

Comment thread skops/io/tests/test_persist_old.py Outdated


@pytest.mark.parametrize("protocol", [0, 1, 2])
def test_operator_func_v2_wrong_child_type_is_rejected(protocol):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could we widen this a little? At the moment it only covers itemgetter with a SliceNode in attrs. Two cases would be worth adding:

  • attrgetter and methodcaller (parametrize over the operator function), so it doesn't matter which __class__ reaches this reader;
  • an attrs that is an __id__ memo reference to an earlier node that isn't a tuple, so the memo route is covered on the old path too, not only fresh nodes.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done in 7aea103

protocol=protocol,
)
msg = "Expected a node of type TupleNode, got SliceNode"
with pytest.raises(ValueError, match=msg):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Small one: the comment says the bad node is "refused before the audit", but the test only checks that loading fails. If get_tree ever moved the type check after node_cls(state, ...), the bad subtree would be built (and possibly memoized) before the error, and this test would still pass. If it's cheap, you could monkeypatch SliceNode.__init__ to fail, or count calls to it, and assert it's never reached. Otherwise just reword the comment to "is refused".

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done in 7aea103

@3urek4

3urek4 commented Nov 29, 2026

Copy link
Copy Markdown
Author

Thanks for the review @adrinjalali! Addressed the test suggestions in the latest commit:

  • Parametrized test_operator_func_v2_wrong_child_type_is_rejected across attrgetter, itemgetter, and methodcaller, and monkeypatched SliceNode.__init__ to verify the bad child is refused before instantiation.
  • Added test_operator_func_v2_aliased_wrong_child_type_is_rejected covering an __id__ memo reference to an earlier ListNode across protocols 0–2.

Agreed that parametrizing the child-type rejection suite in test_audit.py across old protocol readers is the cleaner way to prevent future drift — happy to open a follow-up PR for that once this lands.

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