-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
feat(bitbucket): check bitbucket webhook signature if webhook_secret is defined #84309
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,3 +1,5 @@ | ||||||||||||||||||
import hashlib | ||||||||||||||||||
import hmac | ||||||||||||||||||
import ipaddress | ||||||||||||||||||
import logging | ||||||||||||||||||
from abc import ABC | ||||||||||||||||||
|
@@ -15,8 +17,10 @@ | |||||||||||||||||
from sentry.api.api_owners import ApiOwner | ||||||||||||||||||
from sentry.api.api_publish_status import ApiPublishStatus | ||||||||||||||||||
from sentry.api.base import Endpoint, region_silo_endpoint | ||||||||||||||||||
from sentry.api.exceptions import SentryAPIException | ||||||||||||||||||
from sentry.integrations.base import IntegrationDomain | ||||||||||||||||||
from sentry.integrations.bitbucket.constants import BITBUCKET_IP_RANGES, BITBUCKET_IPS | ||||||||||||||||||
from sentry.integrations.services.integration.service import integration_service | ||||||||||||||||||
from sentry.integrations.source_code_management.webhook import SCMWebhook | ||||||||||||||||||
from sentry.integrations.utils.metrics import IntegrationWebhookEvent, IntegrationWebhookEventType | ||||||||||||||||||
from sentry.models.commit import Commit | ||||||||||||||||||
|
@@ -31,6 +35,42 @@ | |||||||||||||||||
PROVIDER_NAME = "integrations:bitbucket" | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
def is_valid_signature(body: bytes, secret: str, signature: str) -> bool: | ||||||||||||||||||
hash_object = hmac.new( | ||||||||||||||||||
secret.encode("utf-8"), | ||||||||||||||||||
msg=body, | ||||||||||||||||||
digestmod=hashlib.sha256, | ||||||||||||||||||
) | ||||||||||||||||||
expected_signature = hash_object.hexdigest() | ||||||||||||||||||
|
||||||||||||||||||
if not hmac.compare_digest(expected_signature, signature): | ||||||||||||||||||
logger.info( | ||||||||||||||||||
"%s.webhook.invalid-signature", | ||||||||||||||||||
PROVIDER_NAME, | ||||||||||||||||||
extra={"expected": expected_signature, "given": signature}, | ||||||||||||||||||
) | ||||||||||||||||||
return False | ||||||||||||||||||
return True | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
class WebhookMissingSignatureException(SentryAPIException): | ||||||||||||||||||
status_code = 400 | ||||||||||||||||||
code = f"{PROVIDER_NAME}.webhook.missing-signature" | ||||||||||||||||||
message = "Missing webhook signature" | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
class WebhookUnsupportedSignatureMethodException(SentryAPIException): | ||||||||||||||||||
status_code = 400 | ||||||||||||||||||
code = f"{PROVIDER_NAME}.webhook.unsupported-signature-method" | ||||||||||||||||||
message = "Signature method is not supported" | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
class WebhookInvalidSignatureException(SentryAPIException): | ||||||||||||||||||
status_code = 400 | ||||||||||||||||||
code = f"{PROVIDER_NAME}.webhook.invalid-signature" | ||||||||||||||||||
message = "Webhook signature is invalid" | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
class BitbucketWebhook(SCMWebhook, ABC): | ||||||||||||||||||
@property | ||||||||||||||||||
def provider(self) -> str: | ||||||||||||||||||
|
@@ -74,6 +114,8 @@ def event_type(self) -> IntegrationWebhookEventType: | |||||||||||||||||
def __call__(self, event: Mapping[str, Any], **kwargs) -> None: | ||||||||||||||||||
authors = {} | ||||||||||||||||||
|
||||||||||||||||||
if not (request := kwargs.get("request")): | ||||||||||||||||||
raise ValueError("Missing request") | ||||||||||||||||||
if not (organization := kwargs.get("organization")): | ||||||||||||||||||
raise ValueError("Missing organization") | ||||||||||||||||||
|
||||||||||||||||||
|
@@ -86,6 +128,20 @@ def __call__(self, event: Mapping[str, Any], **kwargs) -> None: | |||||||||||||||||
except Repository.DoesNotExist: | ||||||||||||||||||
raise Http404() | ||||||||||||||||||
|
||||||||||||||||||
integration = integration_service.get_integration(integration_id=repo.integration_id) | ||||||||||||||||||
if integration and "webhook_secret" in integration.metadata: | ||||||||||||||||||
secret = integration.metadata["webhook_secret"] | ||||||||||||||||||
try: | ||||||||||||||||||
method, signature = request.META["HTTP_X_HUB_SIGNATURE"].split("=", 1) | ||||||||||||||||||
except (IndexError, KeyError, ValueError): | ||||||||||||||||||
raise WebhookMissingSignatureException() | ||||||||||||||||||
|
||||||||||||||||||
if method != "sha256": | ||||||||||||||||||
raise WebhookUnsupportedSignatureMethodException() | ||||||||||||||||||
|
||||||||||||||||||
if not is_valid_signature(request.body, secret, signature): | ||||||||||||||||||
raise WebhookInvalidSignatureException() | ||||||||||||||||||
|
||||||||||||||||||
# while we're here, make sure repo data is up to date | ||||||||||||||||||
self.update_repo_data(repo, event) | ||||||||||||||||||
|
||||||||||||||||||
|
@@ -206,6 +262,6 @@ def post(self, request: HttpRequest, organization_id: int) -> HttpResponse: | |||||||||||||||||
domain=IntegrationDomain.SOURCE_CODE_MANAGEMENT, | ||||||||||||||||||
provider_key=event_handler.provider, | ||||||||||||||||||
).capture(): | ||||||||||||||||||
event_handler(event, organization=organization) | ||||||||||||||||||
event_handler(event, request=request, organization=organization) | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what do we need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
sentry/src/sentry/integrations/bitbucket/webhook.py Lines 135 to 142 in 90fc14b
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh wait, can we do this in the webhook itself before calling the handler? |
||||||||||||||||||
|
||||||||||||||||||
return HttpResponse(status=204) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are now doing signature check on the upper level (in
post()
method), andrepository
is always in the payload (https://support.atlassian.com/bitbucket-cloud/docs/event-payloads/#Repository), we will always lookup the repo on the upper level as well. Hence passing the repo as kwarg and removing this code.