Repository navigation
Studio: pin managed dataset media URLs to the Hub and scope extracted archive members - #14840
Conversation
…ve members to their source A managed account's media cell was authorized as a Hub file whenever the datasets string_to_dict lookup matched, but that lookup searches anywhere in the string, so a URL on another host that contains a Hub path passed and was opened as given. Match the whole URL against the hf:// or configured endpoint pattern instead, and allow only archive protocols as chained hops. Files under the shared datasets extraction cache were readable by any managed account. Each extraction now records its source archive (in process and in a sidecar beside the extraction, both checked against the hash datasets names the directory with), and a member is readable only when its source archive is.
|
@codex review |
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. |
|
@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: cf85d19532
ℹ️ 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".
| source = _extracted_sources.get(top) | ||
| if source is None: | ||
| try: | ||
| with open(top + _SOURCE_SUFFIX, encoding = "utf-8") as f: | ||
| source = f.read() |
There was a problem hiding this comment.
Cache sidecar provenance after the first read
When an extraction is consumed by a later process, _extracted_sources is empty, so every decoded media cell reopens and rereads the same .unsloth-source file and recomputes its expected extraction path. Large image/audio datasets can therefore add a filesystem open per sample throughout preview or training; after validating the sidecar once, store the source in _extracted_sources[top] as _record_extraction already does for same-process extractions.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in b335791: once a record read from disk matches the extraction directory's hash, it is cached in _extracted_sources, so later cells in the same process do not reopen it. Only records that pass the check are cached. Test test_managed_extraction_record_is_read_once_per_process (3 decodes, 1 open).
| with open(top + _SOURCE_SUFFIX, "x", encoding = "utf-8") as f: | ||
| f.write(source) | ||
| except OSError: # already recorded, or a read-only cache: this process still knows it |
There was a problem hiding this comment.
Publish provenance sidecars atomically
If the process exits, the disk fills, or another process reads between creating this file and completing write, an empty or truncated sidecar remains. Every later extraction catches the resulting FileExistsError and leaves that record untouched, so later processes permanently refuse otherwise valid media from this cache entry; write to a temporary file and atomically replace the sidecar (or validate and repair an existing record) instead.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in b335791: the record is written to a temp file beside the extraction directory, set to 0644 and published with os.replace, so readers never see a partial record and a truncated one left by an interrupted writer is replaced by the next extraction. Readers still check it against the directory hash. Test test_managed_extraction_record_is_published_whole.
| if source is None: | ||
| try: | ||
| with open(top + _SOURCE_SUFFIX, encoding = "utf-8") as f: | ||
| source = f.read() |
There was a problem hiding this comment.
Bound provenance sidecar reads
In a later process, this unbounded read() trusts the size of a file in the shared, archive-populated extraction cache before validating its contents. A managed account can craft a tar member named ../<its extraction hash>.unsloth-source; the supported datasets extractor accepts that sibling because its traversal check uses a string-prefix comparison, allowing the account to plant an arbitrarily large sidecar and make a subsequent cached media decode exhaust the backend's memory. Read only a small path-sized maximum and refuse anything larger.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in b335791: the record is read in binary with a 16 KiB + 1 cap; anything larger or not valid UTF-8 is untrusted and the member is refused before the hash check. Test test_managed_oversized_extraction_record_is_refused_unread (a 16 MiB sidecar: about 100 MB peak before, under 1 MiB now).
…ords Read at most 16 KiB of an .unsloth-source record and refuse a larger one, remember a validated record so later reads in the process skip the file, and publish records through a temp file and os.replace so an interrupted write cannot leave a truncated record that refuses valid media. [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! 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". |
[skip ci]
# Conflicts: # studio/backend/utils/security/dataset_media.py [skip ci]
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 28a7db5a2c
ℹ️ 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".
| with open(top + _SOURCE_SUFFIX, "rb") as f: | ||
| raw = f.read(_SOURCE_MAX_BYTES + 1) |
There was a problem hiding this comment.
Refuse non-regular provenance sidecars before reading
A managed account can leave this sidecar as a FIFO using a tar member such as ../<extraction-hash>.unsloth-source if extraction subsequently errors or is interrupted before _record_extraction replaces it. Opening the FIFO here and calling the newly bounded read() still blocks indefinitely because the byte limit does not make reads from special files nonblocking, allowing a later media decode to hang a backend worker; open the record with nonblocking/no-follow semantics and verify it is a regular file before reading.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in e075a0e. The record is now opened with O_NONBLOCK and O_NOFOLLOW (where the platform has them) and read only when fstat reports a regular file, so a planted FIFO or link is refused straight away and the member is treated as having no record. New test test_managed_extraction_record_planted_as_fifo_is_refused_without_blocking replaces the record with a FIFO; on the previous head the read blocks, now it returns at once and the managed decode is refused. tests/test_dataset_media_decode_guard.py: 29 passed.
…king or following links [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! 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". |
…14963) `studio/backend/utils/security/dataset_media.py` merged in #14840 (09:00 UTC) without the repo formatter, so pre-commit.ci's `ruff-format-with-kwargs` hook (which runs on all files) rewrites it on every PR queued after that. Same class as #14817, #14852, #14878, #14885, #14908 and #14930. `python scripts/run_ruff_format.py` (the hook's entry point, `ruff==0.6.9`) applied to that one file. The AST is identical before and after, `ruff check` passes, and a full run of the hook over every tracked Python file on this branch changes nothing else.
The managed-account media guard in
studio/backend/utils/security/dataset_media.pyhad two gaps.datasets'string_to_dictmatched it. That helper searches anywhere in the string, so a URL on a different host that contains a Hub-style path was accepted and then opened as written. The guard now matches the whole URL against thehf://datasets/...or configured endpoint pattern, checks the repo it derives is the one the decoder keys its token on, and, as on main since Studio: refuse a chained dataset media path whose first hop is not an archive #14529, only allows archive, compression and cache protocols as chained hops.datasetsextraction cache was readable by every managed account, regardless of whose archive it came from. Extractions now record their source archive (in process and in a small file beside the extraction, both verified against the hashdatasetsnames the directory with), and a member is readable only when the account may read the source archive. Extractions with no record are refused for managed accounts.The owner's decoding is unchanged. Tests cover both cases plus the legitimate paths (own archive, Hub archive member with the caller's token), on datasets 3.6.0, 4.8.5 and 5.1.0.
Archives extracted before this change have no record, so managed accounts are refused for those members until the archive is extracted again (owner unaffected). Not run: a full Studio training run and Windows paths.