Repository navigation
fix(license): answer 503 before Keygen when SMTP config is invalid - #2047
Conversation
A bad SMTP_* setting only raised when the mailer was first built, at send time. /license/trial had already created the trial in Keygen by then, so the caller got a 500 and their address was spent. _require_mailer() now builds the mailer as well as checking SMTP_ENABLED, so a configuration error answers 503 before any Keygen call on both POST routes. That also stops /license/resend answering differently for customers and non-customers on a misconfigured deployment. A 503 was chosen over failing at startup so a mail setting cannot take down every other route. Fixes MODSetter#1963 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@mehul2409 is attempting to deploy a commit to the Rohan Verma's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
What
_require_mailer()now builds the mailer after theSMTP_ENABLEDcheck and answers 503 "License email delivery is not configured." when the SMTP configuration is invalid. BothPOST /license/trialandPOST /license/resendrun it before any Keygen call. The portal doc is updated and its Known gaps line removed.Why
A bad
SMTP_*setting only raisedValueErrorat the first send, after/license/trialhad already created and checked out the trial in Keygen. The caller got a 500 and their address was spent (a retry answers 409).The decision the issue asks for: 503 inside
_require_mailer(), not a failed startup. The same process serves the scraper API and the export window, so one mail typo shouldn't take every route down, and the license routes already turn every unavailable-mail case into a 503. The check still runs before any Keygen lookup, so the answer stays outcome-independent. One side effect: on a misconfigured deployment/license/resendused to answer 503 for customers (a send was attempted) and 200 for everyone else; it now answers 503 either way.The two error types in
protocol.pyare untouched. The misconfiguration is logged with itsValueError, so the operator sees which setting is wrong.Fixes #1963
How to test
tests/unit/license/test_mail_guard.pydrives both routes through the real router, with Keygen mocked at its HTTP transport:SMTP_HOSTmissing:/license/trialanswers 503 and Keygen gets no request (fails ondev)/license/resend(fails ondev)cd surfsense_backend uv run pytest -m unit uv run ruff check app/license tests/unit/license python scripts/check_docs.pyI also ran the real backend (Postgres and Redis from
docker/docker-compose.deps-only.yml, a local stand-in for Keygen, Mailpit for SMTP):/license/trial/license/resenddev,SMTP_ENABLED=TRUE, emptySMTP_HOSTsurfsense.licLocal unit run: 3399 passed. The one failure,
tests/unit/knowledge_store/test_naming_byte_length.py::test_derived_name_is_writable_where_raw_title_is_not, fails ondevas well on macOS, where APFS accepts the long filename.🤖 Generated with Claude Code
High-level PR Summary
This PR fixes a bug where invalid SMTP configuration caused
/license/trialto create a trial license in Keygen before failing to send the email, resulting in a spent trial address. The fix moves SMTP configuration validation into_require_mailer()so both/license/trialand/license/resendreturn 503 before making any Keygen API calls when SMTP is misconfigured. This prevents licenses from being created that can never be delivered, while keeping the service operational for non-license routes.⏱️ Estimated Review Time: 5-15 minutes
💡 Review Order Suggestion
surfsense_backend/tests/unit/license/test_mail_guard.pysurfsense_backend/app/license/router.pydocs/architecture/license/portal.md