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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: harden Data Recipe engine inputs (#14424)
Daniel Hanf7325b464a73
diff --git a/studio/backend/core/data_recipe/oxc-validator/oxlintrc.json b/studio/backend/core/data_recipe/oxc-validator/oxlintrc.jsonnew file mode 100644index 00000000000..0967ef424bc--- /dev/null+++ b/studio/backend/core/data_recipe/oxc-validator/oxlintrc.json@@ -0,0 +1 @@+{}diff --git a/studio/backend/core/data_recipe/oxc-validator/validate.mjs b/studio/backend/core/data_recipe/oxc-validator/validate.mjsindex d51921fca13..6682e581e8e 100644--- a/studio/backend/core/data_recipe/oxc-validator/validate.mjs+++ b/studio/backend/core/data_recipe/oxc-validator/validate.mjs@@ -26,6 +26,8 @@ const OXLINT_DEFAULT_BUDGET_MS = 30_000;const OXLINT_BUDGET_MARGIN_MS = 2_000;const OXLINT_MIN_TIMEOUT_MS = 1_000;const TOOL_DIR = dirname(fileURLToPath(import.meta.url));+// Lint with the shipped empty config only: never one found next to or above the snippets.+const OXLINT_CONFIG_PATH = join(TOOL_DIR, "oxlintrc.json");function mapLang(value) {const normalized = String(value || "").trim().toLowerCase();@@ -400,6 +402,9 @@ function runLintBatch(entries, budgetMs) {const oxlintBin = join(TOOL_DIR, "node_modules", ".bin", "oxlint");const oxlintArgs = [+ "--config",+ OXLINT_CONFIG_PATH,+ "--disable-nested-config",...OXLINT_SUPPRESSED_RULES.flatMap((rule) => ["-A", rule]),"--format","json",diff --git a/studio/backend/core/data_recipe/service.py b/studio/backend/core/data_recipe/service.pyindex 8e53f5b09d9..0a485300587 100644--- a/studio/backend/core/data_recipe/service.py+++ b/studio/backend/core/data_recipe/service.py@@ -131,6 +131,7 @@ def _apply_data_designer_image_context_patch() -> None:try:from data_designer.config.models import ImageContext # pyright: ignore[reportMissingImports]+ from data_designer.config.utils.image_helpers import is_image_path # pyright: ignore[reportMissingImports]except ImportError:return@@ -142,7 +143,17 @@ def _apply_data_designer_image_context_patch() -> None:def _patched_auto_resolve(self: Any, context_value: Any, base_path: str | None) -> Any:normalized = _normalize_image_context_value(context_value, base_path = base_path)- return original_auto_resolve(self, normalized, base_path)+ if not managed_account():+ return original_auto_resolve(self, normalized, base_path)+ # Managed image paths load under the workspace rule; with no base_path the engine never opens files.+ if (+ isinstance(normalized, str)+ and not normalized.lower().startswith(("http://", "https://", "data:"))+ and is_image_path(normalized)+ ):+ if as_base64 := _load_image_file_to_base64(normalized, base_path = base_path):+ return self._format_base64_context(as_base64)+ return original_auto_resolve(self, normalized, None)ImageContext._auto_resolve_context_value = _patched_auto_resolvesetattr(ImageContext, "_unsloth_image_context_patch_applied", True)@@ -397,6 +408,53 @@ def _apply_data_designer_mixed_type_parquet_patch() -> None:setattr(ArtifactStorage, "_unsloth_mixed_type_parquet_patch_applied", True)+def _apply_data_designer_seed_uri_quote_patch() -> None:+ # Seeds are read as FROM '<uri>': quote it for DuckDB. No ImportError guard: never run unquoted.+ from data_designer.engine.resources.seed_reader import SeedReader # pyright: ignore[reportMissingImports]++ if getattr(SeedReader, "_unsloth_seed_uri_quote_patch_applied", False):+ return++ original_attach = SeedReader.attach++ @functools.wraps(original_attach)+ def _patched_attach(self: Any, *args: Any, **kwargs: Any) -> None:+ # Calls the class method, so attaching twice never quotes twice.+ self.get_dataset_uri = lambda: type(self).get_dataset_uri(self).replace("'", "''")+ original_attach(self, *args, **kwargs)++ SeedReader.attach = _patched_attach+ setattr(SeedReader, "_unsloth_seed_uri_quote_patch_applied", True)+++def _ignore_sqlfluff_inline_config(*_args: Any, **_kwargs: Any) -> None:+ return None+++def _apply_data_designer_sql_inline_config_patch() -> None:+ # sqlfluff applies inline "-- sqlfluff:" config from the linted text (recipe data); code_lang sets the dialect.+ try:+ from data_designer.engine.validators.sql import SQLValidator # pyright: ignore[reportMissingImports]+ except ImportError:+ return++ if getattr(SQLValidator, "_unsloth_sql_inline_config_patch_applied", False):+ return++ original_validate = SQLValidator._validate_query++ @functools.wraps(original_validate)+ def _patched_validate(self: Any, content: str) -> Any:+ # sqlfluff is imported lazily, on the first SQL cell.+ from sqlfluff.core import FluffConfig # pyright: ignore[reportMissingImports]+ if FluffConfig.process_raw_file_for_config is not _ignore_sqlfluff_inline_config:+ FluffConfig.process_raw_file_for_config = _ignore_sqlfluff_inline_config+ return original_validate(self, content)++ SQLValidator._validate_query = _patched_validate+ setattr(SQLValidator, "_unsloth_sql_inline_config_patch_applied", True)++def _require_public_provider_endpoint(endpoint: str) -> None:"""The recipe engine dials providers itself, so a managed account's endpoint cannot use the pinnedtransport: require HTTPS, which binds the peer to its certificate rather than to a DNS answer that@@ -670,6 +728,8 @@ def create_data_designer(recipe: dict[str, Any], *, artifact_path: str | None =_apply_data_designer_prompt_blank_patch()_apply_data_designer_expression_blank_patch()_apply_data_designer_mixed_type_parquet_patch()+ _apply_data_designer_seed_uri_quote_patch()+ _apply_data_designer_sql_inline_config_patch()from data_designer.interface.data_designer import DataDesigner # pyright: ignore[reportMissingImports]if artifact_path is None:diff --git a/studio/backend/core/training/account_jobs.py b/studio/backend/core/training/account_jobs.pyindex bc891e0b809..fdcccefe0f1 100644--- a/studio/backend/core/training/account_jobs.py+++ b/studio/backend/core/training/account_jobs.py@@ -512,6 +512,22 @@ def validate_recipe_access(recipe) -> None:seed_type = recipe.get("seed_type")if seed_type in {"hf", "github_repo"} and not str(recipe.get("token") or "").strip():raise HTTPException(status_code = 403, detail = "Supply your own seed-source token")+ # A folder seed with no path falls back to host folders (cwd, ~/.codex).+ if (+ seed_type in {"agent_rollout", "directory", "file_contents"}+ and not str(recipe.get("path") or "").strip()+ ):+ raise HTTPException(status_code = 403, detail = "Choose a folder inside your workspace")+ # The engine writes a processor's output under its name, so the name must not be a path.+ processors = recipe.get("processors")+ for processor in processors if isinstance(processors, list) else []:+ name = processor.get("name") if isinstance(processor, dict) else None+ if (+ not isinstance(name, str)+ or not name.strip(" .")+ or any(char in '<>:"/\\|?*' or not char.isprintable() for char in name)+ ):+ raise HTTPException(status_code = 403, detail = "Processor names must be plain names")for key, value in recipe.items():if key in {"api_key_env", "token_env", "hf_token_env"} and value:raise HTTPException(status_code = 403, detail = "Supply your own provider credential")diff --git a/studio/backend/tests/test_data_recipe_preview_image_paths.py b/studio/backend/tests/test_data_recipe_preview_image_paths.pyindex 06a6b2694b0..d377f723315 100644--- a/studio/backend/tests/test_data_recipe_preview_image_paths.py+++ b/studio/backend/tests/test_data_recipe_preview_image_paths.py@@ -5,6 +5,8 @@from __future__ import annotations+import base64+import osfrom pathlib import Pathimport pytest@@ -153,6 +155,77 @@ def test_managed_account_image_file_lookup_skips_the_cwd_fallback(studio_home, massert _load_image_file_to_base64("private.png", base_path = str(base)) is not None+def _image_contexts(cell, base_path: Path) -> list[dict]:+ from core.data_recipe.service import _apply_data_designer_image_context_patch++ models = pytest.importorskip("data_designer.config.models")+ _apply_data_designer_image_context_patch()+ return models.ImageContext(column_name = "img").get_contexts(+ {"img": cell}, base_path = str(base_path)+ )+++def _sent_image_data(contexts: list[dict]) -> str:+ (context,) = contexts+ return context["image_url"]["url"].split(",", 1)[1]++[email protected]("spelling", [str, lambda path: {"path": path}])+def test_managed_account_image_context_cell_outside_its_workspace_is_refused(studio_home, spelling):+ from fastapi import HTTPException++ secret = _png(studio_home / "elsewhere" / "private.png")+ base = run_as(ALICE, lambda: workspace_root() / "artifacts")++ for cell in (spelling(str(secret)), spelling(os.path.relpath(secret, base))):+ with pytest.raises(HTTPException) as refused:+ run_as(ALICE, _image_contexts, cell, base)+ assert refused.value.status_code == 403++[email protected]("spelling", [str, lambda path: {"path": path}])+def test_managed_account_image_context_cell_does_not_fall_back_to_the_cwd(+ studio_home, monkeypatch, spelling+):+ _png(studio_home / "cwd" / "private.png")+ monkeypatch.chdir(studio_home / "cwd")+ base = run_as(ALICE, lambda: workspace_root() / "artifacts")++ # Not a file to the engine either: it takes the bare name for base64 data and rejects it.+ with pytest.raises(ValueError):+ run_as(ALICE, _image_contexts, spelling("private.png"), base)++[email protected]("spelling", [str, lambda path: {"path": path}])+def test_managed_account_image_context_cell_inside_its_workspace_is_sent(studio_home, spelling):+ own = run_as(ALICE, lambda: _png(workspace_root() / "data" / "mine.png"))+ base = run_as(ALICE, lambda: workspace_root() / "artifacts")++ contexts = run_as(ALICE, _image_contexts, spelling(str(own)), base)++ assert _sent_image_data(contexts) == base64.b64encode(own.read_bytes()).decode()+++def test_managed_account_image_context_url_cell_passes_through(studio_home):+ base = run_as(ALICE, lambda: workspace_root() / "artifacts")++ for url in ("https://example.com/cat.png", "https://example.com/../../../../../cat.png"):+ (context,) = run_as(ALICE, _image_contexts, url, base)+ assert context["image_url"] == url+++def test_owner_image_context_cell_loads_any_local_path(studio_home, monkeypatch):+ image = _png(studio_home / "elsewhere" / "local.png")+ _png(studio_home / "cwd" / "local-cwd.png")+ monkeypatch.chdir(studio_home / "cwd")+ base = studio_home / "artifacts"++ sent = _sent_image_data(_image_contexts(str(image), base))++ assert sent == base64.b64encode(image.read_bytes()).decode()+ assert _sent_image_data(_image_contexts("local-cwd.png", base))++def test_managed_account_hub_hosted_images_still_preview(studio_home, monkeypatch):datasets = pytest.importorskip("datasets")hub = "hf://datasets/org/repo@0123abc/cat.png"diff --git a/studio/backend/tests/test_data_recipe_seed_path_quote.py b/studio/backend/tests/test_data_recipe_seed_path_quote.pynew file mode 100644index 00000000000..ed12aadfe4c--- /dev/null+++ b/studio/backend/tests/test_data_recipe_seed_path_quote.py@@ -0,0 +1,55 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++from pathlib import Path++import pytest+++def _preview(tmp_path: Path, seed: Path):+ from core.data_recipe.service import build_config_builder, create_data_designer++ recipe = {+ "seed_config": {"source": {"seed_type": "local", "path": str(seed)}},+ "columns": [{"column_type": "expression", "name": "copy", "expr": "{{ text }}"}],+ }+ designer = create_data_designer(recipe, artifact_path = str(tmp_path / "artifacts"))+ return designer.preview(build_config_builder(recipe), num_records = 1).dataset+++def test_seed_file_name_cannot_close_the_duckdb_literal(tmp_path: Path) -> None:+ # The engine reads a seed as FROM '<path>': this name joined any host file onto a.csv.+ pytest.importorskip("data_designer")+ (tmp_path / "secret.txt").write_text("secret", encoding = "utf-8")+ (tmp_path / "a.csv").write_text("text\nhello\n", encoding = "utf-8")+ target = str(tmp_path / "secret.txt").replace("/", "@")+ seed = tmp_path / (+ f"a.csv', (SELECT content AS stolen FROM read_text(replace('{target}', '@', chr(47)))) t -- .csv"+ )+ seed.write_text("text\nnamed\n", encoding = "utf-8")++ dataset = _preview(tmp_path, seed)++ assert sorted(dataset.columns) == ["copy", "text"]+ assert list(dataset["copy"]) == ["named"]+++def test_seed_file_name_with_an_apostrophe_is_readable(tmp_path: Path) -> None:+ pytest.importorskip("data_designer")+ seed = tmp_path / "bob's rows.csv"+ seed.write_text("text\nhello\n", encoding = "utf-8")++ assert list(_preview(tmp_path, seed)["copy"]) == ["hello"]+++def test_hub_seed_path_cannot_close_the_duckdb_literal() -> None:+ pytest.importorskip("data_designer")+ from core.data_recipe.service import _apply_data_designer_seed_uri_quote_patch+ from data_designer.config.seed_source import HuggingFaceSeedSource+ from data_designer.engine.resources.seed_reader import HuggingFaceSeedReader++ _apply_data_designer_seed_uri_quote_patch()+ reader = HuggingFaceSeedReader()+ reader.attach(HuggingFaceSeedSource(path = "datasets/a/b.parquet'; SELECT 1 -- "), None)++ assert reader.get_dataset_uri() == "hf://datasets/a/b.parquet''; SELECT 1 -- "diff --git a/studio/backend/tests/test_data_recipe_sql_inline_config.py b/studio/backend/tests/test_data_recipe_sql_inline_config.pynew file mode 100644index 00000000000..73b77bc8094--- /dev/null+++ b/studio/backend/tests/test_data_recipe_sql_inline_config.py@@ -0,0 +1,51 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++from pathlib import Path++import pytest+++def _check(tmp_path: Path, sql: str) -> dict:+ from core.data_recipe.service import build_config_builder, create_data_designer++ recipe = {+ "columns": [+ {"column_type": "expression", "name": "query", "expr": sql},+ {+ "column_type": "validation",+ "name": "check",+ "validator_type": "code",+ "validator_params": {"code_lang": "sql:ansi"},+ "target_columns": ["query"],+ },+ ],+ }+ designer = create_data_designer(recipe, artifact_path = str(tmp_path / "artifacts"))+ return designer.preview(build_config_builder(recipe), num_records = 1).dataset["check"][0]++[email protected]("comment", ["-- sqlfluff", "--sqlfluff"])+def test_sql_cell_cannot_name_a_folder_to_import(tmp_path: Path, comment: str) -> None:+ # sqlfluff reads "-- sqlfluff:<key>:<value>" lines of the linted text as its own config, and+ # templater:jinja:library_path imports every module under that folder in the recipe worker.+ pytest.importorskip("data_designer")+ library = tmp_path / "library"+ library.mkdir()+ imported = tmp_path / "imported.txt"+ (library / "helper_macros.py").write_text(+ f"open({str(imported)!r}, 'w').close()\n", encoding = "utf-8"+ )++ result = _check(tmp_path, f"{comment}:templater:jinja:library_path:{library}\nSELECT 1")++ assert result["is_valid"]+ assert not imported.exists()+++def test_sql_cell_cannot_pick_its_own_dialect(tmp_path: Path) -> None:+ pytest.importorskip("data_designer")+ tsql_only = "SELECT [a] FROM [t]"++ assert not _check(tmp_path, tsql_only)["is_valid"]+ assert not _check(tmp_path, f"-- sqlfluff:dialect:tsql\n{tsql_only}")["is_valid"]diff --git a/studio/backend/tests/test_job_accounts.py b/studio/backend/tests/test_job_accounts.pyindex ddc61bf7ed2..6b449c8c514 100644--- a/studio/backend/tests/test_job_accounts.py+++ b/studio/backend/tests/test_job_accounts.py@@ -1029,6 +1029,47 @@ def test_hugging_face_recipe_seeds_use_explicit_account_credentials():run_as(ALICE, jobs.validate_recipe_access, {"source": {**source, "token": ""}})+ "source",+ [+ {"seed_type": "agent_rollout", "format": "codex"},+ {"seed_type": "agent_rollout", "format": "claude_code", "path": None},+ {"seed_type": "agent_rollout", "format": "codex", "path": ""},+ {"seed_type": "directory", "path": ""},+ {"seed_type": "file_contents", "path": ""},+ ],+)+def test_folder_recipe_seeds_cannot_fall_back_to_a_host_folder(source):+ with pytest.raises(HTTPException):+ run_as(ALICE, jobs.validate_recipe_access, {"seed_config": {"source": source}})+ run_as(OWNER, jobs.validate_recipe_access, {"seed_config": {"source": source}})+ own = {**source, "path": str(run_as(ALICE, workspace_root) / "rollouts")}+ run_as(ALICE, jobs.validate_recipe_access, {"seed_config": {"source": own}})+++ "name",+ ["/tmp/elsewhere", "../elsewhere", "..\\elsewhere", "nested/name", "C:x", "..", "", None],+)+def test_recipe_processor_names_cannot_be_paths(name):+ recipe = {"processors": [{"processor_type": "schema_transform", "name": name, "template": {}}]}+ with pytest.raises(HTTPException):+ run_as(ALICE, jobs.validate_recipe_access, recipe)+ run_as(OWNER, jobs.validate_recipe_access, recipe)+++def test_recipe_plain_processor_names_pass():+ for name in ("schema_transform", "schema_transform_2", "chat format v1.2"):+ recipe = {"processors": [{"processor_type": "schema_transform", "name": name}]}+ run_as(ALICE, jobs.validate_recipe_access, recipe)+ # A schema-transform template may name an output column processor_type.+ template = {"processor_type": "{{ kind }}", "text": "{{ text }}"}+ recipe = {+ "processors": [{"processor_type": "schema_transform", "name": "out", "template": template}]+ }+ run_as(ALICE, jobs.validate_recipe_access, recipe)++@pytest.mark.parametrize("account", [OWNER, ALICE])def test_recipe_workflow_key_belongs_to_the_starting_account(account, monkeypatch):from routes.data_recipe import jobs as routediff --git a/studio/backend/tests/test_local_callable_validators.py b/studio/backend/tests/test_local_callable_validators.pyindex f2661800784..1ba55df482b 100644--- a/studio/backend/tests/test_local_callable_validators.py+++ b/studio/backend/tests/test_local_callable_validators.py@@ -6,8 +6,18 @@ import reimport subprocessfrom pathlib import Path+import pytest+from core.data_recipe import local_callable_validators as validators+_HAS_REAL_OXLINT = (+ validators.resolve_node_executable() is not None+ and (validators._OXC_TOOL_DIR / "node_modules" / "oxlint" / "package.json").is_file()+)+real_oxlint = pytest.mark.skipif(+ not _HAS_REAL_OXLINT, reason = "needs Node and oxc-validator/node_modules/oxlint"+)+def test_oxc_batch_falls_back_when_node_times_out(monkeypatch, tmp_path):runner = tmp_path / "validate.mjs"@@ -75,3 +85,38 @@ def test_the_wrapper_kills_oxlint_against_the_remaining_caller_budget():), "oxlint's bound must be the computed remainder, not a constant"# SIGTERM is ignorable, and spawnSync then waits out the child anyway.assert 'killSignal: "SIGKILL"' in options.group(1)+++@real_oxlint[email protected]("depth", [0, 1, 2])[email protected]("config_name", ["oxlint.config.ts", ".oxlintrc.json"])+def test_oxlint_never_loads_a_config_planted_around_the_snippets(+ monkeypatch, tmp_path, config_name, depth+):+ # The snippets land under the account's temp root, which its confined tools can write, while+ # oxlint runs unconfined: a config found there is the account's code run as the Studio user.+ marker = tmp_path / "config-was-imported"+ payload = f'import fs from "node:fs";\nfs.writeFileSync({json.dumps(str(marker))}, "");\n'+ oxc_tmp = tmp_path / "accounts" / "alice" / "oxc-validator"+ oxc_tmp.mkdir(parents = True)+ planted = oxc_tmp.parents[depth - 1] if depth else oxc_tmp+ if config_name.endswith(".ts"):+ (planted / config_name).write_text(payload + "export default {};\n", encoding = "utf-8")+ else:+ (planted / config_name).write_text('{"jsPlugins": ["./plugin.js"]}', encoding = "utf-8")+ (planted / "plugin.js").write_text(+ payload + 'export default { meta: { name: "planted" }, rules: {} };\n',+ encoding = "utf-8",+ )+ monkeypatch.setattr(validators, "oxc_validator_tmp_root", lambda: oxc_tmp)++ results = validators._run_oxc_batch(+ node_lang = "js",+ validation_mode = "lint",+ code_shape = "module",+ code_values = ["debugger;\n"],+ )++ assert not marker.exists()+ # Pinned to the shipped, empty config: the default rules still report.+ assert [result["code"] for result in results] == ["eslint(no-debugger)"]