Repository navigation
Studio: fetch VLM dataset row image URLs through the SSRF-guarded fetch - #14701
Conversation
Row image URLs are dataset content, not user input. Conversion opened them with fsspec.open(..., expand=True), which follows redirects to any host, including loopback, the LAN and cloud metadata addresses, with no size cap. The image-column probe sent an unguarded urllib HEAD to the same values. Both now go through core.inference.tools._fetch_url_raw: public addresses only, pinned to the resolved IP, every redirect re-checked, and conversion downloads are capped at 64 MiB per image. Public image URLs, embedded images, local paths and repo-relative lookups behave as before; a refused URL counts as a failed image like any other download error. Adapted from LeoBorcherding/unsloth-security-staging#85 and #66 (the probe's local-path restriction is left out so local image datasets keep their detection).
[skip ci]
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
…and the URL failure-rate abort [skip ci]
…er call site [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ad0a72f6b3
ℹ️ 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".
| ): | ||
| if "::" in path: | ||
| raise OSError(f"Refusing a remote archive image path: {path}") | ||
| value = {"path": None, "bytes": fetch_untrusted_image_bytes(path)} |
There was a problem hiding this comment.
Defer typed URL decoding until the per-row handler
When an Image()-typed HTTP dataset has a valid first row and any later URL is refused or broken, this eager fetch runs while tqdm(dataset) advances the iterator, before the try around _convert_single_sample in format_conversion.py. The resulting OSError therefore aborts the entire conversion instead of counting and skipping the failed row as string-backed URL datasets do, even when the failure rate is below the permitted 30%.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Pre-existing and unchanged here: on main, datasets also fetches Image() URL cells while the iterator advances, so a broken typed URL already aborts conversion. This PR only routes that same fetch through the SSRF guard.
| ): | ||
| if "::" in path: | ||
| raise OSError(f"Refusing a remote archive image path: {path}") | ||
| value = {"path": None, "bytes": fetch_untrusted_image_bytes(path)} |
There was a problem hiding this comment.
Preserve parallel conversion for typed URL columns
For an Image()-typed public URL column, this wrapper turns each cell into a PIL image before convert_to_vlm_format inspects it. Consequently first_image is no longer a string, has_urls becomes false, and the converter takes its serial iteration branch instead of the URL thread pool and 200-row probe; it also downloads the first image again during conversion. Large typed remote-image datasets therefore become substantially slower than equivalent string-backed datasets.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Pre-existing and unchanged here: Image()-typed columns already reach the converter as decoded PIL images on main, so they took the serial path before this PR too. Only the fetch underneath is now guarded.
…ares fsspec opens each hop of a chained path, so an unsupported scheme or a non-Hub URL anywhere in the chain is refused, not only in the last hop. datasets own archive and compression wrappers (zip, tar, gzip, ...) still chain onto local and Hub paths. UNC paths and file: URLs naming a remote host are refused so a dataset row cannot make Windows open an SMB share.
…ny open [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…at startup (#14888) ## Summary #14701 routes dataset image URLs through the SSRF-guarded fetch by patching `datasets.Image.decode_example`. It applies that patch with a module-level `install_guarded_image_decode()` call in `utils/datasets/remote_images.py`, which runs `from datasets import config`. That module is on the startup import path: ``` routes/__init__.py -> routes/datasets.py -> hub/services/datasets/formatting.py -> hub/utils/dataset_format.py -> utils/datasets/__init__.py -> format_conversion.py -> remote_images.py ``` So every Unsloth Studio and Desktop start loads `datasets`, `pandas` and `pyarrow` (and `torch` in CI). On main this fails `test_startup_defers_torch.py` (4 tests), and the Startup profile job fails its budget: 8.123 s median to a healthy port against 8.0 s. ## Change The guard itself is unchanged. Only when it is installed changes: - If `datasets.features.image` is already imported, it is patched immediately, as before. - Otherwise a meta-path finder wraps that module's loader and patches it as soon as it finishes loading, before any caller can decode a row. This is the same pattern `utils/security/hub_data_files.py` uses. `install_guarded_image_decode()` keeps its name and idempotence. It now takes the module as an optional argument, so it can patch from inside the loader. The finder is inserted once. ## Testing - New `test_guard_applies_when_datasets_loads_after_this_module` runs in a subprocess. It imports `remote_images` first and asserts `datasets` was not loaded, then imports `datasets.features.image` and asserts `decode_example` is guarded. - On main's code: this test and the 4 startup tests fail (5 failed). - With this change: all pass. - Dataset, VLM, image, format and startup test files in `studio/backend/tests`: 2598 passed, 34 skipped. - `import main` wall time, 5 runs each on the same machine: main 9.07 (cold), 6.36, 6.07, 6.26, 6.34 s; this branch 6.12, 6.12, 5.72, 5.70, 5.54 s. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
…sion harness (#14903) ## Summary `TestFormatConversionNoTorchVenv` (4 tests, Python 3.12 and 3.13) fails on main, so Backend CI is red on every PR: ``` File "<string>", line 10, in <module> ImportError: attempted relative import with no known parent package ``` The harness execs `format_conversion.py` as a standalone script and rewrites its relative imports (`.cells`, `.iterable`, `.format_detection`) to top-level ones. #14701 added `from .remote_images import fetch_untrusted_image_bytes, is_network_share` on line 10, which the harness does not rewrite. ## Change Rewrite `from .remote_images import` the same way as `.cells` in the four exec snippets. `remote_images.py` imports only the standard library at module level, so it loads in the no-torch venv. Test-only change. ## Testing - `tests/python/test_studio_import_no_torch.py` on main: 4 failed. With this change: 29 passed. - With #14888's `remote_images.py` (guard installed on datasets import) in place: 29 passed.
…ths, unencoded spaces) (unslothai#14966) ## Summary unslothai#14701 routes dataset image URLs through the SSRF guard, so an untrusted dataset cannot make Studio fetch loopback, LAN or metadata addresses. A recheck of the user flows found that it also blocked things owners do on purpose, with no way back: - images served from the owner's own LAN or loopback server (a NAS, MinIO, a local file server) - images on a Windows share the owner uses - chained cells into a public non-Hub archive, such as `zip://img.png::https://images.example.org/images.zip` - `\\wsl$\...` and `\\wsl.localhost\...` paths (the user's own WSL distro), and `//`-prefixed paths on Linux and macOS, which are ordinary local paths there - image URLs with a raw space (`https://host/my images/a b.png`), which the web tool's URL check rejects but fsspec used to quote ## Changes `utils/datasets/remote_images.py`: - `UNSLOTH_ALLOW_PRIVATE_URL_FETCH=1`, the opt-in `unsloth_zoo` already honours for media URLs, turns the image guard off on a single-user install: image cells and string URLs load the way `datasets` and fsspec loaded them before unslothai#14701. Once managed accounts exist it is ignored, so a row a managed account supplies cannot reach the owner's LAN; an unreadable account store keeps the guard on. It is read on every call, so it takes effect without a restart. - a refusal of a private or local destination, a share or a non-Hub archive names that variable; a 404, timeout or size refusal is reported as is - `is_network_share` only applies on Windows, and treats `wsl$` and `wsl.localhost` as local - `guard_url` percent-encodes a raw space (and other characters a browser would) in the path, query and fragment before the guard; the host is left alone so an internationalized name still goes through IDNA, and an already-encoded URL is unchanged `format_conversion.py` and `format_detection.py` use the same opt-in, share rule and quoting for string rows and the column probe. With the opt-in on, a URL column is still probed (a plain HEAD request), so a broken column loses to a working one. The probe and the string-row fetch share one small `urllib` helper that quotes the URL and IDNA-encodes an internationalized host, and the row fetch keeps the guarded fetch's limits: the 64 MB image cap (a declared size over it is refused, and the read stops at the cap when no size is sent), the 30 s timeout and the 60 s overall deadline. Typed `Image()` cells are decoded by `datasets` itself, as they are without Studio. The default for everyone else is unchanged: public addresses only, every redirect re-checked, size capped. ## Tests `tests/test_vlm_conversion_row_urls.py`: - WSL and `\\?\C:\` paths reach `datasets` on Windows, and `//data/...` reaches it off Windows - refusals name the opt-in - with the opt-in on a single-user install, a loopback image server works for string rows and typed `Image()` cells, and a non-Hub archive chain reaches `datasets` - with managed accounts the opt-in is ignored and a loopback image is refused - only a private-destination refusal suggests the opt-in - with the opt-in, a 404 column loses to a working column - an internationalized host is not percent-encoded - a raw space in an image URL reaches the guard percent-encoded - with the opt-in, an image over the size cap is refused, with and without a declared size - opted-in requests are quoted, IDNA-encoded and carry the image timeout - the existing share-refusal tests pin Windows mode, so they still check the Windows rule on Linux CI The new cases fail on `main` and pass here. The VLM conversion and probe suites give 55 passed (52 on the windows-latest, ubuntu-latest and macos-15 runners before the size-cap commit), and the 14 test files that import the changed modules give 1275 passed. The one failure there, `test_csv_cells_as_written.py::test_csv_files_missing_a_column_still_load_together`, also fails on `main`.
Summary
VLM dataset conversion opened row image URLs with
fsspec.open(url, "rb", expand=True). That call follows redirects to any host, including loopback, the LAN and cloud metadata addresses, and has no size cap. The image-column probe sent an unguardedurllibHEAD request to the same row values. Row values are dataset content, so a crafted dataset could make Studio issue requests to internal addresses once per row.Adapted from LeoBorcherding/unsloth-security-staging#85 and #66.
Changes
convert_to_vlm_format,convert_sharegpt_with_images_to_vlm_format) now fetch throughcore.inference.tools._fetch_url_raw, the web tool's guarded fetch:utils/datasets/remote_images.pywrapsdatasets.Image.decode_exampleonce, at import (idempotent). A typedImage()cell holding only a URL is otherwise fetched bydatasetsitself, wherever a row is read: detection, previews, conversion, streaming and nestedSequence(Image()). For a bytes-less cell:http(s)URL is fetched through the same guard and handed todatasetsas bytes, so EXIF handling is unchangedHF_ENDPOINT) keepdatasets' own token-aware fetch::chained path is checked: a scheme outside local, Hub, cloud object stores anddatasets' archive/compression wrappers (ftp,sftp,smb, ...) is refused, as is a non-Hub URL inside a chainfile:URLs naming a remote host are refused, so a row cannot make Windows open an SMB shareos.path.existsor the conversion'sImage.open.Public image URLs (including HF resolve-to-CDN redirects), embedded images,
data:URLs, local paths,_own_image_pathand repo-relative lookups behave as before. The probe's local-path check is unchanged. #66's restriction to the datasets folder is left out, so that local image datasets keep their column detection.Behaviour change
Image URLs on localhost or private-network hosts, images over 64 MiB, image URLs with schemes such as
ftp://orsmb://, non-Hub URLs inside a::chain, and network share paths are now refused.Tests
tests/test_vlm_conversion_row_urls.py: loopback is refused in both converters, for string rows and forImage()-typed rows (also nested and streamed); public URLs go through the guarded fetch; embedded and Hub images keepdatasets' own decode; lookalike Hub hosts, unsupported schemes, chained hops and network shares are refused; local and Hub archive paths still reachdatasets.tests/test_vlm_probe_untrusted_rows.py: private URLs are never dialled, public URLs use a guarded HEAD, network share paths are never probed.tests/test_vlm_text_only_rows.py: switched from a loopback server to a stubbed fetch.Every new case fails on the head before its fix and passes here. The dataset and VLM suites give 2232 passed, 63 skipped.
test_search_images.pyneeds the optionalddgspackage and fails the same way onmain.