Skip to content

Commit ec7747d

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/main'
2 parents 297a1be + dab7413 commit ec7747d

File tree

3 files changed

+8
-54
lines changed

3 files changed

+8
-54
lines changed

mcpgateway/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ def validate_database(self) -> None:
317317
"text/plain",
318318
"text/html",
319319
"text/css",
320+
"text/markdown",
320321
"text/javascript",
321322
"application/json",
322323
"application/xml",

mcpgateway/services/resource_service.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import mimetypes
2222
import re
2323
from typing import Any, AsyncGenerator, Dict, List, Optional, Union
24-
from urllib.parse import urlparse
2524

2625
# Third-Party
2726
import parse
@@ -156,7 +155,6 @@ async def register_resource(self, db: Session, resource: ResourceCreate) -> Reso
156155
157156
Raises:
158157
ResourceURIConflictError: If resource URI already exists
159-
ResourceValidationError: If resource validation fails
160158
ResourceError: For other resource registration errors
161159
"""
162160
try:
@@ -170,10 +168,6 @@ async def register_resource(self, db: Session, resource: ResourceCreate) -> Reso
170168
resource_id=existing_resource.id,
171169
)
172170

173-
# Validate URI
174-
if not self._is_valid_uri(resource.uri):
175-
raise ResourceValidationError(f"Invalid URI: {resource.uri}")
176-
177171
# Detect mime type if not provided
178172
mime_type = resource.mime_type
179173
if not mime_type:
@@ -642,21 +636,6 @@ async def subscribe_events(self, uri: Optional[str] = None) -> AsyncGenerator[Di
642636
if not self._event_subscribers["*"]:
643637
del self._event_subscribers["*"]
644638

645-
def _is_valid_uri(self, uri: str) -> bool:
646-
"""Validate a resource URI.
647-
648-
Args:
649-
uri: URI to validate
650-
651-
Returns:
652-
True if URI is valid
653-
"""
654-
try:
655-
parsed = urlparse(uri)
656-
return bool(parsed.scheme and parsed.path)
657-
except Exception:
658-
return False
659-
660639
def _detect_mime_type(self, uri: str, content: Union[str, bytes]) -> str:
661640
"""Detect mime type from URI and content.
662641

tests/unit/mcpgateway/services/test_resource_service.py

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ async def test_register_resource_success(self, resource_service, mock_db, sample
172172

173173
# Mock validation and notification
174174
with (
175-
patch.object(resource_service, "_is_valid_uri", return_value=True),
176175
patch.object(resource_service, "_detect_mime_type", return_value="text/plain"),
177176
patch.object(resource_service, "_notify_resource_added", new_callable=AsyncMock),
178177
patch.object(resource_service, "_convert_resource_to_read") as mock_convert,
@@ -238,19 +237,12 @@ async def test_register_resource_uri_conflict_inactive(self, resource_service, m
238237
assert "inactive" in str(exc_info.value)
239238

240239
@pytest.mark.asyncio
241-
async def test_register_resource_invalid_uri(self, resource_service, mock_db, sample_resource_create):
242-
"""Test registration with invalid URI."""
243-
# Mock no existing resource
244-
mock_scalar = MagicMock()
245-
mock_scalar.scalar_one_or_none.return_value = None
246-
mock_db.execute.return_value = mock_scalar
247-
248-
# Mock invalid URI validation
249-
with patch.object(resource_service, "_is_valid_uri", return_value=False):
250-
with pytest.raises(ResourceError) as exc_info: # Changed to ResourceError as that's what's actually raised
251-
await resource_service.register_resource(mock_db, sample_resource_create)
240+
async def test_resource_create_with_invalid_uri(self):
241+
"""Test resource creation with invalid URI."""
242+
with pytest.raises(ValueError) as exc_info:
243+
ResourceCreate(uri="../invalid/uri", name="Bad URI", content="data")
252244

253-
assert "Invalid URI" in str(exc_info.value)
245+
assert "cannot contain directory traversal sequences" in str(exc_info.value)
254246

255247
@pytest.mark.asyncio
256248
async def test_register_resource_integrity_error(self, resource_service, mock_db, sample_resource_create):
@@ -261,7 +253,7 @@ async def test_register_resource_integrity_error(self, resource_service, mock_db
261253
mock_db.execute.return_value = mock_scalar
262254

263255
# Mock validation success
264-
with patch.object(resource_service, "_is_valid_uri", return_value=True), patch.object(resource_service, "_detect_mime_type", return_value="text/plain"):
256+
with patch.object(resource_service, "_detect_mime_type", return_value="text/plain"):
265257
# Mock IntegrityError on commit
266258
mock_db.commit.side_effect = IntegrityError("", "", "")
267259

@@ -283,7 +275,6 @@ async def test_register_resource_binary_content(self, resource_service, mock_db)
283275

284276
# Mock validation
285277
with (
286-
patch.object(resource_service, "_is_valid_uri", return_value=True),
287278
patch.object(resource_service, "_detect_mime_type", return_value="application/octet-stream"),
288279
patch.object(resource_service, "_notify_resource_added", new_callable=AsyncMock),
289280
patch.object(resource_service, "_convert_resource_to_read") as mock_convert,
@@ -1069,23 +1060,6 @@ async def test_reset_metrics(self, resource_service, mock_db):
10691060
class TestUtilityMethods:
10701061
"""Test utility methods."""
10711062

1072-
@pytest.mark.parametrize(
1073-
"uri, expected",
1074-
[
1075-
("http://example.com/test", True),
1076-
("https://example.com/test", True),
1077-
("file:///path/to/resource", True),
1078-
("ftp://example.com/file", True),
1079-
("invalid-uri", False),
1080-
("", False),
1081-
("http://", False),
1082-
],
1083-
)
1084-
def test_is_valid_uri(self, resource_service, uri, expected):
1085-
"""Test URI validation."""
1086-
result = resource_service._is_valid_uri(uri)
1087-
assert result == expected
1088-
10891063
@pytest.mark.parametrize(
10901064
"uri, content, expected",
10911065
[
@@ -1247,7 +1221,7 @@ async def test_register_resource_generic_error(self, resource_service, mock_db,
12471221
mock_db.execute.return_value = mock_scalar
12481222

12491223
# Mock validation success
1250-
with patch.object(resource_service, "_is_valid_uri", return_value=True), patch.object(resource_service, "_detect_mime_type", return_value="text/plain"):
1224+
with patch.object(resource_service, "_detect_mime_type", return_value="text/plain"):
12511225
# Mock generic error on add
12521226
mock_db.add.side_effect = Exception("Generic error")
12531227

0 commit comments

Comments
 (0)