Skip to content

Studio: serve HTTPS from your own certificate (--ssl-certfile / --ssl-keyfile) - #13447

Merged
danielhanchen merged 9 commits into
mainfrom
studio-tls-certfile
Oct 13, 2026
Merged

danielhanchen merged 9 commits into
mainfrom
studio-tls-certfile

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 12, 2026 •

Copy link
Copy Markdown
Member

Fixes #8716

What this adds

unsloth studio can now serve HTTPS itself from a certificate and key you already have, for example the *.ts.net pair from tailscale cert, a Let's Encrypt certificate, or an internal CA:

unsloth studio -H 0.0.0.0 -p 8888 \
  --ssl-certfile /path/to/host.ts.net.crt \
  --ssl-keyfile  /path/to/host.ts.net.key

The banner and the Settings > LAN access URLs switch to https://. A TLS launch does not publish an IP-based server_url: certificates such as tailscale cert name a DNS host, so the API examples and copied links use the origin the browser actually reached. Nothing changes for a launch without these flags: the code path is identical (same uvicorn.Config, same listeners, same banner text).

Behaviour

Launch Result
no flags unchanged, plain HTTP
both flags, valid pair HTTPS on the requested host and port; plain HTTP to that port is refused
one flag only exits before binding: --ssl-certfile and --ssl-keyfile must be passed together.
missing file / key does not match cert exits before binding with the file paths and the OpenSSL reason (e.g. KEY_VALUES_MISMATCH)
with --secure or --cloudflare exits before binding: the tunnel dials the origin over plain HTTP
unsloth studio run --ssl-certfile ... rejected with a message, like the other plain-server-only flags

Implementation notes

  1. Native TLS. Studio injects truststore into ssl (default on for macOS, Windows and the desktop-owned Linux backend). The injected SSLContext verifies the peer chain against the OS store on every handshake, and on a server the peer is the browser, which sends no certificate, so truststore raises (Peer sent no certificates to verify on Windows). utils/studio_tls.py builds the server context from the injected class (uvloop checks isinstance against it) and resets only its sslobject_class hook back to ssl.SSLObject. test_native_tls_entrypoints.py now checks with an AST walk that any TLS server context or uvicorn cert kwargs go through that module.
  2. Studio's own self-calls. Data recipe LLM columns and research runs call this process's /v1 from children that cannot be told to trust a private certificate. A TLS launch therefore also listens for plain HTTP on an ephemeral 127.0.0.1 port (same uvicorn server, lifespan and connection drain, kept out of server.servers so the published port and host are unchanged), and those call sites use it. This is the same loopback exposure every default launch has today.
  3. LAN access listener. Settings > LAN access reuses the same certificate, so turning it on does not reopen plain HTTP on the network. For a TLS launch bound to one address, the LAN status lists https://<bound host>:<port>.
  4. Settings > Remote access tunnel. Points cloudflared at the loopback listener, since cloudflared speaks HTTP to the origin.

Testing

  • studio/backend/tests/test_studio_tls.py (new): pair validation; a real HTTPS handshake through uvicorn with truststore injected and the macOS / Windows empty-chain behaviour reproduced, plus a negative control showing stock uvicorn.Config(ssl_certfile=...) fails under the same injection; banner URLs; LAN listener config; self-call port lookup. The handshake test fails if the hook reset is removed.
  • Live runs on Linux against a built Studio (CPU only), cert for studio.example.ts.net with LAN IP SANs, results in the table below.
Scenario Result
unsloth studio -H 127.0.0.1 --ssl-certfile ... --ssl-keyfile ... GET / 200, /api/health 200, login 200, change-password 200 over HTTPS; plain HTTP to the same port: connection closed
same, self-call listener one extra listener on 127.0.0.1 only, /api/health 200 over HTTP
run.py --host 0.0.0.0 + both flags UI and login 200 over HTTPS at the LAN IP; plain HTTP at the LAN IP: connection closed; self-call port not reachable from the LAN IP
cert + non-matching key exit 1 before binding: Could not load the TLS certificate and key (...): [X509: KEY_VALUES_MISMATCH] key values mismatch
--secure + both flags exit 1: --ssl-certfile/--ssl-keyfile serve HTTPS directly; do not combine them with --secure or --cloudflare.
no flags, this branch vs merge base same single listener, identical banner text, UI and login 200

Related tests (77 files touching the banner, LAN / remote access, data recipes, research runs, native TLS, CLI flags) give the same pass/fail set on this branch and on the merge base, apart from the new tests.

@danielhanchen danielhanchen self-assigned this Oct 12, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T03:39:42.684042Z 7f969c1 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-12T14:56:33.863034Z 1fa7ee6 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1fa7ee6af2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread studio/backend/run.py
Comment on lines +2771 to +2772
self.loopback_http = await asyncio.get_running_loop().create_server(
_protocol, host = "127.0.0.1", port = 0, backlog = config.backlog

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Use an IPv6 loopback for IPv6-only TLS launches

For a TLS launch bound to :: or ::1 on an IPv6-only host, startup still unconditionally creates the required plaintext self-call listener on 127.0.0.1. If IPv4 is disabled or unavailable, this bind fails after the public TLS socket has started and aborts an otherwise valid IPv6 launch. Select the loopback family from the requested bind (and publish that host alongside the ephemeral port) instead of requiring IPv4 for every TLS launch.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not changing this: it needs a host with the IPv4 loopback removed, which Linux, macOS and Windows all keep even on IPv6-only networks.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Oct 12, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e82ece1ad7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

for address in addresses:
url_host = f"[{address}]" if ":" in address else address
urls.append(f"http://{url_host}:{port}")
urls.append(f"{scheme}://{url_host}:{port}")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Do not advertise certificate-invalid LAN IP URLs

With a DNS-only certificate such as the documented Tailscale or Let's Encrypt examples, a wildcard or Settings-managed LAN listener reaches this formatter with literal LAN addresses and now publishes URLs such as https://192.168.1.5:8888; the LAN panel exposes these through its Copy and QR actions, but browsers reject them because the IP is absent from the certificate SANs. Although server_url is now suppressed for TLS launches, _launch_urls still calls this changed formatter, so the panel should retain a certificate-valid/request hostname or omit these literal-IP URLs rather than changing their scheme to HTTPS.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Keeping these: the IP URL is the right address for certificates with IP SANs (self-signed, internal CA), and for DNS-only certificates Studio cannot know which hostname the user reaches it by. Dropping them would hide where the listener answers.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 03c22fb0ae

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

client = ssl.create_default_context(cafile = str(crt))
try:
with socket.create_connection(("127.0.0.1", port), timeout = 10) as raw:
with client.wrap_socket(raw, server_hostname = "studio.example.ts.net") as tls:
@danielhanchen

Copy link
Copy Markdown
Member Author

pre-commit.ci run

chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen added a commit to Etherll/unsloth that referenced this pull request Oct 13, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

danielhanchen added a commit to Etherll/unsloth that referenced this pull request Oct 13, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 7f969c1684

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add parameter for custom SSL files .crt & .key - as an alternative to using Cloudflare

2 participants