Skip to content

Fix Studio custom folders on Linux external drives - #6799

Merged
Imagineer99 merged 15 commits into
unslothai:mainfrom
ramisworld:fix-custom-folder-external-drives
Jul 3, 2026
Merged

Imagineer99 merged 15 commits into
unslothai:mainfrom
ramisworld:fix-custom-folder-external-drives

Conversation

@ramisworld

Copy link
Copy Markdown
Contributor

Summary

Fixes #6798.

Unsloth Studio rejected Custom Folder paths on Linux external drives mounted under:

/run/media/<user>/<volume>/...

The reported issue shows a model folder under /run/media/dspofu/... failing with:

Path under /run is not allowed

The root cause was not model scanning itself. The path was blocked before scanning because scan-folder validation treated every path under /run as unsafe, including valid removable-media mount points created by Linux desktop environments.

This PR allows valid Linux external-drive model folders under /run/media/<user>/<volume>/... while keeping the rest of /run blocked.

Findings

I traced the Custom Folder flow end-to-end:

  • frontend Custom Folder dialog
  • shared folder browser
  • browse-folder API
  • add/scan-folder API
  • scan-folder storage validation
  • local inventory scanning

The important difference is:

  • pasting /run/media/<user>/<volume>/models/... failed during scan-folder registration because the broad /run denylist rejected it
  • browsing to the same folder also needed the /run/media/$USER/<volume> mount root included in the folder-browser allowlist, otherwise the picker could not reliably reach it

So this fix covers both backend paths involved in Custom Folder usage:

  • direct path registration
  • Browse picker navigation/selection

What changed

  • Added a small Linux external-media path helper for /run/media/<user>/<volume>/....
  • Allowed scan-folder registration for valid /run/media/<user>/<volume>/... paths.
  • Kept unsafe /run paths blocked, including:
    • /run
    • /run/media
    • /run/media/<user>
    • /run/user/...
    • /run/systemd/...
    • unrelated /run/*
  • Added readable current-user /run/media/$USER/* volume roots to the Custom Folder browser allowlist.
  • Applied the same behavior to both Hub and legacy /api/models paths because the shared Studio folder browser still uses the legacy browse endpoint.
  • Added focused regression tests for allowed and denied /run cases.

Safety

This does not broadly allow /run.

The exception is intentionally narrow: only Linux removable-media mount paths shaped like:

/run/media/<user>/<volume>/...

Existing protections remain in place:

  • filesystem root rejection
  • readability/traversability checks
  • sensitive path component denylist, such as .ssh, .aws, .config
  • system path denylist for unrelated system locations
  • browse realpath/symlink containment
  • browse enumeration caps

This keeps the original security intent of blocking system runtime paths while allowing the specific external-drive layout shown in the bug report.

Tests

PYTHONPATH=studio/backend uv run --no-project --with pytest --with structlog --with starlette --with fastapi --with pydantic --with typing-extensions python -m pytest studio/backend/tests/test_linux_external_media_paths.py -q
# 24 passed

PYTHONPATH=studio/backend uv run --no-project --with pytest --with fastapi --with pydantic --with typing-extensions python -m pytest studio/backend/hub/tests -q
# 122 passed

PYTHONPATH=studio/backend uv run --no-project --with pytest --with structlog --with starlette --with fastapi --with pydantic --with typing-extensions python -m pytest studio/backend/tests/test_chat_history_storage.py -q
# 20 passed

git diff --check
# passed

Manual Linux UI verification was not performed locally because this was developed on macOS. The focused backend tests cover the exact reported /run/media/<user>/<volume>/... policy, plus both paste-path registration and browse allowlist resolution.

@ramisworld
ramisworld requested a review from danielhanchen as a code owner July 2, 2026 04:00

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for Linux external media mount paths (specifically under /run/media/<user>/<volume>) in the folder browser and scan folder utilities. It adds helper functions to detect and list these paths, updates the folder browser allowlist and validation logic to permit them, and includes comprehensive unit tests. The feedback suggests specifying encoding="utf-8" when reading files in the test suite to ensure cross-platform compatibility.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@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: e353f7fee8

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/backend/storage/studio_db.py Resolved

@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: c113a3513e

ℹ️ 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 thread studio/backend/routes/models.py Resolved
@danielhanchen

Copy link
Copy Markdown
Member

Thanks for the fix. The /run/media/<user>/<volume> registration change looks good and keeps the rest of /run blocked, so I pushed one commit on top (9e5d675) to close the last gap the bots flagged.

The legacy /api/models browse endpoint was getting the new /run/media roots in its allowlist, but not the credential/config guard that scan-folder registration and the Hub browser already enforce. So with show_hidden=true it could still list and descend into .ssh, .aws, .config, etc. under an allowlisted root. Low severity since registration still blocks those and it is a local single-user app, but it was an inconsistency worth closing.

The commit:

  • Adds a public contains_sensitive_path_component helper in storage/studio_db.py.
  • Skips sensitive names during enumeration in browse_folders and rejects them in _resolve_browse_target, matching hub/services/models/folder_browser.py.
  • Extends the legacy browse regression test to assert the resolver returns 403 for a sensitive dir under a media mount.

Tests on the branch: test_linux_external_media_paths.py 25 passed, hub/tests 122 passed.

One request: could you rebase onto the latest main when you get a chance? The branch is a little behind. Thanks again for the contribution.

@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: b0c4f9b400

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

ramisworld and others added 8 commits July 3, 2026 19:57
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
…i#6799

The legacy /api/models browse endpoint gained the new /run/media mount
roots in its allowlist but not the credential/config guard that scan-folder
registration and the Hub browser already enforce. Filter sensitive names
during enumeration and reject them in _resolve_browse_target so .ssh, .aws,
.config, etc. under allowlisted roots stay unbrowseable, matching the Hub
browser. Add a public contains_sensitive_path_component helper and cover the
legacy resolver with a regression test.
@ramisworld
ramisworld force-pushed the fix-custom-folder-external-drives branch from 19633c6 to e63838a Compare July 3, 2026 07:58
Both _resolve_browse_target functions only checked contains_sensitive_path_component
while walking descendant parts, so requesting an allowlisted root itself (empty
relative path) returned it unchecked. A pre-existing scan-folder row under ~/.ssh,
~/.aws, ~/.config, etc. (registerable before the denylist was added) is re-added to
the allowlist on upgrade and could then be browsed. Check the resolved target once
before returning in both the legacy and Hub browsers, and cover the root case in
both test suites.
@danielhanchen

Copy link
Copy Markdown
Member

Thanks for the rebase and the Skip sensitive Linux media roots commit, and for pulling the denylist into a shared utils/paths/sensitive.py. That closes the sensitive-named /run/media volume case and the earlier duplication nicely.

I pushed one more commit (13e8270) to close the related root-level hole that lives in the resolver rather than the media helper. Both _resolve_browse_target functions only checked contains_sensitive_path_component while walking descendant parts, so requesting an allowlisted root itself has an empty relative path and returned unchecked. The realistic trigger is not a media volume but a persisted scan-folder row: ~/.ssh, ~/.aws, ~/.config could be registered through the legacy /api/models/scan-folders before the denylist existed, and _build_browse_allowlist() re-adds that row after upgrade, so browsing the exact path listed it even though descending into it returns 403.

The fix checks the resolved target once before returning, in both the legacy routes/models.py and Hub folder_browser.py browsers, so any sensitive allowlist root is refused regardless of how it got there. Added a root-level case to both test suites.

Tests: test_linux_external_media_paths.py 27 passed, hub/tests 123 passed, test_chat_history_storage.py 20 passed.

Remaining nits, both optional and non-blocking: the legacy studio_db.add_scan_folder still lacks the filesystem-root guard the Hub path has (can register /), and is_linux_run_media_path accepts any user's /run/media/<user>/... for paste-registration while the browser lists only the current user. Neither is exploitable given the readability checks, so I would leave them unless you want parity.

@ramisworld

Copy link
Copy Markdown
Contributor Author

Thanks Daniel that makes sense. I agree with your suggestion on leaving the parity nits alone. Appreciate you tightening the root browse case and running the extra tests.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@Imagineer99

Copy link
Copy Markdown
Member

Added a targeted repro run here: https://github.com/Imagineer99/unsloth/actions/runs/28674767655

It covers the original Linux external-drive case and confirms the existing /run and sensitive-directory guardrails still hold.

Focused tests passed:

  • test_linux_external_media_paths.py: 27 passed
  • hub/tests/test_model_services.py: 97 passed

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@Imagineer99

Copy link
Copy Markdown
Member
image

Targeted Linux media check passed. The staging PR still has an unrelated broad Core CI failure in unsloth_zoo tests, but the PR-specific repro and focused folder tests are green. Merging.

@Imagineer99
Imagineer99 merged commit 01f7e14 into unslothai:main Jul 3, 2026
1 check passed
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.

[Bug] "Custom folders" in Linux

3 participants