Skip to content

Studio: managed-account paths are checked as they are opened - #14428

Merged
danielhanchen merged 9 commits into
mainfrom
studio-account-path-confinement
Oct 21, 2026
Merged

danielhanchen merged 9 commits into
mainfrom
studio-account-path-confinement

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Hardening from a security review of multi-account confinement. The owner, Desktop and notebooks load, train and export any real model as before; managed accounts lose only path spellings that leave their own roots.

Problem. Several places decided "is this path inside the managed account's roots?" on one spelling of the path and then opened another:

  1. account_path checked Path(value).resolve() but returned the original string, which callers reopen later. A name through /proc/<pid>/cwd/... (a process the account controls can chdir) or through a link outside the roots passed the check and could point elsewhere at open time.
  2. In reference mode a bare datasets builder name (pandas, json, text, ...) passed as a Hub id, and load_dataset("pandas") then reads files from the server's working directory (pandas unpickles them).
  3. The model gates checked a path containing \ as written, while the loader normalises \ to /, so <own workspace>/\..\<other account>\model passed the gate and loaded the other account's model.
  4. Weight indexes (model.safetensors.index.json, pytorch_model.bin.index.json, config.json transformers_weights) and sentence-transformers modules.json folders were followed even when absolute or .., so a folder in the account's workspace could load another account's weights.
  5. The adapter base that was authorized was not always the one loaded: export authorized one base but the decision config could name another, and training's preflight returned early for a folder that has both full weights and adapter_config.json.

Change.

  • account_path also requires the path as typed (lexically absolute) to sit under the account roots; a workspace reached through an owner-made symlinked studio home is still accepted. Bare builder names fall through to the path check in reference mode (real Hub ids pass; validation never imports datasets).
  • account_path, model_visible, require_model_allowed, private_directory: both the \ and / spellings must pass.
  • utils/security/file_security.py: escaping_weight_references flags absolute or .. shard names, checked right before load in the export, inference and training workers. A text check, so Hugging Face cache symlinks keep working. FastSentenceTransformer refuses an absolute or .. module folder in modules.json the same way.
  • Export authorizes every base the adapter folder names (adapter_load_bases in core/export/decision.py) and load_checkpoint re-checks the pinned base right before from_pretrained. Training authorizes and pins a managed account's adapter base before the early return; the owner's order is unchanged.

The weight-index and module-folder checks apply to every account, because the worker processes do not know the account; a genuine checkpoint never has absolute or .. shard names. All checks are pure; repeating them is a no-op.

Tests. New studio/backend/tests/test_account_path_confinement.py (17 of 23 fail on main) and tests/test_st_modules_json_folder_containment.py (3 of 4 fail on main); all pass here. The tests passing on both sides are the guards: linked studio home, owner paths, real Hub ids, an unchanged adapter, a stock module layout. 226 backend modules that use the touched code: 14291 passed on both sides with the same failing IDs (pre-existing native-audio and cache-inventory cases); Core sentence-transformers tests: 0 failures.

- account_path also requires the name as given (not only its resolved
  target) to sit under the account roots, so a path through an outside
  link or a procfs alias is refused. Paths through the owner's linked
  studio home still pass.
- Reference mode no longer treats packaged datasets builder names
  (pandas, json, csv, ...) as Hub ids for managed accounts.
- account_path, model_visible, require_model_allowed and
  private_directory judge the forward-slash spelling of a path too.
- Weight index entries and config.json transformers_weights that are
  absolute or contain '..' are refused right before chat, export and
  training loads; sentence-transformers module folders likewise.
- Export authorizes every base an adapter folder names (including the
  decision config's) and re-checks the pinned base right before load.
  Training authorizes and pins the adapter base of a managed folder that
  also carries full weights.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 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-21T04:29:20.629648Z 7e6b82b Manual request
🔒 Security Review ✅ Completed 2026-10-21T02:28:41.126719Z 487f0b4 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 danielhanchen self-assigned this Oct 21, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: 487f0b4c0b

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@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: 487f0b4c0b

ℹ️ 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 +738 to +739
if pinned_base and adapter_base_changed(checkpoint_path, pinned_base):
return "The adapter's base model changed after it was checked. Load it again."

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 Preserve an explicit no-base pin

When a managed account's adapter initially has no base_model_name_or_path, _authorized_adapter_base returns None, so this truthiness check performs no comparison. If the account adds an unauthorized local base after route authorization but before the worker reaches this check, line 866 accepts and loads that newly named base without another account-access check. Representing “checked and no base was named” distinctly from “not pinned” is necessary to close this TOCTOU path.

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.

Below the bar: this needs the account to add a base to its adapter_config.json inside the window between route authorization and the worker's load, a timing race on its own request; a base named at start is pinned and rechecked.

Comment on lines +1902 to +1907
folder = module_config.get("path")
if isinstance(folder, str) and (
os.path.isabs(folder)
or folder.startswith(("/", "\\"))
or ".." in folder.replace("\\", "/").split("/")
):

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 Resolve module-folder symlinks before declaring them contained

A clean relative value such as 0_Transformer passes this syntactic test even when that directory is a symlink to a location outside the selected model folder. For a managed account loading a local SentenceTransformer, the delegated loader then follows the symlink and reads another account's or the operator's files despite the new containment gate. Resolve the module folder against the local model root and verify that the target remains underneath it, while accounting for normal Hub-cache file symlinks.

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.

Not reachable for a managed account: its tools cannot create symlinks in their writable roots (Landlock withholds MAKE_SYM, the macOS profile denies it) and uploads write regular files, so a module folder in its workspace cannot be a link to elsewhere.

Comment on lines 112 to +114
resolved = path.resolve()
# Callers reopen the name itself, so every link it walks through must sit inside the roots too.
lexical = Path(os.path.abspath(path))

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 Reject paths whose intermediate symlinks leave the account roots

Checking only the lexical path and its final resolved target does not establish the comment's invariant that every traversed link stays inside the roots. For example, an in-workspace link can point to an external directory whose next link points back into the workspace; both checks pass, but that external link can be retargeted after validation so the caller later opens a foreign path. Inspect each resolved prefix or use a confined descriptor-based open rather than relying only on the two endpoints.

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.

Same reason: a managed account cannot create links inside its roots, so a chain through an in-workspace link cannot be built; the lexical check covers the /proc//cwd and outside-link names it can produce.

…apter pin on the load target; check weights before every training backend

[skip ci]
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…apter base at export, allow module folders that stay inside

[skip ci]
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@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: 1a843f3245

ℹ️ 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 +827 to +828
for folder in (config["model_name"], lora_base):
if folder and escaping_weight_references(folder):

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 Apply weight confinement to diffusion loads

For a managed account loading a local Diffusers pipeline through /images/load, this check never runs because load_diffusion_model_gated launches engine.begin_load rather than entering _handle_load. Those pipelines load Transformers indexes from component directories such as text_encoder/ and transformer/, so an index there can name ../../victim/model.safetensors and read outside the authorized pipeline folder. Validate the actual diffusion component load roots immediately before the media engine begins loading.

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.

Out of scope here. This PR covers the chat, training and export loaders; the image pipeline loader is a separate path, and reading through it is limited to files that parse as safetensors. Tracking it separately rather than widening this change.

… decision adapter's base at training start

[skip ci]
@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: f0e8507c56

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

raise _training_start_error(400, "training_decision_adapter_base", str(exc))
base = _authorize_adapter_base(str(folder), hf_token)
# The worker re-reads the pin at the model root, so pin only a root adapter.
pin = None if request.model_subfolder else base or ""

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 Retain the pin for subfolder decision adapters

When a managed account selects a local decision adapter through model_subfolder, this discards the authorized base pin. The worker's confinement pass scans only model_name, model_local_path, model_snapshot_path, and adapter_base_pin, so it examines neither the adapter subfolder nor its now-unpinned local base; FastDecisionModel.from_pretrained(root, subfolder=...) subsequently loads both. Thus a base whose weight index escapes its own folder, or an adapter config changed after preflight, bypasses the worker check. Carry the pin and make the recheck resolve the actual subfolder load target.

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.

Leaving this. The subfolder adapter's base is still authorized at start; what remains needs a managed account to change its own adapter after the check or to point it at a base in its own workspace whose index escapes, through the less common subfolder selection. Not worth widening the worker recheck for.

Comment on lines +1903 to +1905
folder = module_config.get("path")
# modules/../1_Pooling stays inside: only a folder that normalizes above the model escapes.
normalized = (

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 Apply module containment to the custom assembly path

This containment guard is called only by the delegated SentenceTransformer(...) branches. When training takes the custom _load_modules path—for example with UNSLOTH_COMPILE_DISABLE=1, or for a model outside the fast-encoder set—the code later passes the unchecked module_config["path"] to _create_transformer_module or os.path.join(model_name, module_path). A local checkpoint with ../victim or an absolute module folder therefore still loads outside the model on that supported path. Reuse this validation before _load_modules opens any declared module folder.

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.

Not needed for the Studio boundary: the training, inference and export workers run escaping_weight_references before any loader, and it refuses a modules.json folder that escapes (../victim or absolute) whichever Sentence Transformers path loads it. In a notebook the user loads their own folder.

…oted shard names inside module folders

[skip ci]
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 21, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 7e6b82b495

ℹ️ 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 e08466e into main Oct 21, 2026
@danielhanchen
danielhanchen deleted the studio-account-path-confinement branch October 21, 2026 08:18
JadeCong pushed a commit to CloudEngineHub/unsloth that referenced this pull request Oct 23, 2026
…es the model (unslothai#14527)

Follow-up to unslothai#14428.

**Problem.** `escaping_weight_references` reads a Sentence Transformers
folder's `modules.json` and flags module folders outside the model.
Before the escape check it skipped paths that `strip("./\\")` empties,
meant for the model root (`""`, `.`, `./`). But `..`, `../..`, `/` and
`\` are also only dots and slashes, so they were skipped too, and a
managed account's model folder could name a module folder above it (its
account root, or the Studio root) without the load, export or training
gate noticing.

**Change.** The escape check now runs first; the self-folder skip comes
after it. A module at the model root is still accepted, so ordinary
Sentence Transformers folders load as before. The check runs for every
account's local model folder (as the existing weight-index check does
since unslothai#14428), but no real model names a module folder like `..` or `/`,
so nothing changes for real models. Notebook loading in `unsloth` is
unchanged.

**Testing.**
- `test_account_path_confinement.py`: 38 passed. The 6 new escape cases
(`..`, `../..`, `/`, `\`, `./..`, `..\..`) fail on main; the root cases
(`""`, `.`, `./`) pass on both.
- Neighbouring suites (`test_embedding_model_security_gate.py`,
`test_export_decision_backend.py`, `test_decision_training_route.py`,
`test_export_absolute_paths.py`): 134 passed. One
`test_export_absolute_paths.py` case fails the same way on main in this
environment (`core.inference.diffusion_lora` not importable).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant