[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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user