fix(mcp): reject YAML aliases in OpenAPI specs

This commit is contained in:
Sisyphus 2026-10-04 13:02:35 +08:00
parent 119906638d
commit 45a3cbc33d
2 changed files with 23 additions and 1 deletions

View file

@ -183,6 +183,11 @@ def _parse_openapi_spec(text: str) -> Mapping[str, Any]:
import yaml
class _NoMergeSafeLoader(yaml.SafeLoader):
def compose_node(self, parent: Any, index: Any) -> Any:
if self.check_event(yaml.events.AliasEvent):
raise yaml.YAMLError("YAML aliases are not supported")
return super().compose_node(parent, index)
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")

View file

@ -315,7 +315,7 @@ def test_parse_openapi_spec_rejects_non_object_document() -> None:
def test_parse_openapi_spec_rejects_yaml_merge_aliases() -> None:
import yaml
with pytest.raises(yaml.YAMLError, match="YAML merge keys are not supported"):
with pytest.raises(yaml.YAMLError, match="YAML aliases are not supported"):
gen._parse_openapi_spec(
"""
base: &base
@ -330,6 +330,23 @@ merged:
)
def test_parse_openapi_spec_rejects_yaml_aliases() -> None:
import yaml
with pytest.raises(yaml.YAMLError, match="YAML aliases are not supported"):
gen._parse_openapi_spec(
"""
shared: &shared
type: string
components:
schemas:
Request:
properties:
first: *shared
"""
)
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.