Skip to content

Commit 43a71e8

Browse files
committed
fix: do not subscribe a webhook_url that is not an absolute URL #2206
The single webhook_url field of the project API was saved as the target URL without any check, so a relative path only showed up as a delivery error once the pipeline had run. The project is now created without that webhook and gets a warning message instead. Signed-off-by: ivasilev <ivasgo@gmail.com>
1 parent 068641d commit 43a71e8

3 files changed

Lines changed: 84 additions & 1 deletion

File tree

‎docs/rest-api.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ You can either provide:
212212

213213
Webhook fields:
214214
- ``webhook_url`` (string, optional): A single webhook URL to be notified.
215+
It must be an absolute ``http(s)`` URL. Any other value is ignored: the
216+
project is created without that webhook and a warning message is added to
217+
the project.
215218
- ``webhooks`` (list, optional): A list of webhook configurations.
216219
- ``target_url`` (string, required): The URL to which the webhook will send a
217220
``POST`` request.

‎scanpipe/api/serializers.py‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
2121
# Visit https://github.com/aboutcode-org/scancode.io for support and download.
2222

23+
from urllib.parse import urlparse
24+
2325
from django.apps import apps
2426

2527
from rest_framework import serializers
@@ -154,6 +156,20 @@ class Meta:
154156
]
155157

156158

159+
def is_absolute_http_url(url):
160+
"""
161+
Return True if the `url` has an http(s) scheme and a host.
162+
163+
Django's URLValidator is not used here as it rejects single-label hosts,
164+
such as ``http://dejacode:8000/``, which are common in container networks.
165+
"""
166+
try:
167+
parsed = urlparse(url)
168+
return parsed.scheme in ("http", "https") and bool(parsed.hostname)
169+
except ValueError:
170+
return False
171+
172+
157173
class WebhookSubscriptionSerializer(serializers.ModelSerializer):
158174
class Meta:
159175
model = WebhookSubscription
@@ -334,7 +350,18 @@ def create(self, validated_data):
334350
project.add_pipeline(pipeline_name, execute_now, selected_groups=groups)
335351

336352
if webhook_url:
337-
project.add_webhook_subscription(target_url=webhook_url)
353+
if is_absolute_http_url(webhook_url):
354+
project.add_webhook_subscription(target_url=webhook_url)
355+
else:
356+
# The project is still created: a client that ignores the response
357+
# would otherwise lose its scan along with the notification.
358+
project.add_warning(
359+
description=(
360+
f'Webhook subscription not created: "{webhook_url}" is not '
361+
f"an absolute http(s) URL."
362+
),
363+
model=WebhookSubscription,
364+
)
338365

339366
for webhook_data in webhooks:
340367
project.add_webhook_subscription(**webhook_data)

‎scanpipe/tests/test_api.py‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from scanpipe.api.serializers import ProjectSerializer
4646
from scanpipe.api.serializers import get_model_serializer
4747
from scanpipe.api.serializers import get_serializer_fields
48+
from scanpipe.api.serializers import is_absolute_http_url
4849
from scanpipe.models import APIToken
4950
from scanpipe.models import CodebaseRelation
5051
from scanpipe.models import CodebaseResource
@@ -577,6 +578,58 @@ def test_scanpipe_api_project_create_pipeline_groups(self):
577578
self.assertEqual(["Java", "JavaScript"], runs[0].selected_groups)
578579
self.assertEqual(["StaticResolver"], runs[1].selected_groups)
579580

581+
def test_scanpipe_api_project_create_webhook_url(self):
582+
webhook_url = "http://server:8000/notifications/IjEi:1sZEvv:_br-G/"
583+
data = {"name": "Project1", "webhook_url": webhook_url}
584+
response = self.csrf_client.post(self.project_list_url, data)
585+
self.assertEqual(status.HTTP_201_CREATED, response.status_code)
586+
587+
project = Project.objects.get(name=data["name"])
588+
webhook_subscription = project.webhooksubscriptions.get()
589+
self.assertEqual(webhook_url, webhook_subscription.target_url)
590+
self.assertEqual(0, project.projectmessages.count())
591+
592+
def test_scanpipe_api_project_create_webhook_url_not_absolute(self):
593+
# A sender with an empty base URL setting ends up providing only the path.
594+
# The ":" in the path makes `requests` fail with "No connection adapters".
595+
webhook_url = "/notifications/send_scan_notification/IjEi:1sZEvv:_br-G/"
596+
data = {"name": "Project1", "webhook_url": webhook_url}
597+
response = self.csrf_client.post(self.project_list_url, data)
598+
self.assertEqual(status.HTTP_201_CREATED, response.status_code)
599+
600+
project = Project.objects.get(name=data["name"])
601+
self.assertEqual(0, project.webhooksubscriptions.count())
602+
message = project.projectmessages.get()
603+
self.assertEqual("warning", message.severity)
604+
self.assertEqual("WebhookSubscription", message.model)
605+
expected = (
606+
f'Webhook subscription not created: "{webhook_url}" is not an absolute '
607+
f"http(s) URL."
608+
)
609+
self.assertEqual(expected, message.description)
610+
611+
def test_scanpipe_api_is_absolute_http_url(self):
612+
valid = [
613+
"https://example.com/webhook",
614+
"http://dejacode:8000/notifications/key/",
615+
"http://127.0.0.1:8000/",
616+
"http://[::1]:8000/hook",
617+
]
618+
for url in valid:
619+
self.assertTrue(is_absolute_http_url(url), msg=url)
620+
621+
invalid = [
622+
"/notifications/send_scan_notification/IjEi:1sZEvv:_br-G/",
623+
"notifications/key/",
624+
"example.com/webhook",
625+
"//example.com/webhook",
626+
"ftp://example.com/webhook",
627+
"http:///webhook",
628+
"http://[::1/webhook",
629+
]
630+
for url in invalid:
631+
self.assertFalse(is_absolute_http_url(url), msg=url)
632+
580633
def test_scanpipe_api_project_create_webhooks(self):
581634
data = {
582635
"name": "Project1",

0 commit comments

Comments
 (0)