[Bugfix] Redact api_key in non-default args log (#52523)

Signed-off-by: LuckyStep <67696304+Andy365-365@users.noreply.github.com>
This commit is contained in:
LuckyStep
2026-08-19 15:52:53 +08:00
committed by GitHub
parent 842dd8fd96
commit 9a9aa2b017
4 changed files with 61 additions and 1 deletions
@@ -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
+6
View File
@@ -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)
+15 -1
View File
@@ -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(
+3
View File
@@ -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",