Skip to content

Studio: pin managed dataset media URLs to the Hub and scope extracted archive members - #14840

Merged
danielhanchen merged 6 commits into
mainfrom
dh/s3-dataset-media-guard
Oct 23, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
dh/s3-dataset-media-guard

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

The managed-account media guard in studio/backend/utils/security/dataset_media.py had two gaps.

  1. A media cell was treated as a Hub file when datasets' string_to_dict matched 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 the hf://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.
  2. Any file under the shared datasets extraction 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 hash datasets names 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.

…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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T07:59:01.464190Z e075a0e Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 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".

Comment on lines +99 to +103
source = _extracted_sources.get(top)
if source is None:
try:
with open(top + _SOURCE_SUFFIX, encoding = "utf-8") as f:
source = f.read()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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).

Comment on lines +141 to +143
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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).

@danielhanchen danielhanchen added conflicts# important# security# Moves a security boundary: sandbox, allowlist, permission or network guard labels Oct 23, 2026
…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]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: b335791fd1

ℹ️ 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".

# Conflicts:
#	studio/backend/utils/security/dataset_media.py

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 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".

Comment on lines +90 to +91
with open(top + _SOURCE_SUFFIX, "rb") as f:
raw = f.read(_SOURCE_MAX_BYTES + 1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: e075a0ee3b

ℹ️ 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".

@danielhanchen
danielhanchen merged commit f443cf9 into main Oct 23, 2026
@danielhanchen
danielhanchen deleted the dh/s3-dataset-media-guard branch October 23, 2026 09:00
danielhanchen added a commit that referenced this pull request Oct 23, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

conflicts# important# security# Moves a security boundary: sandbox, allowlist, permission or network guard

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant