fix(email): detect and surface webhook send failures instead of claiming success
Aegis CI / lint-and-test (push) Has been cancelled
Snyk Security Scan / Python vulnerabilities (backend) (push) Has been cancelled
Snyk Security Scan / npm vulnerabilities (frontend) (push) Has been cancelled
Snyk Security Scan / Docker image vulnerabilities (backend) (push) Has been cancelled
Aegis CI / lint-and-test (push) Has been cancelled
Snyk Security Scan / Python vulnerabilities (backend) (push) Has been cancelled
Snyk Security Scan / npm vulnerabilities (frontend) (push) Has been cancelled
Snyk Security Scan / Docker image vulnerabilities (backend) (push) Has been cancelled
send_webhook_email() only returned False on a network exception — a webhook that responded with a non-2xx status (bad URL, wrong API key, misconfigured Power Automate flow) was still reported as delivered. Now checks response.ok and returns False on rejection too. send_password_setup_email() also never checked the return value at all, so the admin's 'Send Email' action (including resending a password-reset email after the user already set their password) always showed success even when nothing was actually sent. Now raises a clear error instead.
This commit is contained in:
@@ -65,7 +65,11 @@ def send_password_setup_email(db: Session, user: User) -> None:
|
||||
f"{set_password_url}\n\n"
|
||||
"This link expires in 24 hours and can only be used once."
|
||||
)
|
||||
send_webhook_email(db, to=user.email, subject=subject, message=message, full_name=user.full_name)
|
||||
sent = send_webhook_email(db, to=user.email, subject=subject, message=message, full_name=user.full_name)
|
||||
if not sent:
|
||||
raise BusinessRuleViolation(
|
||||
"Failed to send the email — check the webhook URL/API key in Settings and the server logs."
|
||||
)
|
||||
|
||||
|
||||
def _get_valid_token_or_raise(db: Session, token: str) -> PasswordSetupToken:
|
||||
|
||||
@@ -159,8 +159,9 @@ def send_webhook_email(
|
||||
"""POST a notification email to the configured webhook.
|
||||
|
||||
Best-effort: returns False (and logs a warning) if no webhook is
|
||||
configured, *to* is empty, or the request fails — never raises, since
|
||||
a webhook outage must not break whatever triggered the notification.
|
||||
configured, *to* is empty, the request fails, or the webhook responds
|
||||
with a non-2xx status — never raises, since a webhook outage must not
|
||||
break whatever triggered the notification.
|
||||
"""
|
||||
if not to:
|
||||
return False
|
||||
@@ -174,12 +175,18 @@ def send_webhook_email(
|
||||
headers = {"x-api-key": api_key} if api_key else {}
|
||||
|
||||
try:
|
||||
requests.post(
|
||||
response = requests.post(
|
||||
webhook_url,
|
||||
json={"to": to, "subject": subject, "body": build_email_body(full_name, message)},
|
||||
headers=headers,
|
||||
timeout=10,
|
||||
)
|
||||
if not response.ok:
|
||||
logger.warning(
|
||||
"Email webhook rejected the request for %s: HTTP %d — %s",
|
||||
to, response.status_code, response.text[:500],
|
||||
)
|
||||
return False
|
||||
return True
|
||||
except requests.RequestException:
|
||||
logger.warning("Email webhook call failed for %s", to, exc_info=True)
|
||||
|
||||
Reference in New Issue
Block a user