Repository navigation
Stabilize PyPy CI by hardening restricted pickle loads and failing fast - #777
Conversation
Co-authored-by: wbarnha <25623043+wbarnha@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
💡 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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
💡 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".
| if name in {"SHORT_BINUNICODE", "BINUNICODE", "BINUNICODE8", "UNICODE"}: | ||
| stack.append(arg) |
There was a problem hiding this comment.
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 👍 / 👎.
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
GLOBALandSTACK_GLOBALreferences are validated againstRestrictedUnpickler.ALLOWED_CLASSESbefore execution.pickle_restrictedbehavior consistent on PyPy, where overridingfind_class()alone is not sufficient for all payload shapes.PyPy CI: surface the first real failure instead of timing out
scripts/testsin the PyPy workflow leg with a directpytestinvocation.-xso the job stops on the first failing test rather than continuing into a timeout.banditpass bundled intoscripts/tests, which was consuming time in an already slower advisory PyPy run.Behavioral shape
pickle_restrictedstill allows safe value payloads like literalbytes/bytearray.