Skip to content

Studio: add SSL/TLS support for web UI and API endpoints - #5192

Closed
ctsstc wants to merge 2 commits into
unslothai:mainfrom
ctsstc:studio/ssl-tls-support
Closed

ctsstc wants to merge 2 commits into
unslothai:mainfrom
ctsstc:studio/ssl-tls-support

Conversation

@ctsstc

@ctsstc ctsstc commented Apr 26, 2026 •

Copy link
Copy Markdown

Closes #5177.

Why

HTTP is still the default to preserve parity. Existing setups — scripts, integrations, ops runbooks pointing at http://localhost:8888 — keep working unchanged after pulling this. Worth raising as a follow-up though: should HTTPS-via-self-signed become the recommended default for new installs, given the surface handles passwords, HF tokens, and API keys? The one-time cert warning is real first-time-user friction that doesn't exist today, so I left it opt-in for this PR — happy to flip the default if maintainers prefer the safer posture.

Tauri wasn't tested end-to-end. The Rust changes compile clean (cargo check --tests passes locally) and the preflight/scheme-probe/health paths all have unit tests, but a full desktop-app build round trip was more time and disk than I had to spare. Would appreciate a maintainer spot-check that (a) the desktop app still attaches to a running backend over both schemes, and (b) the desktop-secret auth flow works against an HTTPS backend with the self-signed cert (danger_accept_invalid_certs(true) is wired up in desktop_auth.rs).

Future: shared preference storage (out of scope here). Today theme, sidebar state, chat history (IndexedDB via Dexie), and the auth token all live in the browser, and browsers partition that data by full origin (scheme + host + port). So flipping HTTP↔HTTPS hides everything until the user switches back. The Server settings tab now calls this caveat out explicitly, but the proper fix is to migrate the high-pain keys (theme, sidebar, eventually chat threads) to a /api/preferences endpoint and per-user table on the backend.

That's a meaningful trade-off, not an obvious win:

  • Today — "single auth, per-browser bubble": one Studio install, but each browser keeps its own preferences and chat history. Work-machine threads don't appear on the personal laptop. Some users actively prefer that isolation.
  • Centralized: theme / threads / preferences follow the user across browsers, machines, and scheme switches. Closer to most cloud chat UIs, and survives reinstalls cleanly.

Worth flagging as the natural next step, ideally with an opt-in/opt-out toggle so users who depend on per-browser isolation keep it. Maybe consider a migration if anyone wants to keep their current local storage settings and transfer them to a persistent DB storage. I imagine going this route may introduce more tangible user accounts across the persistence layer (for the backend).

Summary

  • uvicorn terminates TLS in-process via --ssl-certfile / --ssl-keyfile, plus a --ssl-self-signed convenience flag that auto-generates and reuses a cert in ~/.unsloth/studio/ssl/. Off by default to preserve parity with current HTTP behavior.
  • Layered configuration (CLI > env vars > app_secrets > default) is surfaced in a new Settings → Server tab with save + in-place restart. Restart re-execs the Python process and confirms success via a per-instance ID exposed on /api/health.
  • Global "restart in progress" gate so authFetch parks (rather than detonates) every request that races the restart window — modal stays open, no toast spam, no /login redirect cascade.
  • --no-ssl is the documented escape hatch when SSL is misconfigured via the UI.
  • Tauri preflight probes both schemes and threads the discovered scheme through the desktop auth flow with self-signed certs accepted.
  • Anchors the Python build-artifact lib/ / lib64/ rules in .gitignore to the project root so they stop shadowing legitimate frontend src/**/lib/ source directories.

Why no ngrok / reverse proxy

uvicorn natively supports ssl_keyfile / ssl_certfile, so TLS terminates in-process. No ngrok, no nginx, no extra hop. Self-signed certs trigger the standard one-time browser warning that the user clicks through.

Configuration precedence

Priority Source
1 --no-ssl flag (escape hatch — overrides everything)
2 --ssl-certfile / --ssl-keyfile / --ssl-self-signed CLI flags
3 UNSLOTH_SSL_CERTFILE / UNSLOTH_SSL_KEYFILE / UNSLOTH_SSL_SELF_SIGNED env vars
4 app_secrets rows written by the Settings UI
5 Default → SSL disabled (HTTP)

The Settings tab calls out which layer is currently in effect (e.g. "TLS is forced on by --ssl-self-signed; saved settings take over when the flag is dropped") so users can tell why "Active" can diverge from the saved toggle.

Test Points to Consider

  • unsloth studio (default) → access banner shows http://, frontend loads, login works (regression)
  • unsloth studio --ssl-self-signed → cert generated at ~/.unsloth/studio/ssl/, banner shows https://, browser shows one-time self-signed warning that can be clicked through, UI loads, login works, SSE training progress and /v1/chat/completions streaming both work
  • User-provided certs: openssl req -x509 -newkey rsa:2048 -nodes -keyout /tmp/k.pem -out /tmp/c.pem -days 1 -subj "/CN=localhost", then unsloth studio --ssl-certfile /tmp/c.pem --ssl-keyfile /tmp/k.pem → HTTPS bound, no regeneration of stored cert
  • UNSLOTH_SSL_SELF_SIGNED=1 unsloth studio → equivalent to --ssl-self-signed
  • Settings → Server: enable TLS via UI → click Restart server → spinner shows for the actual restart window, then "Server restarted." appears (no page reload when scheme unchanged)
  • Disable TLS via UI → Save → Save button correctly drops back to disabled (form is no longer dirty)
  • unsloth studio --no-ssl after enabling SSL via UI → server starts on HTTP, banner reflects scheme, UI is reachable to fix misconfig
  • POST /api/settings/server/test with bad path → returns { ok: false, error: "..." } without crashing; valid pair → { ok: true }
  • Tauri desktop: build, enable SSL in Settings → Server, restart → preflight reports scheme: "https", frontend connects via https://127.0.0.1:<port>

Note: I did not test the Tauri flow since I knew building out could take a while and I'm limited on resources. I'm hoping someone else can spot check it 🤞

Out of scope (intentional)

  • ACME / Let's Encrypt automation — deferred. The resolver is structured so an --ssl-acme-domain source slots in as another precedence layer in a follow-up. Wanted to avoid extra dependencies, and likely use case for users hosting their own domain to expose this is low, meanwhile there's still a method for them to provide their own certs.
  • Mutual TLS / client cert auth.
  • HSTS, OCSP stapling, custom cipher suites — uvicorn defaults are fine.

🤖 Generated with Claude Code

Closes unslothai#5177.

uvicorn terminates TLS in-process via --ssl-certfile / --ssl-keyfile,
plus a --ssl-self-signed convenience flag that auto-generates and
reuses a cert in ~/.unsloth/studio/ssl/. Off by default to preserve
parity with the existing HTTP behavior.

Configuration is layered (CLI > env vars > app_secrets > default) and
surfaced in a new "Server" settings tab that supports save + in-place
restart. The restart re-execs the Python process and confirms success
via a per-instance ID exposed on /api/health, with a global gate so
authFetch parks (rather than detonating) every request that races the
restart window. --no-ssl is the documented escape hatch when SSL is
misconfigured via the UI.

Tauri preflight now probes both schemes and threads the discovered
scheme through the desktop auth flow with self-signed certs accepted.

Anchor the Python build-artifact `lib/` / `lib64/` rules in .gitignore
to the project root so they stop shadowing legitimate frontend
src/**/lib/ source directories.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces SSL/TLS support for Unsloth Studio, allowing users to configure HTTPS through CLI arguments, environment variables, or a new 'Server' tab in the settings UI. The changes include a backend resolver for SSL settings, automated self-signed certificate generation using the cryptography library, and a self-restart feature to apply configuration changes without manual server restarts. The frontend and Tauri components were also updated to handle scheme transitions and HTTPS health checks. Review feedback highlights opportunities to improve error logging in the SSL configuration logic and to remove unused imports in the settings route.

Comment thread studio/backend/ssl_config.py Resolved
Comment thread studio/backend/ssl_config.py Resolved
Comment thread studio/backend/routes/settings.py Resolved
- ssl_config: log at debug level when auth.storage import or
  app_secrets read fails, instead of swallowing silently. Uses the
  project's `loggers.get_logger` for consistency.
- routes/settings: drop unused `os` / `sys` imports.
- server tab: add a green "Note:" callout flagging that flipping
  HTTP↔HTTPS changes the browser origin, so chat history (IndexedDB),
  theme, and the login session live with whichever scheme they were
  saved on. Nothing is deleted, but users will re-sign in on the new
  scheme.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@ctsstc

ctsstc commented Apr 26, 2026

Copy link
Copy Markdown
Author

/gemini review

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces SSL/TLS support for the Unsloth Studio backend and frontend, enabling HTTPS configuration via CLI flags, environment variables, or a new 'Server' settings tab. Key changes include a centralized SSL configuration resolver, automated self-signed certificate generation using the cryptography library, and a self-restart mechanism to apply settings. The frontend now includes a request gate and polling logic to handle server restarts gracefully. Feedback suggests decoupling the settings routes from the runner script to maintain a cleaner separation of concerns.

Comment thread studio/backend/routes/settings.py Resolved
@rolandtannous

Copy link
Copy Markdown
Contributor

@ctsstc for your case , use the following options:
A- Private:

  • SSH tunnel nothing to install on the server beyond Studio itself:
ssh -L 8888:localhost:8888 user@cloud-server

then open http://localhost:8888 with your local computer

No TLS needed. The tunnel is the encryption, and localhost never triggers a cert warning. Bind Studio to 127.0.0.1 so port 8888 isn't open to the outside.

  • Tailscale or Wireguard . Check their respective docs.

B - Public (available to others as well):

  • Cloudflare Tunnel on the cloud server . cloudflared dials out to Cloudflare, no inbound ports opened, valid cert.
  • Caddy + a domain might also work.

This should solve your problem

@ctsstc

ctsstc commented Apr 28, 2026 •

Copy link
Copy Markdown
Author

Thanks for leaving the follow up here. I was initially hoping for something baked-in, without extra config/software/etc, but realistically you bring up good points on the options you've proposed. Especially given my use case is likely not your normal use case. I do wonder if only local connections should be accepted to remove the footgun that this provides, but then that would block off the tunnel option.

This PR should be good to go if you change your mind. I realize the expense to bring in code debt if the use case is minor.

This also introduces a nice server restart flow if you want to rip that out at some point, as well as cleaning up the gitignore to not be so broad that they target working directories.

@rolandtannous

Copy link
Copy Markdown
Contributor

@ctsstc i'll close it for now but i'll keep it in mind might reopen and integrate at a later stage. thank you for the contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] SSL Secure Connection

2 participants