-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
feat(bitbucket): check bitbucket webhook signature if webhook_secret is defined #82541
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
from unittest.mock import patch | ||
|
||
from fixtures.bitbucket import PUSH_EVENT_EXAMPLE | ||
from sentry.integrations.bitbucket.webhook import PROVIDER_NAME | ||
from sentry.integrations.bitbucket.webhook import PROVIDER_NAME, is_valid_signature | ||
from sentry.models.commit import Commit | ||
from sentry.models.commitauthor import CommitAuthor | ||
from sentry.models.repository import Repository | ||
|
@@ -192,3 +192,56 @@ def test_update_repo_url(self): | |
# url has been updated | ||
repo_out_of_date_url.refresh_from_db() | ||
assert repo_out_of_date_url.url == "https://bitbucket.org/maxbittker/newsdiffs" | ||
|
||
|
||
class WebhookSignatureTest(WebhookBaseTest): | ||
method = "post" | ||
|
||
def setUp(self): | ||
super().setUp() | ||
|
||
repo = self.create_repository() | ||
repo.config["webhook_secret"] = "test_secret" | ||
repo.save() | ||
|
||
def send_signed_webhook(self): | ||
return self.get_response( | ||
self.organization_id, | ||
raw_data=PUSH_EVENT_EXAMPLE, | ||
extra_headers=dict( | ||
HTTP_X_EVENT_KEY="repo:push", | ||
HTTP_X_HUB_SIGNATURE=self.signature, | ||
REMOTE_ADDR=BITBUCKET_IP, | ||
), | ||
) | ||
|
||
def test_is_valid_signature(self): | ||
# https://support.atlassian.com/bitbucket-cloud/docs/manage-webhooks/#Examples | ||
assert is_valid_signature( | ||
b"Hello World!", | ||
"It's a Secret to Everybody", | ||
"a4771c39fbe90f317c7824e83ddef3caae9cb3d976c214ace1f2937e133263c9", | ||
) | ||
|
||
def test_success(self): | ||
self.signature = "sha256=ee07bac3b2fa849cf4346113dc5f6b9738660673aca6fa8f07ce459e7543f980" | ||
response = self.send_signed_webhook() | ||
assert response.status_code == 204 | ||
|
||
def test_missing_signature(self): | ||
self.signature = "" | ||
response = self.send_signed_webhook() | ||
assert response.status_code == 400 | ||
assert response.content == b"Missing webhook signature" | ||
|
||
def test_invalid_signature(self): | ||
self.signature = "sha256=definitely-invalid" | ||
response = self.send_signed_webhook() | ||
assert response.status_code == 400 | ||
assert response.content == b"Webhook signature is invalid" | ||
|
||
def test_invalid_method(self): | ||
self.signature = "sha1=b842d7b7d535c446133bcf18cf085fb9472175c7" | ||
response = self.send_signed_webhook() | ||
assert response.status_code == 400 | ||
assert response.content == b"Signature method is not supported" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
why are we storing the webhook secret in each repository? should the webhook secret be for each integration instance, like we have for gitlab?
sentry/src/sentry/integrations/gitlab/webhooks.py
Lines 317 to 322 in 50658af
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.
Oh I see, we create a webhook via corresponding API once the integration is complete, I will look in that direction for Bitbucket.
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.
From what I can see, all of GitLab, GitHub, and Bitbucket support setting separate webhook secrets on repository level.
It seems like for GitLab, we're creating webhooks on project (=repository) level as well:
sentry/src/sentry/integrations/gitlab/client.py
Lines 259 to 269 in e6d22ef
While this change seems misaligned with existing GitLab/GitHub integrations, but it is more secure to have separate secrets for each repo.
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.
we had a DM conversation but the summary is that other SCM integrations currently have integration-level webhook validation so i think it would be a good idea to get bitbucket up to parity with that first. if we want to do repo-level webhooks we should consider adding it across all SCM integrations at once