Skip to content

[Data] Gate unsafe deserialization in WebDataset default decoder - #63469

Merged
bveeramani merged 4 commits into
masterfrom
data-2359-webdataset-unsafe-pickle
May 19, 2026
Merged

bveeramani merged 4 commits into
masterfrom
data-2359-webdataset-unsafe-pickle

Conversation

@bveeramani

@bveeramani bveeramani commented May 18, 2026 •

Copy link
Copy Markdown
Member

Description

The default decoder in read_webdataset runs pickle.loads on .pkl/.pickle files and torch.load(weights_only=False) on .pt/.pth files from attacker-controlled TAR archives, enabling arbitrary code execution with no opt-in. This is the same class of bug as GHSA-mw35-8rx3-xf9r (patched in 2.55.0 for Parquet), but in a different code path that was not addressed.

This PR gates the unsafe .pkl/.pickle and .pt/.pth branches in _default_decoder behind the RAY_DATA_WEBDATASET_ALLOW_UNSAFE_DESERIALIZATION=1 environment variable. The error message points users to the existing decoder parameter on ray.data.read_webdataset() as the safe escape hatch for custom deserialization.

Related issues

Fixes GHSA-hhrp-gw25-jr43

Additional information

  • Existing tests that rely on .pt round-trip encoding/decoding now use a monkeypatch.setenv fixture to opt in
  • New tests verify: rejection of all four unsafe extensions by default, opt-in via env var, and bypass via custom decoder callable

…nd env var

The default decoder in `read_webdataset` runs `pickle.loads` on `.pkl/.pickle`
files and `torch.load(weights_only=False)` on `.pt/.pth` files from
attacker-controlled TAR archives, enabling arbitrary code execution. Gate
these branches behind `RAY_DATA_WEBDATASET_ALLOW_UNSAFE_DESERIALIZATION=1`
and point users to the existing `decoder` parameter as the safe escape hatch.

Signed-off-by: Balaji Veeramani <[email protected]>
@bveeramani
bveeramani requested a review from a team as a code owner May 18, 2026 20:39
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@cursor cursor 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.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit cc6a31b. Configure here.

@bveeramani
bveeramani enabled auto-merge (squash) May 18, 2026 21:34
@github-actions github-actions Bot added the go add ONLY when ready to merge, run all tests label May 18, 2026
_allow_unsafe_deserialization() checked os.environ on workers, which
never saw monkeypatch.setenv from the driver. Instead, read the env
var in WebDatasetDatasource.__init__ (driver-side), store the result
as self._allow_unsafe_deserialization, and pass it to _default_decoder
via functools.partial. The datasource is pickled to workers, so the
flag propagates automatically.

Also fix custom decoder test key match (pkl, not .pkl).

Signed-off-by: Balaji Veeramani <[email protected]>
@github-actions
github-actions Bot disabled auto-merge May 19, 2026 00:49
@bveeramani
bveeramani enabled auto-merge (squash) May 19, 2026 01:11
@bveeramani
bveeramani merged commit 41443a1 into master May 19, 2026
4 checks passed
@bveeramani
bveeramani deleted the data-2359-webdataset-unsafe-pickle branch May 19, 2026 01:37
@ray-gardener ray-gardener Bot added the data Ray Data-related issues label May 19, 2026
TruongQuangPhat pushed a commit to cyhapun/ray-fix-issue that referenced this pull request May 27, 2026
…-project#63469)

## Description

The default decoder in `read_webdataset` runs `pickle.loads` on
`.pkl/.pickle` files and `torch.load(weights_only=False)` on `.pt/.pth`
files from attacker-controlled TAR archives, enabling arbitrary code
execution with no opt-in. This is the same class of bug as
GHSA-mw35-8rx3-xf9r (patched in 2.55.0 for Parquet), but in a different
code path that was not addressed.

This PR gates the unsafe `.pkl/.pickle` and `.pt/.pth` branches in
`_default_decoder` behind the
`RAY_DATA_WEBDATASET_ALLOW_UNSAFE_DESERIALIZATION=1` environment
variable. The error message points users to the existing `decoder`
parameter on `ray.data.read_webdataset()` as the safe escape hatch for
custom deserialization.

## Related issues

Fixes GHSA-hhrp-gw25-jr43

## Additional information

- Existing tests that rely on `.pt` round-trip encoding/decoding now use
a `monkeypatch.setenv` fixture to opt in
- New tests verify: rejection of all four unsafe extensions by default,
opt-in via env var, and bypass via custom `decoder` callable

---------

Signed-off-by: Balaji Veeramani <[email protected]>
Signed-off-by: phattruong <[email protected]>
alexandrplashchinsky pushed a commit to alexandrplashchinsky/ray-alex that referenced this pull request May 29, 2026
…-project#63469)

## Description

The default decoder in `read_webdataset` runs `pickle.loads` on
`.pkl/.pickle` files and `torch.load(weights_only=False)` on `.pt/.pth`
files from attacker-controlled TAR archives, enabling arbitrary code
execution with no opt-in. This is the same class of bug as
GHSA-mw35-8rx3-xf9r (patched in 2.55.0 for Parquet), but in a different
code path that was not addressed.

This PR gates the unsafe `.pkl/.pickle` and `.pt/.pth` branches in
`_default_decoder` behind the
`RAY_DATA_WEBDATASET_ALLOW_UNSAFE_DESERIALIZATION=1` environment
variable. The error message points users to the existing `decoder`
parameter on `ray.data.read_webdataset()` as the safe escape hatch for
custom deserialization.

## Related issues

Fixes GHSA-hhrp-gw25-jr43

## Additional information

- Existing tests that rely on `.pt` round-trip encoding/decoding now use
a `monkeypatch.setenv` fixture to opt in
- New tests verify: rejection of all four unsafe extensions by default,
opt-in via env var, and bypass via custom `decoder` callable

---------

Signed-off-by: Balaji Veeramani <[email protected]>
Signed-off-by: Alexandr Plashchinsky <[email protected]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…-project#63469)

## Description

The default decoder in `read_webdataset` runs `pickle.loads` on
`.pkl/.pickle` files and `torch.load(weights_only=False)` on `.pt/.pth`
files from attacker-controlled TAR archives, enabling arbitrary code
execution with no opt-in. This is the same class of bug as
GHSA-mw35-8rx3-xf9r (patched in 2.55.0 for Parquet), but in a different
code path that was not addressed.

This PR gates the unsafe `.pkl/.pickle` and `.pt/.pth` branches in
`_default_decoder` behind the
`RAY_DATA_WEBDATASET_ALLOW_UNSAFE_DESERIALIZATION=1` environment
variable. The error message points users to the existing `decoder`
parameter on `ray.data.read_webdataset()` as the safe escape hatch for
custom deserialization.

## Related issues

Fixes GHSA-hhrp-gw25-jr43

## Additional information

- Existing tests that rely on `.pt` round-trip encoding/decoding now use
a `monkeypatch.setenv` fixture to opt in
- New tests verify: rejection of all four unsafe extensions by default,
opt-in via env var, and bypass via custom `decoder` callable

---------

Signed-off-by: Balaji Veeramani <[email protected]>
Signed-off-by: 400Ping <[email protected]>
goutamvenkat-anyscale added a commit that referenced this pull request Nov 3, 2026
## Summary

`read_images`, the `read_webdataset` default decoder, and the LeRobot
image-camera decoder passed untrusted bytes straight to `PIL.Image.open`
with no format restriction. PIL picks a plugin by **file signature, not
extension**, so a payload named `a.jpg` that actually contains EPS bytes
is routed to PIL's EPS plugin — which shells out to the external
**Ghostscript** binary. On a host with a vulnerable Ghostscript this can
execute code in the Ray worker.

This is the image sibling of the `.pkl`/`.pt` decoder hardening done in
GHSA-hhrp-gw25-jr43; that fix
(#63469) added a deserialization
guard but did not add an image-format allowlist.

Fix: restrict all three decoders to a raster-format allowlist via
`Image.open(..., formats=[...])`, so disallowed formats (EPS, etc.) are
rejected before any plugin runs. The allowlist lives in one place
(`_internal/datasource/image_utils.py`) so the readers can't drift
apart.

Addresses GHSA-pwm8-8ghq-qw5p.

## Before

- `ray.data.read_images("a.jpg")` where `a.jpg` holds EPS bytes → PIL
selects the EPS plugin → attempts to launch Ghostscript.
- `ray.data.read_webdataset(tar)` with a member `0001.jpg` holding EPS
bytes → same dispatch via the default decoder.
- `ray.data.read_lerobot(...)` with an image-camera parquet cell holding
EPS bytes → same dispatch in `_decode_image_frames`.
- No allowlist: any format PIL can sniff is reachable from untrusted
input.

## After

- One shared allowlist, `_ALLOWED_PIL_FORMATS = ["PNG", "JPEG", "TIFF",
"BMP", "GIF"]`, in `_internal/datasource/image_utils.py` (aligned with
`ImageDatasource._FILE_EXTENSIONS`).
  - `read_images`: uses `_ALLOWED_PIL_FORMATS`.
- LeRobot: uses `_ALLOWED_PIL_FORMATS`. This covers what LeRobot writes
(PNG camera frames, TIFF depth maps) and JPEG frames common in Hub
datasets.
- WebDataset decoder: `_ALLOWED_WEBDATASET_PIL_FORMATS =
_ALLOWED_PIL_FORMATS + ["PPM"]` (PPM covers the `ppm/pgm/pbm/pnm`
extensions it handles).
- A datasource that needs extra formats extends the shared list instead
of defining its own.
- Disallowed content (EPS, etc.) raises `UnidentifiedImageError` / a
clear `ValueError` (`read_images`) **before** any PIL plugin executes —
Ghostscript is never reached. Valid raster images decode exactly as
before.
- Behavior change: images in formats outside the allowlist (e.g. WebP)
that PIL previously decoded are now rejected, including via
`read_images(..., file_extensions=[...])`.

```mermaid
flowchart TD
    subgraph Before
        subgraph BIO[Subsystem: IO]
            subgraph BC[datasource: image / webdataset / lerobot]
                B1[untrusted bytes] --> B2[PIL.Image.open no allowlist]
                B2 --> B3[plugin chosen by signature]
                B3 --> B4[EPS plugin -> Ghostscript]
            end
        end
    end
    subgraph After
        subgraph AIO[Subsystem: IO]
            AU["image_utils._ALLOWED_PIL_FORMATS"]
            subgraph AC[datasource: image / webdataset / lerobot]
                A1[untrusted bytes] --> A2["PIL.Image.open(formats=allowlist)"]
                A2 --> A3{format in allowlist?}
                A3 -- yes --> A4[decode raster image]
                A3 -- no --> A5[reject: UnidentifiedImageError / ValueError]
            end
            AU --> A2
        end
    end
    classDef removed fill:#ffcdd2,stroke:#c62828,color:#000
    classDef added fill:#c8e6c9,stroke:#2e7d32,color:#000
    class B4 removed
    class AU,A2,A3,A5 added
```

## Subsystems

IO (datasources: `image_utils.py` (new), `image_datasource.py`,
`webdataset_datasource.py`, `lerobot_datasource.py`). Behavioral change
is the format gating; no change to block/format handling of valid
images.

## Testing

- `pytest python/ray/data/tests/datasource/test_webdataset.py -k
"disallowed_image_format or allows_png"` → **2 passed**.
- Added
`test_image.py::TestReadImages::test_disallowed_format_not_decoded` (EPS
named `.jpg` → `ValueError`). The read path was verified in-process
(`ImageDatasource._read_stream`: EPS → `ValueError`, PNG → 1 block); the
`.materialize()`-based run should be exercised in CI.
- Added
`test_lerobot.py::test_decode_image_frames_rejects_disallowed_image_format`
(EPS image cell → `UnidentifiedImageError`). `test_lerobot.py` is
skipped locally because `lerobot[dataset]` requires Python ≥3.12, so
`_decode_image_frames` was also exercised directly: PNG, JPEG and 16-bit
TIFF decode to HWC uint8; EPS raises `UnidentifiedImageError`. Without
`formats=`, PIL opens the same EPS bytes as `EPS`, so the test fails if
the allowlist is removed.

---

AI assistance (Claude Code) was used to author this change. Not a
duplicate: searched open PRs on `ray-project/ray` for the GHSA ID and
for "EPS Ghostscript image allowlist" and found none.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Goutam <[email protected]>
Signed-off-by: Ayush Kumar <[email protected]>
Co-authored-by: Claude Opus 4.8 <[email protected]>
Co-authored-by: Ayush Kumar <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data Ray Data-related issues go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants