Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix webhook creation/filtering with invalid org #5707

Merged
merged 4 commits into from
Mar 16, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -63,6 +63,7 @@ Tracks can be exported/imported to/from Datumaro and Sly Pointcloud formats (<ht
- Clean up disk space after a project is removed (<https://github.com/opencv/cvat/pull/5632>)
- \[Server API\] Various errors in the generated schema (<https://github.com/opencv/cvat/pull/5575>)
- SiamMask and TransT serverless functions (<https://github.com/opencv/cvat/pull/5658>)
- An invalid project/org handling in webhooks (<https://github.com/opencv/cvat/pull/5707>)

### Security
- Fixed vulnerability with social authentication (<https://github.com/opencv/cvat/pull/5521>)
10 changes: 8 additions & 2 deletions cvat/apps/webhooks/serializers.py
Original file line number Diff line number Diff line change
@@ -2,15 +2,18 @@
#
# SPDX-License-Identifier: MIT

from rest_framework import serializers

from cvat.apps.engine.models import Project
from cvat.apps.engine.serializers import BasicUserSerializer, WriteOnceMixin

from .event_type import EventTypeChoice, ProjectEvents, OrganizationEvents
from .models import (
Webhook,
WebhookContentTypeChoice,
WebhookTypeChoice,
WebhookDelivery,
)
from rest_framework import serializers
from cvat.apps.engine.serializers import BasicUserSerializer, WriteOnceMixin


class EventTypeValidator:
@@ -126,6 +129,9 @@ class Meta:
validators = [EventTypeValidator()]

def create(self, validated_data):
if (project_id := validated_data.get('project_id')) is not None:
validated_data['organization'] = Project.objects.get(pk=project_id).organization

db_webhook = Webhook.objects.create(**validated_data)
return db_webhook

1 change: 0 additions & 1 deletion cvat/apps/webhooks/signals.py
Original file line number Diff line number Diff line change
@@ -98,7 +98,6 @@ def select_webhooks(project_id, org_id, event):
is_active=True,
events__contains=event,
type=WebhookTypeChoice.PROJECT,
organization=org_id,
project=project_id,
)
selected_webhooks += list(webhooks)
16 changes: 16 additions & 0 deletions tests/python/rest_api/test_webhooks.py
Original file line number Diff line number Diff line change
@@ -266,6 +266,22 @@ def test_can_create_without_unnecessary_fields(self):

assert response.status_code == HTTPStatus.CREATED

def test_can_create_with_mismatching_project_org_fields(self, projects_by_org):
# In this case we could either fail or ignore invalid query param
# Currently, the invalid org id will be ignored and the value
# will be taken from the project.
post_data = deepcopy(self.proj_webhook)
org_id = next(iter(projects_by_org))
project = projects_by_org[org_id][0]
post_data["project_id"] = project["id"]
org_id = next(k for k in projects_by_org if k != org_id)

response = post_method("admin1", "webhooks", post_data, org_id=org_id)

assert response.status_code == HTTPStatus.CREATED
assert response.json()["project_id"] == post_data["project_id"]
assert response.json()["organization"] == project["organization"]

def test_cannot_create_without_target_url(self):
post_data = deepcopy(self.proj_webhook)
post_data.pop("target_url")