mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-15 04:19:08 +00:00
fix(mcp): reject YAML merge keys in OpenAPI specs
This commit is contained in:
parent
b517296e32
commit
119906638d
2 changed files with 25 additions and 1 deletions
|
|
@ -182,7 +182,13 @@ def _parse_openapi_spec(text: str) -> Mapping[str, Any]:
|
|||
except json.JSONDecodeError:
|
||||
import yaml
|
||||
|
||||
parsed = yaml.safe_load(text)
|
||||
class _NoMergeSafeLoader(yaml.SafeLoader):
|
||||
def flatten_mapping(self, node: Any) -> None:
|
||||
if any(key_node.tag == "tag:yaml.org,2002:merge" for key_node, _ in node.value):
|
||||
raise yaml.YAMLError("YAML merge keys are not supported")
|
||||
super().flatten_mapping(node)
|
||||
|
||||
parsed = yaml.load(text, Loader=_NoMergeSafeLoader)
|
||||
|
||||
if not isinstance(parsed, dict):
|
||||
raise TypeError("OpenAPI spec must be a JSON or YAML object")
|
||||
|
|
|
|||
|
|
@ -312,6 +312,24 @@ def test_parse_openapi_spec_rejects_non_object_document() -> None:
|
|||
gen._parse_openapi_spec("- not an OpenAPI object\n")
|
||||
|
||||
|
||||
def test_parse_openapi_spec_rejects_yaml_merge_aliases() -> None:
|
||||
import yaml
|
||||
|
||||
with pytest.raises(yaml.YAMLError, match="YAML merge keys are not supported"):
|
||||
gen._parse_openapi_spec(
|
||||
"""
|
||||
base: &base
|
||||
openapi: 3.0.0
|
||||
info:
|
||||
title: Base
|
||||
version: '1.0.0'
|
||||
paths: {}
|
||||
merged:
|
||||
<<: *base
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
def _create_mock_client(method: str, response_text: str, status_code: int = 200) -> AsyncMock:
|
||||
"""Utility to create a mocked async httpx client for the given method.
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue