Repository navigation
[Data] Restrict PIL image decoding to a safe format allowlist - #67752
goutamvenkat-anyscale merged 3 commits into
Conversation
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]>
There was a problem hiding this comment.
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.
ayushk7102
left a comment
There was a problem hiding this comment.
Thanks for the fix.
Two concerns:
-
We seem to have an insecure
Image.openinlerobot_datsource.pywhich should also probably have an allowlist -
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"}
)
Signed-off-by: Ayush Kumar <[email protected]>
Signed-off-by: Ayush Kumar <[email protected]>
Summary
read_images, theread_webdatasetdefault decoder, and the LeRobot image-camera decoder passed untrusted bytes straight toPIL.Image.openwith no format restriction. PIL picks a plugin by file signature, not extension, so a payload nameda.jpgthat 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/.ptdecoder 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")wherea.jpgholds EPS bytes → PIL selects the EPS plugin → attempts to launch Ghostscript.ray.data.read_webdataset(tar)with a member0001.jpgholding 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.After
_ALLOWED_PIL_FORMATS = ["PNG", "JPEG", "TIFF", "BMP", "GIF"], in_internal/datasource/image_utils.py(aligned withImageDatasource._FILE_EXTENSIONS).read_images: uses_ALLOWED_PIL_FORMATS._ALLOWED_PIL_FORMATS. This covers what LeRobot writes (PNG camera frames, TIFF depth maps) and JPEG frames common in Hub datasets._ALLOWED_WEBDATASET_PIL_FORMATS = _ALLOWED_PIL_FORMATS + ["PPM"](PPM covers theppm/pgm/pbm/pnmextensions it handles).UnidentifiedImageError/ a clearValueError(read_images) before any PIL plugin executes — Ghostscript is never reached. Valid raster images decode exactly as before.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 addedSubsystems
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.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.test_lerobot.py::test_decode_image_frames_rejects_disallowed_image_format(EPS image cell →UnidentifiedImageError).test_lerobot.pyis skipped locally becauselerobot[dataset]requires Python ≥3.12, so_decode_image_frameswas also exercised directly: PNG, JPEG and 16-bit TIFF decode to HWC uint8; EPS raisesUnidentifiedImageError. Withoutformats=, PIL opens the same EPS bytes asEPS, 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/rayfor the GHSA ID and for "EPS Ghostscript image allowlist" and found none.🤖 Generated with Claude Code