From 1996f5109e123e459331cf564fdfd3a079d557ed Mon Sep 17 00:00:00 2001 From: "mars.yu" <18671933+yuyixg@users.noreply.github.com> Date: Wed, 25 Nov 2026 10:33:53 +0800 Subject: [PATCH] fix(openapi): leave a model's api key and endpoint out of the model lists GET /llm-configs and GET /llm-provider/{provider}/models returned the LlmModelSetting objects as bound from LlmProviders, ApiKey and Endpoint included, so any signed-in user could read every model's key. The settings page (/setting/LlmProviders) already masks both through LlmProviderPlugin.MaskSettings; these two lists did not. - LlmModelSetting.WithoutCredentials / LlmProviderSetting.WithoutCredentials: a copy with ApiKey empty and Endpoint null; everything else (capabilities, reasoning, image, cost...) is kept. - LlmProviderController returns those copies. The settings the providers call the models with are not touched. Tests: - LlmProviderControllerTests: neither list carries the key or the endpoint, in the objects or the serialized JSON, and the service's own instances keep both; both tests fail without the change. - BotSharp.Core.UnitTests: 565 passed, 1 failed (SharpCacheAgentMemoryTests.GetAgent_caches_per_scope_without_adding_shared_cache_entries, which fails the same way on master without this change). Co-Authored-By: Claude Opus 5.5 --- .../MLTasks/Settings/LlmModelSetting.cs | 11 +++ .../MLTasks/Settings/LlmProviderSetting.cs | 10 +++ .../Setting/LlmProviderController.cs | 6 +- .../LlmProviderControllerTests.cs | 75 +++++++++++++++++++ 4 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 tests/BotSharp.Core.UnitTests/Infrastructures/LlmProviderControllerTests.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/MLTasks/Settings/LlmModelSetting.cs b/src/Infrastructure/BotSharp.Abstraction/MLTasks/Settings/LlmModelSetting.cs index dccd20377..887fe028b 100644 --- a/src/Infrastructure/BotSharp.Abstraction/MLTasks/Settings/LlmModelSetting.cs +++ b/src/Infrastructure/BotSharp.Abstraction/MLTasks/Settings/LlmModelSetting.cs @@ -72,6 +72,17 @@ public class LlmModelSetting public bool AllowPdfReading => Capabilities?.Contains(LlmModelCapability.PdfReading) == true; + /// + /// A copy without the api key and endpoint, for listing the model to clients + /// + public LlmModelSetting WithoutCredentials() + { + var copy = (LlmModelSetting)MemberwiseClone(); + copy.ApiKey = string.Empty; + copy.Endpoint = null; + return copy; + } + public override string ToString() { return $"[{Type}] {Name} {Endpoint}"; diff --git a/src/Infrastructure/BotSharp.Abstraction/MLTasks/Settings/LlmProviderSetting.cs b/src/Infrastructure/BotSharp.Abstraction/MLTasks/Settings/LlmProviderSetting.cs index 705a9129f..48f6fabcb 100644 --- a/src/Infrastructure/BotSharp.Abstraction/MLTasks/Settings/LlmProviderSetting.cs +++ b/src/Infrastructure/BotSharp.Abstraction/MLTasks/Settings/LlmProviderSetting.cs @@ -6,6 +6,16 @@ public class LlmProviderSetting public List Models { get; set; } = []; + /// + /// A copy whose models carry no api key or endpoint, for listing to clients + /// + public LlmProviderSetting WithoutCredentials() + { + var copy = (LlmProviderSetting)MemberwiseClone(); + copy.Models = Models?.Select(x => x.WithoutCredentials()).ToList() ?? []; + return copy; + } + public override string ToString() { return $"{Provider} with {Models.Count} models"; diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/Setting/LlmProviderController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/Setting/LlmProviderController.cs index 6c2b335c1..9de71c4df 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/Setting/LlmProviderController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/Setting/LlmProviderController.cs @@ -23,17 +23,19 @@ public IEnumerable GetLlmProviders() return _llmProvider.GetProviders(); } + // The two lists below are for choosing a model; any signed-in user can read them, so the api key and endpoint stay on the server. + [HttpGet("/llm-provider/{provider}/models")] public IEnumerable GetLlmProviderModels([FromRoute] string provider, [FromQuery] LlmModelType modelType = LlmModelType.Chat) { var list = _llmProvider.GetProviderModels(provider); - return list.Where(x => x.Type == modelType); + return list.Where(x => x.Type == modelType).Select(x => x.WithoutCredentials()).ToList(); } [HttpGet("/llm-configs")] public List GetLlmConfigs([FromQuery] LlmConfigFilter filter) { var configs = _llmProvider.GetLlmConfigs(filter); - return configs; + return configs.Select(x => x.WithoutCredentials()).ToList(); } } diff --git a/tests/BotSharp.Core.UnitTests/Infrastructures/LlmProviderControllerTests.cs b/tests/BotSharp.Core.UnitTests/Infrastructures/LlmProviderControllerTests.cs new file mode 100644 index 000000000..fe9f85e87 --- /dev/null +++ b/tests/BotSharp.Core.UnitTests/Infrastructures/LlmProviderControllerTests.cs @@ -0,0 +1,75 @@ +using System.Text.Json; +using BotSharp.Abstraction.MLTasks; +using BotSharp.Abstraction.MLTasks.Filters; +using BotSharp.Abstraction.MLTasks.Settings; +using BotSharp.OpenAPI.Controllers; +using Moq; +using Xunit; + +namespace BotSharp.Core.UnitTests.Infrastructures; + +/// +/// The model lists any signed-in user can read must not carry a model's api key or endpoint, and leaving them out must +/// not touch the settings the server itself keeps calling the models with. +/// +public class LlmProviderControllerTests +{ + private const string Key = "sk-test-secret-key"; + private const string Endpoint = "https://private-resource.example.test/"; + + private static LlmModelSetting Model(string name, LlmModelType type = LlmModelType.Chat) => new() + { + Id = name, + Name = name, + ApiKey = Key, + Endpoint = Endpoint, + ApiVersion = "2024-10-21", + Type = type, + Capabilities = [LlmModelCapability.Chat], + Reasoning = new ReasoningSetting { Temperature = 1 } + }; + + private static (LlmProviderController Controller, List Kept) Create() + { + // The same instances every call, as a service that caches its settings would hand out + var kept = new List + { + new() { Provider = "openai", Models = [Model("model-a"), Model("embedding-a", LlmModelType.Embedding)] } + }; + var llm = new Mock(); + llm.Setup(x => x.GetProviderModels("openai")).Returns(kept[0].Models); + llm.Setup(x => x.GetLlmConfigs(It.IsAny())).Returns(kept); + return (new LlmProviderController(Mock.Of(), llm.Object), kept); + } + + [Fact] + public void Provider_models_leave_out_the_key_and_endpoint_and_keep_the_rest() + { + var (controller, kept) = Create(); + + var model = Assert.Single(controller.GetLlmProviderModels("openai")); + + Assert.Equal(("model-a", string.Empty, (string?)null), (model.Name, model.ApiKey, model.Endpoint)); + Assert.Equal("2024-10-21", model.ApiVersion); + Assert.Equal(new[] { LlmModelCapability.Chat }, model.Capabilities); + Assert.Equal(1, model.Reasoning?.Temperature); + Assert.DoesNotContain(Key, JsonSerializer.Serialize(model)); + Assert.All(kept[0].Models, x => Assert.Equal((Key, Endpoint), (x.ApiKey, x.Endpoint))); + } + + [Fact] + public void Llm_configs_leave_out_the_key_and_endpoint_of_every_model() + { + var (controller, kept) = Create(); + + var configs = controller.GetLlmConfigs(new LlmConfigFilter()); + + var provider = Assert.Single(configs); + Assert.Equal("openai", provider.Provider); + Assert.Equal(new[] { "model-a", "embedding-a" }, provider.Models.Select(x => x.Name)); + var json = JsonSerializer.Serialize(configs); + Assert.DoesNotContain(Key, json); + Assert.DoesNotContain("private-resource", json); + Assert.All(kept[0].Models, x => Assert.Equal((Key, Endpoint), (x.ApiKey, x.Endpoint))); + } +}