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
truststore: keep TLS verification on when handshakes overlap on one context (#12403)
Daniel Han0840364e4fb5
diff --git a/studio/backend/tests/test_truststore_concurrent_policy.py b/studio/backend/tests/test_truststore_concurrent_policy.pynew file mode 100644index 00000000000..9aa7bd64692--- /dev/null+++ b/studio/backend/tests/test_truststore_concurrent_policy.py@@ -0,0 +1,261 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""The vendored truststore keeps verification on when handshakes overlap (upstream issue #209).++macOS and Windows switch OpenSSL verification off on the shared context during a wrap and+verify against the OS store afterwards. The flag flipping is reproduced here on any platform,+so the race is exercised without a network or a real handshake.+"""++from __future__ import annotations++import base64+import contextlib+import importlib.util+import ssl+import sys+import threading+from pathlib import Path++import pytest++_PACKAGE = Path(__file__).resolve().parent.parent / "vendor" / "truststore"+++def _load_vendored(name, monkeypatch):+ # Private name: an installed truststore must not stand in. Restore the stdlib class first,+ # since native TLS may already have injected truststore into ssl (macOS / Windows default).+ stdlib = next(c for c in ssl._SSLContext.__subclasses__() if c.__module__ == "ssl")+ monkeypatch.setattr(ssl, "SSLContext", stdlib)+ spec = importlib.util.spec_from_file_location(+ name, _PACKAGE / "__init__.py", submodule_search_locations = [str(_PACKAGE)]+ )+ module = importlib.util.module_from_spec(spec)+ monkeypatch.setitem(sys.modules, name, module)+ spec.loader.exec_module(module)+ return module, sys.modules[f"{name}._api"]+++def truststore(monkeypatch):+ name = "_unsloth_vendored_truststore_under_test"+ module, api = _load_vendored(name, monkeypatch)++ @contextlib.contextmanager+ def flipping(ctx):+ check_hostname, verify_mode = ctx.check_hostname, ctx.verify_mode+ ctx.check_hostname = False+ api._original_super_SSLContext.verify_mode.__set__(ctx, ssl.CERT_NONE)+ try:+ yield+ finally:+ ctx.check_hostname = check_hostname+ api._original_super_SSLContext.verify_mode.__set__(ctx, verify_mode)++ monkeypatch.setattr(api, "_configure_context", flipping)+ # raising=False: an unpatched copy has no such flag, and must fail on behaviour instead.+ monkeypatch.setattr(api, "_HOLDS_POLICY", True, raising = False)+ yield module, api+ for key in [k for k in sys.modules if k.startswith(name)]:+ sys.modules.pop(key, None)+++class _FakeSock:+ def __init__(self, context):+ self.context = context++ def get_unverified_chain(self):+ return []++ def close(self):+ pass+++def _overlap(+ module,+ api,+ monkeypatch,+ during_b = None,+):+ """A enters, B enters while A is open, A verifies and leaves, then B leaves."""+ seen = {}+ a_in, b_in, a_verified = threading.Event(), threading.Event(), threading.Event()++ def impl(+ ssl_context,+ cert_chain,+ server_hostname = None,+ ):+ seen[threading.current_thread().name] = (+ ssl_context.verify_mode,+ ssl_context.check_hostname,+ )+ if threading.current_thread().name == "A":+ a_verified.set()++ monkeypatch.setattr(api, "_verify_peercerts_impl", impl)+ ctx = module.SSLContext(ssl.PROTOCOL_TLS_CLIENT)++ def fake_wrap(sock, **kwargs):+ if threading.current_thread().name == "A":+ a_in.set()+ assert b_in.wait(5)+ else:+ assert a_in.wait(5)+ b_in.set()+ if during_b is not None:+ during_b(ctx)+ assert a_verified.wait(5)+ return _FakeSock(ctx._ctx)++ ctx._ctx.wrap_socket = fake_wrap+ threads = [+ threading.Thread(+ target = ctx.wrap_socket, args = (None,), kwargs = {"server_hostname": "h"}, name = n+ )+ for n in "AB"+ ]+ for t in threads:+ t.start()+ for t in threads:+ t.join(10)+ return ctx, seen+++def test_overlapping_wraps_verify_and_leave_the_context_verified(truststore, monkeypatch):+ module, api = truststore+ ctx, seen = _overlap(module, api, monkeypatch)+ assert seen == {"A": (ssl.CERT_REQUIRED, True), "B": (ssl.CERT_REQUIRED, True)}+ assert (ctx.verify_mode, ctx.check_hostname) == (ssl.CERT_REQUIRED, True)+ assert (ctx._ctx.verify_mode, ctx._ctx.check_hostname) == (ssl.CERT_REQUIRED, True)+++def test_a_snapshot_restore_during_a_wrap_cannot_pin_cert_none(truststore, monkeypatch):+ # urllib3 reads verify_mode before load_verify_locations and writes it back afterwards.+ module, api = truststore++ def snapshot_restore(ctx):+ mode = ctx.verify_mode+ ctx.verify_mode = mode++ ctx, seen = _overlap(module, api, monkeypatch, during_b = snapshot_restore)+ assert seen["B"] == (ssl.CERT_REQUIRED, True)+ assert ctx._ctx.verify_mode == ssl.CERT_REQUIRED+++def test_a_policy_set_during_a_wrap_applies_when_it_closes(truststore, monkeypatch):+ module, api = truststore++ def relax(ctx):+ ctx.check_hostname = False+ ctx.verify_mode = ssl.CERT_NONE++ ctx, _ = _overlap(module, api, monkeypatch, during_b = relax)+ assert (ctx._ctx.verify_mode, ctx._ctx.check_hostname) == (ssl.CERT_NONE, False)+++def test_contexts_are_still_freed(truststore):+ import gc+ import weakref++ module, api = truststore+ refs = []+ for _ in range(20):+ ctx = module.SSLContext(ssl.PROTOCOL_TLS_CLIENT)+ refs.append(weakref.ref(ctx._ctx))+ del ctx+ gc.collect()+ assert not [r for r in refs if r() is not None]+ assert len(api._POLICY_OWNERS) == 0+++def test_setters_inside_a_window_follow_the_ssl_module_rules(truststore):+ module, api = truststore+ ctx = module.SSLContext(ssl.PROTOCOL_TLS_CLIENT)+ ctx.check_hostname = False+ ctx.verify_mode = ssl.CERT_NONE+ with ctx._verification_window():+ ctx.check_hostname = True+ assert (ctx.check_hostname, ctx.verify_mode) == (True, ssl.CERT_REQUIRED)+ with pytest.raises(ValueError):+ ctx.verify_mode = ssl.CERT_NONE+ assert (ctx._ctx.check_hostname, ctx._ctx.verify_mode) == (True, ssl.CERT_REQUIRED)+++def test_a_failed_window_holds_no_policy(truststore, monkeypatch):+ module, api = truststore++ @contextlib.contextmanager+ def broken(ctx):+ raise OSError("bad CA file")+ yield++ monkeypatch.setattr(api, "_configure_context", broken)+ ctx = module.SSLContext(ssl.PROTOCOL_TLS_CLIENT)+ with pytest.raises(OSError):+ with ctx._verification_window():+ pass+ ctx.check_hostname = False+ ctx.verify_mode = ssl.CERT_NONE+ assert (ctx._ctx.check_hostname, ctx._ctx.verify_mode) == (False, ssl.CERT_NONE)+++def test_backends_that_never_flip_flags_write_settings_through(truststore, monkeypatch):+ # Linux: OpenSSL enforces the live context, so a setting must reach it immediately.+ module, api = truststore+ monkeypatch.setattr(api, "_HOLDS_POLICY", False)+ monkeypatch.setattr(api, "_configure_context", lambda ctx: contextlib.nullcontext())+ ctx = module.SSLContext(ssl.PROTOCOL_TLS_CLIENT)+ ctx.check_hostname = False+ ctx.verify_mode = ssl.CERT_NONE+ with ctx._verification_window():+ ctx.verify_mode = ssl.CERT_REQUIRED+ ctx.check_hostname = True+ assert (ctx._ctx.check_hostname, ctx._ctx.verify_mode) == (True, ssl.CERT_REQUIRED)+++# Self-signed leaf for CN=unsloth-test.invalid; no OS store trusts it.+_SELF_SIGNED_DER = base64.b64decode(+ "MIIBlDCCATugAwIBAgIUXEQ8RJeYDgLZpLNQWEqVaXV8NIswCgYIKoZIzj0EAwIwHzEdMBsGA1UEAwwUdW5zbG90"+ "aC10ZXN0LmludmFsaWQwIBcNMjYxMDAxMDczMzI0WhgPMjEyNjA5MDcwNzMzMjRaMB8xHTAbBgNVBAMMFHVuc2xv"+ "dGgtdGVzdC5pbnZhbGlkMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEC3GFqPgFuj4tNTl7hJQID2TU2SS1ycZQ"+ "hjinCUIZwynsjjWHq59sdVOnfzQleHndbEZOndbbx73W13IMGAoZtKNTMFEwHQYDVR0OBBYEFGvPMD1yHK9hNbTR"+ "a6P5I6B2Zv0NMB8GA1UdIwQYMBaAFGvPMD1yHK9hNbTRa6P5I6B2Zv0NMA8GA1UdEwEB/wQFMAMBAf8wCgYIKoZI"+ "zj0EAwIDRwAwRAIgDPyIveRS9sSmo/LG71KofNwbWJxxWhZmwqAZbQeEbycCIHujtZSrOjFRK7vNq7CHzgruUBf8"+ "E765ScsLf2YOhR0/"+)+++class _DerCert(bytes):+ def __new__(cls):+ return super().__new__(cls, _SELF_SIGNED_DER)++ def public_bytes(self, _encoding):+ return bytes(self)+++ sys.platform not in ("darwin", "win32"), reason = "the OS verifier only exists on macOS / Windows"+)+def test_the_real_os_verifier_sees_the_policy_while_a_window_is_open(monkeypatch):+ name = "_unsloth_vendored_truststore_os_verifier"+ try:+ module, api = _load_vendored(name, monkeypatch)+ ctx = module.SSLContext(ssl.PROTOCOL_TLS_CLIENT)++ class _Sock:+ context = ctx._ctx++ def get_unverified_chain(self):+ # Python < 3.13 hands back certificate objects rather than DER bytes.+ return [_DerCert()]++ with ctx._verification_window():+ # The shared context is CERT_NONE now; the caller's policy is still CERT_REQUIRED.+ assert ctx._ctx.verify_mode == ssl.CERT_NONE+ with pytest.raises(ssl.SSLCertVerificationError):+ api._verify_peercerts(_Sock(), server_hostname = "unsloth-test.invalid")+ finally:+ for key in [k for k in sys.modules if k.startswith(name)]:+ sys.modules.pop(key, None)diff --git a/studio/backend/tests/test_vendored_truststore.py b/studio/backend/tests/test_vendored_truststore.pyindex b07cf04b431..8be286dbda1 100644--- a/studio/backend/tests/test_vendored_truststore.py+++ b/studio/backend/tests/test_vendored_truststore.py@@ -1,7 +1,8 @@# SPDX-License-Identifier: AGPL-3.0-only# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0-"""The vendored truststore and laya stay byte-identical to the releases they came from.+"""The vendored truststore and laya stay byte-identical to the releases they came from,+except patches the manifest records with their upstream hash.They are static copies: nothing refreshes them, so any change to these bytes iseither a deliberate version bump that must update the manifest with it, or an@@ -72,6 +73,18 @@ def test_vendored_tree_matches_the_manifests():)+def test_local_patches_are_recorded_and_documented():+ readme = (_VENDOR / "README.md").read_text(encoding = "utf-8")+ for name, manifest in _MANIFESTS.items():+ for path, patch in manifest.get("patches", {}).items():+ assert path in manifest["files"], f"{name}: patched {path} is not a vendored file"+ assert (+ patch["upstream_sha256"] != manifest["files"][path]+ ), f"{path}: no change recorded"+ assert patch["reason"].strip()+ assert f"`{path}`" in readme, f"{path} is patched but README.md does not say why"++def test_no_symlinks_or_special_files():"""A symlink would let the digest check pass while the imported bytes differ."""offenders = [diff --git a/studio/backend/vendor/README.md b/studio/backend/vendor/README.mdindex 924354147c7..943fb271e6b 100644--- a/studio/backend/vendor/README.md+++ b/studio/backend/vendor/README.md@@ -18,7 +18,8 @@ downloads over `urllib` at first use. That cannot work here: behind the proxy thdownload of truststore would itself fail with `CERTIFICATE_VERIFY_FAILED`. The copy has to be presentbefore the network is. pip vendors truststore for the same reason.-The files are byte-identical to upstream, so they carry no Unsloth licence header. The linters and+The files are byte-identical to upstream, except the one local patch below, so they carry no+Unsloth licence header. The linters andformatters are configured to skip this directory (`[tool.ruff] extend-exclude` in `pyproject.toml`and the `ruff-format-with-kwargs` hook's `exclude` in `.pre-commit-config.yaml`); without both,`scripts/enforce_kwargs_spacing.py` rewrites them and they stop matching upstream.@@ -37,6 +38,21 @@ so that dotted route does not exist: it would load the same files under a secondeach copy of `inject_into_ssl()` would wrap an already-wrapped `ssl.SSLContext`. Appending ratherthan prepending also means a real installed truststore still wins.+### Local patch++`truststore/_api.py` carries one Unsloth patch (upstream issue+https://github.com/sethmlarson/truststore/issues/209, open as of 0.10.4). On macOS and Windows,+`wrap_socket()` and `wrap_bio()` switch OpenSSL verification off on the shared context while a+handshake is in flight and verify against the OS store afterwards. Two overlapping handshakes on one+context could save and restore each other's temporary `CERT_NONE`, leaving the context unverified,+and the OS verification read the same shared flags and skipped itself. The patch keeps one window+per context with a reference count, holds the caller's `check_hostname` / `verify_mode` aside while+it is open on macOS and Windows (the public getters and setters use those, with the same rules as+`ssl.SSLContext`), and verifies against them. On Linux the flags are never flipped, so settings+still go straight to the context. Handshakes still run in parallel. `truststore_manifest.json` records the upstream hash under `patches`;+`tests/test_truststore_concurrent_policy.py` pins the behaviour. Drop the patch once a release+fixes the issue.+### UpdatingThis is a static copy. There is no refresh step and nothing updates it automatically, which is thediff --git a/studio/backend/vendor/truststore/_api.py b/studio/backend/vendor/truststore/_api.pyindex 4dada0304c4..2c5b109bd70 100644--- a/studio/backend/vendor/truststore/_api.py+++ b/studio/backend/vendor/truststore/_api.py@@ -6,16 +6,21 @@ import sslimport sysimport threadingimport typing+import weakrefimport _sslfrom ._ssl_constants import (_original_SSLContext,_original_super_SSLContext,+ _set_ssl_context_verify_mode,_truststore_SSLContext_dunder_class,_truststore_SSLContext_super_class,)+# Unsloth patch: only these backends switch OpenSSL verification off during a wrap.+_HOLDS_POLICY = platform.system() in ("Windows", "Darwin")+if platform.system() == "Windows":from ._windows import _configure_context, _verify_peercerts_implelif platform.system() == "Darwin":@@ -86,7 +91,15 @@ class SSLContext(_truststore_SSLContext_super_class): # type: ignore[misc]def __init__(self, protocol: int = None) -> None: # type: ignore[assignment]self._ctx = _original_SSLContext(protocol)- self._ctx_lock = threading.Lock()+ self._ctx_lock = threading.RLock()+ # Unsloth patch (truststore issue #209): verification is switched off on the shared+ # context only while a wrap is in flight. Overlapping wraps share one window, and the+ # caller's policy is held here so neither a restore nor the verifier sees the+ # temporary CERT_NONE.+ self._unverified_depth = 0+ self._unverified_exit: typing.Any = None+ self._policy: tuple[bool, ssl.VerifyMode] | None = None+ _POLICY_OWNERS[self._ctx] = weakref.ref(self)class TruststoreSSLObject(ssl.SSLObject):# This object exists because wrap_bio() doesn't@@ -114,10 +127,7 @@ class SSLContext(_truststore_SSLContext_super_class): # type: ignore[misc]# but we don't need to lock within the# context manager, so we need to expand the# syntactic sugar of the `with` statement.- with contextlib.ExitStack() as stack:- with self._ctx_lock:- stack.enter_context(_configure_context(self._ctx))-+ with self._verification_window():ssl_sock = self._ctx.wrap_socket(sock,server_side=server_side,@@ -141,7 +151,7 @@ class SSLContext(_truststore_SSLContext_super_class): # type: ignore[misc]server_hostname: str | None = None,session: ssl.SSLSession | None = None,) -> ssl.SSLObject:- with _configure_context(self._ctx):+ with self._verification_window():ssl_obj = self._ctx.wrap_bio(incoming,outgoing,@@ -151,6 +161,42 @@ class SSLContext(_truststore_SSLContext_super_class): # type: ignore[misc])return ssl_obj+ @contextlib.contextmanager+ def _verification_window(self) -> typing.Iterator[None]:+ with self._ctx_lock:+ if self._unverified_depth == 0:+ policy = (self._ctx.check_hostname, self._ctx.verify_mode)+ configured = _configure_context(self._ctx)+ configured.__enter__()+ self._unverified_exit = configured.__exit__+ if _HOLDS_POLICY:+ self._policy = policy+ self._unverified_depth += 1+ try:+ yield+ finally:+ with self._ctx_lock:+ self._unverified_depth -= 1+ if self._unverified_depth == 0:+ exit_configured, self._unverified_exit = self._unverified_exit, None+ exit_configured(None, None, None)+ if self._policy is not None:+ # Re-apply what callers set while the window was open.+ check_hostname, verify_mode = self._policy+ self._policy = None+ if verify_mode == ssl.CERT_NONE:+ self._ctx.check_hostname = check_hostname+ _set_ssl_context_verify_mode(self._ctx, verify_mode)+ else:+ _set_ssl_context_verify_mode(self._ctx, verify_mode)+ self._ctx.check_hostname = check_hostname++ def _verification_policy(self) -> tuple[bool, ssl.VerifyMode]:+ with self._ctx_lock:+ if self._policy is not None:+ return self._policy+ return self._ctx.check_hostname, self._ctx.verify_mode+def load_verify_locations(self,cafile: str | bytes | os.PathLike[str] | os.PathLike[bytes] | None = None,@@ -213,11 +259,19 @@ class SSLContext(_truststore_SSLContext_super_class): # type: ignore[misc]@propertydef check_hostname(self) -> bool:- return self._ctx.check_hostname+ return self._verification_policy()[0]@check_hostname.setterdef check_hostname(self, value: bool) -> None:- self._ctx.check_hostname = value+ with self._ctx_lock:+ if self._policy is not None:+ # Same rule as ssl.SSLContext: enabling it upgrades CERT_NONE to CERT_REQUIRED.+ verify_mode = self._policy[1]+ if value and verify_mode == ssl.CERT_NONE:+ verify_mode = ssl.CERT_REQUIRED+ self._policy = (bool(value), verify_mode)+ else:+ self._ctx.check_hostname = value@propertydef hostname_checks_common_name(self) -> bool:@@ -293,13 +347,22 @@ class SSLContext(_truststore_SSLContext_super_class): # type: ignore[misc]@propertydef verify_mode(self) -> ssl.VerifyMode:- return self._ctx.verify_mode+ return self._verification_policy()[1]@verify_mode.setterdef verify_mode(self, value: ssl.VerifyMode) -> None:- _original_super_SSLContext.verify_mode.__set__( # type: ignore[attr-defined]- self._ctx, value- )+ with self._ctx_lock:+ if self._policy is not None:+ value = ssl.VerifyMode(value)+ if value == ssl.CERT_NONE and self._policy[0]:+ raise ValueError(+ "Cannot set verify_mode to CERT_NONE when check_hostname is enabled."+ )+ self._policy = (self._policy[0], value)+ else:+ _original_super_SSLContext.verify_mode.__set__( # type: ignore[attr-defined]+ self._ctx, value+ )# Python 3.13+ makes get_unverified_chain() a public API that only returns DER@@ -336,6 +399,28 @@ def _verify_peercerts(passcert_bytes = _get_unverified_chain_bytes(sslobj)- _verify_peercerts_impl(- sock_or_sslobj.context, cert_bytes, server_hostname=server_hostname- )+ context = sock_or_sslobj.context+ owner_ref = _POLICY_OWNERS.get(context)+ owner = owner_ref() if owner_ref is not None else None+ if owner is not None:+ context = _PolicyView(context, *owner._verification_policy())+ _verify_peercerts_impl(context, cert_bytes, server_hostname=server_hostname)+++# Unsloth patch (truststore issue #209): the context a socket reports is the shared one, whose+# flags may belong to another thread's in-flight wrap. Verify against the owner's policy.+_POLICY_OWNERS: "weakref.WeakKeyDictionary[ssl.SSLContext, weakref.ref[SSLContext]]" = (+ weakref.WeakKeyDictionary()+)+++class _PolicyView:+ def __init__(+ self, ctx: ssl.SSLContext, check_hostname: bool, verify_mode: ssl.VerifyMode+ ) -> None:+ self._view_ctx = ctx+ self.check_hostname = check_hostname+ self.verify_mode = verify_mode++ def __getattr__(self, name: str) -> typing.Any:+ return getattr(self._view_ctx, name)diff --git a/studio/backend/vendor/truststore_manifest.json b/studio/backend/vendor/truststore_manifest.jsonindex 09d4a103d08..72aea352dc7 100644--- a/studio/backend/vendor/truststore_manifest.json+++ b/studio/backend/vendor/truststore_manifest.json@@ -2,7 +2,7 @@"files": {"LICENSE": "33be7b7e8fa4fd19b1760e1a8ed8a668bdab852c91b692dd41424bcb725a9fca","truststore/__init__.py": "06eee4aa49a9ba784bb23e710aef204ffdb992da0f5dc4a7c1ef151e4d46989a",- "truststore/_api.py": "0910c84046483677663181b1c1b9f5db1ce5baddf89b4e9ed94248d8846a232e",+ "truststore/_api.py": "d71b740b420e487183de207282f40869fe12f88a22dabd755f1d76c5c07cfd78","truststore/_macos.py": "9d994b90e9accd413483aaf2470055198e423b33f2b9d72c889b4359aacce4b4","truststore/_openssl.py": "cc1f9242f272764b3bb50d32230acfe860f77d035249a3e2abbcf0e321794f8d","truststore/_ssl_constants.py": "3540f87d529d483d36ae2efe75bd2d9ced15a8b3fd687bb3992b5c5bbb40974f",@@ -14,5 +14,11 @@"source": "https://pypi.org/project/truststore/0.10.4/","version": "0.10.4","wheel": "truststore-0.10.4-py3-none-any.whl",- "wheel_sha256": "adaeaecf1cbb5f4de3b1959b42d41f6fab57b2b1666adb59e89cb0b53361d981"+ "wheel_sha256": "adaeaecf1cbb5f4de3b1959b42d41f6fab57b2b1666adb59e89cb0b53361d981",+ "patches": {+ "truststore/_api.py": {+ "upstream_sha256": "0910c84046483677663181b1c1b9f5db1ce5baddf89b4e9ed94248d8846a232e",+ "reason": "Overlapping handshakes on one context could leave verification disabled (upstream issue #209); see README.md, Local patch."+ }+ }}