TASK-009: Mitigate SSRF vulnerability in blocklist URL validation
- Change BlocklistSourceCreate.url from str to AnyHttpUrl (Pydantic type) - Rejects non-http schemes (file://, ftp://, etc.) at model boundary - Add is_private_ip() utility to detect RFC 1918 private ranges: - 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 (RFC 1918) - 127.0.0.0/8, ::1/128 (loopback) - 169.254.0.0/16, fe80::/10 (link-local) - IPv6 site-local, multicast, and reserved ranges - Add async validate_blocklist_url() function: - Resolves hostname via DNS using loop.run_in_executor() - Rejects if hostname resolves to private/reserved IP - Raises ValueError on validation failure - Integrate validation into service layer: - create_source() calls validate_blocklist_url() before persist - update_source() conditionally validates if url provided - Both raise ValueError on failure - Update router endpoints with error handling: - create_blocklist() and update_blocklist() catch ValueError - Return HTTP 400 Bad Request with descriptive error message - Add comprehensive test coverage (9 new SSRF tests): - file://, ftp://, localhost, 127.0.0.1, 192.168.x.x - 10.x.x.x, 172.16.x.x, 169.254.x.x (link-local) - Valid public URLs (passes validation) - All 36 service tests passing - Update documentation: - Features.md: Document URL validation constraints - Backend-Development.md: Add SSRF prevention pattern section Fixes SSRF vulnerability where authenticated users could supply file://, ftp://, or private IP URLs and the backend would fetch them. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -8,7 +8,7 @@ from __future__ import annotations
|
||||
|
||||
from enum import StrEnum
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
from pydantic import AnyHttpUrl, BaseModel, ConfigDict, Field
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Blocklist source
|
||||
@@ -29,22 +29,30 @@ class BlocklistSource(BaseModel):
|
||||
|
||||
|
||||
class BlocklistSourceCreate(BaseModel):
|
||||
"""Payload for ``POST /api/blocklists``."""
|
||||
"""Payload for ``POST /api/blocklists``.
|
||||
|
||||
URL must use http/https scheme. The hostname must resolve to a public IP
|
||||
(not private, loopback, link-local, or reserved). Validation happens
|
||||
asynchronously in the service layer.
|
||||
"""
|
||||
|
||||
model_config = ConfigDict(strict=True)
|
||||
|
||||
name: str = Field(..., min_length=1, max_length=100, description="Human-readable source name.")
|
||||
url: str = Field(..., min_length=1, description="URL of the blocklist file.")
|
||||
url: AnyHttpUrl = Field(..., description="URL of the blocklist file (http/https only).")
|
||||
enabled: bool = Field(default=True)
|
||||
|
||||
|
||||
class BlocklistSourceUpdate(BaseModel):
|
||||
"""Payload for ``PUT /api/blocklists/{id}``. All fields are optional."""
|
||||
"""Payload for ``PUT /api/blocklists/{id}``. All fields are optional.
|
||||
|
||||
If URL is provided, it must use http/https scheme.
|
||||
"""
|
||||
|
||||
model_config = ConfigDict(strict=True)
|
||||
|
||||
name: str | None = Field(default=None, min_length=1, max_length=100)
|
||||
url: str | None = Field(default=None)
|
||||
url: AnyHttpUrl | None = Field(default=None)
|
||||
enabled: bool | None = Field(default=None)
|
||||
|
||||
|
||||
|
||||
@@ -97,10 +97,16 @@ async def create_blocklist(
|
||||
|
||||
Returns:
|
||||
The newly created :class:`~app.models.blocklist.BlocklistSource`.
|
||||
|
||||
Raises:
|
||||
HTTPException: 400 if URL validation fails.
|
||||
"""
|
||||
return await blocklist_service.create_source(
|
||||
db, payload.name, payload.url, enabled=payload.enabled
|
||||
)
|
||||
try:
|
||||
return await blocklist_service.create_source(
|
||||
db, payload.name, str(payload.url), enabled=payload.enabled
|
||||
)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -283,15 +289,19 @@ async def update_blocklist(
|
||||
_auth: Validated session — enforces authentication.
|
||||
|
||||
Raises:
|
||||
HTTPException: 400 if URL validation fails.
|
||||
HTTPException: 404 if the source does not exist.
|
||||
"""
|
||||
updated = await blocklist_service.update_source(
|
||||
db,
|
||||
source_id,
|
||||
name=payload.name,
|
||||
url=payload.url,
|
||||
enabled=payload.enabled,
|
||||
)
|
||||
try:
|
||||
updated = await blocklist_service.update_source(
|
||||
db,
|
||||
source_id,
|
||||
name=payload.name,
|
||||
url=str(payload.url) if payload.url is not None else None,
|
||||
enabled=payload.enabled,
|
||||
)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc
|
||||
if updated is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Blocklist source not found.")
|
||||
return updated
|
||||
|
||||
@@ -166,15 +166,24 @@ async def create_source(
|
||||
) -> BlocklistSource:
|
||||
"""Create a new blocklist source and return the persisted record.
|
||||
|
||||
Validates that the URL uses http/https and resolves to a public IP address.
|
||||
|
||||
Args:
|
||||
db: Active application database connection.
|
||||
name: Human-readable display name.
|
||||
url: URL of the blocklist text file.
|
||||
url: URL of the blocklist text file (must be http/https and resolve to public IP).
|
||||
enabled: Whether the source is active. Defaults to ``True``.
|
||||
|
||||
Returns:
|
||||
The newly created :class:`~app.models.blocklist.BlocklistSource`.
|
||||
|
||||
Raises:
|
||||
ValueError: If the URL fails SSRF validation.
|
||||
"""
|
||||
from app.utils.ip_utils import validate_blocklist_url
|
||||
|
||||
await validate_blocklist_url(url)
|
||||
|
||||
new_id = await blocklist_repo.create_source(db, name, url, enabled=enabled)
|
||||
source = await get_source(db, new_id)
|
||||
assert source is not None # noqa: S101
|
||||
@@ -192,17 +201,27 @@ async def update_source(
|
||||
) -> BlocklistSource | None:
|
||||
"""Update fields on a blocklist source.
|
||||
|
||||
If url is provided, validates that it uses http/https and resolves to a public IP.
|
||||
|
||||
Args:
|
||||
db: Active application database connection.
|
||||
source_id: Primary key of the source to modify.
|
||||
name: New display name, or ``None`` to leave unchanged.
|
||||
url: New URL, or ``None`` to leave unchanged.
|
||||
url: New URL, or ``None`` to leave unchanged (validated if provided).
|
||||
enabled: New enabled state, or ``None`` to leave unchanged.
|
||||
|
||||
Returns:
|
||||
Updated :class:`~app.models.blocklist.BlocklistSource`, or ``None``
|
||||
if the source does not exist.
|
||||
|
||||
Raises:
|
||||
ValueError: If the URL fails SSRF validation.
|
||||
"""
|
||||
if url is not None:
|
||||
from app.utils.ip_utils import validate_blocklist_url
|
||||
|
||||
await validate_blocklist_url(url)
|
||||
|
||||
updated = await blocklist_repo.update_source(
|
||||
db, source_id, name=name, url=url, enabled=enabled
|
||||
)
|
||||
|
||||
@@ -4,7 +4,10 @@ All IP handling in BanGUI goes through these helpers to enforce consistency
|
||||
and prevent malformed addresses from reaching fail2ban.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import ipaddress
|
||||
import socket
|
||||
from urllib.parse import urlparse
|
||||
|
||||
|
||||
def is_valid_ip(address: str) -> bool:
|
||||
@@ -99,3 +102,97 @@ def ip_version(address: str) -> int:
|
||||
ValueError: If *address* is not a valid IP address.
|
||||
"""
|
||||
return ipaddress.ip_address(address).version
|
||||
|
||||
|
||||
def is_private_ip(address: str) -> bool:
|
||||
"""Return ``True`` if *address* is a private or reserved IP address.
|
||||
|
||||
Private ranges include:
|
||||
- RFC 1918: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16
|
||||
- Loopback: 127.0.0.0/8 (IPv4), ::1/128 (IPv6)
|
||||
- Link-local: 169.254.0.0/16 (IPv4), fe80::/10 (IPv6)
|
||||
- IPv6 ULA: fc00::/7
|
||||
- Multicast and other reserved ranges
|
||||
|
||||
Args:
|
||||
address: A valid IP address string.
|
||||
|
||||
Returns:
|
||||
``True`` if the address is private or reserved, ``False`` if it is public.
|
||||
|
||||
Raises:
|
||||
ValueError: If *address* is not a valid IP address.
|
||||
"""
|
||||
ip = ipaddress.ip_address(address)
|
||||
return (
|
||||
ip.is_private
|
||||
or ip.is_loopback
|
||||
or ip.is_link_local
|
||||
or ip.is_multicast
|
||||
or ip.is_reserved
|
||||
)
|
||||
|
||||
|
||||
async def validate_blocklist_url(url: str) -> None:
|
||||
"""Validate that a blocklist URL points to a public HTTP(S) endpoint.
|
||||
|
||||
Checks that:
|
||||
- The URL uses HTTP or HTTPS scheme
|
||||
- The hostname resolves to a public (non-private, non-reserved) IP address
|
||||
- IPv4-mapped IPv6 addresses are checked against IPv4 private ranges
|
||||
|
||||
Performs DNS resolution asynchronously to check the resolved IP.
|
||||
This is a point-in-time check; DNS rebinding attacks may still be possible
|
||||
at actual fetch time. Callers should re-validate the final connection
|
||||
in the HTTP client layer.
|
||||
|
||||
Args:
|
||||
url: The blocklist URL to validate.
|
||||
|
||||
Raises:
|
||||
ValueError: If the URL has an invalid scheme, hostname cannot be resolved,
|
||||
or the resolved IP is private/reserved.
|
||||
"""
|
||||
try:
|
||||
parsed = urlparse(url)
|
||||
except Exception as exc:
|
||||
raise ValueError(f"Invalid URL format: {exc}") from exc
|
||||
|
||||
if parsed.scheme not in ("http", "https"):
|
||||
raise ValueError(
|
||||
f"Invalid scheme '{parsed.scheme}': only http and https are allowed"
|
||||
)
|
||||
|
||||
if not parsed.hostname:
|
||||
raise ValueError("URL has no hostname")
|
||||
|
||||
hostname = parsed.hostname
|
||||
|
||||
try:
|
||||
loop = asyncio.get_event_loop()
|
||||
addrinfo = await loop.run_in_executor(
|
||||
None,
|
||||
socket.getaddrinfo,
|
||||
hostname,
|
||||
parsed.port or 80,
|
||||
socket.AF_UNSPEC,
|
||||
socket.SOCK_STREAM,
|
||||
)
|
||||
except socket.gaierror as exc:
|
||||
raise ValueError(f"Cannot resolve hostname '{hostname}': {exc}") from exc
|
||||
except Exception as exc:
|
||||
raise ValueError(f"DNS resolution error for '{hostname}': {exc}") from exc
|
||||
|
||||
if not addrinfo:
|
||||
raise ValueError(f"No address resolved for hostname '{hostname}'")
|
||||
|
||||
for family, socktype, proto, canonname, sockaddr in addrinfo:
|
||||
ip_str: str = sockaddr[0] # type: ignore[assignment]
|
||||
try:
|
||||
if is_private_ip(ip_str):
|
||||
raise ValueError(
|
||||
f"Hostname '{hostname}' resolves to private/reserved IP: {ip_str}"
|
||||
)
|
||||
except ipaddress.AddressValueError as exc:
|
||||
raise ValueError(f"Invalid IP address: {ip_str}") from exc
|
||||
|
||||
|
||||
Reference in New Issue
Block a user