Skip to content

[Data] Restrict PIL image decoding to a safe format allowlist - #67752

Merged
goutamvenkat-anyscale merged 3 commits into
ray-project:masterfrom
goutamvenkat-anyscale:data/image-format-allowlist-eps
Nov 3, 2026
Merged

goutamvenkat-anyscale merged 3 commits into
ray-project:masterfrom
goutamvenkat-anyscale:data/image-format-allowlist-eps

Conversation

@goutamvenkat-anyscale

@goutamvenkat-anyscale goutamvenkat-anyscale commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

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=[...]).
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
Loading

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

read_images and the read_webdataset default decoder passed untrusted bytes
to PIL.Image.open with no format restriction. PIL selects a plugin by file
signature rather than extension, so a payload named .jpg carrying EPS bytes
was routed to the EPS plugin, which shells out to Ghostscript (potential RCE
on hosts with a vulnerable Ghostscript). This is the image sibling of the
.pkl/.pt decoder hardening in GHSA-hhrp-gw25-jr43.

Restrict both decoders to a raster-format allowlist via Image.open(formats=...)
so disallowed formats (EPS, etc.) are rejected before any plugin runs.

Addresses GHSA-pwm8-8ghq-qw5p.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Signed-off-by: Goutam <[email protected]>
@goutamvenkat-anyscale
goutamvenkat-anyscale requested a review from a team as a code owner November 3, 2026 18:44

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request enhances security by restricting the PIL image formats allowed during decoding in both ImageDatasource and WebDataset. By introducing explicit allowlists (_ALLOWED_PIL_FORMATS and _ALLOWED_PIL_IMAGE_FORMATS), it prevents untrusted payloads (such as EPS files masquerading as JPEGs) from triggering dangerous parsers like Ghostscript. Corresponding unit tests have been added to verify that disallowed formats are correctly rejected and valid formats are successfully decoded. No review comments were provided, so there is no feedback to address.

@goutamvenkat-anyscale goutamvenkat-anyscale added data Ray Data-related issues go add ONLY when ready to merge, run all tests labels Nov 3, 2026

@ayushk7102 ayushk7102 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the fix.

Two concerns:

  1. We seem to have an insecure Image.open in lerobot_datsource.py which should also probably have an allowlist

  2. Can we structure this so that the safe PIL formats which are common across multiple datasources are in the same shared util? Any datasource that wants to add specific extra safe formats can probably extend this list in its own *_datasource.py

Shared util file: python/ray/data/_internal/datasource/image_utils.py

  # Every PIL plugin vetted as safe on untrusted bytes. Must never
  # include EPS (Ghostscript) or other plugins that shell out or run
  # interpreters.
  _SAFE_PIL_FORMATS = frozenset(
      {"PNG", "JPEG", "TIFF", "BMP", "GIF", "PPM", "WEBP"}
  )

@goutamvenkat-anyscale
goutamvenkat-anyscale enabled auto-merge (squash) November 3, 2026 22:46
@goutamvenkat-anyscale
goutamvenkat-anyscale merged commit 8685bc3 into ray-project:master Nov 3, 2026
7 checks passed
@goutamvenkat-anyscale
goutamvenkat-anyscale deleted the data/image-format-allowlist-eps branch November 3, 2026 23:27
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