|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from mcp.shared.json_schema_ref import ( |
| 6 | + ExternalSchemaRefError, |
| 7 | + find_external_refs, |
| 8 | + is_same_document_ref, |
| 9 | + reject_external_refs, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.parametrize( |
| 14 | + "ref", |
| 15 | + [ |
| 16 | + "#", |
| 17 | + "#/$defs/Foo", |
| 18 | + "#/properties/bar", |
| 19 | + "#Foo", |
| 20 | + ], |
| 21 | +) |
| 22 | +def test_same_document_refs_allowed(ref: str): |
| 23 | + assert is_same_document_ref(ref) is True |
| 24 | + schema = {"type": "object", "properties": {"x": {"$ref": ref}}} |
| 25 | + assert find_external_refs(schema) == [] |
| 26 | + reject_external_refs(schema, context="schema") |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.parametrize( |
| 30 | + "ref", |
| 31 | + [ |
| 32 | + "https://example.com/schema.json", |
| 33 | + "http://localhost:9999/canary.json", |
| 34 | + "https://example.com/schema.json#/$defs/Foo", |
| 35 | + "urn:example:schema", |
| 36 | + "file:///etc/passwd", |
| 37 | + "schema.json", |
| 38 | + "./local.json", |
| 39 | + "//example.com/schema.json", |
| 40 | + ], |
| 41 | +) |
| 42 | +def test_external_refs_detected(ref: str): |
| 43 | + assert is_same_document_ref(ref) is False |
| 44 | + schema = {"type": "object", "properties": {"x": {"$ref": ref}}} |
| 45 | + assert find_external_refs(schema) == [ref] |
| 46 | + |
| 47 | + |
| 48 | +def test_reject_external_refs_raises_with_context(): |
| 49 | + schema = {"properties": {"x": {"$ref": "https://evil.example/s.json"}}} |
| 50 | + with pytest.raises(ExternalSchemaRefError) as exc_info: |
| 51 | + reject_external_refs(schema, context="Output schema for tool 'lookup'") |
| 52 | + message = str(exc_info.value) |
| 53 | + assert "Output schema for tool 'lookup'" in message |
| 54 | + assert "https://evil.example/s.json" in message |
| 55 | + |
| 56 | + |
| 57 | +def test_find_external_refs_nested_in_lists_and_composition(): |
| 58 | + schema = { |
| 59 | + "type": "object", |
| 60 | + "allOf": [ |
| 61 | + {"properties": {"a": {"$ref": "#/$defs/A"}}}, |
| 62 | + {"properties": {"b": {"$ref": "https://example.com/b.json"}}}, |
| 63 | + ], |
| 64 | + "items": [{"$ref": "https://example.com/c.json"}], |
| 65 | + "$defs": {"A": {"type": "string"}}, |
| 66 | + } |
| 67 | + assert sorted(find_external_refs(schema)) == [ |
| 68 | + "https://example.com/b.json", |
| 69 | + "https://example.com/c.json", |
| 70 | + ] |
| 71 | + |
| 72 | + |
| 73 | +def test_non_string_ref_is_ignored(): |
| 74 | + schema = {"$ref": {"not": "a string"}, "properties": {"x": {"$ref": 123}}} |
| 75 | + assert find_external_refs(schema) == [] |
| 76 | + |
| 77 | + |
| 78 | +def test_scalar_and_empty_inputs(): |
| 79 | + assert find_external_refs(None) == [] |
| 80 | + assert find_external_refs("just a string") == [] |
| 81 | + assert find_external_refs(42) == [] |
| 82 | + assert find_external_refs({}) == [] |
| 83 | + assert find_external_refs([]) == [] |
0 commit comments