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.

2.9 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

import assert from "node:assert/strict";
import { test } from "node:test";

import {
  backendPathUrl,
  desktopSignedMediaPath,
  isStudioUrl,
  protectedBackendPath,
  resetApiBase,
  setApiBase,
} from "../src/lib/api-base.ts";

test("only the backend's own URLs count as Studio's, so others never get the sign-in", () => {
  assert.equal(isStudioUrl("/api/chat/attachments/a.png"), true);
  assert.equal(isStudioUrl("https://images.example/a.png"), false);
  assert.equal(isStudioUrl("http://[bad"), false);
  setApiBase(8888);
  assert.equal(isStudioUrl("/api/chat/attachments/a.png"), true);
  assert.equal(isStudioUrl("http://127.0.0.1:8888/api/chat/attachments/a.png"), true);
  assert.equal(isStudioUrl("http://127.0.0.1:9999/a.png"), false);
  assert.equal(isStudioUrl("HTTPS://images.example/a.png"), false);
  assert.equal(isStudioUrl("//images.example/a.png"), false);
  resetApiBase();
});

test("on Desktop a backend link points at the backend and only signed media is a blob", () => {
  const signed = "/v1/audio/a/file-signed?token=t";
  assert.equal(backendPathUrl(signed), signed);
  assert.equal(desktopSignedMediaPath(signed), null);
  setApiBase(8888);
  for (const [path, onBackend, blob, session] of [
    [signed, true, true, false],
    ["/api/x/file-signed?token=t", true, true, false],
    ["/api/x/file-signed?token=t&x=1", true, false, false],
    ["/v1/audio/voices/v1/file", true, false, true],
    ["/api/gallery/x?archived=1", true, false, true],
    ["/studio/v1/audio/voices/v1/file", false, false, true],
    ["https://evil.example/api/x/file-signed?token=t", false, false, false],
    ["https://example.com/v1/x", false, false, false],
    ["//example.com/v1/x", false, false, false],
    ["/chat", false, false, false],
    ["/v1/", true, false, false],
  ] as const) {
    const url = onBackend ? `http://127.0.0.1:8888${path}` : path;
    assert.equal(backendPathUrl(path), url, path);
    assert.equal(desktopSignedMediaPath(path), blob ? path : null, path);
    assert.equal(protectedBackendPath(path), session ? path : null, path);
  }
  resetApiBase();
});

test("a model-written link that resolves to another host never gets the session", () => {
  // Browsers read "/\\" as "//" and drop tabs and newlines, so each of these is a network-path reference.
  const offOrigin = [
    "/\\attacker.example/api/x",
    "/\t/attacker.example/api/x",
    "/\n/attacker.example/v1/audio/voices/v/file",
  ];
  for (const apiBase of [null, 8888]) {
    if (apiBase !== null) setApiBase(apiBase);
    for (const path of offOrigin) {
      assert.equal(protectedBackendPath(path), null, `${apiBase} ${JSON.stringify(path)}`);
    }
    assert.equal(protectedBackendPath("/v1/audio/voices/v1/file"), "/v1/audio/voices/v1/file");
    resetApiBase();
  }
});