Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: refuse tool access to the auth directory and mask keys in tool output (#10895)

Daniel Han56d19467ebce
diff --git a/studio/backend/core/inference/tool_loop_controller.py b/studio/backend/core/inference/tool_loop_controller.py
index 6d197321e38..5796a50e8c1 100644
--- a/studio/backend/core/inference/tool_loop_controller.py
+++ b/studio/backend/core/inference/tool_loop_controller.py
@@ -290,10 +290,13 @@ class ToolCallCompletion:
executed: bool = False
def tool_end_payload(self) -> dict[str, Any]:
+ # Not only in model_message(): the frontend PERSISTS this payload and serializes it back
+ # into a role="tool" message on the next turn, so an unmasked key is replayed then.
+ result = self.result
return {
"tool_name": self.decision.tool_name,
"tool_call_id": self.decision.card_id,
- "result": self.result,
+ "result": redact_studio_credentials(result) if isinstance(result, str) else result,
"provenance": self.decision.provenance,
}
@@ -945,9 +948,42 @@ _SOURCE_MAP_TOOLS = frozenset({"search_knowledge_base", "search_conversation"})
_WORKSPACE_TOOLS = _SANDBOX_TOOLS | {"edit_file"}
-def strip_result_for_model(result: str, tool_name: "str | None" = None) -> str:
- """Remove frontend-only sentinels (image paths, RAG source map) before
- feeding the result back to the model."""
+# `sk-unsloth-` + 32 hex (auth/storage.py), cached in the clear so the CLI can reuse it. Masked on
+# the way to the model, which is where it would leave the machine. The mask carries neither prefix,
+# so re-running is a no-op.
+_STUDIO_API_KEY_RE = re.compile(
+ # 8, not 32: a result cut to fit the window ends mid-key, and half a key is still one. Bare
+ # `sk-unsloth-` (prose about the format) still reads through.
+ # Hex, not alphanumeric: the token is `token_hex`, and the wider alphabet rewrote this repo's
+ # own `sk-unsloth-internal-workflow` to `[redacted]-workflow`.
+ r"sk-unsloth-[0-9a-fA-F]{8,}"
+ # `desktop-` + token_urlsafe(48); the floor keeps "desktop-app" out of it.
+ r"|desktop-[A-Za-z0-9_-]{40,}"
+)
+_STUDIO_SECRET_MASK = "[redacted]"
+
+
+def redact_studio_credentials(text: str) -> str:
+ """Mask any Unsloth Studio credential in text bound for the model/provider."""
+ # Two substring scans first: the alternation has no literal to anchor on and costs ~20x per MB.
+ if "sk-unsloth-" not in text and "desktop-" not in text:
+ return text
+ return _STUDIO_API_KEY_RE.sub(_STUDIO_SECRET_MASK, text)
+
+
+def strip_result_for_model(
+ result: str,
+ tool_name: "str | None" = None,
+ *,
+ redact: bool = True,
+) -> str:
+ """Remove frontend-only sentinels (image paths, RAG source map) and mask Studio credentials
+ before feeding the result back to the model.
+
+ ``redact = False`` is for the one caller that needs the strip to stay suffix-only
+ (`tools._split_frontend_suffix` re-derives the removed envelope from `startswith`); masking
+ rewrites bytes inside the body, which that comparison cannot survive. That path feeds the model
+ through `model_message` afterwards, so the mask is applied either way."""
if tool_name is None or tool_name == "web_search":
from .search_images import strip_images_suffix
result = strip_images_suffix(result)
@@ -958,7 +994,7 @@ def strip_result_for_model(result: str, tool_name: "str | None" = None) -> str:
result = _strip_images_sentinel(result)
if tool_name is None or tool_name in _SOURCE_MAP_TOOLS:
result = _strip_rag_sources_sentinel(result)
- return result
+ return redact_studio_credentials(result) if redact else result
def deferred_nudge_text(msgs: Sequence[dict]) -> str:
diff --git a/studio/backend/core/inference/tool_stream_exec.py b/studio/backend/core/inference/tool_stream_exec.py
index abc6053b64e..f7adfc47fa0 100644
--- a/studio/backend/core/inference/tool_stream_exec.py
+++ b/studio/backend/core/inference/tool_stream_exec.py
@@ -30,6 +30,46 @@ from typing import Any, Callable, Generator
from loggers import get_logger
+# Split across two chunks, neither half matches the key pattern and the credential is painted whole.
+_SECRET_PREFIXES = ("sk-unsloth-", "desktop-")
+# A prefix plus a generous token: bounds the reverse search on a multi-megabyte chunk.
+_SECRET_SCAN_TAIL = 128
+# What ends a credential token in tool output: whitespace, quoting and closing punctuation.
+_SECRET_TERMINATORS = frozenset(" \t\r\n\"'`,;)]}")
+
+
+def _hold_back_partial_secret(text: str) -> int:
+ """Index at which `text` may hold a credential token still open at its end.
+
+ Everything from the returned index is carried to the next chunk. Returns ``len(text)`` when the
+ tail is safe to emit whole. Conservative on purpose: holding back a few bytes that turn out to
+ be ordinary text only delays them until the next chunk or the final flush.
+ """
+ tail_start = max(0, len(text) - _SECRET_SCAN_TAIL)
+ tail = text[tail_start:]
+ best = len(text)
+ for prefix in _SECRET_PREFIXES:
+ index = tail.rfind(prefix)
+ while index != -1:
+ absolute = tail_start + index
+ rest = text[absolute + len(prefix) :]
+ # A terminator ANYWHERE after the token ends it, so the regex has had its chance and
+ # the text is safe to emit. Read only at the ends, `sk-unsloth-<key> done` looked open
+ # and held the chunk (and every chunk after it) back until the tool finished.
+ if not rest or not _SECRET_TERMINATORS.intersection(rest):
+ best = min(best, absolute)
+ # Keep going left: a terminated occurrence says nothing about an EARLIER one, and
+ # stopping at the first one found hid an open token behind it.
+ index = tail.rfind(prefix, 0, index)
+ # Also a tail that is a PREFIX of a prefix ("...sk-unslo"), which no rfind above can see.
+ for prefix in _SECRET_PREFIXES:
+ for length in range(min(len(prefix) - 1, len(text)), 0, -1):
+ if text.endswith(prefix[:length]):
+ best = min(best, len(text) - length)
+ break
+ return best
+
+
logger = get_logger(__name__)
@@ -220,6 +260,21 @@ def stream_tool_execution(
finished = True
return
+ # tool_end is masked downstream, but these chunks are what the frontend paints and persists
+ # first. A key can straddle a chunk boundary, so `_hold_back_partial_secret` returns the index
+ # where a still-open token begins and everything from there is carried into the next chunk.
+ carry = ""
+
+ def _masked(text: str, *, final: bool) -> "tuple[str, str]":
+ """Return (emit, carry) for `text`, with credentials masked and partials held back."""
+ from core.inference.tool_loop_controller import redact_studio_credentials
+
+ combined = carry + text
+ if final:
+ return redact_studio_credentials(combined), ""
+ split = _hold_back_partial_secret(combined)
+ return redact_studio_credentials(combined[:split]), combined[split:]
+
abnormal_exit = False
try:
while not finished:
@@ -265,11 +320,24 @@ def stream_tool_execution(
stream_capped = True
streamed_chars += len(chunk)
if chunk:
+ # The capped chunk is the last one sent, so flush the carry with it.
+ emit, carry = _masked(chunk, final = stream_capped)
+ if emit:
+ yield {
+ "type": "tool_output",
+ "tool_name": tool_name,
+ "tool_call_id": tool_call_id,
+ "text": emit,
+ }
+ # Deferred text that turns out to be ordinary: emit it, or the output is truncated.
+ if carry:
+ emit, carry = _masked("", final = True)
+ if emit:
yield {
"type": "tool_output",
"tool_name": tool_name,
"tool_call_id": tool_call_id,
- "text": chunk,
+ "text": emit,
}
except BaseException:
# The loop only raises when the consumer closes us early: an SSE disconnect calls gen.close() (GeneratorExit at
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index 60dd375ad6e..11907464cec 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -2651,6 +2651,2043 @@ _SENSITIVE_PATH_RE = re.compile(
r"|\w[\w.-]*\.(?:pem|key)(?:$|[\s'\"])",
re.IGNORECASE,
)
+# $STUDIO_HOME/auth holds this install's credentials in the clear, and tool subprocesses run as the
+# backend's own OS user, so their 0600 modes are no boundary against them. Narrow on purpose: these
+# basenames and the auth directory itself, so an app's own auth/ package or auth.py stays ordinary.
+# A name ends where the shell ends a word, so the punctuation that separates commands, redirects and
+# subshells closes it too: without them `cat /tmp/.bootstrap_password; echo done` named the file and
+# matched nothing.
+_WORD_END = r"(?:$|[\s'\";&|)(<>`])"
+_STUDIO_CREDENTIAL_BASENAME_RE = re.compile(
+ # Dotted names nothing else spells, so they match bare too.
+ r"(?:^|[/\\\s'\"=])(?:\.cli_api_key_[^/\\\s'\";&|)(<>`]*|\.bootstrap_password|\.desktop_secret)"
+ + _WORD_END
+ # Path form only: the bare name is an ordinary identifier. auth.db is absent for the same reason,
+ # and Studio's copy is covered by the auth-directory patterns below.
+ + r"|[/\\]llama_api_key"
+ + _WORD_END
+ # `unsloth start` keeps the coding-agent keys here. Path form only: matching the bare name
+ # refused `print('agent_api_key.json')`.
+ + r"|[/\\]agent_api_key\.json"
+ + _WORD_END,
+ re.IGNORECASE,
+)
+_STUDIO_AUTH_DIR_RE = re.compile(
+ r"(?:^|[/\\\s'\"=])\.unsloth[/\\]studio[/\\]auth(?:[/\\]|" + _WORD_END + r")",
+ re.IGNORECASE,
+)
+
+# Only counted with a `cd` INTO the studio root, never a mere mention of it: `grep -rn auth
+# <studio root>/logs/studio.log` is ordinary work.
+_BARE_AUTH_SEGMENT_RE = re.compile(
+ r"(?:^|[/\\\s'\"=])auth(?:[/\\]|" + _WORD_END + r")", re.IGNORECASE
+)
+
+# Prefilter: a new pattern needs a hint here or it never runs.
+# Whether this host distinguishes `auth` from `Auth`. `os.path.normcase` is identity on POSIX other
+# than macOS, which is the same test the path comparisons elsewhere in this file rely on.
+_CASE_SENSITIVE_PATHS = os.path.normcase("A") == "A" and sys.platform != "darwin"
+
+
+_STUDIO_CREDENTIAL_HINTS = (
+ "auth",
+ ".cli_api_key",
+ ".bootstrap_password",
+ ".desktop_secret",
+ "llama_api_key",
+ "agent_api_key",
+)
+
+# Both survive into the tool subprocess environment, so `$STUDIO_HOME/auth` IS the path to the
+# shell, not a rewrite of it.
+_STUDIO_HOME_ENV_VARS = ("UNSLOTH_STUDIO_HOME", "STUDIO_HOME")
+
+
+def _same_directory(left: str, right: str) -> bool:
+ """Whether two spellings name the same directory.
+
+ normcase because Windows paths are case-insensitive, and realpath because `studio_root()`
+ resolves aliases: a `STUDIO_HOME` that is a symlink or junction to the configured root is the
+ root, and reading it as somewhere else drops every spelling of the variable from the guard.
+ """
+
+ def tidy(path: str) -> str:
+ return os.path.normcase(os.path.normpath(path))
+
+ if tidy(left) == tidy(right):
+ return True
+ try:
+ return tidy(os.path.realpath(left)) == tidy(os.path.realpath(right))
+ except OSError:
+ return False
+
+
+def _studio_home_variable_spellings(resolved: "str | None" = None) -> "list[str]":
+ """`$VAR`, `${VAR}`, `%VAR%` and `$env:VAR` for each studio-home variable (sh, cmd, PowerShell).
+
+ A variable that is SET to some other directory is skipped. `STUDIO_HOME` is a generic name, and
+ another application can own it; with the spelling registered unconditionally, a command naming
+ that application's directory was refused in every permission mode even though it never came near
+ this install. A variable that is unset stays registered: the child cannot expand it either, so
+ the spelling reaches nothing, and dropping it would only widen the guard for no gain.
+ """
+ out: "list[str]" = []
+ for var in _STUDIO_HOME_ENV_VARS:
+ value = os.environ.get(var)
+ if value and resolved:
+ try:
+ points_here = _same_directory(os.path.expanduser(value), resolved)
+ except Exception: # noqa: BLE001 - an unreadable value must not break classification
+ points_here = True
+ if not points_here:
+ continue
+ out.extend((f"${var}", f"${{{var}}}", f"%{var}%", f"$env:{var}"))
+ return out
+
+
+_studio_auth_markers_cache: "tuple | None" = None
+
+
+def _studio_auth_dir_markers() -> tuple:
+ """``(plain spellings, variable spellings, "cd into the studio root" pattern)`` for this install.
+
+ Each spelling is a ``(marker, canonical marker)`` pair, both lowercased, so the per-call path
+ does no string building. The variable spellings (``$HOME/...``, ``$STUDIO_HOME/auth``, ``~/...``)
+ are kept apart because a text containing no ``$``, ``%`` or ``~`` cannot match one, and the
+ terminal classifier runs this once per candidate token of every command.
+
+ Resolved once per process: STUDIO_HOME is fixed at startup, and a custom UNSLOTH_STUDIO_HOME is
+ only covered by asking for the real root rather than assuming the default layout. A failure is
+ not cached, so a root that could not be resolved during startup import ordering does not leave
+ the guard half-blind for the process lifetime."""
+ global _studio_auth_markers_cache
+ if _studio_auth_markers_cache is not None:
+ return _studio_auth_markers_cache
+ try:
+ from utils.paths.storage_roots import auth_root
+ resolved = str(auth_root())
+ except Exception: # noqa: BLE001 - an unresolvable root leaves the literal patterns above
+ return (), (), None
+ if not resolved:
+ return (), (), None
+ auth_markers = [resolved]
+ variable_markers: "list[str]" = []
+ root_markers = [os.path.dirname(resolved.rstrip("/\\"))]
+ home = os.path.expanduser("~")
+ # Boundary-aware: a plain startswith reads /home/u2 as under /home/u and mints a "~2/..." marker.
+ if home and (resolved == home or resolved.startswith(home.rstrip(os.sep) + os.sep)):
+ for target, base in ((variable_markers, resolved), (root_markers, root_markers[0])):
+ tail = base[len(home.rstrip(os.sep)) :]
+ target.extend(("~" + tail, "$HOME" + tail, "${HOME}" + tail))
+ # Minus any variable that is set to somewhere else.
+ for spelling in _studio_home_variable_spellings(os.path.dirname(resolved.rstrip("/\\"))):
+ root_markers.append(spelling)
+ variable_markers.extend((spelling + "/auth", spelling + "\\auth"))
+ roots = [m for m in root_markers if m and m not in ("/", "\\")]
+ # The root must END where it matched, or continue into `auth`. Without the boundary
+ # `cd <home>-backup && ls auth` and `cd <home>/models && grep auth README` were both refused.
+ # Only where a shell would RUN one: `echo 'cd <root>'; grep auth README` prints the text and
+ # searches a project, and matched unanchored it read as a move into the studio root.
+ cd_re = (
+ re.compile(
+ r"(?:^|[;&|({\n]\s*|\b(?:then|do|else|if|elif|while|until)\s+)(?:(?:builtin|command|exec)\s+)*"
+ r"(?:cd|pushd)\s+(?:/d\s+)?[\"']?(?:"
+ + "|".join(re.escape(m) for m in roots)
+ + r")(?:[\"']|\s|[;&|]|$|[/\\]auth(?![\w-]))",
+ re.IGNORECASE,
+ )
+ if roots
+ else None
+ )
+
+ def pairs(names: "list[str]") -> tuple:
+ out = []
+ for name in names:
+ if not name:
+ continue
+ lowered = name.lower()
+ # The ORIGINAL spelling travels with the folded ones: on a case-sensitive filesystem
+ # `<home>/Auth` is a different directory from `<home>/auth`, and folding alone refused
+ # an ordinary read under it.
+ out.append((lowered, _canonical_path_text(lowered), name))
+ return tuple(out)
+
+ _studio_auth_markers_cache = (pairs(auth_markers), pairs(variable_markers), cd_re)
+ return _studio_auth_markers_cache
+
+
+# Names neither the path nor the value: this string goes back to the model.
+_STUDIO_CREDENTIAL_BLOCKED = (
+ "Blocked for safety: Unsloth Studio's authentication directory holds this install's own "
+ "credentials and is not readable by tools."
+)
+
+
+def _canonical_path_text(text: str) -> str:
+ """Rewrite *text* into the one spelling the OS would resolve it to, lexically.
+
+ Separators are unified to "/", then `//` and `/./` collapse and `x/..` pairs cancel. The OS
+ opens `<home>/bin/../auth/auth.db` and `C:\\Studio\\.\\auth\\auth.db` as the protected database,
+ so without this the guard matched only the tidiest spelling of a path and every equivalent one
+ walked past it.
+
+ Lexical on purpose: no `realpath`, so no filesystem access and nothing to race. That makes it
+ wrong for a path whose parent is a symlink, which is the accepted limit here -- this is an extra
+ candidate spelling, never a replacement, so a match found in the raw text still counts.
+ """
+ unified = text.replace("\\", "/")
+ out: "list[str]" = []
+ for index, segment in enumerate(unified.split("/")):
+ # `a//b` is `a/b` to every OS; the first two positions keep a leading "/" and a UNC
+ # "//server/share" intact. Mixed separators (`C:\Studio\home//auth//auth.db`) need it here,
+ # since the other candidate collapses slashes in the RAW text only.
+ if segment == "" and index > 1:
+ continue
+ if segment == ".":
+ continue
+ if segment == ".." and out and out[-1] not in ("", ".."):
+ out.pop()
+ continue
+ out.append(segment)
+ return "/".join(out)
+
+
+_GLOB_META_RE = re.compile(r"[*?\[]")
+_BRACKET_CLASS_RE = re.compile(r"\[[^\]/\s]{1,64}\]")
+# A class holding exactly one character expands to that character and nothing else, so
+# `[a][u][t][h]` IS `auth`. Collapsing it to a wildcard threw the literal away, and a segment of
+# nothing but wildcards is deliberately not matched.
+_SINGLETON_CLASS_RE = re.compile(r"\[([^\]/\s!^-])\]")
+
+
+# A backslash before an ordinary word character is quoting, and the shell drops it: `c\d` is `cd`.
+# Restricted to word characters so a Windows separator (`auth\auth.db`) is left alone, which the
+# spelling walk handles separately.
+_ESCAPED_WORD_CHAR_RE = re.compile(r"\\(\w)")
+# The spellings of the home directory a shell expands before the command sees them. The bare `~` only
+# counts at the head of a path, so `file~` and `a~b` are left alone.
+_HOME_VARIABLE_RE = re.compile(r"\$\{HOME\}|\$HOME\b|%HOME%|(?<![\w~.])~(?=[/\\])", re.IGNORECASE)
+# The shell's spellings of the working directory. Bypass sets PWD to the tool workdir, so
+# `$PWD/../..` is the sandbox walked two levels up, exactly as `$HOME/../..` is.
+_CWD_VARIABLE_RE = re.compile(r"\$\{PWD\}|\$PWD\b|%CD%|\$env:PWD\b", re.IGNORECASE)
+
+
+def _glob_can_name_the_marker(lowered: str, marker: str) -> bool:
+ """True when *lowered*, read as a shell glob, can expand to *marker* or something under it.
+
+ `sqlite3 ../../a?th/auth.db` never spells the auth directory, but bash expands `a?th` to `auth`
+ before sqlite3 opens anything, so comparing the literal text alone let the database through.
+ Matched segment by segment, because a shell wildcard does not cross a separator while
+ `fnmatch`'s does.
+
+ A segment whose literal characters are all metacharacters is skipped: `ls <studio root>/*` names
+ the auth directory only in the sense that listing a parent does, and refusing it would break
+ ordinary work for no secret read.
+ """
+ candidate = lowered.split("/")
+ wanted = marker.split("/")
+ if len(candidate) < len(wanted):
+ return False
+ aligned = candidate[len(wanted) - 1]
+ if not _GLOB_META_RE.sub("", aligned).strip("]-"):
+ return False
+ return all(
+ fnmatch.fnmatchcase(want, have) for want, have in zip(wanted, candidate[: len(wanted)])
+ )
+
+
+# The files inside the auth directory, by basename alone. A command that names the studio ROOT and
+# one of these is enumerating for it, however it joins them.
+_CREDENTIAL_BASENAME_RE = re.compile(
+ r"(?<![\w.-])(?:auth\.db|\.desktop_secret|\.bootstrap_password|\.cli_api_key[\w.-]*"
+ r"|llama_api_key[\w.-]*|agent_api_key[\w.-]*)(?![\w-])",
+ re.IGNORECASE,
+)
+
+
+# An assignment to one of the studio home variables, anywhere in the command.
+_STUDIO_HOME_ASSIGN_RE = re.compile(
+ r"(?:^|[;&|(\s])(?:export\s+|set\s+)?(" + "|".join(_STUDIO_HOME_ENV_VARS) + r")\s*=",
+ re.IGNORECASE,
+)
+
+
+def _assignment_is_a_command_prefix(text: str, value_start: int) -> bool:
+ """True when a command follows the assignment's value on the same simple command.
+
+ `H=/tmp cat x` is a prefix assignment; `H=/tmp; cat x` is an assignment that stands alone. The
+ value ends at the first unquoted separator, so the quotes are tracked while scanning it.
+ """
+ index = value_start
+ quote = ""
+ while index < len(text):
+ char = text[index]
+ if quote:
+ if char == quote:
+ quote = ""
+ elif char in "\"'":
+ quote = char
+ elif char in " \t;&|\n":
+ break
+ index += 1
+ following = text[index:].lstrip(" \t")
+ return bool(following) and following[0] not in ";&|\n"
+
+
+def _assignment_is_inert(text: str, index: int) -> bool:
+ """Whether an assignment at *index* binds nothing for the commands that follow it.
+
+ Inside quotes it is DATA that the command merely prints, and inside `( ... )` or
+ `$( ... )` it binds only the SUBSHELL, so the outer commands still expand the
+ inherited value."""
+ quote = ""
+ escaped = False
+ depth = 0
+ for character in text[:index]:
+ if escaped:
+ escaped = False
+ elif character == "\\" and quote != "'":
+ # A backslash escapes the next character everywhere a single quote is not open, so
+ # `echo "x \" H=/tmp;"` never leaves the quoted region that `\"` only prints.
+ escaped = True
+ elif quote:
+ if character == quote:
+ quote = ""
+ elif character in "'\"":
+ quote = character
+ elif character == "(":
+ depth += 1
+ elif character == ")":
+ depth = max(depth - 1, 0)
+ return bool(quote) or depth > 0
+
+
+def _rebinds_the_studio_home_first(text: str) -> bool:
+ """True when *text* assigns a studio home variable BEFORE any use of it.
+
+ The shell assignment is what the child expands, whether or not the backend sets the same name,
+ so `UNSLOTH_STUDIO_HOME=/tmp/project; cat "$UNSLOTH_STUDIO_HOME/auth/config.json"` reads a
+ project file and not this install's directory. An assignment that comes AFTER a use rebinds
+ nothing for that use, and expanding it would hide a real read of the install's own auth
+ directory, so the ordering is what decides it.
+ """
+ lowered = text.lower()
+ assignments: "dict[str, int]" = {}
+ for match in _STUDIO_HOME_ASSIGN_RE.finditer(text):
+ # `echo " H=/tmp;"; cat "$H/auth/auth.db"` only PRINTS the assignment and
+ # `(H=/tmp); cat "$H/auth/auth.db"` binds only the subshell, so in both the later
+ # expansion still uses the inherited home and rewriting it hid a real credential read.
+ if _assignment_is_inert(text, match.end()):
+ continue
+ # A PREFIX assignment (`H=/tmp cmd "$H/auth/auth.db"`) does not govern the expansion of its
+ # own command's arguments: the shell expands the word list before the assignment takes
+ # effect, so that read still happens under the INHERITED home. Only an assignment that
+ # stands as a command of its own, terminated by a separator, rebinds what follows.
+ if (
+ _assignment_is_a_command_prefix(text, match.end())
+ and (os.environ.get(match.group(1).upper()) or "").strip()
+ ):
+ # Only when the variable is SET: that inherited value is what the expansion uses. With
+ # it unset the expansion is empty, which can name no directory of this install's, so the
+ # assigned value is still the closest reading of what the command means.
+ continue
+ # The LAST assignment of a name is the one the expansion applies, so that is the position
+ # the first use has to come after: `H=$H; cat "$H/auth/auth.db"; H=/tmp` reads the real
+ # database and then rebinds, and rewriting every use to `/tmp` erased the marker.
+ assignments[match.group(1).lower()] = match.end()
+ # EVERY assigned name has to be safe, not just one of them: the expansion that follows rewrites
+ # all of them from their last assignment, so one name that is only assigned (`STUDIO_HOME=/tmp;`)
+ # used to authorize rewriting another whose assignment comes AFTER its use, and
+ # `STUDIO_HOME=/tmp; cat "$UNSLOTH_STUDIO_HOME/auth/auth.db"; UNSLOTH_STUDIO_HOME=/tmp` read the
+ # real database under a rewritten path.
+ rebinds = False
+ for name, position in assignments.items():
+ uses = [
+ found
+ for found in (
+ lowered.find(f"${name}"),
+ lowered.find(f"${{{name}}}"),
+ lowered.find(f"%{name}%"),
+ )
+ if found != -1
+ ]
+ if uses and min(uses) < position:
+ return False
+ rebinds = True
+ return rebinds
+
+
+_studio_root_spellings_cache: "tuple | None" = None
+
+
+def _studio_root_spellings() -> "list[str]":
+ """Every lowered spelling of the Studio root: the literal path and its environment variables.
+
+ The variables come from `_studio_home_variable_spellings`, which drops one that is SET to some
+ other directory: `STUDIO_HOME` is a generic name another application can own, and registering it
+ unconditionally refused `find "$STUDIO_HOME" ...` against that application's tree.
+ """
+ global _studio_root_spellings_cache
+ markers = _studio_auth_dir_markers()[0]
+ if _studio_root_spellings_cache is not None and _studio_root_spellings_cache[0] is markers:
+ return _studio_root_spellings_cache[1]
+ root = _studio_home_for_guard()
+ if not root:
+ return []
+ # Both separator styles of the literal, because how the root is STORED must not decide whether
+ # the text naming it matches: a Windows root reaches source as `c:\\dir` and as `c:/dir`.
+ folded = _folded_word(root)
+ spellings = [root.lower()]
+ if os.sep == "\\":
+ spellings += [folded, folded.replace("/", "\\")]
+ spellings.extend(spelling.lower() for spelling in _studio_home_variable_spellings(root))
+ spellings = list(dict.fromkeys(spellings))
+ # Rebuilt for every command otherwise, and this runs on every one of them.
+ _studio_root_spellings_cache = (markers, spellings)
+ return spellings
+
+
+def _text_names_the_studio_root(text: str) -> bool:
+ """True when *text* names the Studio root directory, literally or by one of its variables."""
+ lowered = text.lower()
+ spellings = _studio_root_spellings()
+ if any(spelling in lowered for spelling in spellings):
+ return True
+ # The escaped spellings name the same directory: a shell escapes a space as `\\ `, and a python
+ # literal doubles every separator of a Windows path. Both need a backslash to exist at all, and
+ # rewriting the text is worth paying for only then.
+ if "\\" not in lowered:
+ return False
+ for unescaped in (lowered.replace("\\ ", " "), lowered.replace("\\\\", "\\")):
+ if unescaped != lowered and any(spelling in unescaped for spelling in spellings):
+ return True
+ return False
+
+
+def _quoted_words(text: str) -> "list[str]":
+ """Split on whitespace, honouring quotes but NOT backslash escapes.
+
+ `shlex.split(posix = True)` eats the separators of a Windows path, so
+ `find "C:\\Users\\me\\Unsloth Studio" ...` came back as one mangled word and matched no root.
+ """
+ lexer = shlex.shlex(text, posix = True)
+ lexer.whitespace_split = True
+ lexer.escape = ""
+ try:
+ return list(lexer)
+ except ValueError: # unbalanced quotes: the raw words are the best available reading
+ return text.split()
+
+
+def _folded_word(word: str) -> str:
+ """A word reduced to the directory it names: `"<root>"/.` and `<root>//` are both `<root>`."""
+ # `cp -a /tmp/Studio\\ Home .` names a root WITH a space; the backslash is the shell's escape,
+ # not a separator, so it is removed before the separators are normalised.
+ folded = word.lower().replace("\\ ", " ").replace("\\", "/")
+ while folded.endswith(("/.", "/")):
+ folded = folded[:-2] if folded.endswith("/.") else folded[:-1]
+ return folded
+
+
+def _names_the_studio_root_itself(text: str) -> bool:
+ """True when one WORD of the command is the root itself, so the walk starts there.
+
+ Word by word, because the spelling appearing anywhere is not enough: the pattern in
+ `grep -rn 'cd <root>' src/` searches a project, and `<root>/projects/p/sandbox` is ordinary work
+ inside a project. Only a bare `<root>` (with any trailing separator) is the directory whose walk
+ reaches `auth/`.
+ """
+ # Folded the SAME way as the words, or a Windows root never matches: `_folded_word` turns
+ # `c:\\users\\...` into `c:/users/...`, while a merely right-stripped spelling keeps its
+ # backslashes.
+ spellings = [_folded_word(spelling) for spelling in _studio_root_spellings()]
+ if not spellings:
+ return False
+ words = _quoted_words(text)
+ if "\\ " in text:
+ # Escapes are disabled in the lexer so Windows paths survive, which splits
+ # `Studio\\ Home` in two. The escaped spelling is re-joined before folding.
+ words = words + [
+ word.replace("\x00", " ") for word in _quoted_words(text.replace("\\ ", "\x00"))
+ ]
+ return any(_folded_word(word) in spellings for word in words)
+
+
+# Commands that walk a whole tree and EMIT or COPY what is in it. A plain listing (`ls`, `tree`,
+# `find` with no action) is not one of these: it names files without reading them.
+# `7z a out.7z <dir>` recurses into the directory with no flag at all, so it belongs here rather
+# than with the ones that need `-r`.
+_STUDIO_WALK_COMMANDS = frozenset({"rsync", "tar", "cpio", "rg", "ag", "ack", "7z", "7za", "7zr"})
+_STUDIO_WALK_FLAG_COMMANDS = frozenset({"grep", "egrep", "fgrep", "cp", "scp", "zip"})
+# `find` reads nothing by itself; an ACTION (or a pipe into another command) is what does.
+_STUDIO_FIND_ACTIONS = frozenset({"-exec", "-execdir", "-ok", "-okdir", "-delete", "-fprint"})
+_STUDIO_WALK_SPLIT_RE = re.compile(r"[\s;&()<>]+")
+
+
+def _command_words(text: str) -> "list[str]":
+ """The word in COMMAND position of each pipeline segment, lowered and basenamed.
+
+ `echo tar "$STUDIO_HOME"` runs `echo`; `tar` is data it prints. Reading every non-option token
+ as an executable refused that. Assignments and the usual wrappers are stepped over so
+ `env -u X tar ...` still reports `tar`.
+ """
+ words: "list[str]" = []
+ for segment in _COMMAND_SEPARATOR_RE.split(text.lower()):
+ skip_next = False
+ for token in _quoted_words(segment):
+ if skip_next:
+ skip_next = False
+ continue
+ if token.startswith("-"):
+ # A wrapper option that takes a separate value swallows the token after it, which
+ # would otherwise read as the command (`env -u FOO tar ...`).
+ skip_next = token in _WRAPPER_VALUE_OPTIONS
+ continue
+ if "=" in token: # a NAME=value assignment preceding the command
+ continue
+ base = os.path.basename(token.strip("\"'"))
+ if base in _WALK_TRANSPARENT_WRAPPERS or _WRAPPER_DURATION_RE.match(base):
+ continue
+ words.append(base)
+ break
+ return words
+
+
+# Separators that start a new command, so the word after one is in command position again.
+_COMMAND_SEPARATOR_RE = re.compile(r"[;&|()\n]+|&&|\|\|")
+_STUDIO_WALK_NAME_HINTS = frozenset(_STUDIO_WALK_COMMANDS | _STUDIO_WALK_FLAG_COMMANDS | {"find"})
+_WRAPPER_VALUE_OPTIONS = frozenset(
+ {"-u", "--unset", "-n", "-c", "-i", "-p", "-C", "--chdir", "-k", "--kill-after", "-s"}
+)
+# `timeout 5 tar ...`: a bare duration is the wrapper's own operand.
+_WRAPPER_DURATION_RE = re.compile(r"^\d+(?:\.\d+)?[smhd]?$")
+# Wrappers that run the command that FOLLOWS them, so the walker can still be behind one.
+_WALK_TRANSPARENT_WRAPPERS = frozenset(
+ {
+ "env",
+ "nice",
+ "ionice",
+ "nohup",
+ "time",
+ "timeout",
+ "sudo",
+ "command",
+ "builtin",
+ "exec",
+ "stdbuf",
+ "xargs",
+ "busybox",
+ }
+)
+
+
+def _walks_a_tree_reading_it(text: str) -> bool:
+ """True when the command recursively reads or copies a whole directory tree."""
+ lowered = text.lower()
+ # Splitting into command positions costs a lexer pass, so skip it when no walker is named at
+ # all: that is the case for almost every command that mentions the studio root.
+ if not any(name in lowered for name in _STUDIO_WALK_NAME_HINTS):
+ return False
+ names = set(_command_words(text))
+ if not names:
+ return False
+ if names & _STUDIO_WALK_COMMANDS:
+ return True
+ tokens = [token for token in _STUDIO_WALK_SPLIT_RE.split(text.lower()) if token]
+ if "find" in names and (any(token in _STUDIO_FIND_ACTIONS for token in tokens) or "|" in text):
+ return True
+ if names & _STUDIO_WALK_FLAG_COMMANDS:
+ for token in tokens:
+ if token in ("--recursive", "--archive"):
+ return True
+ if token.startswith("-") and not token.startswith("--") and set(token[1:]) & {"r", "a"}:
+ return True
+ return False
+
+
+_ABSOLUTE_MARKER_RE = re.compile(r"^(?:[/\\]|[a-z]:[/\\])")
+
+
+def _marker_is_absolute(marker: str) -> bool:
+ return bool(_ABSOLUTE_MARKER_RE.match(marker))
+
+
+def _marker_is_a_path_segment(lowered: str, marker: str) -> bool:
+ """True when ``marker`` appears in ``lowered`` as a whole path, not merely as a prefix.
+
+ ``<home>/auth`` must match ``<home>/auth/auth.db`` and a bare ``<home>/auth``, but not
+ ``<home>/authors/notes.txt`` or ``<home>/auth-backup``.
+ """
+ start = lowered.find(marker)
+ while start != -1:
+ end = start + len(marker)
+ ends_here = end == len(lowered) or lowered[end] in "/\\'\" \t\r\n;:&|)"
+ # The LEADING boundary matters too for an absolute marker: `/mnt/backup<home>/auth` embeds
+ # the configured path inside another directory and is not it, and checking only the end
+ # refused an ordinary read under a backup copy.
+ begins_here = (
+ start == 0
+ or not _marker_is_absolute(marker)
+ or lowered[start - 1] in "'\" \t\r\n;&|(=,"
+ )
+ if ends_here and begins_here:
+ return True
+ start = lowered.find(marker, start + 1)
+ return False
+
+
+def _glob_text_can_name_the_auth_dir(lowered: str) -> bool:
+ """True when *lowered*, read as a shell glob, can expand onto the auth directory or below it."""
+ auth_markers, variable_markers, _cd_re = _studio_auth_dir_markers()
+ markers = auth_markers + (variable_markers if ("$" in lowered or "%" in lowered) else ())
+ if not markers:
+ return False
+ candidates = {lowered, _canonical_path_text(lowered)}
+ return any(
+ _glob_can_name_the_marker(candidate, canonical_marker)
+ for _marker, canonical_marker, _original in markers
+ for candidate in candidates
+ )
+
+
+def _references_studio_credential(text: str) -> bool:
+ """True if *text* names Studio's auth directory or one of the credential files in it."""
+ if not text:
+ return False
+ lowered = text.lower()
+ # Once per candidate token of every command, so skip the regexes when nothing can match.
+ if not any(hint in lowered for hint in _STUDIO_CREDENTIAL_HINTS):
+ # A WILDCARD can name the directory without spelling it: `../../a?th/.b*` expands to
+ # `auth/.bootstrap_password` and carries no hint at all, so the prefilter was skipping the
+ # glob analysis that exists for exactly this. Only the marker comparison can match here, the
+ # literal patterns needing a hint by construction, so that is all this runs.
+ if not _GLOB_META_RE.search(lowered):
+ return False
+ return _glob_text_can_name_the_auth_dir(lowered)
+ # `<home>//auth/auth.db` and `<home>/./auth/auth.db` open the same file.
+ normalized = _REDUNDANT_SLASH_RE.sub("", text)
+ lowered_normalized = normalized.lower()
+ # A shell escapes a space in a home name; read as a separator it splits the directory in half.
+ unescaped = text.replace("\\ ", " ") if "\\ " in text else text
+ # A shell concatenates adjacent fragments, so no marker can span the quote in
+ # `"$STUDIO_HOME"/auth/auth.db`.
+ if '"' in unescaped or "'" in unescaped:
+ unescaped = unescaped.replace('"', "").replace("'", "")
+ canonical = _canonical_path_text(text)
+ lowered_canonical = canonical.lower()
+ canonical_candidates = {canonical}
+ if unescaped is not text:
+ canonical_candidates.add(_canonical_path_text(unescaped))
+ if any(
+ pattern.search(candidate)
+ for pattern in (_STUDIO_CREDENTIAL_BASENAME_RE, _STUDIO_AUTH_DIR_RE)
+ for candidate in ({text, normalized} | canonical_candidates)
+ ):
+ return True
+ auth_markers, variable_markers, cd_into_root_re = _studio_auth_dir_markers()
+ # No `$`, `%` or `~` means no variable spelling.
+ if variable_markers and ("$" in lowered or "%" in lowered or "~" in lowered):
+ auth_markers = auth_markers + variable_markers
+ if not auth_markers:
+ return False
+ # Segment-bounded, or `<home>/authors/notes.txt` and `<home>/auth-backup/` are refused too. The
+ # markers are canonicalised alongside the text; on Windows the raw ones carry backslashes.
+ lowered_raw = {lowered, lowered_normalized}
+ lowered_canonicals = {lowered_canonical} | {c.lower() for c in canonical_candidates}
+ if unescaped is not text:
+ lowered_raw.add(unescaped.lower())
+ matched = [
+ original
+ for marker, canonical_marker, original in auth_markers
+ if any(_marker_is_a_path_segment(candidate, marker) for candidate in lowered_raw)
+ or any(
+ _marker_is_a_path_segment(candidate, canonical_marker)
+ for candidate in lowered_canonicals
+ )
+ ]
+ if matched:
+ # On a case-sensitive filesystem the folded match is not enough on its own: `<home>/Auth` is
+ # a different directory from `<home>/auth`, and refusing a read under it is a false refusal.
+ # The check is only applied where the ORIGINAL marker is an ordinary literal path, so a
+ # variable or `~` spelling (which the shell expands, and whose case the shell decides) keeps
+ # the folded answer.
+ raw_candidates = {text, normalized, unescaped, canonical}
+ if _CASE_SENSITIVE_PATHS and not any(
+ original in candidate
+ for original in matched
+ if original[:1] not in ("$", "~", "%")
+ for candidate in raw_candidates
+ ):
+ literal = [o for o in matched if o[:1] not in ("$", "~", "%")]
+ if literal and len(literal) == len(matched):
+ return False
+ return True
+ # Once more as a glob, gated on a wildcard being there at all. Per path-shaped TOKEN, not on
+ # the whole text: the segments of `sqlite3 <home>/a?th/auth.db` start at `sqlite3 <home>`, so
+ # comparing from segment zero could never line up with an absolute marker.
+ if _GLOB_META_RE.search(lowered):
+ glob_tokens = {t for c in lowered_canonicals for t in _PATH_TOKEN_RE.findall(c)}
+ glob_tokens.update(lowered_canonicals)
+ if any(
+ _glob_can_name_the_marker(token, canonical_marker)
+ for _marker, canonical_marker, _original in auth_markers
+ for token in glob_tokens
+ if _GLOB_META_RE.search(token)
+ ):
+ return True
+ return bool(
+ cd_into_root_re is not None
+ and _BARE_AUTH_SEGMENT_RE.search(text)
+ and any(cd_into_root_re.search(candidate) for candidate in (text, unescaped))
+ )
+
+
+# Any run of text that could be a path argument, used to read one token at a time.
+_PATH_TOKEN_RE = re.compile(r"[^\s'\"()\[\]{},;|&<>]+")
+# Only traversal is worth resolving: a relative path without `..` stays inside the sandbox.
+_TRAVERSAL_TOKEN_RE = re.compile(r"[^\s'\"()\[\]{},;|&<>]*\.\.[^\s'\"()\[\]{},;|&<>]*")
+# Used only after a `cd`, where `auth/auth.db` stops meaning "inside the sandbox".
+_RELATIVE_PATH_TOKEN_RE = re.compile(r"[^\s'\"()\[\]{},;|&<>]*[/\\][^\s'\"()\[\]{},;|&<>]*")
+# `cd DIR`, `cd /d DIR` or `pushd DIR` at a command position; `pushd` moves the cwd as `cd` does.
+# Case-insensitive because the shell is `cmd /c` on a Windows host without a trusted bash.
+_CD_TARGET_RE = re.compile(
+ # `builtin cd ..` and `command cd ..` run the same builtin with the same argument, so a walk
+ # that only knows the bare name resolves everything after them against the wrong directory.
+ # `!` and `time` are reserved words, not commands, so they can precede the builtin directly:
+ # `! cd ../..` and `time cd ../..` both move the shell exactly as the bare form does.
+ r"(?:^\s*|[;&|({\n]\s*|\b(?:then|do|else|if|elif|while|until)\s+)"
+ # Bash allows assignment words before a special builtin, so `X=1 cd ../..` moves like a bare `cd`.
+ r"(?:(?:builtin|command|exec|nohup)\s+|time\s+(?:-p\s+)?|!\s*|[A-Za-z_]\w*=[^\s;&|()]*\s+)*"
+ r"(?:cd|pushd)\s+"
+ r"(?:(?:-[LPe@]+|--|/d)\s+)*([^\s;&|)]+)",
+ re.IGNORECASE,
+)
+# `cd`/`pushd` with a target, or one of the moves that RETURNS: `cd -` goes back to the previous
+# directory and `popd` pops the stack `pushd` built. Matched in one pass so the order is kept.
+_DIRECTORY_MOVE_RE = re.compile(
+ r"(?:^\s*|[;&|({\n]\s*|\b(?:then|do|else|if|elif|while|until)\s+)"
+ r"(?:(?:builtin|command|exec|nohup)\s+|time\s+(?:-p\s+)?|!\s*|[A-Za-z_]\w*=[^\s;&|()]*\s+)*"
+ r"(?:(?P<back>cd\s+-(?![\w/\\-])|popd\b)"
+ # A bare `cd` moves to HOME, which both sandbox envs set to the tool workdir, so the commands
+ # after it open from the sandbox again: `cd ../..; cd; cat auth/config.json` reads the
+ # project's own file and was refused as a read of the studio root.
+ r"|(?P<home>cd(?:\s+(?:-[LPe@]+|--))*\s*(?=[;&|)\n]|$))"
+ r"|(?:cd|pushd)\s+(?:(?:-[LPe@]+|--|/d)\s+)*(?P<target>[^\s;&|)]+))",
+ re.IGNORECASE,
+)
+
+
+# `&&` immediately after a move: the next command runs only if that move succeeded.
+_CHAINED_ON_SUCCESS_RE = re.compile(r"\s*&&")
+
+
+# Distinct directories, not `cd` commands: padding with repeats must not spend the budget.
+_MAX_TRACKED_CWDS = 64
+# Hard ceiling on the directories reported, well past any real command.
+_MAX_WALKED_CWDS = 2048
+
+
+# A symlink to the cwd, which the kernel resolves before any `..` that follows. `$$` and `$BASHPID`
+# expand to the shell's own PID, so they name the same one a literal number does.
+_PROC_CWD_RE = re.compile(r"/proc/(?:self|thread-self|\d+|\$\$|\$\{?BASHPID\}?|\$\{?PPID\}?)/cwd")
+
+
+def _unquoted_parens(text: str):
+ """Yield `(index, char)` for each bracket that is shell syntax, skipping quoted ones.
+
+ `echo '('; cd ../..; echo ')'` writes two brackets no shell ever opens, and counted as syntax
+ they ended a subshell that was never entered, dropping the `cd` that came between them.
+ """
+ quote = ""
+ escaped = False
+ # Quotes suspended by a `$(` inside them. A command substitution stays ACTIVE within double
+ # quotes, so `echo "$(cd ../..; pwd)"` opens a real subshell: skipping both of its brackets made
+ # the inner `cd` look like it lasted through the rest of the command. Single quotes suppress
+ # substitution entirely, so only a double quote is ever suspended here.
+ suspended: "list[str]" = []
+ for index, char in enumerate(text):
+ if escaped:
+ escaped = False
+ continue
+ if char == "\\" and quote != "'":
+ escaped = True
+ continue
+ if quote:
+ if quote == '"' and char == "(" and index and text[index - 1] == "$":
+ suspended.append(quote)
+ quote = ""
+ yield index, char
+ continue
+ if char == quote:
+ quote = ""
+ continue
+ if char in "'\"":
+ quote = char
+ continue
+ if char in "()":
+ yield index, char
+ if char == ")" and suspended:
+ quote = suspended.pop()
+
+
+def _subshell_end(text: str, start: int) -> int:
+ """Where the subshell containing *start* closes, or the end of *text* when it is not in one.
+
+ `(cd ../..; ls models); cat auth/config.json` runs the `cat` in the unchanged directory: a
+ subshell's `cd` dies with the subshell, so its move must not reach past the closing bracket.
+ """
+ depth = 0
+ for index, char in _unquoted_parens(text):
+ if index >= start:
+ break
+ depth = depth + 1 if char == "(" else max(0, depth - 1)
+ if not depth:
+ return len(text)
+ for index, char in _unquoted_parens(text):
+ if index < start:
+ continue
+ depth = depth + 1 if char == "(" else depth - 1
+ if char == ")" and not depth:
+ return index
+ return len(text)
+
+
+# `name() {`, `name () {` and `function name {`, the three spellings a shell accepts.
+_SHELL_FUNCTION_RE = re.compile(
+ r"(?:^|[;&|(){}\n]\s*)(?:function\s+([A-Za-z_][\w.:-]*)\s*(?:\(\s*\))?|([A-Za-z_][\w.:-]*)\s*\(\s*\))\s*\{",
+ re.MULTILINE,
+)
+# Bounds the scan on a command that is nothing but nested definitions.
+_MAX_TRACKED_FUNCTIONS = 64
+
+
+def _unquoted_braces(text: str):
+ """Yield `(index, char)` for each `{` or `}` that is shell syntax, skipping quoted ones."""
+ quote = ""
+ escaped = False
+ for index, char in enumerate(text):
+ if escaped:
+ escaped = False
+ continue
+ if char == "\\" and quote != "'":
+ escaped = True
+ continue
+ if quote:
+ if char == quote:
+ quote = ""
+ continue
+ if char in "'\"":
+ quote = char
+ continue
+ if char in "{}":
+ yield index, char
+
+
+def _uncalled_function_spans(text: str) -> "list[tuple[int, int]]":
+ """Spans of function bodies *text* defines but never invokes.
+
+ `helper() { cd ../..; }; cat auth/config.json` never runs the `helper`, so the `cd` in its body
+ moves nothing and the `cat` reads the project's own auth file from the unchanged sandbox.
+ Refusing that is a false positive, and it is the same rule the python walk already applies to an
+ uncalled def.
+ """
+ bodies: "list[tuple[str, int, int, int]]" = []
+ for match in _SHELL_FUNCTION_RE.finditer(text):
+ group = 1 if match.group(1) else 2
+ name = match.group(group)
+ named_at = match.start(group)
+ start = text.index("{", match.end() - 1)
+ depth = 0
+ end = len(text)
+ for index, char in _unquoted_braces(text):
+ if index < start:
+ continue
+ depth = depth + 1 if char == "{" else depth - 1
+ if not depth:
+ end = index
+ break
+ bodies.append((name, named_at, start, end))
+ if len(bodies) >= _MAX_TRACKED_FUNCTIONS:
+ break
+ spans: "list[tuple[int, int]]" = []
+ for name, named_at, start, end in bodies:
+ # Called is the assumption: the body is only inert when the name appears NOWHERE outside its
+ # own definition and its own body, which is the one case that needs no guess about control
+ # flow. A recursive call inside the body does not run it either, so the body is skipped too.
+ called = any(
+ call.start() != named_at and not (start <= call.start() <= end)
+ for call in re.finditer(r"(?<![\w.:-])" + re.escape(name) + r"(?![\w.:-])", text)
+ )
+ if not called:
+ spans.append((start, end))
+ return spans
+
+
+def _token_spellings(token: str) -> "list[str]":
+ """The paths *token* can be, once the shell has had its say.
+
+ A backslash is a separator on Windows and an escape on POSIX, so `au\\th/auth.db` is both
+ `au/th/auth.db` and `auth/auth.db` and the guard has to try each.
+ """
+ if "\\" not in token:
+ return [token]
+ return list(dict.fromkeys([token.replace("\\", "/"), token.replace("\\", "")]))
+
+
+def _path_tokens_containing(text: str, wanted: str):
+ """Whole path tokens of *text* holding any character of *wanted* (or the substring `..`).
+
+ One linear pass, then a substring test per token. The equivalent patterns put the interesting
+ characters in the MIDDLE, so their leading `[^...]*` backtracks over every position of a long
+ token that does not contain one: a fifth of a second for a single 8 KB token, and a command
+ carries many.
+ """
+ for match in _PATH_TOKEN_RE.finditer(text):
+ token = match.group(0)
+ if ".." in token if wanted == ".." else any(ch in token for ch in wanted):
+ yield match
+
+
+def _cwds_after_cd(workdir: str, text: str) -> "list[tuple[int, int, str]]":
+ """Every working directory *text* walks into via `cd`, as `(offset, limit, directory)` in order.
+
+ The offset is where that `cd` ends, because a relative path written BEFORE it opens from the
+ old directory: `cat auth/auth.db; cd ../..` reads inside the sandbox and is ordinary work.
+
+ `cd ../..` from the session sandbox lands on the Studio root, and the `auth/auth.db` that
+ follows is then the protected database under a name that matches nothing on its own.
+ """
+ # A set of possible directories, not one: a `cd` into a path that does not exist FAILS, and a
+ # shell carries on from where it was, so `cd missing; cd ../..; cat auth/auth.db` reaches the
+ # studio root from the sandbox. Assuming every `cd` succeeds resolved the rest against a
+ # directory the command was never in.
+ # Each state carries the position it stays valid until: the end of the text for an outer `cd`,
+ # the closing bracket for one inside a subshell. `(cd ../..; cd auth; sqlite3 auth.db)` needs
+ # the first move to reach the second `cd`, and dropping a subshell move outright resolved that
+ # second one from the outer sandbox and missed the database.
+ states: "list[tuple[str, int]]" = [(workdir, len(text))]
+ walked: "list[tuple[int, int, str]]" = []
+ seen: "set[tuple[str, int]]" = set()
+ inert = _uncalled_function_spans(text) if "(" in text or "function" in text else []
+ # Where each move came FROM. `cd -` and `popd` go back there, and reading them as no move at all
+ # kept the studio root live: `cd ../..; cd -; cat auth/config.json` is back in the sandbox
+ # reading the project's own file, and it was refused in every mode.
+ history: "list[list[tuple[str, int]]]" = []
+ for match in _DIRECTORY_MOVE_RE.finditer(text):
+ returning = match.group("back")
+ if returning:
+ if any(start <= match.start() <= end for start, end in inert):
+ continue
+ states = history.pop() if history else [(workdir, len(text))]
+ # The directories walked so far stop being where a later path opens from, so every span
+ # still open ends HERE. Without this the earlier move still covered the rest of the
+ # command and the return changed nothing.
+ walked = [(offset, min(limit, match.start()), cwd) for offset, limit, cwd in walked]
+ # Those spans are closed, so the SAME directory walked into again afterwards is a new
+ # span rather than a repeat: `cd ../..; cd -; cd ../..` reaches the root twice, and the
+ # dedup that stops padding from spending the budget hid the second one.
+ seen.clear()
+ # Where the return LANDS is where the commands after it open from, so those directories
+ # get spans of their own: `cd ../..; cd sandbox; cd -; cat auth/auth.db` is back at the
+ # studio root reading the real database, and restoring the state without re-opening its
+ # span left nothing covering the `cat`.
+ for cwd, until in states:
+ if cwd == workdir or until <= match.end() or (cwd, until) in seen:
+ continue
+ seen.add((cwd, until))
+ walked.append((match.end(), until, cwd))
+ continue
+ if match.group("home"):
+ # A bare `cd` moves to HOME, which both sandbox envs set to the tool workdir. Unlike a
+ # `cd <path>` it cannot land anywhere else, so it RETURNS the way `cd -` does and the
+ # spans still open end here: `cd ../..; cd; cat auth/config.json` reads the project's
+ # own file. Only outside a subshell, where the move lasts to the end of the command; a
+ # bare `cd` inside `( ... )` leaves the outer directory alone.
+ if any(start <= match.start() <= end for start, end in inert):
+ continue
+ if _subshell_end(text, match.end()) != len(text):
+ continue
+ history.append(states)
+ states = [(workdir, len(text))]
+ walked = [(offset, min(limit, match.start()), cwd) for offset, limit, cwd in walked]
+ seen.clear()
+ continue
+ target = (match.group("target") or "").strip("'\"")
+ if not target or target.startswith("-"):
+ continue
+ # A `cd` in a function body that nothing invokes never runs.
+ if any(start <= match.start() <= end for start, end in inert):
+ continue
+ # A subshell that has already closed takes its moves with it.
+ states = [(cwd, until) for cwd, until in states if until >= match.start()] or [
+ (workdir, len(text))
+ ]
+ # A `cd` inside `( ... )` or `$( ... )` moves only that subshell, so its move stops there.
+ limit = _subshell_end(text, match.end())
+ moved: "list[str]" = []
+ for cwd, _until in states:
+ nxt = target if os.path.isabs(target) else os.path.normpath(os.path.join(cwd, target))
+ if nxt not in moved:
+ moved.append(nxt)
+ # Deduplicated: `cd .` x8 spent the budget before the real ones.
+ if (nxt, limit) not in seen:
+ seen.add((nxt, limit))
+ walked.append((match.end(), limit, nxt))
+ # Both outcomes stay live, capped so a long chain cannot grow the set without bound. The
+ # starting directory is kept FIRST: it is where the shell is when every `cd` fails, which is
+ # what a padded command relies on, and truncating the tail used to drop exactly that state.
+ # The cap bounds the STATES only, never the scan, or the padding hides the `cd ../..`.
+ ordered = (
+ [(workdir, len(text))]
+ + [(c, u) for c, u in states if c != workdir]
+ + [(m, limit) for m in moved]
+ )
+ # `cd x && cd ../..` only reaches the second move when the first SUCCEEDED, so the
+ # unmoved state is not live afterwards. Keeping it modelled a branch no shell can take, and
+ # `cd subdir && cd ../.. && cat auth/config.json` was refused as a read of the studio root
+ # when it reads the project's own file.
+ if _CHAINED_ON_SUCCESS_RE.match(text, match.end()):
+ ordered = [(m, limit) for m in moved]
+ history.append(states)
+ states = list(dict.fromkeys(ordered))[:_MAX_TRACKED_CWDS]
+ if len(walked) >= _MAX_WALKED_CWDS:
+ break
+ return walked
+
+
+def _references_studio_credential_here(
+ text: str,
+ workdir: "str | None",
+ _unescaped: bool = False,
+) -> bool:
+ """`_references_studio_credential`, plus the relative paths *text* would open from *workdir*.
+
+ The tool cwd is `<studio home>/sandbox/<session>`, a sibling of the auth directory, so
+ `open('../../auth/auth.db')` reads the protected database while naming neither the directory
+ nor a credential basename. Resolving only the `..` tokens keeps this to the one shape that can
+ leave the sandbox at all."""
+ # A command that BINDS one of these variables itself means its own directory by it, and the
+ # generic `STUDIO_HOME` is registered as this install's root even when the backend does not
+ # set it. Resolving the local binding first stops `STUDIO_HOME=/opt/app cat
+ # "$STUDIO_HOME/auth/config.json"` from being refused as a read of Studio's own auth directory.
+ if "=" in text and _rebinds_the_studio_home_first(text):
+ text = _expand_shell_assignments(text)
+ if _references_studio_credential(text):
+ return True
+ # Naming the studio ROOT and a credential BASENAME is enough, even with nothing joining them:
+ # `find "$UNSLOTH_STUDIO_HOME" -name auth.db -exec cat` hands the directory to a tool that
+ # walks it, and no single token of it is a path to the database.
+ if _CREDENTIAL_BASENAME_RE.search(text) and _text_names_the_studio_root(text):
+ return True
+ # A recursive read of the root itself emits `auth/auth.db` and `.bootstrap_password` without
+ # naming either: `find "$UNSLOTH_STUDIO_HOME" -type f -exec cat {} +`, `tar czf b.tgz "$STUDIO_HOME"`,
+ # `grep -r sk- "$STUDIO_HOME"`. Work INSIDE a project under the root is untouched.
+ if (
+ _text_names_the_studio_root(text)
+ and _walks_a_tree_reading_it(text)
+ and _names_the_studio_root_itself(text)
+ ):
+ return True
+ # `c\d ../..` runs the `cd` builtin: bash removes the backslash before a word is a command name
+ # at all, so the escaped spelling has to be folded away before the cwd walk reads it.
+ if "\\" in text and not _unescaped:
+ # One level only: `\\` repeated 550 times drops a backslash per pass, and recursing per pass
+ # was a RecursionError raised out of the guard rather than a decision.
+ unescaped = _ESCAPED_WORD_CHAR_RE.sub(r"\1", text)
+ if unescaped != text and _references_studio_credential_here(
+ unescaped, workdir, _unescaped = True
+ ):
+ return True
+ if "[" in text:
+ # A one-character class is deterministic: `[a][u][t][h]/auth.db` is the auth directory
+ # spelled out, and it has to be read as the literal BEFORE the wildcard collapse below
+ # discards it.
+ literal = _SINGLETON_CLASS_RE.sub(r"\1", text)
+ if literal != text and _references_studio_credential_here(literal, workdir):
+ return True
+ # `aut[h]` splits at the brackets, which also end a path token; `aut?` keeps it whole.
+ if "[" in text:
+ collapsed = _BRACKET_CLASS_RE.sub("?", text)
+ if collapsed != text and _references_studio_credential_here(collapsed, workdir):
+ return True
+ # The kernel resolves the symlink FIRST, then applies `..`; a lexical normpath reads
+ # `/proc/self/cwd/../../auth` as `/proc/self/auth` and misses.
+ if workdir and "/proc/" in text:
+ substituted = _PROC_CWD_RE.sub(lambda _m: workdir.rstrip("/"), text)
+ if substituted != text and _references_studio_credential(substituted):
+ return True
+ # Bypass repoints HOME at the tool workdir, so `$HOME/../..` is the auth directory's parent.
+ # Substituted, not replaced: the raw text still carries the real-home spellings.
+ if workdir and ("pwd" in text.lower() or "%cd%" in text.lower()):
+ here = _CWD_VARIABLE_RE.sub(lambda _m: workdir.rstrip("/\\"), text)
+ if here != text and _references_studio_credential_here(here, workdir):
+ return True
+ if workdir and ("home" in text.lower() or "~" in text):
+ homed = _HOME_VARIABLE_RE.sub(lambda _m: workdir.rstrip("/\\"), text)
+ # A workdir spelling `~` would substitute to another match, so recurse only once it is gone.
+ if homed != text and not _HOME_VARIABLE_RE.search(homed):
+ if _references_studio_credential_here(homed, workdir):
+ return True
+ # One level of indirection, `r=$STUDIO_HOME; sqlite3 "$r/auth/auth.db"`. Same substitution the
+ # sensitive-path scan uses, and it only ADDS detections.
+ if "$" in text:
+ expanded = _expand_shell_assignments(text)
+ # The WHOLE workdir-aware analysis, not only the literal scan: `d=../..; cd "$d"` moves the
+ # directory every later relative path opens from, and handing the unexpanded text to the cwd
+ # walk read `$d` as a directory name and never moved.
+ if expanded != text and _references_studio_credential_here(expanded, workdir):
+ return True
+ # A `cd` earlier in the command moves where every later relative path opens from.
+ if workdir and ("cd" in text.lower() or "pushd" in text.lower()):
+ for offset, limit, cwd in _cwds_after_cd(workdir, text):
+ # The directory itself: `cd ../..; cd auth; sqlite3 auth.db` writes no separator at
+ # all, so every token below reads as an ordinary filename. Only when something actually
+ # RUNS there, though: `(cd ../..; cd auth); cat config.json` enters the directory and
+ # leaves without reading anything, and the `cat` outside the subshell is back in the
+ # sandbox.
+ if _references_studio_credential(cwd) and text[offset:limit].strip(" \t\n;&|()"):
+ return True
+ for match in _path_tokens_containing(text, "/\\"):
+ # Only what comes AFTER that `cd`: a path written before it opens from the old
+ # directory, so resolving `cat auth/auth.db; cd ../..` against the root refused a
+ # read that never left the sandbox.
+ if match.start() < offset or match.start() >= limit:
+ continue
+ token = match.group(0)
+ if os.path.isabs(token) or token.startswith("~"):
+ continue
+ if any(
+ _references_studio_credential(os.path.normpath(os.path.join(cwd, spelling)))
+ for spelling in _token_spellings(token)
+ ):
+ return True
+ if not workdir or ".." not in text:
+ return False
+ for token in (m.group(0) for m in _path_tokens_containing(text, "..")):
+ if "/" not in token and "\\" not in token:
+ continue
+ if any(
+ _references_studio_credential(os.path.normpath(os.path.join(workdir, spelling)))
+ for spelling in _token_spellings(token)
+ ):
+ return True
+ return False
+
+
+def _calls_in_uncalled_scopes(tree) -> "set[int]":
+ """Ids of calls sitting in a function or lambda body that nothing in *code* calls.
+
+ Deliberately narrow. A body whose name IS called anywhere in the snippet stays live, because the
+ move is then real and only the ordering is unknown, and the conservative reading is what a guard
+ wants. A branch is not a scope: `if cond: os.chdir(...)` may well run, so it keeps moving.
+
+ A CLASS body is not deferred at all: python executes it when the class statement runs, whether
+ or not anything instantiates the class, so `class C: os.chdir("../..")` moves the process. Only
+ the methods inside it are deferred, and those are function bodies reached in their own right.
+ """
+ called: "set[str]" = set()
+ # A lambda in the CALLED position runs immediately, and a lambda bound to a name runs when that
+ # name is called, so neither is inert.
+ invoked: "set[int]" = set()
+ lambda_names: "dict[str, int]" = {}
+ for node in ast.walk(tree):
+ if isinstance(node, (ast.Assign, ast.AnnAssign)):
+ targets = node.targets if isinstance(node, ast.Assign) else [node.target]
+ if isinstance(node.value, ast.Lambda):
+ for target in targets:
+ if isinstance(target, ast.Name):
+ lambda_names[target.id] = id(node.value)
+ if not isinstance(node, ast.Call):
+ continue
+ func = node.func
+ if isinstance(func, ast.Lambda):
+ invoked.add(id(func))
+ elif isinstance(func, ast.Name):
+ called.add(func.id)
+ elif isinstance(func, ast.Attribute):
+ called.add(func.attr)
+ invoked.update(node_id for name, node_id in lambda_names.items() if name in called)
+ inert: "set[int]" = set()
+ for node in ast.walk(tree):
+ if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
+ if node.name in called:
+ continue
+ elif isinstance(node, ast.Lambda) and id(node) in invoked:
+ # `(lambda: os.chdir("../.."))()` runs its body right there.
+ continue
+ elif not isinstance(node, ast.Lambda):
+ continue
+ # The BODY only. Default arguments, decorators and annotations are evaluated when the
+ # function is DEFINED, so `def f(x = os.chdir("../..")): pass` moves the process even
+ # though nothing ever calls `f`.
+ body = node.body if isinstance(node.body, list) else [node.body]
+ for statement in body:
+ for inner in ast.walk(statement):
+ if isinstance(inner, ast.Call):
+ inert.add(id(inner))
+ return inert
+
+
+# A recursive copy or archive of the studio ROOT carries `auth/` with it, and the copy is then an
+# ordinary file nothing guards. Mirrors the terminal walk rule.
+_PY_TREE_COPY_CALLS = frozenset({"copytree", "make_archive", "copy_tree", "unpack_archive"})
+# A recursive WALK of the root reaches `auth/` the same way a copy of it does, and reading
+# what it yields prints the secrets without any of them being named.
+# Only the RECURSIVE ones: a shallow listing returns the root's own entry names and reads
+# nothing inside `auth`, which is what the terminal side allows for a plain root listing.
+_PY_TREE_WALK_CALLS = frozenset("walk rglob glob iglob".split())
+# `glob` is shallow unless its pattern descends.
+_PY_SHALLOW_UNLESS_RECURSIVE = frozenset({"glob", "iglob"})
+_PY_TREE_ROOT_CALLS = _PY_TREE_COPY_CALLS | _PY_TREE_WALK_CALLS
+# One C-speed scan for any of them, so ordinary code pays a single search rather than a
+# substring test per name.
+_PY_TREE_CALL_RE = re.compile("|".join(sorted(_PY_TREE_ROOT_CALLS)))
+
+
+def _python_copies_the_studio_root(tree) -> bool:
+ """True when a recursive copy, archive or walk call names the studio root as its SOURCE."""
+ root = _studio_home_for_guard()
+ if not root:
+ return False
+ folded_root = _folded_word(root)
+ # `root = os.environ["UNSLOTH_STUDIO_HOME"]; os.walk(root)` names the root through one binding,
+ # which arrives as a bare `Name` that neither the env test nor the fold can resolve.
+ aliases = {
+ target.id: node.value
+ for node in ast.walk(tree)
+ if isinstance(node, ast.Assign)
+ for target in node.targets
+ if isinstance(target, ast.Name)
+ }
+ for node in ast.walk(tree):
+ if not isinstance(node, ast.Call):
+ continue
+ name = getattr(node.func, "attr", None) or getattr(node.func, "id", None)
+ if name not in _PY_TREE_ROOT_CALLS:
+ continue
+ # `copytree(src, dst)` names its source first; `make_archive(base, format, root_dir,
+ # base_dir)` names it third. A walker names it first too, or as the RECEIVER it is called
+ # on: `Path(os.environ["UNSLOTH_STUDIO_HOME"]).rglob("*")`.
+ sources = list(node.args[2:4]) if name == "make_archive" else list(node.args[:1])
+ if name in _PY_SHALLOW_UNLESS_RECURSIVE and not any(
+ isinstance(a, ast.Constant) and isinstance(a.value, str) and "**" in a.value
+ for a in node.args
+ ):
+ continue
+ if name in _PY_TREE_WALK_CALLS:
+ receiver = getattr(node.func, "value", None)
+ sources.append(receiver)
+ # `Path(<root>)` WRAPS the name the walk starts from, so the wrapped name is the source.
+ sources.extend(getattr(receiver, "args", ())[:1])
+ sources.extend(
+ k.value
+ for k in node.keywords
+ if k.arg in ("src", "root_dir", "base_dir", "top", "path")
+ )
+ for argument in sources:
+ if isinstance(argument, ast.Name):
+ argument = aliases.get(argument.id)
+ # `copytree(Path(os.environ["UNSLOTH_STUDIO_HOME"]), ...)`: the constructor WRAPS the
+ # name of the source, and the fold reduces the call itself to a dynamic marker.
+ if isinstance(argument, ast.Call) and getattr(argument, "args", None):
+ argument = argument.args[0]
+ if isinstance(argument, ast.Name):
+ argument = aliases.get(argument.id)
+ if argument is None:
+ continue
+ if _names_the_studio_home_env(argument):
+ return True
+ folded = _folded_path(argument)
+ if isinstance(folded, str) and folded and _folded_word(folded) == folded_root:
+ return True
+ return False
+
+
+def _python_builds_a_credential_path(code: str, workdir: "str | None") -> bool:
+ """True when a path the CODE builds names the auth directory, however it is spelled.
+
+ `os.path.join("..", "..", "auth", "auth.db")` names the protected database in pieces, so the
+ text scan sees four ordinary strings and nothing that looks like a path. `_folded_path` is the
+ same fold the sensitive-path analyzer already applies to python; here its result is put through
+ the credential test, resolved against the cwd like any other relative path."""
+ lowered = code.lower()
+ # A recursive copy of the ROOT carries `auth/` without naming it, so it passes the hint filter
+ # below. Two substring tests gate the parse, which keeps this off the ordinary path.
+ # Three tests gate the parse, cheapest first: the root has to be NAMEABLE in the text at all,
+ # then a tree call has to appear, and only then do the real root tests run. Ordinary numeric or
+ # dataframe code stops at the first one, which is two substring scans.
+ if (
+ (
+ "studio_home" in lowered
+ or any(spelling in lowered for spelling in _studio_home_spellings_lowered())
+ )
+ and _PY_TREE_CALL_RE.search(lowered)
+ and (_text_names_the_studio_root(code) or _code_reads_the_studio_home(code))
+ ):
+ try:
+ if _python_copies_the_studio_root(ast.parse(code)):
+ return True
+ except (SyntaxError, RecursionError, MemoryError, ValueError):
+ pass
+ if not any(hint in lowered for hint in _STUDIO_CREDENTIAL_HINTS):
+ return False
+ try:
+ tree = ast.parse(code)
+ except (SyntaxError, RecursionError, MemoryError, ValueError):
+ # The executor reports a syntax error itself, and a snippet the PARSER cannot hold (10k
+ # chained operators) is not a decision this guard can make either. Both are "nothing to
+ # fold" rather than an exception raised out of the tool.
+ return False
+ # Source order, because `os.chdir('../..')` moves every path after it. String constants are
+ # included: `sqlite3.connect('auth/auth.db')` is no path constructor, so the argument is it.
+ nodes = sorted(
+ (
+ n
+ for n in ast.walk(tree)
+ if isinstance(n, (ast.Call, ast.BinOp, ast.JoinedStr))
+ or (isinstance(n, ast.Constant) and isinstance(n.value, str) and n.value)
+ ),
+ key = lambda n: (getattr(n, "lineno", 0), getattr(n, "col_offset", 0)),
+ )
+ # A list of possible directories, for the same reason the shell walk keeps one: a `chdir` into a
+ # path that does not exist raises, and code that catches it carries on from where it was.
+ chdir_modules = _chdir_modules(tree)
+ chdir_names = _chdir_names(tree, chdir_modules)
+ # A `chdir` inside a function body only moves anything if that function RUNS. An uncalled helper
+ # was moving the walk for the statements after it, so a snippet that defines one and then reads
+ # its own `auth/config.json` was refused although the process never left the sandbox.
+ inert_moves = _calls_in_uncalled_scopes(tree)
+ name_bases = _literal_name_bases(tree)
+ # `root = os.open("../..", os.O_RDONLY)` names a DIRECTORY, and `os.open("auth/auth.db", ...,
+ # dir_fd = root)` opens relative to it. The two calls look unrelated token by token: `../..`
+ # resolves to the studio root and goes nowhere, and `auth/auth.db` is checked against the
+ # sandbox, while the kernel joins them.
+ dir_fds = _literal_directory_descriptors(tree, workdir)
+ studio_env_names, foreign_env_names = _python_env_binding_names(tree)
+ process_aliases, process_functions = _process_module_aliases(tree)
+ cwds: "list[str | None]" = [workdir]
+ # `contextlib.chdir(p)` moves only while its `with` body runs and restores on exit, so its move
+ # is scoped to that body rather than carried forward. Treating it as permanent refused a later
+ # read of the project's OWN `auth/config.json` in every permission mode.
+ scoped_until = _scoped_chdir_bounds(tree)
+ restore: "list[tuple[int, list]]" = []
+ for node in nodes:
+ while restore and getattr(node, "lineno", 0) > restore[-1][0]:
+ cwds = restore.pop()[1]
+ if (
+ isinstance(node, ast.Call)
+ and _is_chdir_call(node, chdir_names, chdir_modules)
+ and id(node) not in inert_moves
+ ):
+ argument = _chdir_argument(node)
+ target = None if argument is None else _folded_path(argument)
+ targets: "list[str]" = []
+ # `os.fchdir(fd)` names its destination by a descriptor, so there is no path to fold and
+ # the move is real. The one destination that matters here is the studio root, so it is
+ # added to the live states: a later `auth/auth.db` is then resolved from there as well
+ # as from the sandbox, which is the fail-closed reading the unknown target calls for.
+ if isinstance(node.func, ast.Attribute) and node.func.attr == "fchdir":
+ root = _studio_home_for_guard()
+ if root:
+ targets = [root]
+ # `os.chdir(os.environ["UNSLOTH_STUDIO_HOME"])` names the root without spelling it, and
+ # `_build_bypass_env` keeps that variable in the child, so the move is real. The fold
+ # has no value for a subscript, which left the walk sitting in the sandbox.
+ if not targets and _names_the_studio_home_env(argument):
+ root = _studio_home_for_guard()
+ if root:
+ targets = [root]
+ if not targets and target and "\x00" not in target:
+ # `os.chdir(Path.cwd().parents[1])` is an ordinary move, and the fold writes the
+ # walk as one marker, so resolve it here rather than ignoring the move.
+ targets = (
+ _parent_walk_targets(argument, target, cwds, name_bases)
+ if "\x02" in target
+ else [target]
+ )
+ if targets:
+ moved: "list[str | None]" = []
+ for one in targets:
+ for cwd in cwds:
+ nxt = (
+ one
+ if os.path.isabs(one) or not cwd
+ else os.path.normpath(os.path.join(cwd, one))
+ )
+ if nxt not in moved:
+ moved.append(nxt)
+ if _references_studio_credential(nxt):
+ return True
+ bound = scoped_until.get(id(node))
+ if bound is not None:
+ restore.append((bound, cwds))
+ cwds = (moved + [c for c in cwds if c not in moved])[:_MAX_TRACKED_CWDS]
+ continue
+ if isinstance(node, ast.Call) and dir_fds and _call_opens_under_a_descriptor(node, dir_fds):
+ return True
+ if isinstance(node, ast.Call) and _call_runs_from_a_credential_directory(
+ node, cwds, name_bases, process_aliases, process_functions
+ ):
+ # `subprocess.run(["strings", "auth/auth.db"], cwd = "../..")` moves nothing in this
+ # process, but the child opens its arguments from the directory it is handed.
+ return True
+ folded = node.value if isinstance(node, ast.Constant) else _folded_path(node)
+ if not folded:
+ continue
+ if "\x02" in folded:
+ # `Path.cwd().parents[1] / "auth" / "auth.db"` folds the parent walk to a marker. The
+ # analyzer that reads that is skipped in bypass mode, so resolve it here, from the base
+ # and the level count in the expression rather than from the folded text.
+ if any(
+ _references_studio_credential(target)
+ for target in _parent_walk_targets(node, folded, cwds, name_bases)
+ ):
+ return True
+ continue
+ if "\x00" in folded:
+ # A dynamic piece is the sensitive-path analyzer's, unless the code reads a
+ # studio-home variable: bypass keeps that in the child env, so its value is known.
+ root = (
+ _studio_home_for_guard()
+ if _code_reads_the_studio_home(code)
+ and _expression_reads_the_studio_home(node, studio_env_names, foreign_env_names)
+ else None
+ )
+ if root and any(
+ _references_studio_credential_here(folded.replace("\x00", root), cwd)
+ for cwd in cwds
+ ):
+ return True
+ # `os.environ["PWD"] + "/../../auth/auth.db"` reads the cwd the same way, and bypass
+ # sets PWD to the tool workdir, so the dynamic piece is known here too.
+ if _code_reads_the_working_directory(code) and any(
+ cwd and _references_studio_credential_here(folded.replace("\x00", cwd), cwd)
+ for cwd in cwds
+ ):
+ return True
+ continue
+ for cwd in cwds:
+ # Against the cwd by then: after `os.chdir('../..')`, `auth/auth.db` is the database.
+ if cwd and not os.path.isabs(folded):
+ joined = os.path.normpath(os.path.join(cwd, folded.replace("\\", "/")))
+ if _references_studio_credential(joined):
+ return True
+ if _references_studio_credential_here(folded, cwd):
+ return True
+ return False
+
+
+def _parent_chain(node) -> "tuple | None":
+ """``(base node, levels walked up)`` for a `.parent` / `.parents[n]` chain, else None."""
+ levels = 0
+ current = node
+ while True:
+ if isinstance(current, ast.Attribute) and current.attr == "parent":
+ levels += 1
+ current = current.value
+ continue
+ if (
+ isinstance(current, ast.Subscript)
+ and isinstance(current.value, ast.Attribute)
+ and current.value.attr == "parents"
+ ):
+ index = current.slice
+ if isinstance(index, ast.Constant) and isinstance(index.value, int):
+ levels += index.value + 1
+ current = current.value.value
+ continue
+ return None
+ break
+ return (current, levels) if levels else None
+
+
+def _is_cwd_call(node) -> bool:
+ """`Path.cwd()`, `os.getcwd()`, `Path.home()` and the bare spellings of each.
+
+ `home` counts because both subprocess environment builders set `HOME` to the session workdir, so
+ `Path.home()` returns the same directory `Path.cwd()` does and a parent walk off it lands in the
+ studio root just the same.
+ """
+ if not isinstance(node, ast.Call):
+ return False
+ func = node.func
+ name = func.attr if isinstance(func, ast.Attribute) else getattr(func, "id", "")
+ return name in ("cwd", "getcwd", "home", "expanduser")
+
+
+def _literal_name_bases(tree) -> "dict[str, str]":
+ """Names bound once to a literal path, so `root = Path('/tmp/p')` is not read as the cwd.
+
+ A name assigned more than once, or bound to something this fold cannot read, is left out: the
+ caller then falls back to the working directory, which is where an unqualified name usually is.
+ """
+ bases: "dict[str, str]" = {}
+ rebound: "set[str]" = set()
+ for node in ast.walk(tree):
+ if not isinstance(node, ast.Assign) or len(node.targets) != 1:
+ continue
+ target = node.targets[0]
+ if not isinstance(target, ast.Name):
+ continue
+ if target.id in bases or target.id in rebound:
+ rebound.add(target.id)
+ bases.pop(target.id, None)
+ continue
+ value = _folded_path(node.value)
+ if value and "\x00" not in value and "\x02" not in value:
+ bases[target.id] = value
+ else:
+ rebound.add(target.id)
+ return bases
+
+
+def _parent_walk_targets(
+ node,
+ folded: str,
+ cwds: "list",
+ name_bases: "dict | None" = None,
+) -> "list[str]":
+ """Where a `.parent` / `.parents[n]` expression can land, resolved rather than guessed.
+
+ The fold writes one marker for the whole walk, so the level count and the base come from the
+ expression itself: `Path('/tmp').parent / 'auth'` is `/auth`, not something under the sandbox.
+ """
+ tail = folded.rsplit("\x02", 1)[-1].lstrip("/\\")
+ left = node
+ while isinstance(left, ast.BinOp):
+ left = left.left
+ chain = _parent_chain(left)
+ if chain is None:
+ return []
+ base_node, levels = chain
+ bases: "list[str]" = []
+ named = (name_bases or {}).get(base_node.id) if isinstance(base_node, ast.Name) else None
+ if named:
+ bases = (
+ [named]
+ if os.path.isabs(named)
+ else [os.path.normpath(os.path.join(c, named)) for c in cwds if c]
+ )
+ elif _is_cwd_call(base_node) or isinstance(base_node, ast.Name):
+ bases = [c for c in cwds if c]
+ else:
+ folded_base = _folded_path(base_node)
+ if folded_base and "\x00" not in folded_base and "\x02" not in folded_base:
+ bases = (
+ [folded_base]
+ if os.path.isabs(folded_base)
+ else [os.path.normpath(os.path.join(c, folded_base)) for c in cwds if c]
+ )
+ out: "list[str]" = []
+ for base in bases:
+ for _ in range(min(levels, 64)):
+ parent = os.path.dirname(base.rstrip("/\\"))
+ if not parent or parent == base:
+ break
+ base = parent
+ out.append(os.path.normpath(os.path.join(base, tail)) if tail else base)
+ return out
+
+
+def _scoped_chdir_bounds(tree) -> dict:
+ """`id(call) -> last line of the with body`, for a chdir used as a context manager.
+
+ `with contextlib.chdir(p):` restores the directory on exit, so the move applies to the body and
+ nothing after it. A call that is not a `with` item is absent here and stays permanent, which is
+ what a bare `os.chdir(p)` does.
+ """
+ bounds: dict = {}
+ for node in ast.walk(tree):
+ if not isinstance(node, (ast.With, ast.AsyncWith)):
+ continue
+ end = getattr(node, "end_lineno", None)
+ if end is None:
+ continue
+ for item in node.items:
+ if isinstance(item.context_expr, ast.Call):
+ bounds[id(item.context_expr)] = end
+ return bounds
+
+
+# The APIs that start a child process and accept a `cwd`. Anything else carrying that keyword is an
+# ordinary function whose argument means whatever its author decided.
+_CHILD_PROCESS_RECEIVERS = {
+ "subprocess": frozenset(
+ {"run", "Popen", "call", "check_call", "check_output", "getoutput", "getstatusoutput"}
+ ),
+ "asyncio": frozenset({"create_subprocess_exec", "create_subprocess_shell"}),
+ "os": frozenset({"popen"}),
+}
+_CHILD_PROCESS_BARE_NAMES = frozenset(
+ {
+ "run",
+ "Popen",
+ "call",
+ "check_call",
+ "check_output",
+ "create_subprocess_exec",
+ "create_subprocess_shell",
+ }
+)
+
+
+def _process_module_aliases(tree) -> "tuple[dict, set]":
+ """`(module aliases, bare process-function names)` for the import forms that rename either one.
+
+ `import subprocess as sp` renames the module and `from subprocess import run as launch` renames
+ the function, and both leave the call spelled under a name the fixed tables do not hold.
+ """
+ aliases: dict = {}
+ # NOT pre-seeded with the generic names. `run`, `call` and `check_output` are ordinary function
+ # names, and treating a snippet's own `def run(...)` as a process launch refused ordinary code in
+ # every permission mode. Only a name an import actually binds from a process module counts.
+ bare: "set[str]" = set()
+ for node in ast.walk(tree):
+ if isinstance(node, ast.ImportFrom):
+ root = (node.module or "").split(".")[0]
+ functions = _CHILD_PROCESS_RECEIVERS.get(root)
+ if not functions:
+ continue
+ for entry in node.names:
+ if entry.name in functions:
+ bare.add(entry.asname or entry.name)
+ continue
+ if not isinstance(node, ast.Import):
+ continue
+ for entry in node.names:
+ root = entry.name.split(".")[0]
+ if root in _CHILD_PROCESS_RECEIVERS and entry.asname:
+ aliases[entry.asname] = root
+ return aliases, bare
+
+
+def _launches_a_child_process(
+ node: "ast.Call",
+ aliases: "dict | None" = None,
+ bare: "set | None" = None,
+) -> bool:
+ """True for a call that starts a process, by module attribute or by a bare imported name.
+
+ The receiver is resolved through the import aliases first. `import subprocess as sp` leaves the
+ call spelled `sp.run(...)`, and reading the receiver literally missed it, so a child handed a
+ `cwd` outside the sandbox went unchecked.
+ """
+ func = node.func
+ if isinstance(func, ast.Attribute):
+ receiver = func.value
+ name = receiver.id if isinstance(receiver, ast.Name) else getattr(receiver, "attr", "")
+ name = (aliases or {}).get(name, name)
+ return func.attr in _CHILD_PROCESS_RECEIVERS.get(name, frozenset())
+ return isinstance(func, ast.Name) and func.id in (
+ bare if bare is not None else _CHILD_PROCESS_BARE_NAMES
+ )
+
+
+def _literal_directory_descriptors(tree, workdir: "str | None") -> dict:
+ """Name -> the directory an `os.open` of a literal path bound to it names.
+
+ Only the shape that can reach the auth directory is resolved: a literal path, opened as a
+ descriptor, held in a plain name. Anything else leaves the name absent and the `dir_fd` below
+ unresolved, which is what it already was.
+ """
+ descriptors: dict = {}
+ for node in ast.walk(tree):
+ if isinstance(node, ast.AnnAssign) and node.value is not None:
+ targets, value = [node.target], node.value
+ elif isinstance(node, ast.Assign):
+ targets, value = node.targets, node.value
+ else:
+ continue
+ if not isinstance(value, ast.Call) or not value.args:
+ continue
+ called = getattr(value.func, "attr", None) or getattr(value.func, "id", None)
+ if called != "open":
+ continue
+ folded = _folded_path(value.args[0])
+ if not folded or "\x00" in folded or "\x02" in folded:
+ continue
+ here = (
+ folded
+ if os.path.isabs(folded) or not workdir
+ else os.path.normpath(os.path.join(workdir, folded))
+ )
+ for target in targets:
+ if isinstance(target, ast.Name):
+ descriptors[target.id] = here
+ return descriptors
+
+
+def _call_opens_under_a_descriptor(node: "ast.Call", dir_fds: dict) -> bool:
+ """True when a call opens a credential path relative to a tracked directory descriptor."""
+ given = next((kw.value for kw in node.keywords if kw.arg == "dir_fd"), None)
+ if not isinstance(given, ast.Name):
+ return False
+ base = dir_fds.get(given.id)
+ if not base:
+ return False
+ for argument in node.args:
+ folded = _folded_path(argument)
+ if not folded or "\x00" in folded or "\x02" in folded or os.path.isabs(folded):
+ continue
+ if _references_studio_credential(
+ os.path.normpath(os.path.join(base, folded.replace("\\", "/")))
+ ):
+ return True
+ return False
+
+
+def _call_runs_from_a_credential_directory(
+ node: "ast.Call",
+ cwds: "list",
+ name_bases: "dict",
+ process_aliases: "dict | None" = None,
+ process_functions: "set | None" = None,
+) -> bool:
+ """True when a call hands a child process a directory that makes one of its paths a credential.
+
+ `subprocess.run([...], cwd = "../..")` leaves this process where it is, so the walk above never
+ moves, and each literal argument was tested against the sandbox instead of against the
+ directory the child actually runs from.
+
+ Restricted to the APIs that actually start a process. `cwd` is an ordinary keyword name, and
+ reading it as process semantics on any call refused ordinary code:
+ `describe("auth/config.json", cwd = "../..")` was blocked in every permission mode even though
+ the function may never touch that path.
+ """
+ if not _launches_a_child_process(node, process_aliases, process_functions):
+ return False
+ given = next((kw.value for kw in node.keywords if kw.arg == "cwd"), None)
+ if given is None:
+ return False
+ folded = _folded_path(given)
+ if (not folded or "\x00" in folded) and _names_the_studio_home_env(given):
+ # `cwd = os.environ["UNSLOTH_STUDIO_HOME"]` hands the child the studio root itself, and the
+ # fold has no value for the subscript. Same resolution the chdir walk uses.
+ root = _studio_home_for_guard()
+ folded = root or folded
+ if not folded or "\x00" in folded:
+ return False
+ targets = (
+ _parent_walk_targets(given, folded, cwds, name_bases) if "\x02" in folded else [folded]
+ )
+ directories: "list[str]" = []
+ for target in targets:
+ for cwd in cwds:
+ here = (
+ target
+ if os.path.isabs(target) or not cwd
+ else os.path.normpath(os.path.join(cwd, target))
+ )
+ if here not in directories:
+ directories.append(here)
+ if any(_references_studio_credential(here) for here in directories):
+ return True
+ for inner in ast.walk(node):
+ if isinstance(inner, ast.Constant) and isinstance(inner.value, str) and inner.value:
+ piece = inner.value
+ else:
+ piece = _folded_path(inner) if isinstance(inner, (ast.BinOp, ast.Call)) else ""
+ if not piece or "\x00" in piece or "\x02" in piece or os.path.isabs(piece):
+ continue
+ for here in directories:
+ if _references_studio_credential(
+ os.path.normpath(os.path.join(here, piece.replace("\\", "/")))
+ ):
+ return True
+ return False
+
+
+def _chdir_argument(node: "ast.Call"):
+ """The path a `chdir` call is given, positionally or as `path=`."""
+ if node.args:
+ return node.args[0]
+ for keyword in node.keywords:
+ if keyword.arg == "path":
+ return keyword.value
+ return None
+
+
+def _chdir_names(tree, modules: "set[str] | None" = None) -> "set[str]":
+ """Every name that refers to `os.chdir` in this snippet, including aliases.
+
+ `from os import chdir as move` and `move = os.chdir` are both ordinary python, and a walk that
+ only knows the literal name resolves everything after `move('../..')` against the wrong place.
+ An alias of somebody else's `chdir`, `move = ftp.chdir`, is not one of them.
+ """
+ modules = modules or {"os", "contextlib"}
+ # The BARE name only counts once an import binds it. A snippet's own `def chdir(path)` is an
+ # ordinary function, and treating a call to it as a move refused code that never leaves the
+ # sandbox. The qualified `os.chdir(...)` form does not go through this set.
+ names: "set[str]" = set()
+ defined_locally = any(
+ isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name == "chdir"
+ for node in ast.walk(tree)
+ )
+ for node in ast.walk(tree):
+ if isinstance(node, ast.ImportFrom) and node.module in ("os", "contextlib"):
+ for alias in node.names:
+ if alias.name == "chdir":
+ names.add(alias.asname or alias.name)
+ elif isinstance(node, ast.Assign) and len(node.targets) == 1:
+ target, value = node.targets[0], node.value
+ if not isinstance(target, ast.Name):
+ continue
+ if (
+ isinstance(value, ast.Attribute)
+ and value.attr == "chdir"
+ and isinstance(value.value, ast.Name)
+ and value.value.id in modules
+ ) or (isinstance(value, ast.Name) and value.id in names):
+ names.add(target.id)
+ if defined_locally:
+ names.discard("chdir")
+ return names
+
+
+def _chdir_modules(tree) -> "set[str]":
+ """The names that stand for a module whose `chdir` moves THIS process.
+
+ `ftp.chdir('../..')` changes a remote directory and leaves the local one alone, so reading a
+ project's own `auth/config.json` afterwards was refused for a move that never happened.
+ """
+ # `posix` on POSIX and `nt` on Windows are the platform modules `os` itself is built on, so
+ # `posix.chdir` is the same primitive under a different name.
+ known = ("os", "contextlib", "posix", "nt")
+ modules = set(known)
+ for node in ast.walk(tree):
+ if isinstance(node, ast.Import):
+ for alias in node.names:
+ if alias.name in known and alias.asname:
+ modules.add(alias.asname)
+ return modules
+
+
+# The calls that change the process directory. `os.fchdir(fd)` moves to whatever the descriptor
+# names, which this scan cannot resolve: the move is real and its destination is unknown, so it is
+# tracked as an UNRESOLVED move rather than ignored.
+_CHDIR_METHODS = frozenset({"chdir", "fchdir"})
+
+
+def _is_chdir_call(
+ node: "ast.Call",
+ names: "set[str] | None" = None,
+ modules: "set[str] | None" = None,
+) -> bool:
+ """True for `os.chdir(...)`, a bare `chdir(...)`, and any alias bound from it.
+
+ `names` is the set of BARE names that refer to it, which is empty for a snippet that defines its
+ own `chdir` and never imports one. Empty is meaningful here, so it is distinguished from the
+ None the callers that do not compute it pass. The qualified `os.chdir(...)` form is unaffected:
+ the receiver is what identifies it there.
+ """
+ bare = {"chdir"} if names is None else names
+ func = node.func
+ if isinstance(func, ast.Attribute):
+ if func.attr not in _CHDIR_METHODS and func.attr not in bare:
+ return False
+ # Only a module that owns the process directory. An unknown receiver is not one.
+ return isinstance(func.value, ast.Name) and func.value.id in (
+ modules or {"os", "contextlib"}
+ )
+ return isinstance(func, ast.Name) and func.id in bare
+
+
+def _reads_an_environment_variable(node) -> bool:
+ """True for `os.environ[...]`, `os.environ.get(...)` and `os.getenv(...)`."""
+ if isinstance(node, ast.Subscript):
+ receiver = node.value
+ return (getattr(receiver, "attr", None) or getattr(receiver, "id", None)) == "environ"
+ if isinstance(node, ast.Call):
+ called = getattr(node.func, "attr", None) or getattr(node.func, "id", None)
+ if called == "getenv":
+ return True
+ receiver = getattr(node.func, "value", None)
+ return (
+ called == "get"
+ and (getattr(receiver, "attr", None) or getattr(receiver, "id", None)) == "environ"
+ )
+ return False
+
+
+def _python_env_binding_names(tree) -> "tuple[set, set]":
+ """`(names holding the studio home, names holding some OTHER environment variable)`.
+
+ A dynamic path piece is only the studio root when the expression that built it actually read
+ that variable. Attributing it to any snippet that mentions the variable ANYWHERE refused
+ `project = os.environ["PROJECT_HOME"]; open(project + "/auth/config.json")`, which names an
+ unrelated application's directory.
+ """
+ studio: "set[str]" = set()
+ foreign: "set[str]" = set()
+ for node in ast.walk(tree):
+ if isinstance(node, ast.AnnAssign) and node.value is not None:
+ targets, value = [node.target], node.value
+ elif isinstance(node, ast.Assign):
+ targets, value = node.targets, node.value
+ else:
+ continue
+ if not _reads_an_environment_variable(value):
+ continue
+ for target in targets:
+ if isinstance(target, ast.Name):
+ (studio if _names_the_studio_home_env(value) else foreign).add(target.id)
+ return studio, foreign
+
+
+def _expression_reads_the_studio_home(node, studio_names, foreign_names) -> bool:
+ """Whether THIS expression's dynamic piece is the studio home.
+
+ Positive attribution both ways: the expression reads the variable itself or uses a name bound to
+ it, or every environment-backed name in it belongs to a different variable. Anything this cannot
+ attribute keeps the old whole-snippet answer, which is the fail-closed one.
+ """
+ names = {piece.id for piece in ast.walk(node) if isinstance(piece, ast.Name)}
+ if any(_names_the_studio_home_env(piece) for piece in ast.walk(node)):
+ return True
+ if names & set(studio_names):
+ return True
+ return not (names & set(foreign_names))
+
+
+def _variable_points_at_this_install(name: str) -> bool:
+ """Whether a studio-home variable's VALUE is this install's root.
+
+ `STUDIO_HOME` is a generic name another application can own, and bypass keeps that foreign value
+ in the child, so `open(os.environ["STUDIO_HOME"] + "/auth/config.json")` reads the other
+ application. Unset stays True: the child cannot expand it either, so nothing is granted.
+ """
+ value = (os.environ.get(name.upper()) or "").strip()
+ if not value:
+ return True
+ root = _studio_home_for_guard()
+ if not root:
+ return True
+ try:
+ return _same_directory(os.path.expanduser(value), root)
+ except Exception: # noqa: BLE001 - an unreadable value must not break classification
+ return True
+
+
+def _names_the_studio_home_env(node) -> bool:
+ """True when *node* reads an environment variable that holds the studio home.
+
+ `os.environ["UNSLOTH_STUDIO_HOME"]`, the `.get` spelling and `os.getenv` all return the same
+ directory. Case-insensitive, because `os.environ` upper-cases every key it is handed on Windows.
+ """
+ if node is None:
+ return False
+ if isinstance(node, ast.Subscript):
+ receiver = node.value
+ named = getattr(receiver, "attr", None) or getattr(receiver, "id", None)
+ key = node.slice
+ return (
+ named == "environ"
+ and isinstance(key, ast.Constant)
+ and isinstance(key.value, str)
+ and key.value.upper() in _STUDIO_HOME_ENV_VARS
+ and _variable_points_at_this_install(key.value)
+ )
+ if isinstance(node, ast.Call) and node.args:
+ called = getattr(node.func, "attr", None) or getattr(node.func, "id", None)
+ first = node.args[0]
+ return (
+ called in ("get", "getenv")
+ and isinstance(first, ast.Constant)
+ and isinstance(first.value, str)
+ and first.value.upper() in _STUDIO_HOME_ENV_VARS
+ and _variable_points_at_this_install(first.value)
+ )
+ return False
+
+
+def _code_reads_the_studio_home(code: str) -> bool:
+ """True when *code* mentions one of the environment variables that name the studio home.
+
+ Case-insensitive: on Windows `os.environ` upper-cases every key it is given (`os._createenviron`
+ sets `encodekey = str.upper` for `nt`), so `os.environ["unsloth_studio_home"]` returns the same
+ value the upper-case spelling does. The variable markers the text scan uses are already compared
+ lowercased, so this brings the python fold into line with them."""
+ lowered = code.lower()
+ # A variable SET to another application's directory names that one, not this install's root.
+ return any(
+ var.lower() in lowered and _variable_points_at_this_install(var)
+ for var in _STUDIO_HOME_ENV_VARS
+ )
+
+
+def _code_reads_the_working_directory(code: str) -> bool:
+ """True when *code* asks for the working directory by any of its usual names."""
+ lowered = code.lower()
+ return '"pwd"' in lowered or "'pwd'" in lowered or "getcwd" in lowered or "cwd()" in lowered
+
+
+_studio_home_lowered_cache: "tuple | None" = None
+
+
+def _studio_home_spellings_lowered() -> "tuple[str, ...]":
+ """The root as source text can spell it, memoized on the marker table it comes from.
+
+ A per-call prefilter, so rebuilding the root for every python snippet showed up. Three forms,
+ because a Windows root reaches the text as `c:\\dir`, as `c:\\\\dir` inside a python literal and
+ as `c:/dir`; on posix all three are the same string.
+ """
+ global _studio_home_lowered_cache
+ markers = _studio_auth_dir_markers()[0]
+ if _studio_home_lowered_cache is None or _studio_home_lowered_cache[0] is not markers:
+ # Built from the FOLDED root, so the separators the root happens to be stored with do not
+ # decide the answer: only the ones the source text spells do.
+ base = _folded_word(_studio_home_for_guard() or "\x00")
+ _studio_home_lowered_cache = (
+ markers,
+ tuple({base, base.replace("/", "\\"), base.replace("/", "\\\\")}),
+ )
+ return _studio_home_lowered_cache[1]
+
+
+def _studio_home_for_guard() -> "str | None":
+ """This install's studio root, derived from the same resolution the markers use.
+
+ From the ORIGINAL spelling, not the folded one: a root with an uppercase character in it would
+ otherwise be reconstructed in lowercase, and on a case-sensitive host the marker check then
+ rejected its own synthesized path, letting a move to the real root through.
+ """
+ auth_markers, _variable_markers, _cd_re = _studio_auth_dir_markers()
+ if not auth_markers:
+ return None
+ return os.path.dirname(auth_markers[0][2].rstrip("/\\")) or None
+
+
+def _needs_a_workdir(text: str) -> bool:
+ """Whether resolving *text* needs the cwd at all.
+
+ Traversal needs it, and so does anything that MOVES the directory: `pushd <studio home>;
+ sqlite3 auth/auth.db` carries no `..`, and without the workdir the walk that would have caught
+ it never ran.
+ """
+ if ".." in text:
+ return True
+ lowered = text.lower()
+ return (
+ "cd" in lowered
+ or "pushd" in lowered
+ or "chdir" in lowered
+ # `os.path.dirname(os.path.dirname(os.getcwd()))` walks up without writing a `..`, and the
+ # python analyzer resolves `getcwd` itself -- but only if it is handed the workdir.
+ or "getcwd" in lowered
+ or "cwd" in lowered
+ # `Path.cwd().parents[1] / "auth"` walks up from the cwd without writing a `..`.
+ or "parent" in lowered
+ )
+
+
+def _tool_workdir_for_guard(session_id: "str | None") -> "str | None":
+ """The cwd the executor is about to use, or None if it cannot be resolved cheaply."""
+ try:
+ return _get_workdir(session_id)
+ except Exception: # noqa: BLE001 - an unresolvable workdir leaves the textual match in place
+ return None
+
+
# A shell redirection with no following space (cat <../../notes) keeps `..` adjacent to `<`/`>`, so those count as
# leading delimiters here too.
_PARENT_TRAVERSAL_RE = re.compile(r"(?:^|[\s/\\'\"=:<>])\.\.(?:[/\\]|$|[\s'\"])")
@@ -2829,7 +4866,13 @@ def _references_sensitive_path(text: str) -> bool:
if norm != text and _SENSITIVE_PATH_RE.search(norm):
return True
debracket = _GLOB_BRACKET_RE.sub(lambda m: m.group(1)[0], text)
- return bool(debracket != text and _SENSITIVE_PATH_RE.search(debracket))
+ # The three checks the conflicting revisions both spelled out here (parent traversal, and the
+ # pattern against the raw and slash-normalised text) already returned above, so only the
+ # credential test and the de-bracketed rewrite are left to decide.
+ return bool(
+ _references_studio_credential(text)
+ or (debracket != text and _SENSITIVE_PATH_RE.search(debracket))
+ )
def _pattern_matches_dir(pattern: str, target: str) -> bool:
@@ -4200,6 +6243,27 @@ _MCP_CREDENTIAL_KEY_RE = re.compile(
)
+def _mcp_arguments_reference_studio_credential(arguments) -> bool:
+ """True if an MCP call's arguments point at Studio's auth directory. An MCP server runs outside
+ the terminal sandbox, so a filesystem server would read the credential the local tools refuse.
+ Prose fields are skipped for the same reason they are below: an issue body that mentions the
+ filename is text to store, not a file to open."""
+
+ def walk(value, is_prose: bool = False) -> bool:
+ if isinstance(value, str):
+ return False if is_prose else _references_studio_credential(value)
+ if isinstance(value, dict):
+ return any(
+ walk(v, is_prose or (isinstance(k, str) and k.lower() in _MCP_PROSE_KEYS))
+ for k, v in value.items()
+ )
+ if isinstance(value, (list, tuple)):
+ return any(walk(v, is_prose) for v in value)
+ return False
+
+ return walk(arguments)
+
+
def _mcp_arguments_reference_sensitive(arguments) -> bool:
"""True if any string in an MCP call's arguments names a credential path, a credential/secret
environment variable, or a cloud-metadata host."""
@@ -9525,6 +11589,10 @@ def _edit_file(
disable_sandbox: bool = False,
) -> str:
"""Replace exact strings in a file. See the notes above."""
+ # The receipt echoes a window of the file back, so an edit is a read, and Bypass Permissions
+ # lifts the containment that would otherwise keep it out.
+ if _references_studio_credential(str(arguments.get("path") or "")):
+ return _STUDIO_CREDENTIAL_BLOCKED
edits, error = _edit_file_parse_edits(arguments.get("edits"))
if error:
return error
@@ -9533,6 +11601,12 @@ def _edit_file(
)
if error:
return error
+ # Again on the RESOLVED path: the sandbox is a sibling of the auth directory, so
+ # `../../auth/agents/...` only names it once the resolve has joined the two.
+ if _references_studio_credential(target) or _references_studio_credential(
+ os.path.realpath(target)
+ ):
+ return _STUDIO_CREDENTIAL_BLOCKED
name = os.path.basename(target)
# Decided before the no-op check below, not after: both strings empty is the documented way to create __init__.py
# or .gitkeep, and read as identical, nothing to change it was refused, leaving no way to write a zero-byte file.
@@ -10502,6 +12576,9 @@ def execute_tool(
if name == "render_html":
return _fit_result_to_room(_render_html_result(arguments), name)
if name.startswith(MCP_TOOL_PREFIX):
+ # An MCP server is not inside the terminal sandbox, so the local refusal has to hold here too.
+ if _mcp_arguments_reference_studio_credential(arguments):
+ return _STUDIO_CREDENTIAL_BLOCKED
try:
_, server_id, tool_name = name.split("__", 2)
except ValueError:
@@ -14706,7 +16783,9 @@ def _split_frontend_suffix(text: str, name: "str | None") -> "tuple[str, str]":
from .tool_loop_controller import strip_result_for_model
try:
- body = strip_result_for_model(text, name)
+ # Unredacted: this subtracts the body to recover the envelope, so the strip has to remove a
+ # suffix and nothing else. The model-bound copy is masked in `model_message`.
+ body = strip_result_for_model(text, name, redact = False)
except Exception:
logger.debug("frontend suffix split failed", exc_info = True)
return text, ""
@@ -15909,6 +17988,13 @@ def _python_exec(
if not code or not code.strip():
return "No code provided."
+ # Refused in every mode, as in _bash_exec. Relative too: the cwd is a sibling of the auth dir.
+ _guard_workdir = _tool_workdir_for_guard(session_id) if _needs_a_workdir(code) else None
+ if _references_studio_credential_here(code, _guard_workdir) or _python_builds_a_credential_path(
+ code, _guard_workdir
+ ):
+ return _STUDIO_CREDENTIAL_BLOCKED
+
# Validate imports and code safety (skipped when the sandbox is disabled)
if not disable_sandbox:
error = _check_code_safety(code)
@@ -16078,6 +18164,13 @@ def _bash_exec(
if not command or not command.strip():
return "No command provided."
+ # Refused in every mode, unlike the blocklist below, which Bypass Permissions opts out of: this
+ # install's live bearer would be replayed to whatever provider is serving the turn.
+ if _references_studio_credential_here(
+ command, _tool_workdir_for_guard(session_id) if _needs_a_workdir(command) else None
+ ):
+ return _STUDIO_CREDENTIAL_BLOCKED
+
# Block dangerous commands (skipped when the sandbox is disabled)
if not disable_sandbox:
blocked = _find_blocked_commands(command)
diff --git a/studio/backend/tests/test_permission_mode.py b/studio/backend/tests/test_permission_mode.py
index cfdaab65db2..fa7cfbe88f8 100644
--- a/studio/backend/tests/test_permission_mode.py
+++ b/studio/backend/tests/test_permission_mode.py
@@ -363,6 +363,13 @@ def test_terminal_classifier(command, unsafe):
("cat ~/.ssh/id_rsa", True),
("cat ~/.aws/credentials", True),
("cat /proc/1/environ", True),
+ # Studio's own auth dir: the cached CLI bearer, the bootstrap password and auth.db live there.
+ ("cat ~/.unsloth/studio/auth/.cli_api_key_cli_99bb88401742", True),
+ ("ls -a ~/.unsloth/studio/auth", True),
+ ("cat /custom/home/auth/.bootstrap_password", True),
+ # ...while an application's own auth code is ordinary work.
+ ("grep -rn auth src/", False),
+ ("cat src/auth.py", False),
# --- prompt: sandbox-escape via env that hijacks loading/lookup ---
("LD_PRELOAD=/tmp/x.so ls", True),
# --- prompt: a verb hidden behind an assignment / default param ---
@@ -1408,6 +1415,10 @@ def test_terminal_high_risk_classifier(command, high_risk):
# --- prompt: credential-path read/write ---
("open('/etc/shadow').read()", True),
("open('/root/.ssh/id_rsa').read()", True),
+ ("Path('/custom/home/auth/.cli_api_key_cli_99bb88401742').read_text()", True),
+ ("open('/home/u/.unsloth/studio/auth/auth.db', 'rb').read()", True),
+ # an application's own auth module stays ordinary
+ ("import auth\nprint(auth.__file__)", False),
# --- prompt: destructive filesystem deletion (parity with terminal rm) ---
("import os; os.remove('important.py')", True),
("import os; os.unlink('x')", True),
@@ -2489,6 +2500,7 @@ def test_blender_cli_summaries_require_approval(tool, approval):
({"path": "/etc/passwd"}, True), # read-named tool at a credential path
({"path": "../../.ssh/id_rsa"}, True),
({"nested": {"file": "~/.aws/credentials"}}, True),
+ ({"path": "~/.unsloth/studio/auth/.cli_api_key_cli_99bb88401742"}, True),
({"name": "OPENAI_API_KEY"}, True), # explicit credential env-var read
({"name": "AWS_SECRET_ACCESS_KEY"}, True),
({"key": "DATABASE_PASSWORD"}, True),
diff --git a/studio/backend/tests/test_studio_auth_guard.py b/studio/backend/tests/test_studio_auth_guard.py
new file mode 100644
index 00000000000..9c72144d661
--- /dev/null
+++ b/studio/backend/tests/test_studio_auth_guard.py
@@ -0,0 +1,2411 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Studio's own credentials are not tool input.
+
+The CLI caches the raw API key at ``$STUDIO_HOME/auth/.cli_api_key_<stem>_<digest>`` so a relaunch
+reuses it instead of minting another. Tool subprocesses run as the backend's OS user, so that file's
+0600 mode is no boundary against them: a provider that emits ``cat <auth dir>/.cli_api_key_*`` would
+get a live bearer replayed into its next inference request, and that key authenticates the terminal
+and python tools in turn.
+
+Two independent guards, so neither one has to be complete:
+ - the executors refuse a call that names the auth directory, in every permission mode (Bypass
+ Permissions included, unlike the command blocklist);
+ - an API key or desktop secret that reaches a result by any other route is masked on the
+ model-bound copy only, leaving the tool card the user is looking at untouched. The remaining
+ credentials in that directory (bootstrap password, llama stream key, auth.db) have no
+ recognisable shape to mask, so for those the executor refusal is the guard.
+"""
+
+from __future__ import annotations
+
+import os
+
+import pytest
+
+from core.inference import tools
+from core.inference.mcp_client import MCP_TOOL_PREFIX
+from core.inference.tool_loop_controller import redact_studio_credentials, strip_result_for_model
+from core.inference.tools import is_high_risk_tool_call
+
+_SESSION = "studio-auth-guard-session"
+# Shaped like the real thing (auth/storage.py: API_KEY_PREFIX + secrets.token_hex(16)), but minted here.
+_FAKE_KEY = "sk-unsloth-" + "a1b2c3d4" * 4
+
+
+def studio_home(monkeypatch, tmp_path):
+ """A studio home with `auth/` and this session's sandbox, and the marker cache reset around it.
+
+ Every guard test needs the same four lines of setup and the same teardown, so they live here.
+ """
+ home = tmp_path / "studio-home"
+ (home / "auth").mkdir(parents = True)
+ (home / "sandbox" / _SESSION).mkdir(parents = True)
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(home))
+ monkeypatch.delenv("STUDIO_HOME", raising = False)
+ monkeypatch.setattr(tools, "_studio_auth_markers_cache", None)
+ try:
+ yield home
+ finally:
+ tools._studio_auth_markers_cache = None
+
+
+_CREDENTIAL_COMMANDS = (
+ "cat ~/.unsloth/studio/auth/.cli_api_key_cli_99bb88401742",
+ "ls -a ~/.unsloth/studio/auth",
+ "cat /home/u/.unsloth/studio/auth/.bootstrap_password",
+ "sqlite3 /home/u/.unsloth/studio/auth/auth.db .dump",
+ # `unsloth start` caches the coding-agent keys here, in the same directory.
+ "cat /home/u/.unsloth/studio/auth/agent_api_key.json",
+ "cd ~/.unsloth/studio/auth && cat agent_api_key.json",
+)
+_CREDENTIAL_CODE = (
+ "from pathlib import Path\n"
+ "print(Path('/home/u/.unsloth/studio/auth/.cli_api_key_cli_99bb88401742').read_text())",
+ "print(open('/home/u/.unsloth/studio/auth/.desktop_secret').read())",
+ "import os\nprint(os.listdir('/home/u/.unsloth/studio/auth'))",
+)
+
+
[email protected](autouse = True)
+def _no_subprocess(monkeypatch):
+ """A refusal must happen before anything is spawned, so make a spawn an outright failure."""
+
+ def _boom(*args, **kwargs):
+ raise AssertionError("a blocked call reached the subprocess layer")
+
+ monkeypatch.setattr(tools.subprocess, "Popen", _boom)
+ monkeypatch.setattr(tools.subprocess, "run", _boom)
+
+
[email protected]("command", _CREDENTIAL_COMMANDS)
[email protected]("disable_sandbox", [False, True])
+def test_terminal_refuses_studio_credentials(command, disable_sandbox):
+ result = tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = disable_sandbox)
+ assert result == tools._STUDIO_CREDENTIAL_BLOCKED
+
+
[email protected]("code", _CREDENTIAL_CODE)
[email protected]("disable_sandbox", [False, True])
+def test_python_refuses_studio_credentials(code, disable_sandbox):
+ result = tools._python_exec(code, None, 30, _SESSION, disable_sandbox = disable_sandbox)
+ assert result == tools._STUDIO_CREDENTIAL_BLOCKED
+
+
+def test_refusal_names_no_path_and_no_value():
+ result = tools._bash_exec(_CREDENTIAL_COMMANDS[0], None, 30, _SESSION)
+ assert ".cli_api_key" not in result
+ assert "auth" in result # it still says what it refused, in prose
+
+
+ "command",
+ [
+ "grep -rn auth src/",
+ "cat src/auth.py",
+ "ls -la",
+ "python -m pytest tests/ -q",
+ # A bare filename is a string, not a read: `print('agent_api_key.json')` was refused.
+ "echo agent_api_key.json",
+ "grep -rn agent_api_key.json src/",
+ ],
+)
+def test_ordinary_commands_are_not_blocked(command):
+ # Not asserting they run (the fixture forbids spawning); only that the credential guard, which
+ # returns before anything else, lets them past.
+ assert not tools._references_studio_credential(command)
+ assert tools._STUDIO_CREDENTIAL_BLOCKED != command
+
+
+def test_a_custom_studio_home_is_covered(monkeypatch, tmp_path):
+ auth_dir = tmp_path / "studio-home" / "auth"
+ auth_dir.mkdir(parents = True)
+ key_file = auth_dir / ".cli_api_key_cli_99bb88401742"
+ key_file.write_text(_FAKE_KEY + "\n", encoding = "utf-8")
+
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "studio-home"))
+ monkeypatch.setattr(tools, "_studio_auth_markers_cache", None)
+ try:
+ # The basename is enough on its own; the resolved root covers a directory listing, which
+ # carries no Studio-specific filename at all.
+ assert tools._references_studio_credential(f"cat {key_file}")
+ assert tools._references_studio_credential(f"ls -a {auth_dir}")
+ assert tools._bash_exec(f"cat {key_file}", None, 30, _SESSION) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ finally:
+ tools._studio_auth_markers_cache = None
+
+
+def test_the_auth_dir_named_relative_to_a_cd_into_the_studio_root(monkeypatch, tmp_path):
+ # `cd <studio home> && ls -a auth` carries no absolute auth path and no Studio-specific
+ # basename, so it is only recognisable through the root the same command entered.
+ import utils.paths.storage_roots as roots
+
+ home = tmp_path / "studio-home"
+ monkeypatch.setattr(roots, "auth_root", lambda: home / "auth")
+ monkeypatch.setattr(tools, "_studio_auth_markers_cache", None)
+ try:
+ assert tools._references_studio_credential(f"cd {home} && ls -a auth")
+ assert tools._references_studio_credential(f'cd "{home}"; ls auth/')
+ # ...while merely naming the studio root is ordinary work: reading your own Studio logs,
+ # or grepping them for the word, must not turn into a refusal.
+ assert not tools._references_studio_credential(f"grep -rn auth {home}/logs/studio.log")
+ assert not tools._references_studio_credential(f"ls {home}/models && grep -c auth app.log")
+ assert not tools._references_studio_credential(f"cd {home} && ls models")
+ # The root has to END where it matched, or continue into `auth`. A path that merely STARTS
+ # with it is a different directory: without the boundary both of these were refused in
+ # every permission mode.
+ assert not tools._references_studio_credential(f"cd {home}/models && grep auth README")
+ assert not tools._references_studio_credential(f"cd {home}-backup && ls auth")
+ # ...and the boundary must not cost the real thing.
+ assert tools._references_studio_credential(f"cd {home}/auth && ls")
+ finally:
+ tools._studio_auth_markers_cache = None
+
+
+def test_equivalent_spellings_of_the_auth_path_are_all_covered(monkeypatch, tmp_path):
+ # The OS opens every one of these as the same file, so matching only the tidiest spelling left
+ # the others walking straight past the guard.
+ home = tmp_path / "studio-home"
+ (home / "auth").mkdir(parents = True)
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(home))
+ monkeypatch.setattr(tools, "_studio_auth_markers_cache", None)
+ try:
+ for spelling in (
+ f"{home}/auth/auth.db",
+ f"{home}/bin/../auth/auth.db",
+ f"{home}/./auth/auth.db",
+ f"{home}//auth//auth.db",
+ f"{home}/a/b/../../auth/auth.db",
+ str(home).replace("/", "\\") + "\\.\\auth\\auth.db",
+ # Mixed separators with a doubled slash, which is what a Windows runner produces. The
+ # canonical form collapsed `//` only in the slash-normalised candidate, built from the
+ # raw text, so this spelling matched neither until the collapse moved into the fold.
+ str(home).replace("/", "\\") + "//auth//auth.db",
+ ):
+ assert tools._references_studio_credential(f"cat {spelling}"), spelling
+ # Cancelling `..` must not invent a match that was never there.
+ assert not tools._references_studio_credential(f"cat {home}/auth-backup/../notes.txt")
+ finally:
+ tools._studio_auth_markers_cache = None
+
+
+def test_the_environment_variable_spelling_of_the_studio_home(monkeypatch, tmp_path):
+ # `cat $STUDIO_HOME/auth/auth.db` reaches the same file as the resolved path: both studio-home
+ # variables survive into the tool subprocess environment, so the shell expands this for real.
+ home = tmp_path / "studio-home"
+ (home / "auth").mkdir(parents = True)
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(home))
+ monkeypatch.setattr(tools, "_studio_auth_markers_cache", None)
+ try:
+ for spelling in (
+ 'cat "$STUDIO_HOME/auth/auth.db"',
+ "cat ${UNSLOTH_STUDIO_HOME}/auth/auth.db",
+ "ls -a $UNSLOTH_STUDIO_HOME/auth",
+ r"type %UNSLOTH_STUDIO_HOME%\auth\.desktop_secret",
+ r"Get-Content $env:STUDIO_HOME\auth\auth.db",
+ 'cd "$STUDIO_HOME" && ls auth',
+ 'sqlite3 "$UNSLOTH_STUDIO_HOME"/auth/auth.db "select jwt_secret from auth_user"',
+ ):
+ assert tools._references_studio_credential(spelling), spelling
+ # Naming the variable without going into the auth directory stays ordinary work.
+ assert not tools._references_studio_credential("export STUDIO_HOME=/tmp/studio")
+ assert not tools._references_studio_credential("ls $STUDIO_HOME/models")
+ assert not tools._references_studio_credential("grep -rn auth $STUDIO_HOME/logs/studio.log")
+ finally:
+ tools._studio_auth_markers_cache = None
+
+
+def test_a_studio_home_whose_name_contains_a_space(monkeypatch, tmp_path):
+ # macOS installs under "Application Support" put a space in the path, and a shell spells that
+ # with a backslash escape. Read as a separator it split the directory name and slipped past.
+ home = tmp_path / "Studio Data"
+ (home / "auth").mkdir(parents = True)
+ escaped = str(home).replace(" ", "\\ ")
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(home))
+ monkeypatch.setattr(tools, "_studio_auth_markers_cache", None)
+ try:
+ # A shell concatenates adjacent fragments, so the quote sits INSIDE the path here and no
+ # marker could span it until the candidate was dequoted.
+ assert tools._references_studio_credential(f'sqlite3 "{home}"/auth/auth.db "select 1"')
+ assert tools._references_studio_credential(f"cat '{home}'/auth/.desktop_secret")
+ assert not tools._references_studio_credential(f'cat "{home}"/models/notes.txt')
+ assert tools._references_studio_credential(f"cat {escaped}/auth/auth.db")
+ assert tools._references_studio_credential(f"cd {escaped} && ls -a auth")
+ assert tools._references_studio_credential(f'cat "{home}/auth/auth.db"')
+ assert not tools._references_studio_credential(f"cat {escaped}/authors/notes.txt")
+ finally:
+ tools._studio_auth_markers_cache = None
+
+
+def test_traversal_out_of_the_sandbox_into_the_auth_dir(monkeypatch, tmp_path):
+ # The tool cwd is <studio home>/sandbox/<session>, a sibling of the auth directory, so
+ # `open('../../auth/auth.db')` reads the protected database while naming neither the directory
+ # nor a credential basename. Bypass Permissions skips the blocklist, so only this guard is left.
+ home = tmp_path / "studio-home"
+ (home / "auth").mkdir(parents = True)
+ (home / "auth" / "auth.db").write_text("not a real database", encoding = "utf-8")
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(home))
+ monkeypatch.setattr(tools, "_studio_auth_markers_cache", None)
+ try:
+ for call, args in (
+ (tools._bash_exec, "cat ../../auth/auth.db"),
+ (tools._python_exec, "print(open('../../auth/auth.db').read())"),
+ ):
+ assert call(args, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), args
+ # Traversal that lands anywhere else is ordinary work: it is not refused. (The suite blocks
+ # real subprocesses, so the refusal string is what is checked, not the output.)
+ for ordinary in ("cat ../notes.txt", "ls ../../models", "cp ../a.txt ../../b.txt"):
+ assert (
+ tools._bash_exec(
+ ordinary,
+ None,
+ 30,
+ _SESSION,
+ disable_sandbox = True,
+ )
+ != tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+ finally:
+ tools._studio_auth_markers_cache = None
+
+
+def test_mcp_call_at_the_auth_dir_is_refused():
+ name = f"{MCP_TOOL_PREFIX}fs__read_file"
+ args = {"path": "~/.unsloth/studio/auth/.cli_api_key_cli_99bb88401742"}
+ assert tools._mcp_arguments_reference_studio_credential(args) is True
+ # Nested and list-shaped arguments reach the same answer.
+ assert (
+ tools._mcp_arguments_reference_studio_credential(
+ {"files": [{"path": "/home/u/.unsloth/studio/auth/.bootstrap_password"}]}
+ )
+ is True
+ )
+ # Prose stays prose: an issue body that mentions the file is text, not a read.
+ assert (
+ tools._mcp_arguments_reference_studio_credential(
+ {"body": "the key is cached in .cli_api_key_cli_99bb88401742"}
+ )
+ is False
+ )
+ # And auto mode would have paused on it even before the refusal.
+ assert is_high_risk_tool_call(name, args) is True
+
+
+def test_an_mcp_call_at_the_auth_dir_never_reaches_the_server(monkeypatch):
+ # Through `execute_tool`, so the refusal is asserted where it has to happen: BEFORE dispatch.
+ # Deleting the refusal from that function left every test here passing, because the test above
+ # checks the predicate and the classifier and never the dispatch.
+ name = f"{MCP_TOOL_PREFIX}fs__read_file"
+ reached = []
+
+ def _never(*args, **kwargs):
+ reached.append(args)
+ raise AssertionError("the MCP server was reached for a credential path")
+
+ for attribute in ("_mcp_server_for_tool", "_call_mcp_tool", "call_mcp_tool"):
+ if hasattr(tools, attribute):
+ monkeypatch.setattr(tools, attribute, _never, raising = False)
+ result = tools.execute_tool(
+ name, {"path": "~/.unsloth/studio/auth/.cli_api_key_cli_99bb88401742"}, _SESSION
+ )
+ assert result == tools._STUDIO_CREDENTIAL_BLOCKED
+ assert not reached
+
+
+def test_auto_mode_prompts_on_studio_credential_reads():
+ assert is_high_risk_tool_call("terminal", {"command": _CREDENTIAL_COMMANDS[0]}) is True
+ assert is_high_risk_tool_call("python", {"code": _CREDENTIAL_CODE[0]}) is True
+
+
+def test_a_key_in_a_result_never_reaches_the_model():
+ result = f"UNSLOTH_API_KEY={_FAKE_KEY}\nok\n"
+ masked = strip_result_for_model(result, "terminal")
+ assert _FAKE_KEY not in masked
+ assert masked == "UNSLOTH_API_KEY=[redacted]\nok\n"
+
+
+def test_redaction_is_idempotent_and_leaves_other_output_alone():
+ once = redact_studio_credentials(f"a {_FAKE_KEY} b {_FAKE_KEY}")
+ assert redact_studio_credentials(once) == once
+ assert _FAKE_KEY not in once
+ plain = "model sk-unsloth loaded; 12 files; sk-openai-style-name; desktop-app starts"
+ assert redact_studio_credentials(plain) == plain
+ # The desktop credential (`desktop-` + token_urlsafe(48)) goes the same way.
+ desktop = "desktop-" + "Ab3_-x9Z" * 8
+ assert desktop not in redact_studio_credentials(f"secret={desktop}\n")
+ # A key cut in half by the window fit is still credential material, so it is masked too, while
+ # prose about the prefix itself reads through.
+ assert redact_studio_credentials(_FAKE_KEY[:24]) == "[redacted]"
+ assert redact_studio_credentials("keys look like sk-unsloth-<hex>") == (
+ "keys look like sk-unsloth-<hex>"
+ )
+ # The token is hex, so a name that only shares the prefix is left alone. On the alphanumeric
+ # pattern this repository's own `sk-unsloth-internal-workflow` came back as
+ # `[redacted]-workflow` in source listings and test output.
+ for not_a_key in (
+ "container name sk-unsloth-internal-workflow here",
+ "sk-unsloth-zzzzzzzzzz",
+ ):
+ assert redact_studio_credentials(not_a_key) == not_a_key
+
+
+def test_the_envelope_split_still_sees_a_suffix_only_strip():
+ # _split_frontend_suffix subtracts the stripped body from the text, so masking has to stay off
+ # that path or the envelope-aware truncation silently degrades to cutting the whole result.
+ body = f"line with {_FAKE_KEY}"
+ suffix = '\n__FILES__:[{"name": "a.txt", "size": 3}]'
+ kept, envelope = tools._split_frontend_suffix(body + suffix, "terminal")
+ assert kept == body
+ assert envelope == suffix
+
+
+def test_edit_file_cannot_reach_the_auth_dir_even_with_the_sandbox_off():
+ result = tools._edit_file(
+ {
+ "path": "~/.unsloth/studio/auth/.bootstrap_password",
+ "edits": [{"old_string": "a", "new_string": "b"}],
+ },
+ session_id = _SESSION,
+ disable_sandbox = True,
+ )
+ assert result == tools._STUDIO_CREDENTIAL_BLOCKED
+
+
+def test_the_user_facing_tool_card_is_masked_too():
+ # Originally the mask was on the model path only, so the card kept the raw text. That is the
+ # replay hole: the frontend PERSISTS this payload and serializes the stored value back into a
+ # role="tool" message on the user's next turn, so the key was hidden from the continuation and
+ # then sent to the provider one turn later. The card shows the user their own key, so masking
+ # it costs them nothing and closes the stored copy as well.
+ from core.inference.tool_loop_controller import ToolCallCompletion, ToolCallDecision
+
+ decision = ToolCallDecision(
+ action = "execute",
+ tool_name = "terminal",
+ arguments = {"command": "echo $UNSLOTH_API_KEY"},
+ tool_call_id = "call-1",
+ )
+ completion = ToolCallCompletion(
+ decision = decision, result = _FAKE_KEY, is_error = False, executed = True
+ )
+ assert _FAKE_KEY not in completion.tool_end_payload()["result"]
+ assert _FAKE_KEY not in completion.tool_end_event()["result"]
+ assert _FAKE_KEY not in completion.tool_message()["content"]
+
+
+def test_the_cli_cache_path_still_matches_what_the_guard_blocks():
+ # Ties the guard to the CLI's actual filename, so renaming one without the other fails here.
+ import importlib.util
+
+ spec = importlib.util.find_spec("unsloth_cli.commands.studio")
+ if spec is None or not spec.origin or not os.path.isfile(spec.origin):
+ pytest.skip("unsloth_cli is not importable from the backend test env")
+ with open(spec.origin, encoding = "utf-8") as f:
+ source = f.read()
+ assert 'CLI_API_KEY_FILE_PREFIX = ".cli_api_key_"' in source
+ assert 'BOOTSTRAP_PASSWORD_FILE = ".bootstrap_password"' in source
+
+
+def test_tool_end_payload_masks_a_leaked_studio_key():
+ """The frontend persists this payload and replays it into a role="tool" message on the user's
+ next turn, so redacting only the in-memory continuation hides the key this turn and sends it
+ the next one."""
+ from core.inference.tool_loop_controller import ToolLoopController
+
+ key = "sk-unsloth-" + "A" * 48
+ controller = ToolLoopController(tools = [{"type": "function", "function": {"name": "terminal"}}])
+ decision = controller.prepare_call(
+ {"id": "call_0", "function": {"name": "terminal", "arguments": '{"command":"env"}'}}
+ )
+ completion = controller.record_result(decision, f"UNSLOTH_API_KEY={key}")
+
+ assert key not in completion.tool_end_payload()["result"]
+ assert key not in completion.tool_end_event()["result"]
+ assert key not in completion.model_message()["content"]
+
+
+def _stream_chunks(chunks):
+ """Feed `chunks` through the live-stream masking exactly as the generator does."""
+ from core.inference.tool_loop_controller import redact_studio_credentials
+ from core.inference.tool_stream_exec import _hold_back_partial_secret
+
+ carry = ""
+ out = []
+ for chunk in chunks:
+ combined = carry + chunk
+ split = _hold_back_partial_secret(combined)
+ out.append(redact_studio_credentials(combined[:split]))
+ carry = combined[split:]
+ out.append(redact_studio_credentials(carry))
+ return "".join(out)
+
+
+def test_the_real_stream_generator_masks_a_key_across_chunk_boundaries():
+ # Driven through `stream_tool_execution` itself, not through a copy of its masking. A reviewer
+ # deleted the redaction from both of that generator's `_masked` return paths and all 94 tests
+ # here still passed, because the test below re-implements the loop instead of running it.
+ from core.inference.tool_stream_exec import stream_tool_execution
+ key = "sk-unsloth-0123456789abcdef0123456789abcdef"
+ for chunks in (
+ [key],
+ [key[:15], key[15:]],
+ ["sk-unslo", key[8:]],
+ ["out ", key[:20], key[20:], " tail"],
+ ):
+
+ def invoke(emit, _chunks = chunks):
+ for chunk in _chunks:
+ emit(chunk)
+ return "".join(_chunks)
+
+ generator = stream_tool_execution(invoke, tool_name = "terminal", tool_call_id = "c1")
+ streamed = []
+ try:
+ while True:
+ event = next(generator)
+ if event.get("type") == "tool_output":
+ streamed.append(event.get("text") or "")
+ except StopIteration as stop:
+ returned = stop.value
+ rendered = "".join(streamed)
+ assert key not in rendered, chunks
+ assert "[redacted]" in rendered, chunks
+ # The RETURNED result is what the card and the model get; it is masked by the loop
+ # controller, so this asserts only that the generator hands back what the tool produced.
+ assert isinstance(returned, str)
+
+
+def test_a_key_is_masked_in_the_live_stream_however_it_is_chunked():
+ # tool_end is not the first thing the card paints. The live tool_output chunks are, so a key
+ # that is only masked at the end is still rendered in full and persisted that way. Found by
+ # screenshotting the card rather than by a unit test: the tool_end payload said [redacted]
+ # while the output block above it showed the key.
+ key = "sk-unsloth-0123456789abcdef0123456789abcdef"
+ for chunks in (
+ [key],
+ [key[:15], key[15:]], # split mid-token
+ ["sk-unslo", key[8:]], # split mid-PREFIX, which no per-chunk regex can see
+ ["out ", key[:20], key[20:], " tail"],
+ [("a" * 500) + key],
+ ):
+ rendered = _stream_chunks(chunks)
+ assert key not in rendered, chunks
+ assert "[redacted]" in rendered, chunks
+
+ # Held-back text that turns out to be ordinary must still be emitted, not swallowed.
+ assert _stream_chunks(["hello ", "sk-unslo"]) == "hello sk-unslo"
+ assert _stream_chunks(["plain output\n"]) == "plain output\n"
+
+ # A terminator anywhere after the token ends it, so a chunk that carries on past a key is
+ # emitted at once. Read only at the ends, `<key> done` looked open and held this chunk and
+ # every chunk after it back until the tool finished, which starved the stream of output.
+ from core.inference.tool_stream_exec import _hold_back_partial_secret
+
+ for text in (
+ key + " done",
+ "out " + key + " more text here",
+ key + "\nnext line",
+ ):
+ assert _hold_back_partial_secret(text) == len(text), text
+ # A token still open at the end of the chunk is still held back.
+ assert _hold_back_partial_secret("out " + key) == 4
+
+
+def test_the_auth_path_assembled_through_a_shell_variable(monkeypatch, tmp_path):
+ # Bypass Permissions keeps STUDIO_HOME in the child env, so the shell resolves
+ # `r=$STUDIO_HOME; sqlite3 "$r/auth/auth.db"` to the protected database while the literal text
+ # names neither the directory nor a credential basename. auth.db carries no `sk-unsloth-`
+ # prefix either, so the result redactor cannot mask the JWT secret on the way back out.
+ home = tmp_path / "studio-home"
+ (home / "auth").mkdir(parents = True)
+ sandbox = home / "sandbox" / "sess1"
+ sandbox.mkdir(parents = True)
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(home))
+ monkeypatch.setenv("STUDIO_HOME", str(home))
+ monkeypatch.setattr(tools, "_studio_auth_markers_cache", None)
+ try:
+ for command in (
+ 'r=$STUDIO_HOME; sqlite3 "$r/auth/auth.db" "select jwt_secret from auth_user"',
+ "r=$STUDIO_HOME; cat $r/auth/auth.db",
+ f"h={home}; cat $h/auth/auth.db",
+ 'sqlite3 "$STUDIO_HOME/auth/auth.db"',
+ ):
+ assert tools._references_studio_credential_here(command, str(sandbox)), command
+
+ # Naming the studio home is ordinary work, and so is any other assignment.
+ for command in (
+ "r=$STUDIO_HOME; ls $r/models",
+ "d=/tmp; cat $d/notes.txt",
+ "echo $HOME",
+ "git commit -m fix",
+ ):
+ assert not tools._references_studio_credential_here(command, str(sandbox)), command
+ finally:
+ tools._studio_auth_markers_cache = None
+
+
+def test_a_python_path_built_in_pieces_is_still_the_auth_dir(studio_home):
+ home = studio_home
+ # `os.path.join("..", "..", "auth", "auth.db")` names the protected database in pieces, so the
+ # text scan sees four ordinary strings and no path at all, and Bypass Permissions has already
+ # skipped the code analyzer. auth.db carries no prefix the result mask can key on either.
+ for code in (
+ 'import os\nprint(open(os.path.join("..", "..", "auth", "auth.db")).read())',
+ 'from pathlib import Path\nprint((Path("..") / ".." / "auth" / "auth.db").read_text())',
+ # as_posix(), not str(): a Windows path inside a python string literal turns its
+ # separators into escapes (\U, \A, \t), so the snippet stops being the path it names.
+ f'import os\nprint(open(os.path.join("{home.as_posix()}", "auth", "auth.db")).read())',
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+ # A path built the same way that lands anywhere else is ordinary work.
+ for code in (
+ 'import os\nprint(os.path.join("..", "data", "x.csv"))',
+ 'import os\nprint(os.path.join("src", "auth", "views.py"))',
+ 'print("auth")',
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+
+
+def test_edit_file_is_checked_on_the_resolved_target(monkeypatch, tmp_path):
+ # The raw argument can be relative, and the resolve joins it to the sandbox workdir, a sibling
+ # of the auth directory. Bypass Permissions also lifts containment, and the receipt echoes a
+ # window of the file back, so an edit there is a read.
+ home = tmp_path / "studio-home"
+ agent_dir = home / "auth" / "agents" / "opencode"
+ agent_dir.mkdir(parents = True)
+ (agent_dir / "opencode.json").write_text("{}", encoding = "utf-8")
+ (home / "sandbox" / _SESSION).mkdir(parents = True)
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(home))
+ monkeypatch.setattr(tools, "_studio_auth_markers_cache", None)
+ try:
+ assert (
+ tools._edit_file(
+ {
+ "path": "../../auth/agents/opencode/opencode.json",
+ "edits": [{"old_string": "{}", "new_string": "{ }"}],
+ },
+ _SESSION,
+ disable_sandbox = True,
+ )
+ == tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ # An ordinary relative edit in the sandbox still goes through.
+ assert "Created" in tools._edit_file(
+ {"path": "notes.txt", "edits": [{"old_string": "", "new_string": "hi"}]},
+ _SESSION,
+ disable_sandbox = True,
+ )
+ finally:
+ tools._studio_auth_markers_cache = None
+
+
+def test_a_cd_earlier_in_the_command_moves_what_a_relative_path_means(monkeypatch, tmp_path):
+ # `cd ../..` from the session sandbox lands on the Studio root, and the `auth/auth.db` that
+ # follows is then the protected database under a name that matches nothing on its own. Resolving
+ # the traversal and the later relative path INDEPENDENTLY against the original cwd misses it:
+ # neither half reaches the auth directory, only the composition does.
+ home = tmp_path / "studio-home"
+ (home / "auth").mkdir(parents = True)
+ sandbox = home / "sandbox" / "sess1"
+ sandbox.mkdir(parents = True)
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(home))
+ monkeypatch.setattr(tools, "_studio_auth_markers_cache", None)
+ try:
+ for command in (
+ "cd ../..; python -c 'import sqlite3; sqlite3.connect(\"auth/auth.db\")'",
+ "cd ../.. && cat auth/auth.db",
+ "cd ../../ && cat auth/.cli_api_key_cli_99bb88401742",
+ f"cd {home} && cat auth/auth.db",
+ ):
+ assert tools._references_studio_credential_here(command, str(sandbox)), command
+
+ # Moving around is ordinary, and an `auth` directory the user owns DEEPER in the sandbox is
+ # still theirs: after `cd subdir` that path is sandbox/subdir/auth, not Studio's.
+ for command in (
+ "cd ../.. && ls models",
+ "cd ../.. && cat README.md",
+ "cd subdir && cat auth/config.json",
+ "cd build && make",
+ ):
+ assert not tools._references_studio_credential_here(command, str(sandbox)), command
+ finally:
+ tools._studio_auth_markers_cache = None
+
+
+def test_a_chain_of_cds_that_ends_inside_the_auth_dir(studio_home):
+ home = studio_home
+ # `cd ../..; cd auth; sqlite3 auth.db` never writes a path with a separator in it, so every
+ # token in it reads as an ordinary filename and only the walked directory itself gives it away.
+ for command in (
+ 'cd ../..; cd auth; sqlite3 auth.db "select jwt_secret from auth_user"',
+ "cd ../.. && cd auth && cat .cli_api_key_cli_1",
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ for ordinary in ("cd ../..; cd models; ls", "cd .. && cd data && cat x.csv"):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_traversal_through_the_proc_cwd_symlink(studio_home):
+ home = studio_home
+ # The kernel resolves /proc/self/cwd to the session sandbox BEFORE applying the `..` that
+ # follows, so a lexical normpath reads this as /proc/self/auth/auth.db and misses the file the
+ # call actually opens.
+ for code in (
+ "print(open('/proc/self/cwd/../../auth/auth.db').read())",
+ "import sqlite3\nprint(sqlite3.connect('/proc/self/cwd/../../auth/auth.db'))",
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+ assert (
+ tools._bash_exec(
+ "cat /proc/self/cwd/../../auth/.desktop_secret",
+ None,
+ 30,
+ _SESSION,
+ disable_sandbox = True,
+ )
+ == tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ # Reading procfs itself is ordinary work.
+ for ordinary in ("cat /proc/self/status", "ls /proc/self/cwd"):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_padding_a_command_with_cds_does_not_spend_the_walk(studio_home):
+ home = studio_home
+ # The walk counted `cd` commands, so eight no-op ones filled the budget and the two that
+ # entered the auth directory after them were never looked at.
+ padded = ("cd .; " * 8) + 'cd ../..; cd auth; sqlite3 auth.db "select 1"'
+ assert tools._bash_exec(padded, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ distinct = (
+ "cd a; cd b; cd c; cd d; cd e; cd f; cd g; cd h; "
+ "cd ../..; cd ../../..; cd auth; cat .cli_api_key_cli_1"
+ )
+ assert tools._bash_exec(distinct, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ assert (
+ tools._bash_exec(
+ "cd .; cd models; ls",
+ None,
+ 30,
+ _SESSION,
+ disable_sandbox = True,
+ )
+ != tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+
+
+def test_the_shell_pid_spelling_of_the_proc_cwd_symlink(studio_home):
+ home = studio_home
+ # `$$` is the shell's own PID, expanded before the path is opened, so it names the same symlink
+ # a literal number does.
+ assert (
+ tools._bash_exec(
+ "cat /proc/$$/cwd/../../auth/auth.db",
+ None,
+ 30,
+ _SESSION,
+ disable_sandbox = True,
+ )
+ == tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ assert (
+ tools._python_exec(
+ "print(open('/proc/$$/cwd/../../auth/auth.db').read())",
+ None,
+ 30,
+ _SESSION,
+ disable_sandbox = True,
+ )
+ == tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ assert (
+ tools._bash_exec(
+ "echo $$",
+ None,
+ 30,
+ _SESSION,
+ disable_sandbox = True,
+ )
+ != tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+
+
+def test_a_python_path_rooted_in_the_studio_home_variable(studio_home, monkeypatch):
+ home = studio_home
+ # `os.environ["UNSLOTH_STUDIO_HOME"] + "/auth/auth.db"` folds to a dynamic piece plus the rest.
+ # Bypass mode keeps that variable in the child env, so the dynamic piece has a known value and
+ # substituting it is not a guess.
+ monkeypatch.setenv("STUDIO_HOME", str(home))
+ for code in (
+ 'import os, sqlite3\np = os.environ["UNSLOTH_STUDIO_HOME"] + "/auth/auth.db"\n'
+ "print(sqlite3.connect(p))",
+ 'import os\nprint(open(os.getenv("STUDIO_HOME") + "/auth/.desktop_secret").read())',
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+ # The same variable pointed anywhere else, and an unrelated variable, stay ordinary work.
+ for code in (
+ 'import os\nprint(os.environ["UNSLOTH_STUDIO_HOME"] + "/models")',
+ 'import os\nprint(os.environ.get("HOME") + "/auth")',
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+
+
+def test_cmd_exe_spells_the_directory_change_in_any_case(studio_home):
+ home = studio_home
+ # On a Windows host without a trusted bash the shell is `cmd /c`, whose built-ins are
+ # case-insensitive, so `CD ..\..` moves the working directory exactly as `cd ../..` does.
+ for command in (
+ 'CD ..\\.. & CD auth & sqlite3 auth.db "select jwt_secret from auth_user"',
+ "Cd ../..; cd auth; cat .cli_api_key_cli_1",
+ "CD ../.. && cat auth/auth.db",
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ for ordinary in ("CD ../.. && ls models", "Cd src & type README.md"):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_the_studio_home_variable_is_read_in_any_case(studio_home):
+ home = studio_home
+ # `os.environ` upper-cases every key on Windows (`os._createenviron` sets `encodekey = str.upper`
+ # for `nt`), so a lower-case lookup returns the same studio root the upper-case spelling does.
+ for var in ("unsloth_studio_home", "Unsloth_Studio_Home", "studio_home"):
+ code = (
+ "import os, sqlite3\n"
+ 'print(sqlite3.connect(os.environ["%s"] + "/auth/auth.db"))' % var
+ )
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), var
+ ordinary = 'import os\nprint(open(os.environ["data_dir"] + "/notes.txt").read())'
+ assert tools._python_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+
+
+def test_a_wildcard_that_expands_to_the_auth_directory(studio_home):
+ home = studio_home
+ # The shell expands `a?th` to `auth` before the command opens anything, so comparing the literal
+ # text alone let the database through under a name that matches no marker.
+ for command in (
+ 'sqlite3 ../../a?th/auth.db "select jwt_secret from auth_user"',
+ "cat ../../aut[h]/auth.db",
+ "cat ../../a[a-z]th/auth.db",
+ "cat ../../au*/auth.db",
+ "cat ../../auth/auth.d?",
+ "cat ../../a?th/.cli_api_key_cli_1",
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ # A segment that is nothing but a wildcard names the auth directory only in the sense that
+ # listing its parent does, so it stays ordinary work.
+ for ordinary in (
+ "ls ../../*",
+ "ls ../../models/*.gguf",
+ "grep -r auth src/*.py",
+ "ls data[0].csv",
+ ):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_home_is_the_workdir_under_bypass_permissions(studio_home):
+ home = studio_home
+ # `_build_bypass_env` repoints HOME at the tool workdir, so `$HOME/../..` is the sandbox walked
+ # two levels up, which is the auth directory's parent. Joining the literal token under the
+ # workdir read it as `<workdir>/$HOME/...` and missed.
+ for command in (
+ 'sqlite3 "$HOME/../../auth/auth.db" "select jwt_secret from auth_user"',
+ "cat ${HOME}/../../auth/auth.db",
+ "cat ~/../../auth/auth.db",
+ "cat $HOME/../../auth/.cli_api_key_cli_1",
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ for ordinary in (
+ "cat $HOME/notes.md",
+ "ls ~/models",
+ "cat $HOME/../../models/m.gguf",
+ "echo $HOMEBREW_PREFIX",
+ "cat backup~/x.txt",
+ ):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_a_backslash_escape_inside_a_relative_path(studio_home):
+ home = studio_home
+ # On POSIX bash drops the backslash and opens `auth/auth.db`, but the join read it as a
+ # separator and resolved `au/th/auth.db`, which names nothing. Both spellings are tried now,
+ # because the same character IS the separator on Windows.
+ for command in (
+ "cd ../..; cat au\\th/auth.db",
+ "cd ../..; cat auth/au\\th.db",
+ "cat ../../au\\th/auth.db",
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ for ordinary in (
+ "cd ../..; cat models\\sub\\file.txt",
+ "cat notes\\ file.txt",
+ "cd ../..; cat models/m.gguf",
+ ):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_a_quoted_bracket_is_not_a_subshell(studio_home):
+ home = studio_home
+ # `echo '('; cd ../..; echo ')'` opens no subshell at all. Counted as syntax, the quoted
+ # brackets ended a subshell that was never entered and dropped the real `cd` between them.
+ for command in (
+ "echo '('; cd ../..; echo ')'; strings auth/auth.db",
+ 'echo "("; cd ../..; cat auth/auth.db',
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ # A real subshell still ends there, and quoted brackets around ordinary work are ordinary.
+ for ordinary in (
+ "(cd ../..; ls models); cat auth/config.json",
+ "echo '('; ls; echo ')'; cat auth/config.json",
+ ):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_a_child_process_runs_from_the_directory_it_is_handed(studio_home):
+ home = studio_home
+ # `subprocess.run([...], cwd = "../..")` moves nothing in this process, so the walk never
+ # moved and each literal argument was tested against the sandbox instead of against the
+ # directory the child actually opens them from.
+ for code in (
+ 'import subprocess\nsubprocess.run(["strings", "auth/auth.db"], cwd = "../..")',
+ 'import subprocess\nsubprocess.run("cat auth/.desktop_secret", shell = True, cwd = "../..")',
+ 'import subprocess\nsubprocess.run(["ls"], cwd = "../../auth")',
+ 'import subprocess\nfrom pathlib import Path\nsubprocess.run(["strings", "auth/auth.db"], cwd = Path.cwd().parents[1])',
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+ for code in (
+ 'import subprocess\nsubprocess.run(["ls", "auth"], cwd = "/tmp/project")',
+ 'import subprocess\nsubprocess.run(["ls", "models"], cwd = "../..")',
+ 'import subprocess\nsubprocess.run(["cat", "auth/config.json"], cwd = ".")',
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+
+
+def test_a_subshell_cd_does_not_outlive_the_subshell(studio_home):
+ home = studio_home
+ # `(cd ../..; ls models); cat auth/config.json` runs the `cat` where it started, because a
+ # subshell's directory dies with it. Carried past the bracket, the move refused a project's
+ # own auth/ read. Inside the subshell the move is real and still counts.
+ for ordinary in (
+ "(cd ../..; ls models); cat auth/config.json",
+ "(cd ../..; ls models)\ncat auth/notes.txt",
+ ):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+ for command in (
+ "(cd ../..; cat auth/auth.db)",
+ "x=$(cd ../.. && cat auth/auth.db)",
+ "(cd ../../auth; ls)",
+ "(cd ../..); cd ../..; cat auth/auth.db",
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+
+
+def test_only_a_module_that_owns_the_process_directory_changes_it(studio_home):
+ home = studio_home
+ # `ftp.chdir('../..')` moves a remote directory, not this process's, so the local read that
+ # follows never leaves the sandbox. Matched on the method name alone, it was refused.
+ assert (
+ tools._python_exec(
+ 'import ftplib\nftp = ftplib.FTP("h")\nftp.chdir("../..")\nprint(open("auth/config.json").read())',
+ None,
+ 30,
+ _SESSION,
+ disable_sandbox = True,
+ )
+ != tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ # An ALIAS of somebody else's chdir is not one either.
+ assert (
+ tools._python_exec(
+ 'import ftplib\nftp = ftplib.FTP("h")\nmove = ftp.chdir\nmove("../..")\nprint(open("auth/config.json").read())',
+ None,
+ 30,
+ _SESSION,
+ disable_sandbox = True,
+ )
+ != tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ for code in (
+ 'import os\nos.chdir("../..")\nprint(open("auth/auth.db", "rb").read())',
+ 'import os as o\no.chdir("../..")\nprint(open("auth/auth.db", "rb").read())',
+ 'from os import chdir\nchdir("../..")\nprint(open("auth/auth.db", "rb").read())',
+ 'import contextlib\nwith contextlib.chdir("../.."):\n print(open("auth/auth.db", "rb").read())',
+ 'import os\nmove = os.chdir\nmove("../..")\nprint(open("auth/auth.db", "rb").read())',
+ 'import os as o\ngo = o.chdir\ngo("../..")\nprint(open("auth/auth.db", "rb").read())',
+ 'from contextlib import chdir as cd\nwith cd("../.."):\n print(open("auth/auth.db", "rb").read())',
+ 'import os\nmove = os.chdir\nagain = move\nagain("../..")\nprint(open("auth/auth.db", "rb").read())',
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+
+
+def test_a_cd_in_a_conditional_position_moves_the_directory(studio_home):
+ home = studio_home
+ # `if cd ../..; then ...; fi` runs the move as the condition itself, and a brace group is just
+ # another command position. Only `then`/`do`/`else` were recognised, so those moves went
+ # unrecorded and the auth database that followed was resolved against the sandbox.
+ root = str(home)
+ for command in (
+ "if cd ../..; then sqlite3 auth/auth.db 'select jwt_secret from auth_user'; fi",
+ "while cd ../..; do cat auth/auth.db; done",
+ "until cd ../..; do cat auth/.desktop_secret; done",
+ "{ cd ../..; cat auth/auth.db; }",
+ "if false; then :; elif cd ../..; then cat auth/auth.db; fi",
+ f"if cd {root}; then cat auth/auth.db; fi",
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ for ordinary in (
+ "if cd ../..; then cat models/m.gguf; fi",
+ "echo 'if cd ../..'; grep auth README",
+ "if true; then cat auth.py; fi",
+ ):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_a_chdir_to_a_parent_walk_moves_the_directory(studio_home):
+ home = studio_home
+ # `os.chdir(Path.cwd().parents[1])` is an ordinary move into the studio root, but the fold
+ # writes the walk as one marker, so the move was ignored and the `auth/auth.db` that followed
+ # was still resolved against the sandbox.
+ for code in (
+ 'import os, sqlite3\nfrom pathlib import Path\nos.chdir(Path.cwd().parents[1])\nprint(sqlite3.connect("auth/auth.db"))',
+ 'import os\nfrom pathlib import Path\nos.chdir(Path.cwd().parent.parent)\nprint(open("auth/.desktop_secret").read())',
+ 'import os\nfrom pathlib import Path\nos.chdir(Path.cwd().parents[1] / "auth")\nprint(open("auth.db", "rb").read())',
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+ # The same move, then ordinary work, and a walk from a literal base that is not the sandbox.
+ for code in (
+ 'import os\nfrom pathlib import Path\nos.chdir(Path.cwd().parents[1])\nprint(open("models/m.gguf", "rb").read(4))',
+ 'import os\nfrom pathlib import Path\nroot = Path("/tmp/project")\nos.chdir(root.parent)\nprint(open("auth/auth.db", "rb").read(4))',
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+
+
+def test_a_quoted_cd_into_the_studio_root_is_not_a_move(studio_home):
+ home = studio_home
+ # `echo 'cd <root>'; grep auth README` prints the text and searches a project. Matched wherever
+ # it appeared, the quoted text read as a move into the studio root and refused ordinary work.
+ root = str(home)
+ for ordinary in (
+ f"echo 'cd {root}'; grep auth README",
+ f"echo cd {root}; cat auth.py",
+ f"grep -rn 'cd {root}' src/; grep auth README",
+ f"cd {root}/models && grep auth README",
+ f"cd {root}-backup && ls auth",
+ ):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+ # A real move into the root, in every position a shell runs one, still counts.
+ for command in (
+ f"cd {root} && cat auth/auth.db",
+ f"cd {root}; ls auth",
+ f"cd '{root}'/auth && ls",
+ f"builtin cd {root} && cat auth/auth.db",
+ f"if true; then cd {root}; cat auth/auth.db; fi",
+ f"pushd {root} && cat auth/auth.db",
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+
+
+def test_shell_punctuation_ends_a_credential_name(studio_home):
+ home = studio_home
+ # A name ends where the shell ends a word, so `cat /tmp/.bootstrap_password; echo done` names
+ # the file. Accepting only whitespace or a quote as the boundary matched none of these.
+ for command in (
+ "cat /tmp/.bootstrap_password; echo done",
+ "cat /tmp/llama_api_key;",
+ "(cat /tmp/.desktop_secret)",
+ "cat /tmp/.cli_api_key_cli_1;echo x",
+ "cat /opt/agent_api_key.json|head",
+ "cat /tmp/.bootstrap_password&",
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ # The same punctuation after ordinary work is still ordinary work.
+ for ordinary in (
+ "cat notes.txt; echo done",
+ "grep -rn auth src/;",
+ "cat auth.py;",
+ 'python -c "import auth"',
+ ):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_a_builtin_wrapped_cd_moves_the_directory(studio_home):
+ home = studio_home
+ # `builtin cd ..` and `command cd ..` run the same shell builtin with the same argument, so
+ # everything after them is relative to the new directory. Matched on the bare name only, the
+ # move went unrecorded and the auth database that followed still looked like the sandbox's.
+ for command in (
+ 'builtin cd ../..; sqlite3 auth/auth.db "select jwt_secret from auth_user"',
+ "command cd ../.. && cat auth/.desktop_secret",
+ "builtin cd ../..; cat auth/.cli_api_key_cli_1",
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ for ordinary in (
+ "echo builtin cd ../..",
+ "builtin cd ../..; cat models/m.gguf",
+ "command ls auth/",
+ ):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_a_parent_walk_from_a_named_literal_base_is_that_base(studio_home):
+ home = studio_home
+ # A name holding a literal path is not the working directory, so `root = Path('/tmp/project')`
+ # walked up twice is `/`, not the studio root, and a project's own auth/ must still be readable.
+ for code in (
+ 'from pathlib import Path\nroot = Path("/tmp/project")\nprint(root.parent.parent / "auth" / "auth.db")',
+ 'from pathlib import Path\nroot = Path("/srv/app")\nprint(root.parent / "auth" / "auth.db")',
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+ # A name bound to the working directory, or rebound so its value is unknown, still counts.
+ for code in (
+ 'from pathlib import Path\nroot = Path.cwd()\nprint((root.parent.parent / "auth" / "auth.db").read_bytes())',
+ 'from pathlib import Path\nprint((Path.cwd().parents[1] / "auth" / "auth.db").read_bytes())',
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+
+
+def test_pwd_is_the_workdir_under_bypass_permissions(studio_home):
+ home = studio_home
+ # Bash rewrites PWD on every `cd`, so under bypass it names the tool workdir and `$PWD/../..`
+ # is the auth directory's parent. Left as a literal segment it read as `<workdir>/$PWD/...`.
+ for command in (
+ 'sqlite3 "$PWD/../../auth/auth.db" "select jwt_secret from auth_user"',
+ "cat ${PWD}/../../auth/.desktop_secret",
+ "cat $PWD/../../auth/.cli_api_key_cli_1",
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ # Python reads the same directory by name or by call.
+ for code in (
+ 'import os, sqlite3\nprint(sqlite3.connect(os.environ["PWD"] + "/../../auth/auth.db"))',
+ 'import os\nprint(open(os.getcwd() + "/../../auth/auth.db").read())',
+ 'from pathlib import Path\nprint((Path.cwd() / ".." / ".." / "auth" / "auth.db").read_bytes())',
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+ # Ordinary work names the working directory constantly and must still run.
+ for ordinary in (
+ "echo $PWD",
+ "ls $PWD/../models",
+ "cat $PWD/notes.md",
+ "echo $PWDX",
+ ):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+ assert (
+ tools._python_exec(
+ 'import os\nprint(os.environ["PWD"] + "/data")',
+ None,
+ 30,
+ _SESSION,
+ disable_sandbox = True,
+ )
+ != tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+
+
+def test_pushd_and_a_python_chdir_move_the_directory_too(studio_home):
+ home = studio_home
+ # `pushd DIR` makes DIR the working directory exactly as `cd` does, and `os.chdir('../..')`
+ # moves every path AFTER it in the same snippet. Checked independently, the move reaches only
+ # the studio root and the `auth/auth.db` that follows still looks like it is in the sandbox.
+ for command in (
+ 'pushd ../..; sqlite3 auth/auth.db "select jwt_secret from auth_user"',
+ "pushd ../.. && cat auth/.desktop_secret",
+ # cmd.exe environment names are case-insensitive, so %home% expands like %HOME%.
+ r"type %home%\..\..\auth\auth.db",
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ for code in (
+ "import os, sqlite3\nos.chdir('../..')\nprint(sqlite3.connect('auth/auth.db'))",
+ "import os\nos.chdir('..')\nos.chdir('..')\nprint(open('auth/auth.db').read())",
+ "import os\nos.chdir('../../auth')\nprint(open('.cli_api_key_cli_1').read())",
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+ # Moving anywhere else, and an `auth` that is a string rather than a path, stay ordinary.
+ for ordinary in ("pushd ../models; ls", "popd"):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+ for code in (
+ "import os\nos.chdir('../data')\nprint(open('x.csv').read())",
+ "print(open('auth/auth.db').read())",
+ "print('authentication helper')",
+ "x = 'auth'\nprint(x)",
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+
+
+def test_a_studio_home_variable_pointing_elsewhere_is_not_ours(studio_home, monkeypatch, tmp_path):
+ home = studio_home
+ # `STUDIO_HOME` is a generic name another application can own. With the spelling registered
+ # unconditionally, a command naming that application's directory was refused in every permission
+ # mode even though it never came near this install.
+ monkeypatch.setenv("STUDIO_HOME", str(tmp_path / "other-app"))
+ assert not tools._references_studio_credential("cat $STUDIO_HOME/auth/auth.db")
+ assert tools._references_studio_credential("cat $UNSLOTH_STUDIO_HOME/auth/auth.db")
+
+
+def test_an_unset_studio_home_variable_stays_registered(studio_home):
+ home = studio_home
+ # The child cannot expand it either, so the spelling reaches nothing and dropping it would only
+ # widen the guard for no gain.
+ assert tools._references_studio_credential("cat $STUDIO_HOME/auth/auth.db")
+
+
+def test_a_differently_cased_studio_home_value_is_still_ours(monkeypatch, tmp_path):
+ # Windows paths are case-insensitive, so a value spelled with a different drive-letter case is
+ # the same directory. Comparing it exactly dropped every spelling of the variable.
+ home = tmp_path / "studio-home"
+ (home / "auth").mkdir(parents = True)
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(home))
+ monkeypatch.setenv("STUDIO_HOME", str(home).upper())
+ monkeypatch.setattr(tools, "_studio_auth_markers_cache", None)
+ try:
+ assert tools._references_studio_credential("cat $UNSLOTH_STUDIO_HOME/auth/auth.db")
+ if os.path.normcase("A") == os.path.normcase("a"): # a case-insensitive filesystem
+ assert tools._references_studio_credential(r"type %STUDIO_HOME%\auth\auth.db")
+ finally:
+ tools._studio_auth_markers_cache = None
+
+
+def test_a_symlinked_studio_home_is_still_ours(monkeypatch, tmp_path):
+ # `studio_root()` resolves aliases, so a STUDIO_HOME that is a symlink to the configured root
+ # compared unequal to it and every spelling of the variable was dropped from the guard.
+ real = tmp_path / "real-home"
+ (real / "auth").mkdir(parents = True)
+ alias = tmp_path / "alias-home"
+ try:
+ alias.symlink_to(real, target_is_directory = True)
+ except (OSError, NotImplementedError): # no symlink privilege on this host
+ pytest.skip("symlinks unavailable")
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(alias))
+ monkeypatch.setenv("STUDIO_HOME", str(alias))
+ monkeypatch.setattr(tools, "_studio_auth_markers_cache", None)
+ try:
+ assert tools._references_studio_credential(
+ 'sqlite3 "$UNSLOTH_STUDIO_HOME/auth/auth.db" "select 1"'
+ )
+ assert tools._references_studio_credential("cat $STUDIO_HOME/auth/.desktop_secret")
+ finally:
+ tools._studio_auth_markers_cache = None
+
+
+def test_a_failed_directory_change_leaves_the_cwd_where_it_was(studio_home):
+ home = studio_home
+ # `cd missing` fails and the shell carries on from where it was, so the NEXT `cd ../..` starts
+ # at the real sandbox and reaches the studio root. Assuming every change succeeds resolved the
+ # rest against a directory the command was never in. Python catching an OSError is the same.
+ for command in (
+ "cd definitely-missing; cd ../..; cat auth/auth.db",
+ 'cd nope || true; cd ../.. ; sqlite3 auth/auth.db "select 1"',
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ for code in (
+ "import os\ntry:\n os.chdir('definitely-missing')\nexcept OSError:\n pass\n"
+ "os.chdir('../..')\nprint(open('auth/auth.db').read())",
+ "import os, sqlite3\ntry:\n os.chdir('nope')\nexcept Exception:\n pass\n"
+ "os.chdir('../..')\nprint(sqlite3.connect('auth/auth.db'))",
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+ for ordinary in ("cd missing; ls", "cd ../data && cat x.csv"):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+ assert (
+ tools._python_exec(
+ "import os\nos.chdir('sub')\nprint(open('auth.db').read())",
+ None,
+ 30,
+ _SESSION,
+ disable_sandbox = True,
+ )
+ != tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+
+
+def test_cd_options_padding_and_a_keyword_chdir(studio_home):
+ home = studio_home
+ # `cd [-L|[-P [-e]] [-@]] [dir]` is what bash documents, so an option is not the target;
+ # padding with failing `cd`s must not spend the state budget that holds the real sandbox; and
+ # `os.chdir(path=...)` is the same call as the positional one.
+ padded = "; ".join(f"cd missing{i}" for i in range(7)) + "; cd ../..; cat auth/auth.db"
+ for command in (
+ "cd -P ../..; cat auth/auth.db",
+ 'cd -L -e ../.. && sqlite3 auth/auth.db "select 1"',
+ padded,
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ assert (
+ tools._python_exec(
+ "import os\nos.chdir(path='../..')\nprint(open('auth/auth.db').read())",
+ None,
+ 30,
+ _SESSION,
+ disable_sandbox = True,
+ )
+ == tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ for ordinary in ("cd -P ../models; ls", "cd -; ls"):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+ assert (
+ tools._python_exec(
+ "import os\nos.chdir(path='../data')\nprint(1)",
+ None,
+ 30,
+ _SESSION,
+ disable_sandbox = True,
+ )
+ != tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+
+
+def test_a_glob_is_read_per_token_not_per_command(studio_home):
+ home = studio_home
+ # `sqlite3 <home>/a?th/auth.db` starts its segments at `sqlite3 <home>`, so comparing from
+ # segment zero could never line up with an absolute marker; the glob pass reads each path-shaped
+ # token on its own now.
+ for command in (
+ f"sqlite3 {home}/a?th/auth.db 'select jwt_secret from auth_user'",
+ f"cat {home}/au*/.desktop_secret",
+ f"cat {home}/a[u]th/auth.db",
+ "sqlite3 ../../a?th/auth.db 'select 1'",
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ # Listing a parent, or globbing anywhere else, is ordinary work.
+ for ordinary in (
+ f"ls {home}/*",
+ f"ls {home}/models/*.gguf",
+ f"cat {home}/logs/*.log",
+ "grep -rn 'auth' src/*.py",
+ ):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_a_pushd_to_an_absolute_home_and_paths_before_the_cd(studio_home):
+ home = studio_home
+ # Two halves of the same walk: the workdir has to be resolved for a command that MOVES the
+ # directory even when it carries no `..`, and a relative path written BEFORE the move opens
+ # from the old directory, so it must not be resolved against the new one.
+ for command in (
+ f'pushd {home}; sqlite3 auth/auth.db "select jwt_secret from auth_user"',
+ f"cd {home} && cat auth/.desktop_secret",
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ for ordinary in (
+ "cat auth/auth.db; cd ../..",
+ "cat auth/auth.db",
+ f"pushd {home}/models; ls",
+ ):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_a_pathlib_parent_walk_and_the_option_terminator(studio_home):
+ home = studio_home
+ # `Path.cwd().parents[1] / "auth" / "auth.db"` walks up from the sandbox without writing a `..`,
+ # and bash accepts `--` as the end of options, so it is not the target of the `cd`.
+ for code in (
+ "import sqlite3\nfrom pathlib import Path\n"
+ 'print(sqlite3.connect(Path.cwd().parents[1] / "auth" / "auth.db"))',
+ "from pathlib import Path\n"
+ 'print((Path.cwd().parent.parent / "auth" / ".desktop_secret").read_text())',
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+ assert (
+ tools._bash_exec(
+ "cd -- ../..; cat auth/auth.db",
+ None,
+ 30,
+ _SESSION,
+ disable_sandbox = True,
+ )
+ == tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ # A parent walk that lands anywhere else stays ordinary work.
+ for code in (
+ 'from pathlib import Path\nprint(Path.cwd().parent / "data" / "x.csv")',
+ 'from pathlib import Path\nprint(Path("x.txt").parent)',
+ 'from pathlib import Path\nprint((Path.cwd().parents[1] / "models" / "m.gguf").exists())',
+ # The sandbox is <home>/sandbox/<session>, so parents[1] is the home and parents[3] is
+ # two levels above it, which is not this install.
+ 'from pathlib import Path\nprint((Path.cwd().parents[3] / "auth" / "auth.db"))',
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+ assert (
+ tools._bash_exec(
+ "cd -- ../models; ls",
+ None,
+ 30,
+ _SESSION,
+ disable_sandbox = True,
+ )
+ != tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+
+
+def test_command_positions_explicit_bases_and_chdir_aliases(studio_home):
+ home = studio_home
+ for command in (
+ "cd ../..; cat auth/auth.db",
+ # An indented command line moves the shell exactly as an unindented one does.
+ " cd ../..; cat auth/auth.db",
+ "x=1 && cd ../.. && cat auth/auth.db",
+ "if true; then cd ../..; cat auth/auth.db; fi",
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ # ...and nowhere else: `cd` as an ARGUMENT changes no directory, and reading a project's own
+ # auth/ after one was refused in every mode.
+ for ordinary in (
+ "echo cd ../..; cat auth/config.json",
+ 'echo "cd ../.."',
+ "grep -rn cd ../../src",
+ ):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+ # An alias of os.chdir moves the directory exactly as the name does.
+ for code in (
+ "from os import chdir as move\nmove('../..')\nprint(open('auth/auth.db').read())",
+ "import os\ngo = os.chdir\ngo('../..')\nprint(open('auth/auth.db').read())",
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+ # A parent walk from an EXPLICIT base is that base's parent, not the sandbox's.
+ assert (
+ tools._python_exec(
+ "from pathlib import Path\nprint(Path('/tmp').parent / 'auth' / 'auth.db')",
+ None,
+ 30,
+ _SESSION,
+ disable_sandbox = True,
+ )
+ != tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+
+
+def test_a_generic_cwd_keyword_is_not_a_child_process(studio_home):
+ home = studio_home
+ # `cwd` is an ordinary keyword name. Reading it as process semantics on any call refused
+ # ordinary code whose function may never touch the path it was handed.
+ for ordinary in (
+ 'describe("auth/config.json", cwd = "../..")',
+ 'import subprocess as sp\nsp.run(["ls"], cwd = "build")',
+ ):
+ assert tools._python_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+ for code in (
+ 'import subprocess\nsubprocess.run(["cat", "auth/auth.db"], cwd = "../..")',
+ 'from subprocess import run\nrun(["cat", "auth/auth.db"], cwd = "../..")',
+ # An import alias leaves the call spelled `sp.run(...)`.
+ 'import subprocess as sp\nsp.run(["cat", "auth/auth.db"], cwd = "../..")',
+ 'import asyncio as aio\naio.create_subprocess_exec("cat", "auth/auth.db", cwd = "../..")',
+ # A renamed from-import leaves the call under a name no fixed table holds.
+ 'from subprocess import run as launch\nlaunch(["cat", "auth/auth.db"], cwd = "../..")',
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+
+
+def test_a_context_manager_chdir_restores_on_exit(studio_home):
+ home = studio_home
+ # `contextlib.chdir` moves only while its body runs, so a read AFTER the block is back in the
+ # sandbox and must not be resolved against the studio root.
+ after = (
+ "import contextlib\n"
+ 'with contextlib.chdir("../.."):\n'
+ " pass\n"
+ 'print(open("auth/config.json").read())'
+ )
+ assert tools._python_exec(after, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ inside = (
+ "import contextlib\n"
+ 'with contextlib.chdir("../.."):\n'
+ ' print(open("auth/auth.db", "rb").read())'
+ )
+ assert tools._python_exec(inside, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ permanent = 'import os\nos.chdir("../..")\nprint(open("auth/auth.db", "rb").read())'
+ assert tools._python_exec(permanent, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+
+
+def test_a_shell_variable_supplying_the_cd_target(studio_home):
+ home = studio_home
+ # `d=../..; cd "$d"` moves the directory every later relative path opens from. Handing the
+ # UNexpanded text to the cwd walk read `$d` as a directory name, so the walk never moved.
+ command = 'd=../..; cd "$d"; sqlite3 auth/auth.db "select jwt_secret from auth_user"'
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ assert (
+ tools._bash_exec('d=build; cd "$d"; make', None, 30, _SESSION, disable_sandbox = True)
+ != tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+
+
+def test_a_snippets_own_run_is_not_a_child_process(studio_home):
+ home = studio_home
+ # `run`, `call` and `check_output` are ordinary function names. Treating a snippet's own
+ # definition as a process launch refused ordinary code in every permission mode.
+ ordinary = 'def run(*args, **kwargs):\n pass\nrun(["auth/config.json"], cwd = "../..")'
+ assert tools._python_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ imported = 'from subprocess import run\nrun(["cat", "auth/auth.db"], cwd = "../..")'
+ assert tools._python_exec(imported, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+
+
+def test_a_command_substitution_inside_double_quotes_is_a_subshell(studio_home):
+ home = studio_home
+ # Substitution stays ACTIVE inside double quotes, so `"$(cd ../..; pwd)"` opens a real subshell
+ # whose move dies with it. Skipping both brackets made the inner `cd` look like it lasted
+ # through the rest of the command, and the project's own auth/config.json was refused.
+ ordinary = 'echo "$(cd ../..; pwd)"; cat auth/config.json'
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ # Inside the substitution the move is real, and a literal bracket in single quotes is not
+ # syntax at all, so neither loses the credential read.
+ for blocked in (
+ 'echo "$(cd ../..; cat auth/auth.db)"',
+ "echo '('; cd ../..; echo ')'; cat auth/auth.db",
+ ):
+ assert tools._bash_exec(blocked, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), blocked
+
+
+def test_consecutive_cds_inside_one_subshell(studio_home):
+ home = studio_home
+ # A subshell's move has to reach the NEXT command inside the same subshell. Dropping it outright
+ # resolved the second `cd` from the outer sandbox and missed the database entirely.
+ blocked = '(cd ../..; cd auth; sqlite3 auth.db "select jwt_secret from auth_user")'
+ assert tools._bash_exec(blocked, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ # ...and must not reach past the closing bracket. Entering the directory and leaving
+ # without reading anything is not a credential read.
+ for ordinary in (
+ "(cd ../..; cd auth); cat config.json",
+ "(cd ../..; ls models); cat auth/config.json",
+ ):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_a_chdir_in_an_uncalled_helper_does_not_move_the_walk(studio_home):
+ home = studio_home
+ # A `chdir` inside a function body only moves anything if that function runs.
+ ordinary = (
+ "import os\n"
+ "def helper():\n"
+ ' os.chdir("../..")\n'
+ 'print(open("auth/config.json").read())'
+ )
+ assert tools._python_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ # A body whose name IS called stays live, and a branch is not a scope.
+ for blocked in (
+ 'import os\ndef helper():\n os.chdir("../..")\nhelper()\n'
+ 'print(open("auth/auth.db", "rb").read())',
+ 'import os\nif x:\n os.chdir("../..")\nprint(open("auth/auth.db", "rb").read())',
+ ):
+ assert tools._python_exec(blocked, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), blocked
+
+
+def test_a_wildcard_that_carries_no_literal_hint(studio_home):
+ home = studio_home
+ # `../../a?th/.b*` expands onto `auth/.bootstrap_password` while containing none of the literal
+ # hints, so the prefilter was returning before the glob analysis that exists for this could run.
+ for command in ("cat ../../a?th/.b*", "cat ../../a?th/*"):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ for ordinary in ("ls *.py", "ls ../../m*/", "ls ../../models/*.gguf", "grep -r x src/*.py"):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_fchdir_is_a_move_with_an_unknown_destination(studio_home):
+ home = studio_home
+ # `os.fchdir(fd)` names where it goes by descriptor, so there is no path to fold. The move is
+ # real, so the studio root joins the live directories and a later relative credential path is
+ # resolved from there as well as from the sandbox.
+ blocked = (
+ "import os\n"
+ 'fd = os.open("../..", os.O_RDONLY)\n'
+ "os.fchdir(fd)\n"
+ 'print(open("auth/auth.db", "rb").read())'
+ )
+ assert tools._python_exec(blocked, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ ordinary = (
+ "import os\n"
+ 'fd = os.open("data", os.O_RDONLY)\n'
+ "os.fchdir(fd)\n"
+ 'print(open("notes.txt").read())'
+ )
+ assert tools._python_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+
+
+def test_a_snippets_own_chdir_is_not_a_move(studio_home):
+ home = studio_home
+ # A bare `chdir` only moves the walk once an import binds it. A snippet's own definition is an
+ # ordinary function, and a call to it was resolving later paths under the studio root.
+ ordinary = 'def chdir(path):\n pass\nchdir("../..")\nprint(open("auth/config.json").read())'
+ assert tools._python_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ for blocked in (
+ 'from os import chdir\nchdir("../..")\nprint(open("auth/auth.db", "rb").read())',
+ 'from os import chdir as move\nmove("../..")\nprint(open("auth/auth.db", "rb").read())',
+ 'import os\nmove = os.chdir\nmove("../..")\nprint(open("auth/auth.db", "rb").read())',
+ ):
+ assert tools._python_exec(blocked, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), blocked
+
+
+def test_a_reserved_word_before_cd_still_moves_the_directory(studio_home):
+ home = studio_home
+ # `!` and `time` are reserved words rather than commands, so the shell runs the `cd` builtin
+ # straight after them and the move lands. Accepting only a command position that started at a
+ # separator, the guard read `! cd ../..; cat auth/auth.db` as a command that never moved.
+ for command in (
+ "! cd ../..; cat auth/auth.db",
+ "time cd ../..; cat auth/.desktop_secret",
+ # Bash accepts assignment words before a special builtin, so these move too.
+ "X=1 cd ../..; cat auth/auth.db",
+ 'LC_ALL=C TZ=UTC cd ../..; sqlite3 auth/auth.db "select jwt_secret from auth_user"',
+ "time -p cd ../..; cat auth/.cli_api_key_cli_1",
+ 'nohup cd ../..; sqlite3 auth/auth.db "select jwt_secret from auth_user"',
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ for ordinary in (
+ "! true; cat auth/config.json",
+ "X=1 cat models/m.gguf",
+ "echo X=1 cd ../..",
+ "time cat models/m.gguf",
+ "echo time cd ../..",
+ ):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_a_cd_in_a_function_nothing_calls_does_not_move_the_directory(studio_home):
+ home = studio_home
+ # Defining a function does not run it, so `helper() { cd ../..; }; cat auth/config.json` reads
+ # the project's own auth file from the unchanged sandbox. Carried out of the body regardless,
+ # the move turned an ordinary read into a refusal in every permission mode.
+ for ordinary in (
+ "helper() { cd ../..; }; cat auth/config.json",
+ "function helper { cd ../..; }; cat auth/auth.db",
+ "helper () { cd ../..; helper; }; cat auth/auth.db",
+ ):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+ # Invoking it, or moving outside any function at all, is the same move it always was.
+ for command in (
+ "helper() { cd ../..; }; helper; cat auth/auth.db",
+ "function helper { cd ../..; }; helper && cat auth/.desktop_secret",
+ "helper() { echo hi; }; cd ../..; cat auth/auth.db",
+ "{ cd ../..; cat auth/auth.db; }",
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+
+
+def test_a_singleton_class_and_an_escaped_builtin_are_the_literals_they_expand_to(
+ studio_home, monkeypatch, tmp_path
+):
+ home = studio_home
+ # `[a][u][t][h]` expands to exactly `auth`, and `c\d` is the `cd` builtin with its backslash
+ # removed. Both were folded away before the guard read them: the classes collapsed to wildcards,
+ # and a segment of nothing but wildcards is deliberately not matched, while the escaped name
+ # matched no command position at all.
+ for command in (
+ 'sqlite3 ../../[a][u][t][h]/auth.db "select jwt_secret from auth_user"',
+ "cat ../../[a][u][t][h]/.desktop_secret",
+ 'c\\d ../..; sqlite3 auth/auth.db "select jwt_secret from auth_user"',
+ "\\c\\d ../..; cat auth/.desktop_secret",
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ # A broad class still names nothing in particular, and a singleton elsewhere is ordinary.
+ for ordinary in (
+ "ls ../../[a-z]*",
+ "cat data/[a]/notes.txt",
+ "cat notes.txt",
+ ):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_a_chdir_to_the_studio_home_variable_moves_there(studio_home):
+ home = studio_home
+ # `os.chdir(os.environ["UNSLOTH_STUDIO_HOME"])` names the root without spelling it, and bypass
+ # keeps that variable in the child, so the move is real. The fold has no value for a subscript,
+ # which left the walk sitting in the sandbox and the later relative path looking ordinary.
+ for code in (
+ 'import os\nos.chdir(os.environ["UNSLOTH_STUDIO_HOME"])\nprint(open("auth/auth.db").read())',
+ # Windows upper-cases every key `os.environ` is handed, so the lower-case spelling
+ # resolves to the same directory there.
+ 'import os\nos.chdir(os.environ["unsloth_studio_home"])\nprint(open("auth/.desktop_secret").read())',
+ 'import os\nos.chdir(os.getenv("STUDIO_HOME"))\nprint(open("auth/auth.db").read())',
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+ for ordinary in (
+ 'import os\nos.chdir("data")\nprint(open("notes.txt").read())',
+ 'import os\nos.chdir(os.environ["HOME"])\nprint(open("notes.txt").read())',
+ ):
+ assert tools._python_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_a_child_cwd_and_a_directory_descriptor_both_carry_the_studio_root(studio_home):
+ home = studio_home
+ # Two ways to reach the auth directory without ever naming it in one path. The child's `cwd`
+ # can be the studio home variable, which the fold has no value for, and `dir_fd` joins a
+ # descriptor opened on `../..` to a later relative path, which the kernel combines and a scan
+ # reading the two calls separately does not.
+ for code in (
+ "import subprocess, os\n"
+ 'subprocess.run(["cat", "auth/auth.db"], cwd = os.environ["UNSLOTH_STUDIO_HOME"])',
+ "import subprocess, os\n"
+ 'subprocess.run(["cat", "auth/.desktop_secret"], cwd = os.getenv("STUDIO_HOME"))',
+ 'import os\nroot = os.open("../..", os.O_RDONLY)\n'
+ 'fd = os.open("auth/auth.db", os.O_RDONLY, dir_fd = root)',
+ 'import os\nroot = os.open("../..", os.O_RDONLY)\n'
+ 'fd = os.open("auth/.desktop_secret", os.O_RDONLY, dir_fd = root)',
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+ for ordinary in (
+ 'import subprocess, os\nsubprocess.run(["ls"], cwd = os.environ["UNSLOTH_STUDIO_HOME"])',
+ 'import os\nd = os.open("data", os.O_RDONLY)\n'
+ 'fd = os.open("notes.txt", os.O_RDONLY, dir_fd = d)',
+ # The project's OWN auth file, opened relative to the sandbox it sits in.
+ 'import os\nd = os.open(".", os.O_RDONLY)\n'
+ 'fd = os.open("auth/config.json", os.O_RDONLY, dir_fd = d)',
+ ):
+ assert tools._python_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_a_class_body_runs_at_definition_but_its_methods_do_not(studio_home):
+ home = studio_home
+ # Python executes a class body when the class statement runs, whether or not anything
+ # instantiates it, so `class C: os.chdir("../..")` moves the process. Grouped with the deferred
+ # bodies, that move was dropped and the database that followed resolved from the sandbox.
+ for code in (
+ 'import os, sqlite3\nclass C:\n os.chdir("../..")\n'
+ 'print(sqlite3.connect("auth/auth.db"))',
+ 'import os\nclass C:\n os.chdir("../..")\nprint(open("auth/.desktop_secret").read())',
+ # An invoked method is a real move for the same reason it always was.
+ 'import os\nclass C:\n def m(self):\n os.chdir("../..")\n'
+ 'C().m()\nprint(open("auth/auth.db").read())',
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+ for ordinary in (
+ # A method nothing calls is still deferred, and so is a plain function.
+ 'import os\nclass C:\n def m(self):\n os.chdir("../..")\n'
+ 'print(open("auth/config.json").read())',
+ 'import os\ndef f():\n os.chdir("../..")\nprint(open("auth/config.json").read())',
+ 'import os\nclass C:\n x = 1\nprint(open("notes.txt").read())',
+ ):
+ assert tools._python_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_an_invoked_lambda_moves_and_so_does_the_platform_os_module(studio_home):
+ home = studio_home
+ # `(lambda: os.chdir("../.."))()` runs its body right there, and `posix.chdir` is the same
+ # process primitive `os.chdir` is, under the platform module `os` is built on. Both were read as
+ # no move at all, so the database that followed resolved from the sandbox.
+ for code in (
+ 'import os, sqlite3\n(lambda: os.chdir("../.."))()\n'
+ 'print(sqlite3.connect("auth/auth.db"))',
+ 'import posix\nposix.chdir("../..")\nprint(open("auth/auth.db").read())',
+ 'import posix as p\np.chdir("../..")\nprint(open("auth/.desktop_secret").read())',
+ # Bound to a name and then called: the same body, one step further out.
+ 'import os\nmove = lambda: os.chdir("../..")\nmove()\n'
+ 'print(open("auth/auth.db").read())',
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+ for ordinary in (
+ # Defined and never called, which is what the inert rule is for.
+ 'import os\nmove = lambda: os.chdir("../..")\nprint(open("auth/config.json").read())',
+ # A remote directory change leaves the local one alone.
+ 'import ftplib\nftp = ftplib.FTP()\nftp.chdir("../..")\n'
+ 'print(open("auth/config.json").read())',
+ ):
+ assert tools._python_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_ordinary_directory_work_is_not_read_as_a_move_to_the_studio_root(monkeypatch, tmp_path):
+ # Four false refusals, all of them ordinary work: a `&&` chain only reaches its second `cd` when
+ # the first succeeded, `cd -` and `popd` go BACK, a directory whose path merely embeds the
+ # studio path is not the studio directory, and a case-sensitive filesystem tells `Auth` from
+ # `auth`. Each read below is the project's OWN auth file, not Studio's.
+ home = tmp_path / "studio-home"
+ (home / "auth").mkdir(parents = True)
+ # A case-INSENSITIVE filesystem (Windows, macOS) already has this directory: `Auth` and `auth`
+ # are the same one there, which is exactly why the case check below only runs elsewhere.
+ if tools._CASE_SENSITIVE_PATHS:
+ (home / "Auth").mkdir()
+ (home / "sandbox" / _SESSION / "subdir").mkdir(parents = True)
+ (tmp_path / "backup").mkdir()
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(home))
+ monkeypatch.setattr(tools, "_studio_auth_markers_cache", None)
+ try:
+ ordinary = [
+ "cd subdir && cd ../.. && cat auth/config.json",
+ "cd ../..; cd - >/dev/null; cat auth/config.json",
+ "pushd ../.. >/dev/null; popd >/dev/null; cat auth/config.json",
+ f"cat /mnt/backup{home}/auth/config.json",
+ ]
+ if tools._CASE_SENSITIVE_PATHS:
+ ordinary.append(f"cat {home}/Auth/notes.txt")
+ for command in ordinary:
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ # The moves themselves still land where they always did.
+ for command in (
+ "cd ../..; cat auth/auth.db",
+ "cd ../.. && cat auth/auth.db",
+ "cd ../..; cd -; cd ../..; cat auth/auth.db",
+ "cd -- ../..; cat auth/auth.db",
+ f"cat {home}/auth/auth.db",
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ finally:
+ tools._studio_auth_markers_cache = None
+
+
+def test_enumerating_the_studio_root_for_a_credential_name_is_refused(studio_home):
+ home = studio_home
+ # `find "$UNSLOTH_STUDIO_HOME" -name auth.db -exec cat {} \;` hands the directory to a tool that
+ # walks it, so no single token of the command is a path to the database. Naming the ROOT and a
+ # credential BASENAME is the shape, however they are joined.
+ for command in (
+ 'find "$UNSLOTH_STUDIO_HOME" -name auth.db -exec cat {} \;',
+ f'find {home} -name ".desktop_secret" -exec cat {{}} \;',
+ 'grep -r sk-unsloth "$UNSLOTH_STUDIO_HOME" --include=auth.db',
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ for ordinary in (
+ 'find . -name "*.py" -exec wc -l {} \;',
+ "find data -name notes.txt",
+ # A command that BINDS the generic variable means its own directory by it.
+ 'STUDIO_HOME=/opt/app cat "$STUDIO_HOME/auth/config.json"',
+ ):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_an_uppercase_studio_root_is_reconstructed_with_its_case(monkeypatch, tmp_path):
+ # The root was rebuilt from the FOLDED marker, so a path with an uppercase character in it came
+ # back lowercase; the case-sensitive check then rejected the guard's own synthesized path and a
+ # move to the real root went through.
+ home = tmp_path / "Studio-Home"
+ (home / "auth").mkdir(parents = True)
+ (home / "sandbox" / _SESSION).mkdir(parents = True)
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(home))
+ monkeypatch.setattr(tools, "_studio_auth_markers_cache", None)
+ try:
+ assert tools._studio_home_for_guard() == str(home)
+ code = 'import os\nos.chdir(os.environ["UNSLOTH_STUDIO_HOME"])\nopen("auth/auth.db")'
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ assert (
+ tools._python_exec('open("notes.txt")', None, 30, _SESSION, disable_sandbox = True)
+ != tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ finally:
+ tools._studio_auth_markers_cache = None
+
+
+def test_a_foreign_studio_home_variable_is_not_this_installs_root(
+ studio_home, monkeypatch, tmp_path
+):
+ home = studio_home
+ # `STUDIO_HOME` is a generic name another application can own. Registered unconditionally, the
+ # recursive-root check refused a walk of that application's tree in every permission mode.
+ other = tmp_path / "other-app"
+ other.mkdir()
+ monkeypatch.setenv("STUDIO_HOME", str(other))
+ assert (
+ tools._bash_exec(
+ 'find "$STUDIO_HOME" -type f -exec cat {} +',
+ None,
+ 30,
+ _SESSION,
+ disable_sandbox = True,
+ )
+ != tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ assert (
+ tools._bash_exec(
+ 'find "$UNSLOTH_STUDIO_HOME" -type f -exec cat {} +',
+ None,
+ 30,
+ _SESSION,
+ disable_sandbox = True,
+ )
+ == tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+
+
+def test_a_definition_time_call_is_not_inert(studio_home):
+ home = studio_home
+ # Default arguments and decorators run when the function is DEFINED, so the move happens even
+ # though nothing calls `f`. Only the BODY of an uncalled function is inert.
+ read = "print(open('auth/auth.db','rb').read())"
+ for code in (
+ f"import os\ndef f(x = os.chdir('../..')): pass\n{read}",
+ f"import os\[email protected]('../..')\ndef f(): pass\n{read}",
+ ):
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), code
+ # A call in the body of a function nothing invokes still moves nothing.
+ inert = f"import os\ndef f():\n os.chdir('../..')\n{read}"
+ assert tools._python_exec(inert, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+
+
+def test_a_shell_local_studio_home_wins_over_the_backend_one(studio_home):
+ home = studio_home
+ # An assignment that stands as a command of its own rebinds what FOLLOWS it, so a command that
+ # rebinds the variable before using it names its own directory. Two things it does not cover: a
+ # PREFIX assignment, which POSIX expands the rest of its own command line before applying, and
+ # an assignment after a use, which rebinds nothing for that use.
+ for ordinary in (
+ 'UNSLOTH_STUDIO_HOME=/tmp/project; cat "$UNSLOTH_STUDIO_HOME/auth/config.json"',
+ 'export UNSLOTH_STUDIO_HOME=/tmp/p\ncat "$UNSLOTH_STUDIO_HOME/auth/config.json"',
+ "UNSLOTH_STUDIO_HOME=/tmp/a; UNSLOTH_STUDIO_HOME=/tmp/b;"
+ ' cat "$UNSLOTH_STUDIO_HOME/auth/config.json"',
+ ):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+ for refused in (
+ 'cat "$UNSLOTH_STUDIO_HOME/auth/auth.db"; UNSLOTH_STUDIO_HOME=/tmp/project',
+ # The LAST assignment is the one the expansion applies, so a read between two of them
+ # still happens under the real root.
+ 'UNSLOTH_STUDIO_HOME=$UNSLOTH_STUDIO_HOME; sqlite3 "$UNSLOTH_STUDIO_HOME/auth/auth.db"'
+ " .dump; UNSLOTH_STUDIO_HOME=/tmp",
+ # A PREFIX assignment does not govern its own command's expansion: the shell builds the
+ # word list first, so this still opens the INHERITED home's database.
+ 'UNSLOTH_STUDIO_HOME=/tmp sqlite3 "$UNSLOTH_STUDIO_HOME/auth/auth.db" .dump',
+ 'UNSLOTH_STUDIO_HOME=/tmp/project cat "$UNSLOTH_STUDIO_HOME/auth/auth.db"',
+ f'UNSLOTH_STUDIO_HOME={home}; cat "$UNSLOTH_STUDIO_HOME/auth/auth.db"',
+ # A quoted assignment is DATA that `echo` prints, so the later expansion still uses the
+ # inherited home and the database read is a real one.
+ 'echo " UNSLOTH_STUDIO_HOME=/tmp;"; sqlite3 "$UNSLOTH_STUDIO_HOME/auth/auth.db" .dump',
+ # An assignment inside `( ... )` binds only the subshell, so the outer command still
+ # expands the inherited home.
+ '(UNSLOTH_STUDIO_HOME=/tmp); sqlite3 "$UNSLOTH_STUDIO_HOME/auth/auth.db" .dump',
+ # A backslash escapes the quote it precedes, so the region is still open and the
+ # assignment inside it is still data.
+ 'echo "x \\" UNSLOTH_STUDIO_HOME=/tmp;"; sqlite3 "$UNSLOTH_STUDIO_HOME/auth/auth.db" .dump',
+ ):
+ assert tools._bash_exec(refused, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), refused
+
+
+def test_a_path_walked_up_from_getcwd_is_resolved(studio_home):
+ home = studio_home
+ # `os.path.dirname(os.path.dirname(os.getcwd()))` walks out of the sandbox without writing a
+ # `..`, so the workdir was never handed to the analyzer that resolves `getcwd` itself.
+ code = (
+ "import os, sqlite3\nroot = os.path.dirname(os.path.dirname(os.getcwd()))\n"
+ 'sqlite3.connect(os.path.join(root, "auth", "auth.db"))'
+ )
+ assert tools._python_exec(code, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ assert (
+ tools._python_exec(
+ "import os\nprint(os.getcwd())", None, 30, _SESSION, disable_sandbox = True
+ )
+ != tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+
+
+def test_a_foreign_studio_home_value_is_not_read_as_this_installs_root(
+ studio_home, monkeypatch, tmp_path
+):
+ home = studio_home
+ # Bypass keeps a foreign `STUDIO_HOME` in the child, so python that reads it names the other
+ # application's directory. The variable NAME alone was enough to refuse the call.
+ other = tmp_path / "other-app"
+ other.mkdir()
+ monkeypatch.setenv("STUDIO_HOME", str(other))
+ for ordinary in (
+ 'import os\nopen(os.environ["STUDIO_HOME"] + "/auth/config.json").read()',
+ 'import os\nopen(os.getenv("STUDIO_HOME") + "/auth/config.json").read()',
+ ):
+ assert tools._python_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+ refused = 'import os\nopen(os.environ["UNSLOTH_STUDIO_HOME"] + "/auth/auth.db").read()'
+ assert tools._python_exec(refused, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+
+
+def test_a_foreign_environment_variable_is_not_the_studio_home(studio_home):
+ home = studio_home
+ # A dynamic path piece was attributed to the studio root whenever the snippet mentioned the
+ # variable ANYWHERE, so an unrelated project directory read from a different variable was
+ # refused. The expression that built the path is what decides it.
+ ordinary = (
+ 'import os\nprint(os.environ["UNSLOTH_STUDIO_HOME"])\n'
+ 'project = os.environ["PROJECT_HOME"]\nopen(project + "/auth/config.json").read()'
+ )
+ assert tools._python_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ for refused in (
+ 'import os\nroot = os.environ["UNSLOTH_STUDIO_HOME"]\nopen(root + "/auth/auth.db").read()',
+ 'import os\nopen(os.environ["UNSLOTH_STUDIO_HOME"] + "/auth/auth.db").read()',
+ 'import os\nroot = os.getenv("UNSLOTH_STUDIO_HOME")\nopen(root + "/auth/auth.db").read()',
+ ):
+ assert tools._python_exec(refused, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), refused
+
+
+def test_windows_spellings_of_the_root_are_read_as_paths(monkeypatch, tmp_path):
+ # Platform independent, because the marker comparison is: a backslash path must not be mangled
+ # by escape processing when the words are split, and a drive-qualified marker embedded inside
+ # ANOTHER directory is a different path, exactly as a POSIX one is.
+ home = r"c:\users\runneradmin\appdata\local\temp\studio-home"
+ marker = home + r"\auth"
+ assert tools._marker_is_a_path_segment("cat " + home + r"\auth\auth.db", marker)
+ assert not tools._marker_is_a_path_segment(
+ r"cat /mnt/backup" + home + r"\auth\config.json", marker
+ )
+ assert tools._quoted_words(r'find "C:\Users\me\Unsloth Studio" -type f') == [
+ "find",
+ r"C:\Users\me\Unsloth Studio",
+ "-type",
+ "f",
+ ]
+
+
+def test_an_escaped_space_in_the_root_still_matches(monkeypatch):
+ # The default root has a space in it, so `cp -a /tmp/Studio\\ Home .` is the ordinary POSIX
+ # spelling of the root itself and has to fold to the same directory as the quoted form.
+ root = "/tmp/unsloth/studio home"
+ monkeypatch.setattr(tools, "_studio_root_spellings", lambda: [root])
+ for named in (
+ "cp -a /tmp/unsloth/studio\\ home ./leak",
+ 'cp -a "/tmp/unsloth/studio home" ./leak',
+ ):
+ assert tools._names_the_studio_root_itself(named), named
+ assert not tools._names_the_studio_root_itself(
+ "cp -a /tmp/unsloth/studio\\ home/projects/p ./d"
+ )
+
+
+def test_the_root_spellings_cache_follows_a_changed_home(studio_home, tmp_path, monkeypatch):
+ # The spellings are memoized per command, so a home that changes has to invalidate them or the
+ # guard keeps answering for the previous install.
+ assert tools._references_studio_credential_here(f'cp -a "{studio_home}" ./leak', None)
+ other = tmp_path / "other-home"
+ (other / "auth").mkdir(parents = True)
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(other))
+ tools._studio_auth_markers_cache = None
+ assert tools._references_studio_credential_here(f'cp -a "{other}" ./leak', None)
+ assert not tools._references_studio_credential_here(f'cp -a "{studio_home}" ./leak', None)
+
+
+def test_a_windows_root_is_matched_through_the_same_fold(monkeypatch):
+ # `_folded_word` turns a backslash root into forward slashes, so the spellings it is compared
+ # against have to be folded the same way. This shape only occurs on Windows, so it is driven
+ # through the spellings directly and runs on every host.
+ root = r"C:\Users\runneradmin\AppData\Local\Temp\pytest-0\test_x0\studio-home"
+ monkeypatch.setattr(tools, "_studio_root_spellings", lambda: [root.lower()])
+ for named in (
+ f"find {root} -type f -exec cat {{}} +",
+ f'tar -czf b.tgz "{root}"',
+ f"cp -a {root}\\. ./copy",
+ ):
+ assert tools._names_the_studio_root_itself(named), named
+ for ordinary in (f"find {root}\\projects\\p -type f", "find . -type f"):
+ assert not tools._names_the_studio_root_itself(ordinary), ordinary
+
+
+def test_a_windows_root_survives_python_literal_escaping(monkeypatch):
+ # A python literal doubles the separators, so the cheap prefilter needs that spelling too.
+ root = r"C:\Users\runner\Temp\studio-home"
+ monkeypatch.setattr(tools, "_studio_home_lowered_cache", None)
+ monkeypatch.setattr(tools, "_studio_home_for_guard", lambda: root)
+ monkeypatch.setattr(
+ tools,
+ "_studio_auth_dir_markers",
+ lambda: (((root + "/auth", "", root + "/auth"),), (), None),
+ )
+ code = "import shutil\nshutil.copytree(%r, './leak')" % root
+ assert any(
+ spelling in code.lower() for spelling in tools._studio_home_spellings_lowered()
+ ), code
+ # The same doubled spelling has to satisfy the root-naming test the guard gates on.
+ monkeypatch.setattr(tools, "_studio_root_spellings", lambda: [root.lower()])
+ assert tools._text_names_the_studio_root(code), code
+
+
+def test_a_recursive_read_of_the_studio_root_is_refused(studio_home):
+ home = studio_home
+ # `find "$UNSLOTH_STUDIO_HOME" -type f -exec cat {} +` names no credential and no auth segment,
+ # but the child inherits the variable and emits `auth/auth.db` and `.bootstrap_password`. Only
+ # the forms that READ or COPY the tree count: a listing names files without opening them, and
+ # work inside a project under the root is ordinary.
+ for command in (
+ 'find "$UNSLOTH_STUDIO_HOME" -type f -exec cat {} +',
+ 'find "$UNSLOTH_STUDIO_HOME" -type f | xargs cat',
+ f"find {home} -type f -exec cat {{}} +",
+ f'tar -czf backup.tgz "{home}"',
+ f'grep -r sk-unsloth "{home}"',
+ f'cp -a "{home}" ./copy',
+ f'rsync -av "{home}/" ./copy',
+ # `7z a` recurses into a directory with no flag at all.
+ f'7z a out.7z "{home}"',
+ # The walker behind a wrapper, whose option value must not read as the command.
+ 'env -u FOO tar -czf b.tgz "$UNSLOTH_STUDIO_HOME"',
+ 'timeout 5 tar -czf b.tgz "$UNSLOTH_STUDIO_HOME"',
+ # `<root>/.` is the root itself: `cp --help` defines `-a` as recursive.
+ 'cp -a "$UNSLOTH_STUDIO_HOME"/. ./copy',
+ 'cp -a "$UNSLOTH_STUDIO_HOME"/./ ./copy',
+ f'zip -r out.zip "{home}"',
+ ):
+ assert tools._bash_exec(command, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), command
+ for ordinary in (
+ f'find "{home}" -type f',
+ f'ls -la "{home}"',
+ f'cp -r ./out "{home}/projects/p/sandbox/"',
+ f'grep -r TODO "{home}/projects/p"',
+ "grep -r TODO .",
+ "tar -czf out.tgz .",
+ "7z a out.7z src",
+ f'du -sh "{home}"',
+ # A walker's NAME as data is not a walk: `echo` is what runs here.
+ 'echo tar "$UNSLOTH_STUDIO_HOME"',
+ 'echo "rsync is a tool" "$UNSLOTH_STUDIO_HOME"',
+ 'printf "%s" "$UNSLOTH_STUDIO_HOME"',
+ "cp -a ./src/. ./copy",
+ 'cp -a "$UNSLOTH_STUDIO_HOME/projects/p/." ./copy',
+ ):
+ assert tools._bash_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_a_recursive_python_copy_of_the_studio_root_is_refused(studio_home):
+ # `shutil.copytree(<root>, "/tmp/leak")` carries `auth/` with it and names no credential, and
+ # the copy is then an ordinary file nothing guards.
+ for refused in (
+ 'import shutil, os\nshutil.copytree(os.environ["UNSLOTH_STUDIO_HOME"], "./leak")',
+ # The constructor wraps the name of the source rather than replacing it.
+ "import shutil, os\nfrom pathlib import Path\n"
+ 'shutil.copytree(Path(os.environ["UNSLOTH_STUDIO_HOME"]), "./leak")',
+ # A recursive WALK of the root reaches the same files a copy of it does.
+ "import os\nfrom pathlib import Path\n"
+ 'for p in Path(os.environ["UNSLOTH_STUDIO_HOME"]).rglob("*"):\n print(p.read_text())',
+ 'import os\nfor r, d, f in os.walk(os.environ["UNSLOTH_STUDIO_HOME"]):\n print(f)',
+ # One binding between the root and the walker resolves to the same directory.
+ 'import os\nroot = os.environ["UNSLOTH_STUDIO_HOME"]\n'
+ "for r, d, f in os.walk(root):\n print(f)",
+ 'import shutil, os\nshutil.copytree(src = os.environ["UNSLOTH_STUDIO_HOME"], dst = "./l")',
+ 'import shutil, os\nshutil.make_archive("b", "zip", os.environ["UNSLOTH_STUDIO_HOME"])',
+ f'import shutil\nshutil.copytree({str(studio_home)!r}, "./leak")',
+ ):
+ assert tools._python_exec(refused, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), refused
+ for ordinary in (
+ 'import shutil\nshutil.copytree("./src", "./dst")',
+ 'import shutil\nshutil.make_archive("b", "zip", "./src")',
+ # A SHALLOW listing returns the root's own entry names and opens nothing inside `auth`,
+ # exactly as the terminal side's plain root listing does.
+ 'import os\nprint(os.listdir(os.environ["UNSLOTH_STUDIO_HOME"]))',
+ "import os\nfrom pathlib import Path\n"
+ 'print(list(Path(os.environ["UNSLOTH_STUDIO_HOME"]).iterdir()))',
+ f'import shutil\nshutil.copytree({str(studio_home / "projects" / "p")!r}, "./dst")',
+ ):
+ assert tools._python_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ ), ordinary
+
+
+def test_a_parent_walk_from_path_home_lands_in_the_studio_root(studio_home):
+ # Both environment builders set HOME to the session workdir, so `Path.home()` is `Path.cwd()`.
+ refused = 'from pathlib import Path\nprint(open(Path.home().parents[1] / "auth" / "auth.db", "rb").read())'
+ assert tools._python_exec(refused, None, 30, _SESSION, disable_sandbox = True) == (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+ ordinary = 'from pathlib import Path\nprint(open(Path.home() / "notes.txt").read())'
+ assert tools._python_exec(ordinary, None, 30, _SESSION, disable_sandbox = True) != (
+ tools._STUDIO_CREDENTIAL_BLOCKED
+ )
+
+
+def test_two_open_secrets_in_one_chunk_hold_from_the_first(studio_home):
+ # `rfind` walks right to left and the loop stopped at the first hit, so a chunk holding two
+ # open tokens emitted the earlier partial key.
+ from core.inference.tool_stream_exec import _hold_back_partial_secret
+ assert _hold_back_partial_secret("sk-unsloth-abcdsk-unsloth-") == 0
+ assert _hold_back_partial_secret("sk-unsloth-0123 done") == len("sk-unsloth-0123 done")
+
+
+def test_a_snippet_the_parser_cannot_hold_is_a_decision(studio_home):
+ # `ast.parse` raises RecursionError on ~10k chained operators. Caught here, the guard returns a
+ # decision; uncaught, the tool raised an internal exception instead of the interpreter's own
+ # error. The credential hint is what gets the snippet this far at all.
+ huge = "x = " + "+".join(["1"] * 20000) + "\n# auth\n"
+ workdir = str(studio_home / "sandbox" / _SESSION)
+ assert tools._python_builds_a_credential_path(huge, workdir) is False
+
+
+def test_a_long_run_of_escapes_is_a_decision_rather_than_a_crash(studio_home):
+ home = studio_home
+ # Unescaping recursed once per backslash, so 550 of them raised RecursionError out of the guard,
+ # before the command was classified at all.
+ workdir = str(home / "sandbox" / _SESSION)
+ assert tools._references_studio_credential_here("echo " + "\\" * 1100 + "a", workdir) is (False)
+ # The one level it needs still works.
+ assert (
+ tools._references_studio_credential_here(
+ 'c\\d ../..; sqlite3 auth/auth.db "select jwt_secret from auth_user"', workdir
+ )
+ is True
+ )
+
+
+def test_a_bare_cd_is_a_move_back_to_the_sandbox(studio_home):
+ home = studio_home
+ # Both sandbox environments set HOME to the tool workdir, so a bare `cd` returns there and the
+ # commands after it open from the sandbox again. Reading it as no move at all kept the studio
+ # root live and refused `cd ../..; cd; cat auth/config.json`, which reads the project's own file.
+ workdir = str(home / "sandbox" / _SESSION)
+ for ordinary in (
+ "cd ../..; cd; cat auth/config.json",
+ "cd ../..; cd --; ls -a auth",
+ "cd; cat notes.txt",
+ "cd ../..; cd; cd auth; cat auth.db",
+ ):
+ assert tools._references_studio_credential_here(ordinary, workdir) is False, ordinary
+ # ...and the return must not cost the real reads. A bare `cd` inside a subshell moves only
+ # that subshell, and `cd -` after it goes back to the root.
+ for refused in (
+ "cd ../..; cat auth/auth.db",
+ "cd ../..; (cd); cat auth/auth.db",
+ "cd ../..; cd; cd -; cat auth/auth.db",
+ "cd ../..; cd; cat ../../auth/auth.db",
+ ):
+ assert tools._references_studio_credential_here(refused, workdir) is True, refused
+
+
+def test_a_return_reopens_the_directory_it_lands_in(studio_home):
+ home = studio_home
+ # `cd -` and `popd` land somewhere, and that is where every later relative path opens from.
+ # Restoring the state without re-opening its span left nothing covering the read, so
+ # `cd ../..; cd sandbox; cd -; cat auth/auth.db` walked back to the root unnoticed.
+ workdir = str(home / "sandbox" / _SESSION)
+ for refused in (
+ "cd ../..; cd sandbox; cd -; cat auth/auth.db",
+ "cd ../..; pushd /tmp; popd; cat auth/auth.db",
+ ):
+ assert tools._references_studio_credential_here(refused, workdir) is True, refused
+ # A return that lands back in the sandbox is ordinary work.
+ for ordinary in (
+ "cd sub; cd -; cat auth/config.json",
+ "pushd sub; popd; ls -a auth",
+ ):
+ assert tools._references_studio_credential_here(ordinary, workdir) is False, ordinary
+
+
+def test_every_home_variable_is_checked_before_the_expansion(studio_home):
+ home = studio_home
+ # The expansion rewrites all of the studio-home names from their last assignment, so one name
+ # that is only assigned must not authorize rewriting another whose assignment comes AFTER its
+ # use: the shell expands that use to the INHERITED home, which is this install's own.
+ workdir = str(home / "sandbox" / _SESSION)
+ assert (
+ tools._references_studio_credential_here(
+ 'STUDIO_HOME=/tmp/p; cat "$UNSLOTH_STUDIO_HOME/auth/auth.db"; '
+ "UNSLOTH_STUDIO_HOME=/tmp/p",
+ workdir,
+ )
+ is True
+ )
+ # A genuine rebind before the use still names the caller's own directory, not this install's.
+ assert (
+ tools._references_studio_credential_here(
+ 'UNSLOTH_STUDIO_HOME=/tmp/project; cat "$UNSLOTH_STUDIO_HOME/auth/config.json"',
+ workdir,
+ )
+ is False
+ )
diff --git a/unsloth_cli/commands/studio.py b/unsloth_cli/commands/studio.py
index 3f09582b200..c9ec87e43d3 100644
--- a/unsloth_cli/commands/studio.py
+++ b/unsloth_cli/commands/studio.py
@@ -771,6 +771,12 @@ def _load_backend_auth_storage():
def _write_auth_secret(path: Path, secret: str) -> None:
path.parent.mkdir(parents = True, exist_ok = True)
+ # mkdir under a 022 umask leaves auth/ world-readable when this runs before the DB connection does it; the files
+ # below are 0600 either way, but the directory listing names them. Best-effort, like the chmods below.
+ try:
+ os.chmod(path.parent, 0o700)
+ except OSError:
+ pass
fd, tmp_name = tempfile.mkstemp(prefix = f".{path.name}.", dir = path.parent)
tmp_path = Path(tmp_name)
try: