-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
feat(sentry-apps): send custom headers on all external requests #119530
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
Merged
cvxluo
merged 1 commit into
cvxluo/extract-webhook-header-parsing-and-masking-helpers
from
cvxluo/send-custom-headers-on-all-external-requests
Jul 13, 2026
Merged
Changes from all commits
Commits
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 hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import logging | ||
| import re | ||
| from collections.abc import Mapping | ||
| from typing import Any | ||
| from urllib.parse import urlparse | ||
|
|
||
|
|
@@ -9,7 +10,9 @@ | |
| from requests.models import Response | ||
| from rest_framework import serializers | ||
|
|
||
| from sentry import features | ||
| from sentry.http import safe_urlopen | ||
| from sentry.organizations.services.organization.service import organization_service | ||
| from sentry.sentry_apps.event_types import SentryAppEventType | ||
| from sentry.sentry_apps.metrics import ( | ||
| SentryAppExternalRequestFailureReason, | ||
|
|
@@ -19,6 +22,7 @@ | |
| from sentry.sentry_apps.models.sentry_app import SentryApp, track_response_code | ||
| from sentry.sentry_apps.services.app.model import RpcSentryApp | ||
| from sentry.sentry_apps.utils.errors import SentryAppIntegratorError | ||
| from sentry.sentry_apps.utils.headers import mask_header_values, parse_custom_headers | ||
| from sentry.utils.sentry_apps import SentryAppWebhookRequestsBuffer | ||
| from sentry.utils.sentry_apps.webhooks import TIMEOUT_STATUS_CODE | ||
|
|
||
|
|
@@ -102,16 +106,33 @@ def validate(instance, schema_type): | |
| return True | ||
|
|
||
|
|
||
| def _custom_request_headers(sentry_app: SentryApp | RpcSentryApp) -> dict[str, str]: | ||
| if not sentry_app.webhook_headers: | ||
| return {} | ||
| owner_context = organization_service.get_organization_by_id( | ||
| id=sentry_app.owner_id, | ||
| include_projects=False, | ||
| include_teams=False, | ||
| ) | ||
| if owner_context is None or not features.has( | ||
| "organizations:sentry-apps-custom-webhook-headers", owner_context.organization | ||
| ): | ||
| return {} | ||
| return parse_custom_headers(sentry_app.webhook_headers) | ||
|
|
||
|
|
||
| def send_and_save_sentry_app_request( | ||
| url: str, | ||
| sentry_app: SentryApp | RpcSentryApp, | ||
| org_id: int, | ||
| event: str, | ||
| headers: Mapping[str, str], | ||
| **kwargs: Any, | ||
| ) -> Response: | ||
| """ | ||
| Send a webhook request, and save the request into the Redis buffer for the | ||
| app dashboard request log. Returns the response of the request. | ||
| Send a request to a Sentry App's endpoint, attaching the app's custom | ||
| headers, and save the request into the Redis buffer for the app dashboard | ||
| request log. Returns the response of the request. | ||
|
|
||
| kwargs ends up being the arguments passed into safe_urlopen | ||
| """ | ||
|
|
@@ -123,8 +144,13 @@ def send_and_save_sentry_app_request( | |
| buffer = SentryAppWebhookRequestsBuffer(sentry_app) | ||
| slug = sentry_app.slug_for_metrics | ||
|
|
||
| custom_headers = _custom_request_headers(sentry_app) | ||
| send_headers = {**custom_headers, **headers} | ||
| # Since some headers may carry secrets, we mask them to avoid logging them | ||
| loggable_headers = {**mask_header_values(custom_headers), **headers} | ||
|
Contributor
Author
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. note that we mask all custom headers in the log, rather than just potentially sensitive fields. i figure this is a little easier + more secure if we treat all the custom headers as sensitive. |
||
|
|
||
| try: | ||
| resp = safe_urlopen(url=url, **kwargs) | ||
| resp = safe_urlopen(url=url, headers=send_headers, **kwargs) | ||
| except (Timeout, ConnectionError) as e: | ||
| error_type = e.__class__.__name__.lower() | ||
| lifecycle.add_extras( | ||
|
|
@@ -142,7 +168,7 @@ def send_and_save_sentry_app_request( | |
| org_id=org_id, | ||
| event=event, | ||
| url=url, | ||
| headers=kwargs.get("headers"), | ||
| headers=loggable_headers, | ||
| ) | ||
| lifecycle.record_halt(e) | ||
| # Re-raise the exception because some of these tasks might retry on the exception | ||
|
|
@@ -157,7 +183,7 @@ def send_and_save_sentry_app_request( | |
| error_id=resp.headers.get("Sentry-Hook-Error"), | ||
| project_id=resp.headers.get("Sentry-Hook-Project"), | ||
| response=resp, | ||
| headers=kwargs.get("headers"), | ||
| headers=loggable_headers, | ||
| ) | ||
| try: | ||
| resp.raise_for_status() | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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
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.
this is a relatively inelegant way to check that the feature flag is enabled. we can remove it once we remove the flag