diff --git a/python/sglang/srt/arg_groups/field_order.py b/python/sglang/srt/arg_groups/field_order.py index 7890b2bdbd..4074ce6b5e 100644 --- a/python/sglang/srt/arg_groups/field_order.py +++ b/python/sglang/srt/arg_groups/field_order.py @@ -136,6 +136,7 @@ POSITIONAL_FIELD_ORDER = ( "served_model_name", "weight_version", "chat_template", + "trust_request_chat_template", "hf_chat_template_name", "completion_template", "file_storage_path", diff --git a/python/sglang/srt/arg_groups/fields/serving.py b/python/sglang/srt/arg_groups/fields/serving.py index d5664ae689..1cb15c02e4 100644 --- a/python/sglang/srt/arg_groups/fields/serving.py +++ b/python/sglang/srt/arg_groups/fields/serving.py @@ -170,6 +170,10 @@ class Serving(msgspec.Struct): Optional[str], "The buliltin chat template name or the path of the chat template file. This is only used for OpenAI-compatible API server.", ] = None + trust_request_chat_template: A[ + bool, + "Allow a request to override the server chat template via its chat_template_kwargs. Off by default for safety.", + ] = False hf_chat_template_name: A[ Optional[str], "When the HuggingFace tokenizer has multiple chat templates (e.g., 'default', 'tool_use', 'rag'), specify which named template to use. If not set, the first available template is used.", diff --git a/python/sglang/srt/entrypoints/openai/serving_chat.py b/python/sglang/srt/entrypoints/openai/serving_chat.py index 38fd20470e..d5e6bf5f3e 100644 --- a/python/sglang/srt/entrypoints/openai/serving_chat.py +++ b/python/sglang/srt/entrypoints/openai/serving_chat.py @@ -1014,6 +1014,16 @@ class OpenAIServingChat(OpenAIServingBase): if not request.messages: return "Messages cannot be empty." + if ( + request.chat_template_kwargs + and "chat_template" in request.chat_template_kwargs + and not get_serving().trust_request_chat_template + ): + return ( + "Request-supplied chat_template is not allowed. Start the server " + "with --trust-request-chat-template to permit it." + ) + if request.return_sampling_mask and not request.return_meta_info: return "return_sampling_mask requires return_meta_info=true." diff --git a/test/registered/unit/entrypoints/openai/test_serving_chat.py b/test/registered/unit/entrypoints/openai/test_serving_chat.py index 17a4cc47f9..116459e7b4 100644 --- a/test/registered/unit/entrypoints/openai/test_serving_chat.py +++ b/test/registered/unit/entrypoints/openai/test_serving_chat.py @@ -4642,5 +4642,46 @@ class InklingReasoningEffortTest(unittest.TestCase): ) +class TestRequestChatTemplateTrustGate(CustomTestCase): + def setUp(self): + super().setUp() + reset_context() + self.addCleanup(reset_context) + publish(ServerArgs(model_path="dummy"), role="tokenizer") + self.chat = OpenAIServingChat(_MockTokenizerManager(), _MockTemplateManager()) + + def _request(self, **kwargs): + return ChatCompletionRequest( + model="test-model", messages=[{"role": "user", "content": "hi"}], **kwargs + ) + + def test_rejected_by_default(self): + for template in ("{{ messages }}", "", None): + with self.subTest(template=template): + error = self.chat._validate_request( + self._request(chat_template_kwargs={"chat_template": template}) + ) + self.assertIn("--trust-request-chat-template", error) + + def test_allowed_when_trusted(self): + with get_context().override_server_args(trust_request_chat_template=True): + self.assertIsNone( + self.chat._validate_request( + self._request( + chat_template_kwargs={"chat_template": "{{ messages }}"} + ) + ) + ) + + def test_other_kwargs_unchanged(self): + for kwargs in (None, {}, {"enable_thinking": False}, {"chat_templates": "x"}): + with self.subTest(kwargs=kwargs): + self.assertIsNone( + self.chat._validate_request( + self._request(chat_template_kwargs=kwargs) + ) + ) + + if __name__ == "__main__": unittest.main(verbosity=2) diff --git a/test/registered/unit/test_server_args_cli_metadata.py b/test/registered/unit/test_server_args_cli_metadata.py index 4ebf060d6c..04f92850e7 100644 --- a/test/registered/unit/test_server_args_cli_metadata.py +++ b/test/registered/unit/test_server_args_cli_metadata.py @@ -82,6 +82,14 @@ class TestServerArgsMigratedCliMetadata(CustomTestCase): self.assertEqual(args.dp_size, 3) self.assertEqual(ServerArgs.from_cli_args(args).dp_size, 3) + def test_request_chat_template_requires_explicit_opt_in(self): + for flags, expected in (([], False), (["--trust-request-chat-template"], True)): + with self.subTest(flags=flags): + args = self.parser.parse_args(["--model", "dummy", *flags]) + self.assertIs( + ServerArgs.from_cli_args(args).trust_request_chat_template, expected + ) + def test_prefill_max_context_accepts_human_readable_values(self): for option in ( "--cuda-graph-prefill-max-context",