From 9a9aa2b017b468082ca538b6f7a725a60d7f8b47 Mon Sep 17 00:00:00 2001 From: LuckyStep <67696304+Andy365-365@users.noreply.github.com> Date: Wed, 19 Aug 2026 15:52:53 +0800 Subject: [PATCH] [Bugfix] Redact api_key in non-default args log (#52523) Signed-off-by: LuckyStep <67696304+Andy365-365@users.noreply.github.com> --- .../entrypoints/serve/utils/test_api_utils.py | 37 +++++++++++++++++++ tests/test_envs.py | 6 +++ vllm/entrypoints/serve/utils/api_utils.py | 16 +++++++- vllm/envs.py | 3 ++ 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/tests/entrypoints/serve/utils/test_api_utils.py b/tests/entrypoints/serve/utils/test_api_utils.py index c23d0e9e68..669bff18d3 100644 --- a/tests/entrypoints/serve/utils/test_api_utils.py +++ b/tests/entrypoints/serve/utils/test_api_utils.py @@ -1,10 +1,14 @@ # SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project +from argparse import Namespace + import pytest from vllm.entrypoints.openai.engine.protocol import StreamOptions +from vllm.entrypoints.serve.utils import api_utils from vllm.entrypoints.serve.utils.api_utils import ( + _redact_sensitive_args, get_max_tokens, should_include_usage, ) @@ -110,3 +114,36 @@ class TestGetMaxTokens: input_length=150, default_sampling_params={"max_tokens": 2048}, ) + + +class TestRedactSensitiveArgs: + API_KEY = "sk-test-secret-12345" + + def test_redact_replaces_sensitive_values_only(self): + args = {"api_key": self.API_KEY, "other": "visible"} + redacted = _redact_sensitive_args(args) + assert redacted == {"api_key": "***", "other": "visible"} + # original dict must not be mutated + assert args == {"api_key": self.API_KEY, "other": "visible"} + + def test_no_sensitive_fields_returns_original(self): + args = {"model_tag": "org/model", "other": "visible"} + assert _redact_sensitive_args(args) is args + + def test_api_key_not_in_log(self, monkeypatch, caplog): + non_default = { + "model_tag": "org/model", + "default_chat_template_kwargs": {"enable_thinking": False}, + "api_key": self.API_KEY, + "enable_auto_tool_choice": True, + "tool_call_parser": "qwen3_coder", + } + monkeypatch.setattr(api_utils, "get_non_default_args", lambda args: non_default) + with caplog.at_level("INFO", logger="vllm.entrypoints.serve.utils.api_utils"): + api_utils.log_non_default_args(args=Namespace()) + message = caplog.text + assert self.API_KEY not in message + assert "'api_key': '***'" in message + # non-sensitive args are still logged + assert "org/model" in message + assert "qwen3_coder" in message diff --git a/tests/test_envs.py b/tests/test_envs.py index 3d214fdbff..1ef40cde0c 100644 --- a/tests/test_envs.py +++ b/tests/test_envs.py @@ -37,6 +37,12 @@ def test_nixl_side_channel_host_is_not_compile_factor( assert "VLLM_NIXL_SIDE_CHANNEL_HOST" not in envs.compile_factors() +def test_api_key_is_not_compile_factor(monkeypatch: pytest.MonkeyPatch): + monkeypatch.setenv("VLLM_API_KEY", "sk-super-secret") + + assert "VLLM_API_KEY" not in envs.compile_factors() + + def test_p2p_side_channel_defaults_and_override(monkeypatch: pytest.MonkeyPatch): monkeypatch.delenv("VLLM_P2P_SIDE_CHANNEL_HOST", raising=False) monkeypatch.delenv("VLLM_P2P_SIDE_CHANNEL_PORT", raising=False) diff --git a/vllm/entrypoints/serve/utils/api_utils.py b/vllm/entrypoints/serve/utils/api_utils.py index c75cd16e54..f186ec862b 100644 --- a/vllm/entrypoints/serve/utils/api_utils.py +++ b/vllm/entrypoints/serve/utils/api_utils.py @@ -267,9 +267,23 @@ def jsonify_non_default_args( return {key: _jsonify_arg_value(value) for key, value in non_default_args.items()} +# Fields whose values must never be logged verbatim. +_SENSITIVE_ARG_FIELDS = frozenset({"api_key"}) + + +def _redact_sensitive_args(args: dict[str, Any]) -> dict[str, Any]: + """Return a copy of `args` with sensitive values redacted for logging.""" + if not any(key in _SENSITIVE_ARG_FIELDS for key in args): + return args + return { + key: ("***" if key in _SENSITIVE_ARG_FIELDS else value) + for key, value in args.items() + } + + def log_non_default_args(args: Namespace | EngineArgs): non_default_args = get_non_default_args(args) - logger.info("non-default args: %s", non_default_args) + logger.info("non-default args: %s", _redact_sensitive_args(non_default_args)) def should_include_usage( diff --git a/vllm/envs.py b/vllm/envs.py index f80c51207f..b49841d9ff 100755 --- a/vllm/envs.py +++ b/vllm/envs.py @@ -2243,6 +2243,9 @@ def compile_factors() -> dict[str, object]: "S3_ACCESS_KEY_ID", "S3_SECRET_ACCESS_KEY", "S3_ENDPOINT_URL", + # Credential; never affects compiled artifacts and must not be + # persisted in cache_key_factors.json. + "VLLM_API_KEY", "VLLM_USAGE_STATS_SERVER", "VLLM_NO_USAGE_STATS", "VLLM_DO_NOT_TRACK",