From b0e6583bb2297fdb6ef005f684538ddb49cfcde7 Mon Sep 17 00:00:00 2001 From: drbh Date: Wed, 29 Apr 2026 17:27:01 -0400 Subject: [PATCH 01/11] feat: add trusted orgs and flag --- kernels/src/kernels/__init__.py | 2 ++ kernels/src/kernels/layer/func.py | 16 +++++++--- kernels/src/kernels/layer/layer.py | 18 ++++++++--- kernels/src/kernels/utils.py | 48 ++++++++++++++++++++++++++++++ kernels/tests/test_basic.py | 38 +++++++++++++++++++++++ 5 files changed, 114 insertions(+), 8 deletions(-) diff --git a/kernels/src/kernels/__init__.py b/kernels/src/kernels/__init__.py index 8ef7add4c..29865f235 100644 --- a/kernels/src/kernels/__init__.py +++ b/kernels/src/kernels/__init__.py @@ -24,6 +24,7 @@ use_kernel_mapping, ) from kernels.utils import ( + TRUSTED_KERNEL_ORGS, LoadedKernel, RepoInfo, get_kernel, @@ -52,6 +53,7 @@ "Metadata", "Mode", "RepoInfo", + "TRUSTED_KERNEL_ORGS", "get_kernel", "get_loaded_kernels", "get_local_kernel", diff --git a/kernels/src/kernels/layer/func.py b/kernels/src/kernels/layer/func.py index d98551e3b..1398d22fa 100644 --- a/kernels/src/kernels/layer/func.py +++ b/kernels/src/kernels/layer/func.py @@ -64,12 +64,14 @@ def __init__( func_name: str, revision: str | None = None, version: int | None = None, + trust_remote_code: bool | list[str] = False, ): if revision is not None and version is not None: raise ValueError("Either a revision or a version must be specified, not both.") self._repo_id = repo_id self.func_name = func_name + self._trust_remote_code = trust_remote_code # We are going to resolve these lazily, since we do not want # to do a network request for every registered FuncRepository. @@ -85,7 +87,7 @@ def _resolve_revision(self) -> str: ) def load(self) -> Type["nn.Module"]: - kernel = get_kernel(self._repo_id, revision=self._resolve_revision()) + kernel = get_kernel(self._repo_id, revision=self._resolve_revision(), trust_remote_code=self._trust_remote_code) return _get_kernel_func(self, kernel) def __eq__(self, other): @@ -95,10 +97,11 @@ def __eq__(self, other): and self._repo_id == other._repo_id and self._revision == other._revision and self._version == other._version + and self._trust_remote_code == other._trust_remote_code ) def __hash__(self): - return hash((self.func_name, self._repo_id, self._revision, self._version)) + return hash((self.func_name, self._repo_id, self._revision, self._version, self._trust_remote_code)) def __str__(self) -> str: return f"`{self._repo_id}` (revision: {self._resolve_revision()}), function `{self.func_name}`" @@ -224,6 +227,7 @@ def __init__( *, lockfile: Path | None = None, func_name: str, + trust_remote_code: bool | list[str] = False, ): """ Construct a function repository. @@ -233,10 +237,13 @@ def __init__( lockfile (`Path`, *optional*): Path to the lockfile. If not provided, the lockfile will be inferred from the caller's context. func_name (`str`): The name of the function within the kernel repository. + trust_remote_code (`bool`, *optional*, defaults to `False`): + Whether to allow loading kernels from untrusted organisations. """ self._repo_id = repo_id self._lockfile = lockfile self.func_name = func_name + self._trust_remote_code = trust_remote_code self._revision = self._resolve_revision() def _resolve_revision(self) -> str: @@ -252,7 +259,7 @@ def _resolve_revision(self) -> str: return locked_sha def load(self) -> Type["nn.Module"]: - kernel = get_kernel(repo_id=self._repo_id, revision=self._revision) + kernel = get_kernel(repo_id=self._repo_id, revision=self._revision, trust_remote_code=self._trust_remote_code) return _get_kernel_func(self, kernel) def __eq__(self, other): @@ -261,10 +268,11 @@ def __eq__(self, other): and self.func_name == other.func_name and self._repo_id == other._repo_id and self._revision == other._revision + and self._trust_remote_code == other._trust_remote_code ) def __hash__(self): - return hash((self.func_name, self._repo_id, self._revision)) + return hash((self.func_name, self._repo_id, self._revision, self._trust_remote_code)) def __str__(self) -> str: return f"`{self._repo_id}` (revision: {self._revision}), function `{self.func_name}`" diff --git a/kernels/src/kernels/layer/layer.py b/kernels/src/kernels/layer/layer.py index 505aacb81..60f9a11c4 100644 --- a/kernels/src/kernels/layer/layer.py +++ b/kernels/src/kernels/layer/layer.py @@ -42,6 +42,10 @@ class LayerRepository: The specific revision (branch, tag, or commit) to download. Cannot be used together with `version`. version (`int`, *optional*): The kernel version to download. Cannot be used together with `revision`. + trust_remote_code (`bool | list[str]`, *optional*, defaults to `False`): + Whether to allow loading kernels from untrusted organisations. A list + of signing identities can be provided for future verification support; + until then it warns and falls back to the default trust check. Example: ```python @@ -63,12 +67,14 @@ def __init__( layer_name: str, revision: str | None = None, version: int | None = None, + trust_remote_code: bool | list[str] = False, ): if revision is not None and version is not None: raise ValueError("Either a revision or a version must be specified, not both.") self._repo_id = repo_id self.layer_name = layer_name + self._trust_remote_code = trust_remote_code # We are going to resolve these lazily, since we do not want # to do a network request for every registered LayerRepository. @@ -84,7 +90,7 @@ def _resolve_revision(self) -> str: ) def load(self) -> Type["nn.Module"]: - kernel = get_kernel(self._repo_id, revision=self._resolve_revision()) + kernel = get_kernel(self._repo_id, revision=self._resolve_revision(), trust_remote_code=self._trust_remote_code) return _get_kernel_layer(self, kernel) def __eq__(self, other): @@ -94,10 +100,11 @@ def __eq__(self, other): and self._repo_id == other._repo_id and self._revision == other._revision and self._version == other._version + and self._trust_remote_code == other._trust_remote_code ) def __hash__(self): - return hash((self.layer_name, self._repo_id, self._revision, self._version)) + return hash((self.layer_name, self._repo_id, self._revision, self._version, self._trust_remote_code)) def __str__(self) -> str: return f"`{self._repo_id}` (revision: {self._resolve_revision()}), layer `{self.layer_name}`" @@ -168,6 +175,7 @@ def __init__( *, lockfile: Path | None = None, layer_name: str, + trust_remote_code: bool | list[str] = False, ): """ Construct a layer repository. @@ -178,6 +186,7 @@ def __init__( self._repo_id = repo_id self._lockfile = lockfile self.layer_name = layer_name + self._trust_remote_code = trust_remote_code self._revision = self._resolve_revision() def _resolve_revision(self) -> str: @@ -193,7 +202,7 @@ def _resolve_revision(self) -> str: return locked_sha def load(self) -> Type["nn.Module"]: - kernel = get_kernel(repo_id=self._repo_id, revision=self._revision) + kernel = get_kernel(repo_id=self._repo_id, revision=self._revision, trust_remote_code=self._trust_remote_code) return _get_kernel_layer(self, kernel) def __eq__(self, other): @@ -202,10 +211,11 @@ def __eq__(self, other): and self.layer_name == other.layer_name and self._repo_id == other._repo_id and self._revision == other._revision + and self._trust_remote_code == other._trust_remote_code ) def __hash__(self): - return hash((self.layer_name, self._repo_id, self._revision)) + return hash((self.layer_name, self._repo_id, self._revision, self._trust_remote_code)) def __str__(self) -> str: return f"`{self._repo_id}` (revision: {self._revision}), layer `{self.layer_name}`" diff --git a/kernels/src/kernels/utils.py b/kernels/src/kernels/utils.py index 829c740fa..4d1012ff8 100644 --- a/kernels/src/kernels/utils.py +++ b/kernels/src/kernels/utils.py @@ -31,6 +31,45 @@ KNOWN_BACKENDS = {"cpu", "cuda", "metal", "neuron", "rocm", "xpu", "npu"} +TRUSTED_KERNEL_ORGS = {"kernels-community", "kernels-test", "sglang"} + + +def _check_trust_remote_code(repo_id: str, trust_remote_code: bool | list[str]) -> None: + """Check whether a kernel repository is trusted. + + When ``trust_remote_code`` is ``False`` (the default), only repositories + belonging to a known trusted organisation are allowed. Repositories from + any other organisation will raise a ``ValueError``. + + When ``trust_remote_code`` is ``True``, all repositories are allowed. + + When ``trust_remote_code`` is a list of strings, it is treated as a list + of signing identities to verify against. Signing verification is not yet + implemented, so passing a list currently emits a warning and falls back + to the default trust check (i.e. only trusted organisations are allowed). + """ + if trust_remote_code is True: + return + + if isinstance(trust_remote_code, list): + import warnings + + warnings.warn( + "Signing identity verification is not yet implemented. " + "The provided signing identities will be ignored and the " + "kernel will be treated as untrusted. Use trust_remote_code=True " + "to bypass trust checks.", + stacklevel=3, + ) + + org = repo_id.split("/", 1)[0] + if org not in TRUSTED_KERNEL_ORGS: + raise ValueError( + f"Kernel repository '{repo_id}' is not from a trusted organisation. " + f"Trusted organisations: {', '.join(sorted(TRUSTED_KERNEL_ORGS))}. " + f"Set trust_remote_code=True to allow loading kernels from untrusted sources." + ) + @dataclass(frozen=True) class RepoInfo: @@ -293,6 +332,7 @@ def get_kernel( version: int | None = None, backend: str | None = None, user_agent: str | dict | None = None, + trust_remote_code: bool | list[str] = False, ) -> ModuleType: """ Load a kernel from the kernel hub. @@ -312,6 +352,12 @@ def get_kernel( The backend will be detected automatically if not provided. user_agent (`Union[str, dict]`, *optional*): The `user_agent` info to pass to `snapshot_download()` for internal telemetry. + trust_remote_code (`bool | list[str]`, *optional*, defaults to `False`): + Whether to allow loading kernels from untrusted organisations. When ``False``, + only kernels from trusted organisations are allowed. When ``True``, all + repositories are allowed. A list of strings will be used to verify signing + identities in a future release; for now it emits a warning and falls + back to the default trust check. Returns: `ModuleType`: The imported kernel module. @@ -331,6 +377,8 @@ def get_kernel( if override is not None: return get_local_kernel(override) + _check_trust_remote_code(repo_id, trust_remote_code) + revision = select_revision_or_version(repo_id, revision=revision, version=version) repo_info = RepoInfo( repo_id=repo_id, diff --git a/kernels/tests/test_basic.py b/kernels/tests/test_basic.py index c2978a04e..62ff99c82 100644 --- a/kernels/tests/test_basic.py +++ b/kernels/tests/test_basic.py @@ -233,6 +233,44 @@ def test_neuron(): torch.testing.assert_close(relu.relu(x), x.relu()) +def test_trust_remote_code_blocks_untrusted_org(): + """Kernels from untrusted orgs should be rejected by default.""" + with pytest.raises(ValueError, match=r"not from a trusted organisation"): + get_kernel("some-random-org/some-kernel", version=1) + + +def test_trust_remote_code_allows_trusted_org(): + """Kernels from trusted orgs should not raise a trust error. + + This only checks that the trust check passes — it may still fail + for other reasons (e.g. network, missing variant). + """ + # kernels-community is in TRUSTED_KERNEL_ORGS, so trust check passes. + # The call may fail later (no CUDA, etc.), but not with a trust error. + try: + get_kernel("kernels-community/relu", version=1) + except ValueError as e: + assert "not from a trusted organisation" not in str(e) + + +def test_trust_remote_code_flag_allows_untrusted(): + """trust_remote_code=True should bypass the org check.""" + # This will likely fail for network/variant reasons, but NOT with trust error. + try: + get_kernel("some-random-org/some-kernel", version=1, trust_remote_code=True) + except ValueError as e: + assert "not from a trusted organisation" not in str(e) + except Exception: + pass # Any other error is fine — we only care about trust check + + +def test_trust_remote_code_list_warns_and_falls_back(): + """trust_remote_code as a list should warn and fall back to the default check.""" + with pytest.warns(match=r"Signing identity verification is not yet implemented"): + with pytest.raises(ValueError, match=r"not from a trusted organisation"): + get_kernel("some-random-org/some-kernel", version=1, trust_remote_code=["identity-1"]) + + def silu_and_mul_torch(x: torch.Tensor): d = x.shape[-1] // 2 return F.silu(x[..., :d]) * x[..., d:] From 25f5be0a62dd61e6c5718b2d70b4aba54f79fdb6 Mon Sep 17 00:00:00 2001 From: drbh Date: Thu, 30 Apr 2026 15:01:50 -0400 Subject: [PATCH 02/11] fix: format changes --- kernels/src/kernels/layer/func.py | 4 +++- kernels/src/kernels/layer/layer.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/kernels/src/kernels/layer/func.py b/kernels/src/kernels/layer/func.py index 1398d22fa..b7cfd567f 100644 --- a/kernels/src/kernels/layer/func.py +++ b/kernels/src/kernels/layer/func.py @@ -87,7 +87,9 @@ def _resolve_revision(self) -> str: ) def load(self) -> Type["nn.Module"]: - kernel = get_kernel(self._repo_id, revision=self._resolve_revision(), trust_remote_code=self._trust_remote_code) + kernel = get_kernel( + self._repo_id, revision=self._resolve_revision(), trust_remote_code=self._trust_remote_code + ) return _get_kernel_func(self, kernel) def __eq__(self, other): diff --git a/kernels/src/kernels/layer/layer.py b/kernels/src/kernels/layer/layer.py index 60f9a11c4..d280b6b9e 100644 --- a/kernels/src/kernels/layer/layer.py +++ b/kernels/src/kernels/layer/layer.py @@ -90,7 +90,9 @@ def _resolve_revision(self) -> str: ) def load(self) -> Type["nn.Module"]: - kernel = get_kernel(self._repo_id, revision=self._resolve_revision(), trust_remote_code=self._trust_remote_code) + kernel = get_kernel( + self._repo_id, revision=self._resolve_revision(), trust_remote_code=self._trust_remote_code + ) return _get_kernel_layer(self, kernel) def __eq__(self, other): From 3b27521251f49adc32221057a0c5ea040fed3e93 Mon Sep 17 00:00:00 2001 From: drbh Date: Fri, 1 May 2026 10:31:59 -0400 Subject: [PATCH 03/11] Update kernels/tests/test_basic.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniël de Kok --- kernels/tests/test_basic.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/kernels/tests/test_basic.py b/kernels/tests/test_basic.py index 62ff99c82..e5d260df4 100644 --- a/kernels/tests/test_basic.py +++ b/kernels/tests/test_basic.py @@ -247,10 +247,7 @@ def test_trust_remote_code_allows_trusted_org(): """ # kernels-community is in TRUSTED_KERNEL_ORGS, so trust check passes. # The call may fail later (no CUDA, etc.), but not with a trust error. - try: - get_kernel("kernels-community/relu", version=1) - except ValueError as e: - assert "not from a trusted organisation" not in str(e) + get_kernel("kernels-community/relu", version=1) def test_trust_remote_code_flag_allows_untrusted(): From bc9db7515844ce92c2705f4a07360ba204c15298 Mon Sep 17 00:00:00 2001 From: drbh Date: Fri, 1 May 2026 10:50:00 -0400 Subject: [PATCH 04/11] fix: remove public TRUSTED_KERNEL_ORGS and update remote code tests --- kernels/src/kernels/__init__.py | 2 -- kernels/src/kernels/utils.py | 2 +- kernels/tests/test_basic.py | 11 +++-------- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/kernels/src/kernels/__init__.py b/kernels/src/kernels/__init__.py index 29865f235..8ef7add4c 100644 --- a/kernels/src/kernels/__init__.py +++ b/kernels/src/kernels/__init__.py @@ -24,7 +24,6 @@ use_kernel_mapping, ) from kernels.utils import ( - TRUSTED_KERNEL_ORGS, LoadedKernel, RepoInfo, get_kernel, @@ -53,7 +52,6 @@ "Metadata", "Mode", "RepoInfo", - "TRUSTED_KERNEL_ORGS", "get_kernel", "get_loaded_kernels", "get_local_kernel", diff --git a/kernels/src/kernels/utils.py b/kernels/src/kernels/utils.py index 4d1012ff8..789eb885f 100644 --- a/kernels/src/kernels/utils.py +++ b/kernels/src/kernels/utils.py @@ -31,7 +31,7 @@ KNOWN_BACKENDS = {"cpu", "cuda", "metal", "neuron", "rocm", "xpu", "npu"} -TRUSTED_KERNEL_ORGS = {"kernels-community", "kernels-test", "sglang"} +TRUSTED_KERNEL_ORGS = {"kernels-community", "kernels-staging", "kernels-test", "sglang"} def _check_trust_remote_code(repo_id: str, trust_remote_code: bool | list[str]) -> None: diff --git a/kernels/tests/test_basic.py b/kernels/tests/test_basic.py index e5d260df4..05b2eb9f5 100644 --- a/kernels/tests/test_basic.py +++ b/kernels/tests/test_basic.py @@ -235,8 +235,8 @@ def test_neuron(): def test_trust_remote_code_blocks_untrusted_org(): """Kernels from untrusted orgs should be rejected by default.""" - with pytest.raises(ValueError, match=r"not from a trusted organisation"): - get_kernel("some-random-org/some-kernel", version=1) + with pytest.raises(ValueError, match=r"not from a trusted org"): + get_kernel("drbh/not-a-trused-org-kernel", version=1) def test_trust_remote_code_allows_trusted_org(): @@ -253,12 +253,7 @@ def test_trust_remote_code_allows_trusted_org(): def test_trust_remote_code_flag_allows_untrusted(): """trust_remote_code=True should bypass the org check.""" # This will likely fail for network/variant reasons, but NOT with trust error. - try: - get_kernel("some-random-org/some-kernel", version=1, trust_remote_code=True) - except ValueError as e: - assert "not from a trusted organisation" not in str(e) - except Exception: - pass # Any other error is fine — we only care about trust check + get_kernel("drbh/ci-test-kernel", version=1, trust_remote_code=True) def test_trust_remote_code_list_warns_and_falls_back(): From 933be677b04eedfbf14a8870b4383f3f4c9f313b Mon Sep 17 00:00:00 2001 From: drbh Date: Fri, 1 May 2026 11:03:21 -0400 Subject: [PATCH 05/11] feat: prefer checking against the hub info response rather than hardcoded list --- kernels/src/kernels/utils.py | 24 +++++++++++++++--------- kernels/tests/test_basic.py | 18 ++---------------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/kernels/src/kernels/utils.py b/kernels/src/kernels/utils.py index 789eb885f..b78398199 100644 --- a/kernels/src/kernels/utils.py +++ b/kernels/src/kernels/utils.py @@ -31,22 +31,20 @@ KNOWN_BACKENDS = {"cpu", "cuda", "metal", "neuron", "rocm", "xpu", "npu"} -TRUSTED_KERNEL_ORGS = {"kernels-community", "kernels-staging", "kernels-test", "sglang"} - def _check_trust_remote_code(repo_id: str, trust_remote_code: bool | list[str]) -> None: """Check whether a kernel repository is trusted. When ``trust_remote_code`` is ``False`` (the default), only repositories - belonging to a known trusted organisation are allowed. Repositories from - any other organisation will raise a ``ValueError``. + whose publisher is marked as trusted on the Hub are allowed. Repositories + from untrusted publishers will raise a ``ValueError``. When ``trust_remote_code`` is ``True``, all repositories are allowed. When ``trust_remote_code`` is a list of strings, it is treated as a list of signing identities to verify against. Signing verification is not yet implemented, so passing a list currently emits a warning and falls back - to the default trust check (i.e. only trusted organisations are allowed). + to the default trust check (i.e. only trusted publishers are allowed). """ if trust_remote_code is True: return @@ -62,11 +60,19 @@ def _check_trust_remote_code(repo_id: str, trust_remote_code: bool | list[str]) stacklevel=3, ) - org = repo_id.split("/", 1)[0] - if org not in TRUSTED_KERNEL_ORGS: + api = HfApi() + try: + info = api.repo_info(repo_id, repo_type="kernel") + except Exception: + raise ValueError( + f"Kernel repository '{repo_id}' is not from a trusted publisher. " + f"Could not verify publisher trust status. " + f"Set trust_remote_code=True to allow loading kernels from untrusted sources." + ) + + if not getattr(info, "trustedPublisher", False): raise ValueError( - f"Kernel repository '{repo_id}' is not from a trusted organisation. " - f"Trusted organisations: {', '.join(sorted(TRUSTED_KERNEL_ORGS))}. " + f"Kernel repository '{repo_id}' is not from a trusted publisher. " f"Set trust_remote_code=True to allow loading kernels from untrusted sources." ) diff --git a/kernels/tests/test_basic.py b/kernels/tests/test_basic.py index 05b2eb9f5..5d37e9bba 100644 --- a/kernels/tests/test_basic.py +++ b/kernels/tests/test_basic.py @@ -235,34 +235,20 @@ def test_neuron(): def test_trust_remote_code_blocks_untrusted_org(): """Kernels from untrusted orgs should be rejected by default.""" - with pytest.raises(ValueError, match=r"not from a trusted org"): + with pytest.raises(ValueError, match=r"not from a trusted publisher"): get_kernel("drbh/not-a-trused-org-kernel", version=1) def test_trust_remote_code_allows_trusted_org(): - """Kernels from trusted orgs should not raise a trust error. - - This only checks that the trust check passes — it may still fail - for other reasons (e.g. network, missing variant). - """ - # kernels-community is in TRUSTED_KERNEL_ORGS, so trust check passes. - # The call may fail later (no CUDA, etc.), but not with a trust error. + """Kernels from trusted orgs should not raise a trust error.""" get_kernel("kernels-community/relu", version=1) def test_trust_remote_code_flag_allows_untrusted(): """trust_remote_code=True should bypass the org check.""" - # This will likely fail for network/variant reasons, but NOT with trust error. get_kernel("drbh/ci-test-kernel", version=1, trust_remote_code=True) -def test_trust_remote_code_list_warns_and_falls_back(): - """trust_remote_code as a list should warn and fall back to the default check.""" - with pytest.warns(match=r"Signing identity verification is not yet implemented"): - with pytest.raises(ValueError, match=r"not from a trusted organisation"): - get_kernel("some-random-org/some-kernel", version=1, trust_remote_code=["identity-1"]) - - def silu_and_mul_torch(x: torch.Tensor): d = x.shape[-1] // 2 return F.silu(x[..., :d]) * x[..., d:] From 95e079cfb26b90c16dbaf7ee716d9027b3d52e29 Mon Sep 17 00:00:00 2001 From: drbh Date: Fri, 1 May 2026 11:07:39 -0400 Subject: [PATCH 06/11] fix: prefer using _get_hf_api --- kernels/src/kernels/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernels/src/kernels/utils.py b/kernels/src/kernels/utils.py index b78398199..fe7830bf5 100644 --- a/kernels/src/kernels/utils.py +++ b/kernels/src/kernels/utils.py @@ -60,7 +60,7 @@ def _check_trust_remote_code(repo_id: str, trust_remote_code: bool | list[str]) stacklevel=3, ) - api = HfApi() + api = _get_hf_api() try: info = api.repo_info(repo_id, repo_type="kernel") except Exception: From 21ddb7aa30b3f233a7e72678e897f6c018f8107d Mon Sep 17 00:00:00 2001 From: drbh Date: Fri, 1 May 2026 11:08:22 -0400 Subject: [PATCH 07/11] fix: short circuit remote check for kernels-test org --- kernels/src/kernels/utils.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kernels/src/kernels/utils.py b/kernels/src/kernels/utils.py index fe7830bf5..9e8ddbe3f 100644 --- a/kernels/src/kernels/utils.py +++ b/kernels/src/kernels/utils.py @@ -31,6 +31,8 @@ KNOWN_BACKENDS = {"cpu", "cuda", "metal", "neuron", "rocm", "xpu", "npu"} +_ALWAYS_TRUSTED_ORGS = {"kernels-test"} + def _check_trust_remote_code(repo_id: str, trust_remote_code: bool | list[str]) -> None: """Check whether a kernel repository is trusted. @@ -60,6 +62,10 @@ def _check_trust_remote_code(repo_id: str, trust_remote_code: bool | list[str]) stacklevel=3, ) + org = repo_id.split("/", 1)[0] + if org in _ALWAYS_TRUSTED_ORGS: + return + api = _get_hf_api() try: info = api.repo_info(repo_id, repo_type="kernel") From 9b975646d37a1a673e58e0a71cb4b49fa158a876 Mon Sep 17 00:00:00 2001 From: drbh Date: Mon, 4 May 2026 07:59:59 -0400 Subject: [PATCH 08/11] fix: adjust error text in kernels/src/kernels/utils.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniël de Kok --- kernels/src/kernels/utils.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/kernels/src/kernels/utils.py b/kernels/src/kernels/utils.py index 9e8ddbe3f..31e8acf0a 100644 --- a/kernels/src/kernels/utils.py +++ b/kernels/src/kernels/utils.py @@ -69,12 +69,11 @@ def _check_trust_remote_code(repo_id: str, trust_remote_code: bool | list[str]) api = _get_hf_api() try: info = api.repo_info(repo_id, repo_type="kernel") - except Exception: + except Exception as e: raise ValueError( - f"Kernel repository '{repo_id}' is not from a trusted publisher. " - f"Could not verify publisher trust status. " - f"Set trust_remote_code=True to allow loading kernels from untrusted sources." - ) + f"Could not verify publisher trust status for kernel repository '{repo_id}'. " + "Set trust_remote_code=True to allow loading kernels from untrusted sources." + ) from e if not getattr(info, "trustedPublisher", False): raise ValueError( From 7820c04a3216a261c3c49fcd38ba7fc4eec3044d Mon Sep 17 00:00:00 2001 From: drbh Date: Mon, 4 May 2026 08:03:20 -0400 Subject: [PATCH 09/11] fix: remove always trusted orgs and update test org --- kernels/src/kernels/utils.py | 6 ------ kernels/tests/test_basic.py | 4 ++-- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/kernels/src/kernels/utils.py b/kernels/src/kernels/utils.py index 31e8acf0a..cb4bc94c1 100644 --- a/kernels/src/kernels/utils.py +++ b/kernels/src/kernels/utils.py @@ -31,8 +31,6 @@ KNOWN_BACKENDS = {"cpu", "cuda", "metal", "neuron", "rocm", "xpu", "npu"} -_ALWAYS_TRUSTED_ORGS = {"kernels-test"} - def _check_trust_remote_code(repo_id: str, trust_remote_code: bool | list[str]) -> None: """Check whether a kernel repository is trusted. @@ -62,10 +60,6 @@ def _check_trust_remote_code(repo_id: str, trust_remote_code: bool | list[str]) stacklevel=3, ) - org = repo_id.split("/", 1)[0] - if org in _ALWAYS_TRUSTED_ORGS: - return - api = _get_hf_api() try: info = api.repo_info(repo_id, repo_type="kernel") diff --git a/kernels/tests/test_basic.py b/kernels/tests/test_basic.py index 5d37e9bba..fd1d9f016 100644 --- a/kernels/tests/test_basic.py +++ b/kernels/tests/test_basic.py @@ -236,7 +236,7 @@ def test_neuron(): def test_trust_remote_code_blocks_untrusted_org(): """Kernels from untrusted orgs should be rejected by default.""" with pytest.raises(ValueError, match=r"not from a trusted publisher"): - get_kernel("drbh/not-a-trused-org-kernel", version=1) + get_kernel("kernels-test-untrusted/not-a-trused-org-kernel", version=1) def test_trust_remote_code_allows_trusted_org(): @@ -246,7 +246,7 @@ def test_trust_remote_code_allows_trusted_org(): def test_trust_remote_code_flag_allows_untrusted(): """trust_remote_code=True should bypass the org check.""" - get_kernel("drbh/ci-test-kernel", version=1, trust_remote_code=True) + get_kernel("kernels-test-untrusted/ci-test-kernel", version=1, trust_remote_code=True) def silu_and_mul_torch(x: torch.Tensor): From 75590a6496f3cacb451001d2057e18db096ceb81 Mon Sep 17 00:00:00 2001 From: drbh Date: Mon, 4 May 2026 12:01:02 -0400 Subject: [PATCH 10/11] fix: add hardcoded trusted orgs for now --- kernels/src/kernels/utils.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kernels/src/kernels/utils.py b/kernels/src/kernels/utils.py index cb4bc94c1..f78fe976b 100644 --- a/kernels/src/kernels/utils.py +++ b/kernels/src/kernels/utils.py @@ -31,6 +31,8 @@ KNOWN_BACKENDS = {"cpu", "cuda", "metal", "neuron", "rocm", "xpu", "npu"} +_ALWAYS_TRUSTED_ORGS = {"kernels-community", "kernels-staging", "kernels-test", "sglang"} + def _check_trust_remote_code(repo_id: str, trust_remote_code: bool | list[str]) -> None: """Check whether a kernel repository is trusted. @@ -60,6 +62,10 @@ def _check_trust_remote_code(repo_id: str, trust_remote_code: bool | list[str]) stacklevel=3, ) + org = repo_id.split("/", 1)[0] + if org in _ALWAYS_TRUSTED_ORGS: + return + api = _get_hf_api() try: info = api.repo_info(repo_id, repo_type="kernel") From 4a44385e0864fd2e70d6a420b07939b39de5e743 Mon Sep 17 00:00:00 2001 From: drbh Date: Mon, 4 May 2026 12:10:42 -0400 Subject: [PATCH 11/11] fix: avoid repo info fetch path --- kernels/src/kernels/utils.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/kernels/src/kernels/utils.py b/kernels/src/kernels/utils.py index f78fe976b..84fb05d00 100644 --- a/kernels/src/kernels/utils.py +++ b/kernels/src/kernels/utils.py @@ -66,20 +66,28 @@ def _check_trust_remote_code(repo_id: str, trust_remote_code: bool | list[str]) if org in _ALWAYS_TRUSTED_ORGS: return - api = _get_hf_api() - try: - info = api.repo_info(repo_id, repo_type="kernel") - except Exception as e: - raise ValueError( - f"Could not verify publisher trust status for kernel repository '{repo_id}'. " - "Set trust_remote_code=True to allow loading kernels from untrusted sources." - ) from e + raise ValueError( + f"Kernel repository '{repo_id}' is not from a trusted publisher. " + f"Set trust_remote_code=True to allow loading kernels from untrusted sources." + ) - if not getattr(info, "trustedPublisher", False): - raise ValueError( - f"Kernel repository '{repo_id}' is not from a trusted publisher. " - f"Set trust_remote_code=True to allow loading kernels from untrusted sources." - ) + # TODO: revisit and update logic when we can check trusted publishers at the + # user/organization level + # + # api = _get_hf_api() + # try: + # info = api.repo_info(repo_id, repo_type="kernel") + # except Exception as e: + # raise ValueError( + # f"Could not verify publisher trust status for kernel repository '{repo_id}'. " + # "Set trust_remote_code=True to allow loading kernels from untrusted sources." + # ) from e + + # if not getattr(info, "trustedPublisher", False): + # raise ValueError( + # f"Kernel repository '{repo_id}' is not from a trusted publisher. " + # f"Set trust_remote_code=True to allow loading kernels from untrusted sources." + # ) @dataclass(frozen=True)