fix(url-safety): allow only http and https schemes
This commit is contained in:
@@ -22,6 +22,14 @@ class TestIsSafeUrl:
|
|||||||
]):
|
]):
|
||||||
assert is_safe_url("https://example.com/image.png") is True
|
assert is_safe_url("https://example.com/image.png") is True
|
||||||
|
|
||||||
|
def test_ftp_scheme_blocked(self):
|
||||||
|
"""Only http/https should be allowed for fetch tools."""
|
||||||
|
assert is_safe_url("ftp://example.com/file.txt") is False
|
||||||
|
|
||||||
|
def test_missing_scheme_blocked(self):
|
||||||
|
"""Bare host/path should be rejected to avoid ambiguous handling."""
|
||||||
|
assert is_safe_url("example.com/path") is False
|
||||||
|
|
||||||
def test_localhost_blocked(self):
|
def test_localhost_blocked(self):
|
||||||
with patch("socket.getaddrinfo", return_value=[
|
with patch("socket.getaddrinfo", return_value=[
|
||||||
(2, 1, 6, "", ("127.0.0.1", 0)),
|
(2, 1, 6, "", ("127.0.0.1", 0)),
|
||||||
|
|||||||
@@ -263,6 +263,9 @@ def is_safe_url(url: str) -> bool:
|
|||||||
parsed = urlparse(url)
|
parsed = urlparse(url)
|
||||||
hostname = (parsed.hostname or "").strip().lower().rstrip(".")
|
hostname = (parsed.hostname or "").strip().lower().rstrip(".")
|
||||||
scheme = (parsed.scheme or "").strip().lower()
|
scheme = (parsed.scheme or "").strip().lower()
|
||||||
|
if scheme not in {"http", "https"}:
|
||||||
|
logger.warning("Blocked request — unsupported URL scheme: %s", scheme or "<empty>")
|
||||||
|
return False
|
||||||
if not hostname:
|
if not hostname:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user