Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

Refuse internal targets for user-set webhook URLs

Juan Denisa8c5237d325b
diff --git a/backend/app/queue_bus/consumers/webhook_consumer.py b/backend/app/queue_bus/consumers/webhook_consumer.py
index c90e3b76..e825884e 100644
--- a/backend/app/queue_bus/consumers/webhook_consumer.py
+++ b/backend/app/queue_bus/consumers/webhook_consumer.py
@@ -8,7 +8,7 @@ import time
import uuid
from datetime import datetime
-import requests as http_requests
+from app.utils import safe_http
from app import db
from app.queue_bus.service import QueueBusService
@@ -131,7 +131,7 @@ class WebhookConsumer:
start_time = time.time()
try:
- resp = http_requests.post(
+ resp = safe_http.post(
subscription.url,
data=payload_json,
headers=headers,
diff --git a/backend/app/services/notification_service.py b/backend/app/services/notification_service.py
index 17de391b..3e453978 100644
--- a/backend/app/services/notification_service.py
+++ b/backend/app/services/notification_service.py
@@ -21,6 +21,7 @@ from typing import Dict, List, Optional
from pathlib import Path
from app import paths
+from app.utils import safe_http
from app.utils.config_store import load_json_config, save_json_config
@@ -183,7 +184,7 @@ class NotificationService:
if config.get('avatar_url'):
payload['avatar_url'] = config['avatar_url']
- response = requests.post(
+ response = safe_http.post(
webhook_url,
json=payload,
timeout=10
@@ -288,7 +289,7 @@ class NotificationService:
if config.get('channel'):
payload['channel'] = config['channel']
- response = requests.post(
+ response = safe_http.post(
webhook_url,
json=payload,
timeout=10
@@ -517,7 +518,7 @@ class NotificationService:
headers = {'Content-Type': 'application/json'}
headers.update(config.get('headers', {}))
- response = requests.post(
+ response = safe_http.post(
webhook_url,
json=payload,
headers=headers,
diff --git a/backend/app/services/webhook_gateway_service.py b/backend/app/services/webhook_gateway_service.py
index e7e50b7a..58491f30 100644
--- a/backend/app/services/webhook_gateway_service.py
+++ b/backend/app/services/webhook_gateway_service.py
@@ -8,11 +8,11 @@ import uuid
from datetime import datetime
from typing import Dict, List, Optional
-import requests
from app import db
from app.exceptions import ConflictError, NotFoundError, ValidationError
from app.models import WebhookEndpoint, WebhookDelivery
+from app.utils import safe_http
from app.utils.slug import unique_slug
logger = logging.getLogger(__name__)
@@ -211,12 +211,11 @@ class WebhookGatewayService:
}
for attempt in range(max(1, endpoint.retry_count)):
try:
- resp = requests.post(
+ resp = safe_http.post(
endpoint.forward_url,
data=payload,
headers=forward_headers,
timeout=30,
- verify=True,
)
return {
'success': 200 <= resp.status_code < 300,
diff --git a/backend/app/utils/safe_http.py b/backend/app/utils/safe_http.py
new file mode 100644
index 00000000..d4eefb29
--- /dev/null
+++ b/backend/app/utils/safe_http.py
@@ -0,0 +1,102 @@
+"""Outbound HTTP to URLs that users configure (webhooks, notification hooks).
+
+A URL a developer types in is a request the panel makes from inside the
+network: to its own API on loopback, to other hosts on the LAN, or to the
+cloud metadata service. :func:`post` refuses those targets, connects to the
+exact address it checked (so a DNS answer can't change between the check
+and the connect), never follows redirects, and caps the response body it
+keeps. ``SERVERKIT_ALLOW_PRIVATE_WEBHOOKS=1`` lifts the address rule for
+operators who deliberately forward to internal hosts.
+"""
+import ipaddress
+import socket
+from urllib.parse import urlsplit, urlunsplit
+
+import requests
+from requests.adapters import HTTPAdapter
+
+from app.utils.env import env_bool
+
+ALLOW_PRIVATE_ENV = 'SERVERKIT_ALLOW_PRIVATE_WEBHOOKS'
+MAX_RESPONSE_BYTES = 64 * 1024
+
+
+class UnsafeURLError(ValueError):
+ """The URL points somewhere the panel must not send requests."""
+
+
+def _is_public(addr):
+ if addr.version == 6 and addr.ipv4_mapped:
+ addr = addr.ipv4_mapped
+ return addr.is_global and not addr.is_multicast
+
+
+def resolve_public(url):
+ """Validate *url* and return ``(parts, ip)`` — the address to connect to.
+
+ Raises :class:`UnsafeURLError` for a non-http(s) URL, an unresolvable
+ host, or a host with any non-public address.
+ """
+ parts = urlsplit(url or '')
+ if parts.scheme not in ('http', 'https'):
+ raise UnsafeURLError('URL must start with http:// or https://')
+ host = parts.hostname
+ if not host:
+ raise UnsafeURLError('URL has no host')
+ try:
+ port = parts.port or (443 if parts.scheme == 'https' else 80)
+ except ValueError:
+ raise UnsafeURLError('URL has an invalid port')
+ try:
+ infos = socket.getaddrinfo(host, port, type=socket.SOCK_STREAM)
+ except (socket.gaierror, UnicodeError) as exc:
+ raise UnsafeURLError(f'Could not resolve {host}: {exc}')
+ addrs = [ipaddress.ip_address(info[4][0].split('%')[0]) for info in infos]
+ if not addrs:
+ raise UnsafeURLError(f'Could not resolve {host}')
+ if not env_bool(ALLOW_PRIVATE_ENV):
+ blocked = [a for a in addrs if not _is_public(a)]
+ if blocked:
+ raise UnsafeURLError(
+ f'{host} resolves to a non-public address ({blocked[0]}); set '
+ f'{ALLOW_PRIVATE_ENV}=1 to allow internal targets')
+ return parts, addrs[0]
+
+
+class _PinnedAdapter(HTTPAdapter):
+ """Keeps TLS SNI and certificate checks on the hostname while the socket
+ goes to the pre-resolved IP in the URL."""
+
+ def __init__(self, hostname, **kwargs):
+ self._hostname = hostname
+ super().__init__(**kwargs)
+
+ def init_poolmanager(self, *args, **kwargs):
+ kwargs['server_hostname'] = self._hostname
+ kwargs['assert_hostname'] = self._hostname
+ super().init_poolmanager(*args, **kwargs)
+
+
+def post(url, *, timeout=10, max_bytes=MAX_RESPONSE_BYTES, headers=None, **kwargs):
+ """``requests.post`` to a public address only; see the module docstring.
+
+ The returned response's body is truncated to *max_bytes*.
+ """
+ parts, ip = resolve_public(url)
+ ip_host = f'[{ip}]' if ip.version == 6 else str(ip)
+ netloc = ip_host if parts.port is None else f'{ip_host}:{parts.port}'
+ pinned = urlunsplit((parts.scheme, netloc, parts.path, parts.query, parts.fragment))
+
+ host_header = parts.hostname if parts.port is None else f'{parts.hostname}:{parts.port}'
+ headers = {**(headers or {}), 'Host': host_header}
+
+ with requests.Session() as session:
+ if parts.scheme == 'https':
+ session.mount('https://', _PinnedAdapter(parts.hostname))
+ resp = session.post(pinned, headers=headers, timeout=timeout,
+ allow_redirects=False, stream=True, **kwargs)
+ try:
+ resp._content = resp.raw.read(max_bytes, decode_content=True)
+ finally:
+ resp.close()
+ return resp
diff --git a/backend/tests/test_notification_chat_connections.py b/backend/tests/test_notification_chat_connections.py
index 24bf70cf..202b8b09 100644
--- a/backend/tests/test_notification_chat_connections.py
+++ b/backend/tests/test_notification_chat_connections.py
@@ -366,7 +366,7 @@ class TestConnectionTesting:
captured.update({'url': url, 'json': json, 'timeout': timeout})
return _Resp()
- monkeypatch.setattr('app.services.notification_service.requests.post', fake_post)
+ monkeypatch.setattr('app.services.notification_service.safe_http.post', fake_post)
conn = ChatWebhookService.add({
'kind': 'discord',
'name': 'Disabled room',
@@ -595,7 +595,7 @@ class TestApi:
text = ''
monkeypatch.setattr(
- 'app.services.notification_service.requests.post',
+ 'app.services.notification_service.safe_http.post',
lambda *args, **kwargs: _Resp(),
)
conn = ChatWebhookService.add({
diff --git a/backend/tests/test_queue_bus_webhooks.py b/backend/tests/test_queue_bus_webhooks.py
index dd4428c3..0b1566e7 100644
--- a/backend/tests/test_queue_bus_webhooks.py
+++ b/backend/tests/test_queue_bus_webhooks.py
@@ -61,7 +61,7 @@ class TestWebhookQueueIntegration:
calls.append((url, data, headers, timeout))
return Resp()
- monkeypatch.setattr('app.queue_bus.consumers.webhook_consumer.http_requests.post', fake_post)
+ monkeypatch.setattr('app.queue_bus.consumers.webhook_consumer.safe_http.post', fake_post)
consumer = WebhookConsumer(app)
consumer._process_batch()
@@ -92,7 +92,7 @@ class TestWebhookQueueIntegration:
text = 'error'
return Resp()
- monkeypatch.setattr('app.queue_bus.consumers.webhook_consumer.http_requests.post', fake_post)
+ monkeypatch.setattr('app.queue_bus.consumers.webhook_consumer.safe_http.post', fake_post)
consumer = WebhookConsumer(app)
# Three attempts: initial + 2 retries. Bypass the bus retry delay in tests
diff --git a/backend/tests/test_safe_http.py b/backend/tests/test_safe_http.py
new file mode 100644
index 00000000..f7f1bc51
--- /dev/null
+++ b/backend/tests/test_safe_http.py
@@ -0,0 +1,150 @@
+"""safe_http.post must refuse internal targets, connect to the address it
+checked, never follow redirects, and cap the body it keeps."""
+import socket
+import threading
+from http.server import BaseHTTPRequestHandler, HTTPServer
+
+import pytest
+
+from app.utils import safe_http
+from app.utils.safe_http import UnsafeURLError, post, resolve_public
+
+_real_getaddrinfo = socket.getaddrinfo
+
+
+def _fake_dns(mapping):
+ def getaddrinfo(host, port, *args, **kwargs):
+ if host in mapping:
+ family = socket.AF_INET6 if ':' in mapping[host] else socket.AF_INET
+ return [(family, socket.SOCK_STREAM, 6, '', (mapping[host], port))]
+ return _real_getaddrinfo(host, port, *args, **kwargs)
+ return getaddrinfo
+
+
+ '127.0.0.1', '10.0.0.5', '172.16.3.4', '192.168.1.1', '169.254.169.254',
+ '100.64.0.1', '0.0.0.0', '::1', 'fd00::1', 'fe80::1', '::ffff:127.0.0.1',
+ '::ffff:169.254.169.254', '224.0.0.1',
+])
+def test_internal_addresses_are_refused(monkeypatch, ip):
+ monkeypatch.setattr(socket, 'getaddrinfo', _fake_dns({'hook.test': ip}))
+ with pytest.raises(UnsafeURLError):
+ resolve_public('https://hook.test/x')
+
+
+def test_a_name_with_any_internal_answer_is_refused(monkeypatch):
+ def getaddrinfo(host, port, *a, **k):
+ return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', ('93.184.216.34', port)),
+ (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('10.0.0.1', port))]
+ monkeypatch.setattr(socket, 'getaddrinfo', getaddrinfo)
+ with pytest.raises(UnsafeURLError):
+ resolve_public('https://hook.test/x')
+
+
[email protected]('url', ['file:///etc/passwd', 'gopher://x/', 'ftp://h/', '', 'http://'])
+def test_non_http_urls_are_refused(url):
+ with pytest.raises(UnsafeURLError):
+ resolve_public(url)
+
+
+def test_public_address_is_allowed(monkeypatch):
+ monkeypatch.setattr(socket, 'getaddrinfo', _fake_dns({'hook.test': '93.184.216.34'}))
+ parts, ip = resolve_public('https://hook.test/x')
+ assert str(ip) == '93.184.216.34' and parts.hostname == 'hook.test'
+
+
+def test_refused_before_any_connection(monkeypatch):
+ monkeypatch.setattr(socket, 'getaddrinfo', _fake_dns({'hook.test': '169.254.169.254'}))
+ monkeypatch.setattr(safe_http.requests, 'Session',
+ lambda: pytest.fail('a request was attempted'))
+ with pytest.raises(UnsafeURLError):
+ post('http://hook.test/latest/meta-data/')
+
+
+def server():
+ seen = []
+
+ class Handler(BaseHTTPRequestHandler):
+ def do_POST(self):
+ seen.append({'host': self.headers['Host'], 'path': self.path,
+ 'body': self.rfile.read(int(self.headers['Content-Length'] or 0))})
+ if self.path == '/redirect':
+ self.send_response(302)
+ self.send_header('Location', 'http://169.254.169.254/')
+ self.end_headers()
+ return
+ body = b'x' * (1024 * 1024) if self.path == '/big' else b'ok'
+ self.send_response(200)
+ self.send_header('Content-Length', str(len(body)))
+ self.end_headers()
+ self.wfile.write(body)
+
+ def log_message(self, *args):
+ pass
+
+ httpd = HTTPServer(('127.0.0.1', 0), Handler)
+ thread = threading.Thread(target=httpd.serve_forever, daemon=True)
+ thread.start()
+ yield httpd.server_address[1], seen
+ httpd.shutdown()
+
+
+def test_connects_to_the_checked_address_with_the_original_host(monkeypatch, server):
+ port, seen = server
+ monkeypatch.setenv(safe_http.ALLOW_PRIVATE_ENV, '1')
+ monkeypatch.setattr(socket, 'getaddrinfo', _fake_dns({'hook.test': '127.0.0.1'}))
+ resp = post(f'http://hook.test:{port}/in?a=1', data=b'payload')
+ assert resp.status_code == 200 and resp.text == 'ok'
+ assert seen == [{'host': f'hook.test:{port}', 'path': '/in?a=1', 'body': b'payload'}]
+
+
+def test_redirects_are_not_followed(monkeypatch, server):
+ port, seen = server
+ monkeypatch.setenv(safe_http.ALLOW_PRIVATE_ENV, '1')
+ resp = post(f'http://127.0.0.1:{port}/redirect')
+ assert resp.status_code == 302
+ assert len(seen) == 1
+
+
+def test_response_body_is_capped(monkeypatch, server):
+ port, _ = server
+ monkeypatch.setenv(safe_http.ALLOW_PRIVATE_ENV, '1')
+ resp = post(f'http://127.0.0.1:{port}/big', max_bytes=1000)
+ assert len(resp.content) == 1000
+
+
+def test_loopback_needs_the_opt_in(server):
+ port, seen = server
+ with pytest.raises(UnsafeURLError):
+ post(f'http://127.0.0.1:{port}/in')
+ assert seen == []
+
+
+# The user-configured sinks all go through safe_http: the metadata address
+# (an IP literal, so no DNS involved) must be refused by each of them.
+METADATA = 'http://169.254.169.254/latest/meta-data/'
+_ALERT = [{'type': 'cpu', 'severity': 'critical', 'message': 'hot', 'value': 99, 'threshold': 90}]
+
+
[email protected]('sender', ['send_discord', 'send_slack', 'send_generic_webhook'])
+def test_notification_webhooks_refuse_internal_urls(app, monkeypatch, sender):
+ from app.services.notification_service import NotificationService
+ monkeypatch.setattr(safe_http.requests, 'Session',
+ lambda: pytest.fail('a request was attempted'))
+ result = getattr(NotificationService, sender)(
+ _ALERT, {'enabled': True, 'webhook_url': METADATA, 'url': METADATA})
+ assert result['success'] is False
+ assert 'non-public' in result['error']
+
+
+def test_gateway_forward_refuses_internal_urls(app, monkeypatch):
+ from types import SimpleNamespace
+ from app.services.webhook_gateway_service import WebhookGatewayService
+ monkeypatch.setattr(safe_http.requests, 'Session',
+ lambda: pytest.fail('a request was attempted'))
+ endpoint = SimpleNamespace(forward_url=METADATA, retry_count=1)
+ delivery = SimpleNamespace(event_id='e1')
+ result = WebhookGatewayService._forward(endpoint, delivery, b'{}', {})
+ assert result['success'] is False
+ assert 'non-public' in result['error']