Repository navigation
fix: an object NEWOBJ builds from an allowlisted class is not an import name - #174
Conversation
…rt name joblib 1.5 and later write an object array as a second pickle inside the file, read with a fresh memo, and reuse a memo slot number that the first part gave to a NumpyArrayWrapper built by NEWOBJ. Walked as one stream, that object looked like a name computed at load time, and every such file was rated CRITICAL as UNRESOLVED_IMPORT. An unpickler takes an import name only if it is exactly a str. A NEWOBJ result is now treated as an ordinary object when its class is on strict mode's allowlist and is not str or an alias of it. Other classes keep counting as a possible name: ast.Index.__new__, for one, returns its argument unchanged. REDUCE, OBJ and INST are unchanged, because they can call a function that returns a string.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1f2bb7e018
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if not _is_safe_import(*imported): | ||
| return _COMPUTED | ||
| return None |
There was a problem hiding this comment.
Preserve computed-name tracking for allowlisted string aliases
For environments using NumPy 1.26, numpy.compat.py3k.unicode and basestring are additional aliases of str, as confirmed by the NumPy source. Both pass _is_safe_import, so this code incorrectly treats their NEWOBJ results as non-string objects. A pickle can construct and memoize 'posix' and 'system' through these aliases, clear recent with an unrelated safe STACK_GLOBAL, then retrieve the memoized strings for another STACK_GLOBAL and invoke its result. I verified that this stream produces no findings in either mode, whereas the parent reports UNRESOLVED_IMPORT. Restrict this exemption to verified non-string-producing classes rather than assuming every class in an allowlisted module is safe.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed, and fixed in the latest commit. A whole allowlisted package can't be vouched for, so the exemption is now the one class the joblib layout needs, joblib.numpy_pickle.NumpyArrayWrapper. It inherits object.new in joblib 1.3, 1.5 and 1.6. Every other NEWOBJ result, including numpy.compat.py3k.unicode, numpy.compat.basestring and collections.OrderedDict, still counts as a possible name; each is now a regression test in both modes. On 400,000 random streams the branch now loses no finding against main, and the joblib object-array files are clean again.
A whole allowlisted package can hold str under another name: NumPy 1.26's numpy.compat.py3k.unicode and basestring both pass the allowlist. The exemption is now the one class the joblib layout needs, joblib.numpy_pickle.NumpyArrayWrapper, which inherits object.__new__ in joblib 1.3, 1.5 and 1.6. Every other NEWOBJ result still counts as a possible name.
Fixes a false positive from PR 173 that never shipped: every object array saved with joblib 1.5 or later was rated
CRITICAL (UNRESOLVED_IMPORT).Cause:
NumpyArrayWrapperbuilt byNEWOBJ.Fix: a
NEWOBJresult ofjoblib.numpy_pickle.NumpyArrayWrappercounts as an ordinary object. That class inheritsobject.__new__in joblib 1.3, 1.5 and 1.6, and an unpickler accepts an import name only if it's exactly astr. Every otherNEWOBJresult still counts as a possible name, because a class can hand back a string:ast.Index.__new__returns its argument unchanged. Python's ownpickleconfirmsNEWOBJ(ast.Index, ("posix",))reaches the import.strunder another name:typing.Text, or NumPy 1.26'snumpy.compat.py3k.unicode. The first version of this PR exempted every allowlisted class, and Codex showed the NumPy route gets past that.REDUCE,OBJandINSTare unchanged, because they can call a function, and a function can return a string.Checks:
bypass-scorecard --check.main.