Skip to content

fix: an object NEWOBJ builds from an allowlisted class is not an import name - #174

Merged
lab700xdev merged 2 commits into
mainfrom
newobj-instances-are-not-names
Dec 4, 2026
Merged

lab700xdev merged 2 commits into
mainfrom
newobj-instances-are-not-names

Conversation

@lab700xdev

@lab700xdev lab700xdev commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • joblib 1.5 and later write an object array as a second pickle inside the file, which joblib reads with a fresh unpickler and memo.
  • That second pickle reuses a memo slot number the first part gave to a NumpyArrayWrapper built by NEWOBJ.
  • The scanner walks the file as one stream, so the wrapper object appeared as the module of an import, and PR 173 counts any call result as a possibly computed name.

Fix: a NEWOBJ result of joblib.numpy_pickle.NumpyArrayWrapper counts as an ordinary object. That class inherits object.__new__ in joblib 1.3, 1.5 and 1.6, and an unpickler accepts an import name only if it's exactly a str. Every other NEWOBJ result still counts as a possible name, because a class can hand back a string:

  • ast.Index.__new__ returns its argument unchanged. Python's own pickle confirms NEWOBJ(ast.Index, ("posix",)) reaches the import.
  • A package can hold str under another name: typing.Text, or NumPy 1.26's numpy.compat.py3k.unicode. The first version of this PR exempted every allowlisted class, and Codex showed the NumPy route gets past that.

REDUCE, OBJ and INST are unchanged, because they can call a function, and a function can return a string.

Checks:

  • Pickles written by dill 0.4.1, scikit-learn 1.9.1 and joblib 1.6.0, 1.5.2 and 1.3.2 (464 files, every compression option, both modes): results now identical to the CLI before PR 173. With PR 173 alone, 12 joblib object-array files changed.
  • Full suite (8,669 tests) at 96% coverage, and bypass-scorecard --check.
  • 2,738 real local file-and-mode results: unchanged.
  • 400,000 random opcode streams: no finding lost against main.

…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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-04T03:49:56.341421Z 1f2bb7e PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 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".

Comment thread aisbom/safety.py Outdated
Comment on lines +523 to +525
if not _is_safe_import(*imported):
return _COMPUTED
return None

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.
@lab700xdev
lab700xdev merged commit 1800fc8 into main Dec 4, 2026
2 checks passed
@lab700xdev
lab700xdev deleted the newobj-instances-are-not-names branch December 4, 2026 04:12
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.

1 participant