|
|
@@ -13,6 +13,7 @@ from django.core.exceptions import ImproperlyConfigured, ValidationError
|
|
|
from django.http import HttpResponse
|
|
|
from django.test import RequestFactory, TestCase, override_settings, tag
|
|
|
from django.urls import reverse
|
|
|
+from jinja2.sandbox import SecurityError
|
|
|
from PIL import Image
|
|
|
from requests import Session
|
|
|
from rest_framework import status
|
|
|
@@ -39,7 +40,7 @@ from netbox.event_rules import (
|
|
|
)
|
|
|
from netbox.registry import registry
|
|
|
from netbox.tests.dummy_plugin.event_rules import DummyRaisingAction
|
|
|
-from users.models import ObjectPermission
|
|
|
+from users.models import ObjectPermission, User
|
|
|
from utilities.testing import APITestCase, create_test_device, disable_warnings
|
|
|
from utilities.testing.mixins import RQQueueTestMixin
|
|
|
|
|
|
@@ -2046,3 +2047,90 @@ class WebhookRenderHeadersTest(TestCase):
|
|
|
self.assertEqual(list(headers.keys()), ['X-Object'])
|
|
|
self.assertNotIn('X-Injected', headers)
|
|
|
self.assertEqual(headers['X-Object'], 'legitX-Injected: evil')
|
|
|
+
|
|
|
+
|
|
|
+@override_settings(TEMPLATE_RENDERING={'webhooks': 'restricted'})
|
|
|
+class WebhookRestrictedRenderingTest(TestCase):
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def setUpTestData(cls):
|
|
|
+ cls.webhook = Webhook.objects.create(
|
|
|
+ name='Webhook 1',
|
|
|
+ payload_url='http://localhost:9000/{{ data.slug }}/',
|
|
|
+ additional_headers='X-Object: {{ data.name | header_safe }}',
|
|
|
+ body_template='{{ data.status.label.upper() }} {{ request.GET.q }} {{ context.plugin.foo }}',
|
|
|
+ )
|
|
|
+ cls.event_rule = EventRule.objects.create(
|
|
|
+ name='Event Rule 1',
|
|
|
+ event_types=[OBJECT_CREATED],
|
|
|
+ action_type=EventRuleActionChoices.WEBHOOK,
|
|
|
+ action_object_type=ObjectType.objects.get_for_model(Webhook),
|
|
|
+ action_object_id=cls.webhook.pk,
|
|
|
+ )
|
|
|
+ cls.site = Site.objects.create(name='Site 1', slug='site-1')
|
|
|
+
|
|
|
+ def test_render(self):
|
|
|
+ context = {'data': {'name': 'Site 1', 'slug': 'site-1', 'status': {'label': 'Active'}}}
|
|
|
+ self.assertEqual(self.webhook.render_payload_url(context), 'http://localhost:9000/site-1/')
|
|
|
+ self.assertEqual(self.webhook.render_headers(context), {'X-Object': 'Site 1'})
|
|
|
+
|
|
|
+ def test_render_restricts_access(self):
|
|
|
+ webhook = Webhook(name='Webhook 2', payload_url='http://localhost:9000/', body_template='{{ data.name }}')
|
|
|
+ with self.assertRaises(SecurityError):
|
|
|
+ webhook.render_body({'data': Site(name='Site 1')})
|
|
|
+
|
|
|
+ def test_send_webhook(self):
|
|
|
+ def plain_callback(object_type, event_type, data, request):
|
|
|
+ return {'plugin': {'foo': 'bar'}}
|
|
|
+
|
|
|
+ def object_callback(object_type, event_type, data, request):
|
|
|
+ return {'site': Site.objects.first()}
|
|
|
+
|
|
|
+ def dummy_send(_, request, **kwargs):
|
|
|
+ # Lazy choice labels and the request's query parameters are plain data
|
|
|
+ self.assertEqual(request.url, 'http://localhost:9000/site-1/')
|
|
|
+ self.assertEqual(request.headers['X-Object'], 'Site 1')
|
|
|
+ self.assertEqual(request.body, b'ACTIVE foo bar')
|
|
|
+ return HttpResponse()
|
|
|
+
|
|
|
+ request = RequestFactory().get('/dcim/sites/', {'q': 'foo'})
|
|
|
+ request.user = User.objects.create_user(username='testuser')
|
|
|
+ with (
|
|
|
+ patch.dict(registry, {'webhook_callbacks': [plain_callback, object_callback]}),
|
|
|
+ patch.object(Session, 'send', dummy_send),
|
|
|
+ self.assertLogs('netbox.webhooks', level='WARNING') as logs,
|
|
|
+ ):
|
|
|
+ send_webhook(
|
|
|
+ event_rule=self.event_rule,
|
|
|
+ object_type=ObjectType.objects.get_for_model(Site),
|
|
|
+ event_type=OBJECT_CREATED,
|
|
|
+ data=serialize_for_event(self.site),
|
|
|
+ timestamp='2026-12-04T00:00:00+00:00',
|
|
|
+ request=request,
|
|
|
+ )
|
|
|
+ # The callback returning an object is omitted
|
|
|
+ self.assertIn('object_callback', logs.output[0])
|
|
|
+ self.assertIn("can't be serialized", logs.output[0])
|
|
|
+
|
|
|
+ def test_send_webhook_default_body(self):
|
|
|
+ Webhook.objects.filter(pk=self.webhook.pk).update(body_template='')
|
|
|
+ self.event_rule.refresh_from_db()
|
|
|
+
|
|
|
+ def dummy_send(_, request, **kwargs):
|
|
|
+ body = json.loads(request.body)
|
|
|
+ self.assertEqual(body['data']['name'], 'Site 1')
|
|
|
+ self.assertEqual(body['data']['status']['label'], 'Active')
|
|
|
+ self.assertEqual(body['request']['GET'], {'q': 'foo'})
|
|
|
+ return HttpResponse()
|
|
|
+
|
|
|
+ request = RequestFactory().get('/dcim/sites/', {'q': 'foo'})
|
|
|
+ request.user = User.objects.create_user(username='testuser')
|
|
|
+ with patch.object(Session, 'send', dummy_send):
|
|
|
+ send_webhook(
|
|
|
+ event_rule=EventRule.objects.get(pk=self.event_rule.pk),
|
|
|
+ object_type=ObjectType.objects.get_for_model(Site),
|
|
|
+ event_type=OBJECT_CREATED,
|
|
|
+ data=serialize_for_event(self.site),
|
|
|
+ timestamp='2026-12-04T00:00:00+00:00',
|
|
|
+ request=request,
|
|
|
+ )
|