Fix: Update SendGrid webhook when client_id changes#616
Merged
Conversation
When a domain is re-registered or configuration changes generate a new client_id, SetupSendgridMailbox now updates the existing SendGrid inbound parse webhook URL instead of silently skipping it. This prevents inbound emails from being rejected with 'unregistered_client' errors. Previously, if a mailbox already existed in SendGrid, the code would exit early without checking if the webhook URL matched the current client_id. This caused production domains to lose inbound mail after re-registration. Changes: - Added http_patch import to sendgrid.py - Modified SetupSendgridMailbox._run() to compare existing webhook URL with expected URL - When URLs mismatch, PATCH the webhook to update with correct client_id - Preserves existing spam_check and send_raw settings during update - Updated test_skips_request_when_domain_already_exists to mock JSON response - Added test_updates_webhook_when_url_mismatches to verify PATCH behavior Fixes inbound email loss bug reported for production domains.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
When a domain is re-registered or configuration changes generate a new
client_id, inbound emails are rejected withunregistered_clienterrors. This happens because the SendGrid webhook URL still points to the old client ID, while the server expects the new one.Root Cause
SetupSendgridMailbox._run()would exit early when it found an existing SendGrid mailbox, without checking if the webhook URL matched the current client_id. This caused a silent mismatch between:Impact
Production domains lose all inbound email after re-registration until manual SendGrid reconfiguration.
Solution
Modified
SetupSendgridMailbox._run()to:Changes
http_patchimport tosendgrid.pySetupSendgridMailbox._run()with URL comparison and PATCH logictest_skips_request_when_domain_already_existsto include JSON response in mocktest_updates_webhook_when_url_mismatchesto verify PATCH behaviorTesting
✅ All SendGrid service tests pass
✅ Code formatted with yapf (column_limit=120)
✅ Validated with production domain reproduction (lokole-repro-20260601.lokole.ca)
Before Fix
After Fix
Deployment Notes
After merge and PyPI release, this fix will automatically apply when
SetupSendgridMailboxruns (typically during client registration or periodic sync). Affected domains will see log messages:"Updating stale webhook for {domain}".