Repository navigation
Warn when the pickle codec deserializes message data (CWE-502) - #776
Conversation
The built-in pickle codec calls pickle.loads() directly on raw Kafka message bytes. Kafka does not authenticate producers, so any client able to write to a topic consumed with value_serializer/key_serializer= 'pickle' can trigger arbitrary code execution in the worker process via a crafted payload. Since pickle is a documented, first-party codec and removing it would be a breaking change, add a SecurityWarning raised on every pickle _loads() call (Python's default warning filter dedupes by call site, so this doesn't spam logs) and document the risk in the codecs module docstring and user guide.
Covers the remaining remediations from the pickle-RCE report: - Warn at codec initialization: Registry and Schema now emit a SecurityWarning as soon as an app/topic/model is configured with serializer='pickle', not just when a message is actually deserialized. - RestrictedUnpickler: a pickle.Unpickler subclass that overrides find_class() to only construct an explicit allowlist of stdlib container/value types, closing off __reduce__-based RCE gadgets (os.system, subprocess.Popen, etc). Users extend RestrictedUnpickler.ALLOWED_CLASSES for their own types. - New pickle_restricted codec (raw_pickle | binary equivalent, but deserializing through RestrictedUnpickler) registered alongside the existing unrestricted pickle codec, for cases where pickle's object support is needed but producers can't be fully trusted. Documented both in the codecs module docstring and the user guide.
Codecov Reportβ
All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #776 +/- ##
==========================================
+ Coverage 96.13% 96.14% +0.01%
==========================================
Files 105 105
Lines 11271 11304 +33
Branches 1200 1204 +4
==========================================
+ Hits 10835 10868 +33
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: f01106386d
βΉοΈ 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".
β¦ gadget - Schema.update() now checks the *resolved* key/value serializer (after falling back to key_type/value_type._options.serializer) instead of only the explicit key_serializer/value_serializer argument. Previously Schema(value_type=SomeRecord) with SomeRecord declaring serializer="pickle" silently skipped the configuration-time SecurityWarning. - Drop bytes/bytearray from RestrictedUnpickler's default allowlist. Both accept a single int and allocate that many zero bytes, so as REDUCE-invoked callables they let a payload of a few dozen bytes force a multi-gigabyte allocation (e.g. `(bytearray, (2_000_000_000,))`). Ordinary bytes/bytearray values are unaffected: pickle has dedicated opcodes for literal bytes/bytearray data that bypass find_class entirely (bytes always; bytearray from protocol 5 onward, which restricted_pickle now dumps with).
There was a problem hiding this comment.
π‘ Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b3cfe0ccab
βΉοΈ 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".
| """ | ||
|
|
||
| def _loads(self, s: bytes) -> Any: | ||
| return RestrictedUnpickler(io.BytesIO(s)).load() |
There was a problem hiding this comment.
Reject oversized memo indexes before restricted unpickling
When pickle_restricted consumes data from an untrusted producer, a nine-byte raw pickle such as 80024e7200e1f5052e uses LONG_BINPUT 100000000 to make CPython expand the unpickler memo to hundreds of MB before find_class is involved; its Base64 form can therefore remotely exhaust a worker despite containing no globals or reducers. Fresh evidence beyond the fixed bytearray reducer is this opcode-only allocation path, which reaches the unrestricted .load() here, so validate or cap memo-index opcodes before loading untrusted data.
Useful? React with πΒ / π.
| warn_if_unsafe_pickle(key_serializer) | ||
| warn_if_unsafe_pickle(value_serializer) |
There was a problem hiding this comment.
Warn when app settings are finalized
When an application configures only its default serializer, for example App("id", value_serializer="pickle"), app.finalize() merely constructs Settings; the Registry is a separate lazy app.serializers property and is not instantiated until the first schema/store serialization call. Consequently these checks do not provide the documented configuration-time warning for app-level settings and may run only when the first message is decoded, alongside the load-time warning. Invoke the check while the serialization settings are normalized or during app finalization rather than relying solely on Registry.__init__.
Useful? React with πΒ / π.
The built-in pickle codec calls pickle.loads() directly on raw Kafka
message bytes. Kafka does not authenticate producers, so any client able
to write to a topic consumed with value_serializer/key_serializer=
'pickle' can trigger arbitrary code execution in the worker process via
a crafted payload.
Since pickle is a documented, first-party codec and removing it would be
a breaking change, add a SecurityWarning raised on every pickle _loads()
call (Python's default warning filter dedupes by call site, so this
doesn't spam logs) and document the risk in the codecs module docstring
and user guide.