Skip to content

Stabilize PyPy CI by hardening restricted pickle loads and failing fast - #777

Merged
wbarnha merged 4 commits into
masterfrom
copilot/troubleshoot-pypy-builds
Aug 11, 2026
Merged

wbarnha merged 4 commits into
masterfrom
copilot/troubleshoot-pypy-builds

Conversation

Copilot AI commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

The PyPy CI leg was timing out instead of surfacing the real failure, and the new restricted-pickle tests were not reliably rejecting disallowed globals under PyPy. This change makes restricted pickle validation interpreter-independent and trims the PyPy job down to the test work it actually needs to do.

  • Restricted pickle: reject disallowed globals before unpickling

    • Added a bytecode pre-scan for restricted pickle payloads so GLOBAL and STACK_GLOBAL references are validated against RestrictedUnpickler.ALLOWED_CLASSES before execution.
    • Keeps pickle_restricted behavior consistent on PyPy, where overriding find_class() alone is not sufficient for all payload shapes.
  • PyPy CI: surface the first real failure instead of timing out

    • Replaced scripts/tests in the PyPy workflow leg with a direct pytest invocation.
    • Added -x so the job stops on the first failing test rather than continuing into a timeout.
    • Avoids the extra bandit pass bundled into scripts/tests, which was consuming time in an already slower advisory PyPy run.
  • Behavioral shape

    • pickle_restricted still allows safe value payloads like literal bytes / bytearray.
    • Payloads that rely on global resolution for disallowed callables are rejected deterministically.
def _loads(self, s: bytes) -> Any:
    _restricted_pickle_validate_globals(s, RestrictedUnpickler.ALLOWED_CLASSES)
    return RestrictedUnpickler(io.BytesIO(s)).load()

Co-authored-by: wbarnha <25623043+wbarnha@users.noreply.github.com>
@codecov

codecov Bot commented Aug 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.16%. Comparing base (4af58d2) to head (6d88d59).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #777      +/-   ##
==========================================
+ Coverage   96.14%   96.16%   +0.02%     
==========================================
  Files         105      105              
  Lines       11304    11367      +63     
  Branches     1204     1223      +19     
==========================================
+ Hits        10868    10931      +63     
  Misses        344      344              
  Partials       92       92              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@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: 1927392f8c

ℹ️ 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".

)
memo_index = int(arg)
memo[memo_index] = stack[-1]
next_memo_index = max(next_memo_index, memo_index + 1)

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 Mirror MEMOIZE's actual memo index

On PyPy, where this pre-scan is the only protection against STACK_GLOBAL bypassing find_class, a hostile protocol-4 stream can mix sparse BINPUT entries with MEMOIZE to evade the allowlist. MEMOIZE writes at len(memo), potentially overwriting an existing slot, but the scanner advances to the greatest explicit index plus one; a stream can therefore make the scanner resolve BINGET 1/BINGET 2 as builtins.list while the real unpickler resolves them as os.system, after which REDUCE enables code execution. Track the memo exactly according to pickle semantics rather than using the maximum index.

Useful? React with 👍 / 👎.

)
stack.pop()
for item in opcode.stack_after:
stack.append(_STACK_MARK if item.name == "mark" else _STACK_PLACEHOLDER)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve concrete values through DUP

When a valid safe pickle uses a stack operation that preserves a value, this generic simulation replaces that value with placeholders. For example, SHORT_BINUNICODE 'builtins'; DUP; POP; SHORT_BINUNICODE 'list'; STACK_GLOBAL is a valid protocol-4 pickle that resolves the allowlisted builtins.list, but the scanner turns the duplicated module string into _STACK_PLACEHOLDER and rejects it with STACK_GLOBAL expected module/name strings. Model value-preserving opcodes such as DUP explicitly so the new pre-scan does not reject allowed payloads that the restricted unpickler previously accepted.

Useful? React with 👍 / 👎.

@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: acb7069b3e

ℹ️ 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 on lines +465 to +466
if name in {"SHORT_BINUNICODE", "BINUNICODE", "BINUNICODE8", "UNICODE"}:
stack.append(arg)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Track legacy string opcodes for STACK_GLOBAL

When a valid stream supplies STACK_GLOBAL operands with STRING, BINSTRING, or SHORT_BINSTRING, the scanner records placeholders instead of their concrete string values and rejects an otherwise allowlisted global. For example, b"S'builtins'\nS'list'\n\x93." resolves to builtins.list and was accepted by RestrictedUnpickler, but now fails with “expected module/name strings”; include these string-producing opcodes in the concrete-value handling so compatible mixed-protocol payloads continue to load.

Useful? React with 👍 / 👎.

@wbarnha
wbarnha merged commit e50ae86 into master Aug 11, 2026
32 checks passed
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