Skip to content

Warn when the pickle codec deserializes message data (CWE-502) - #776

Merged
wbarnha merged 3 commits into
masterfrom
claude/faust-pickle-rce-ros5l3
Aug 10, 2026
Merged

wbarnha merged 3 commits into
masterfrom
claude/faust-pickle-rce-ros5l3

Conversation

@wbarnha

@wbarnha wbarnha commented Aug 10, 2026

Copy link
Copy Markdown
Member

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.

claude added 2 commits August 10, 2026 22:41
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

codecov Bot commented Aug 10, 2026 •

Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 96.14%. Comparing base (1a58aa9) to head (b3cfe0c).

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.
πŸ“’ 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: 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".

Comment thread faust/serializers/schemas.py Resolved
Comment thread faust/serializers/codecs.py Resolved
… 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).

@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: 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()

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 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 πŸ‘Β / πŸ‘Ž.

Comment on lines +36 to +37
warn_if_unsafe_pickle(key_serializer)
warn_if_unsafe_pickle(value_serializer)

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 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 πŸ‘Β / πŸ‘Ž.

@wbarnha
wbarnha merged commit 4af58d2 into master Aug 10, 2026
31 of 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