ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)
Go aiai-agentsautonomous-agentsbug-bountydockergolanglinuxllmmcpnextjspenetration-testingpentestpostgresqlreareactred-teamsecuritysecurity-automationsqlitetypescript
fix(ollama): keyless env config and /v1 base URL normalization
BaaRai978f308a9e64
diff --git a/agent/provider.go b/agent/provider.goindex 3bfa67a..e79d60c 100644--- a/agent/provider.go+++ b/agent/provider.go@@ -224,7 +224,10 @@ func FromEnv() (Config, bool) {}case "ollama":c.Format = FormatOllama- c.APIKey = oaiKey // optional; a local Ollama server is usable keyless+ // Deliberately NOT inheriting OPENAI_API_KEY: Ollama ignores the bearer+ // token, so the only thing the inheritance could do is ship a real cloud+ // key to the local (or LAN) endpoint. An authenticated Ollama gateway can+ // still be configured as a saved profile with an explicit key.if c.BaseURL == "" {c.BaseURL = DefaultOllamaBaseURL}@@ -275,12 +278,15 @@ func ConfigFrom(provider, model, baseURL, apiKey, proxy string) Config {case "ollama":c.Format = FormatOllama// Ollama's OpenAI-compatible endpoint lives at /v1 and the provider- // appends "/chat/completions"; tolerate a full endpoint URL. Never leave- // it empty: an empty BaseURL would let the SDK fall back to OpenAI's- // public endpoint with this profile's request.- c.BaseURL = strings.TrimRight(strings.TrimSuffix(c.BaseURL, "/chat/completions"), "/")+ // appends "/chat/completions"; tolerate a full endpoint URL and the+ // natural Ollama URL without the /v1 suffix. Never leave it empty: an+ // empty BaseURL would let the SDK fall back to OpenAI's public endpoint+ // with this profile's request.+ c.BaseURL = strings.TrimRight(strings.TrimSuffix(strings.TrimSpace(c.BaseURL), "/chat/completions"), "/")if c.BaseURL == "" {c.BaseURL = DefaultOllamaBaseURL+ } else if !strings.HasSuffix(c.BaseURL, "/v1") {+ c.BaseURL += "/v1"}if c.Model == "" {c.Model = "llama3.2"diff --git a/server/llm_ollama_test.go b/server/llm_ollama_test.goindex dbbf6ba..8a65966 100644--- a/server/llm_ollama_test.go+++ b/server/llm_ollama_test.go@@ -44,6 +44,12 @@ func TestOllamaConfigFromKeylessDefaults(t *testing.T) {if k.APIKey != "ollama-secret" {t.Fatalf("custom key lost: %q", k.APIKey)}+ // The natural Ollama URL has no /v1, but the provider appends+ // /chat/completions, so the base must be normalized to the /v1 root.+ n := agent.ConfigFrom("ollama", "m", "http://box:11434", "", "")+ if n.BaseURL != "http://box:11434/v1" {+ t.Fatalf("bare host URL not normalized: %q", n.BaseURL)+ }}// The provider built from an Ollama config speaks OpenAI Chat Completions on@@ -79,8 +85,11 @@ func TestOllamaProviderWiresOpenAIProtocol(t *testing.T) {if _, _, _, err = prov.Complete(ctx, req); err != nil {t.Fatalf("Complete: %v", err)}- if p, _ := gotPath.Load().(string); p != "/chat/completions" {- t.Fatalf("path=%q, want /chat/completions (base URL must NOT be the OpenAI fallback)", p)+ // ConfigFrom normalizes the fake server URL (no /v1) onto the OpenAI root,+ // so the request must land on /v1/chat/completions. A path of /chat/completions+ // would mean the base URL leaked through unnormalized (or fell back to OpenAI).+ if p, _ := gotPath.Load().(string); p != "/v1/chat/completions" {+ t.Fatalf("path=%q, want /v1/chat/completions", p)}if a, _ := gotAuth.Load().(string); a != "Bearer ollama" {t.Fatalf("Authorization=%q, want placeholder Bearer ollama", a)@@ -221,3 +230,19 @@ func TestOllamaListModels(t *testing.T) {t.Fatalf("both calls must hit /v1/models, got %v", paths)}}++// An Ollama config resolved from the environment must not inherit OPENAI_API_KEY:+// Ollama ignores the bearer token, so inheritance would only ship the cloud key+// to the local or LAN endpoint.+func TestOllamaFromEnvDoesNotInheritOpenAIKey(t *testing.T) {+ t.Setenv("ARTEX_LLM_PROVIDER", "ollama")+ t.Setenv("OPENAI_API_KEY", "sk-cloud-secret")+ t.Setenv("ARTEX_LLM_BASE_URL", "")+ c, ok := agent.FromEnv()+ if !ok {+ t.Fatal("ollama env config must be usable without a key")+ }+ if c.APIKey != "" {+ t.Fatalf("ollama inherited a cloud key: %q", c.APIKey)+ }+}diff --git a/server/server_mgmt.go b/server/server_mgmt.goindex 3791b22..65be6bc 100644--- a/server/server_mgmt.go+++ b/server/server_mgmt.go@@ -2104,7 +2104,7 @@ func (s *Server) pgListModels(w http.ResponseWriter, r *http.Request) {// without the /v1 suffix (and a full endpoint URL). No auth needed; when a// key was provided anyway, send it as Bearer (Ollama ignores it).if baseURL == "" {- baseURL = "http://127.0.0.1:11434/v1"+ baseURL = agent.DefaultOllamaBaseURL}b := strings.TrimSuffix(baseURL, "/chat/completions")if !strings.HasSuffix(b, "/v1") {