Skip to content

fix: validate SMTP port before connection to prevent 500/CommandError… - #9487

Open
sakthimahalakshmi-vc wants to merge 1 commit into
makeplane:previewfrom
sakthimahalakshmi-vc:fix/smtp-port-validation-500
Open

fix: validate SMTP port before connection to prevent 500/CommandError…#9487
sakthimahalakshmi-vc wants to merge 1 commit into
makeplane:previewfrom
sakthimahalakshmi-vc:fix/smtp-port-validation-500

Conversation

@sakthimahalakshmi-vc

@sakthimahalakshmi-vc sakthimahalakshmi-vc commented Jul 28, 2026

Copy link
Copy Markdown

Description

This PR fixes an unhandled crash in the SMTP test-email flow.

In EmailCredentialCheckEndpoint.post (apps/api/plane/license/api/views/configuration.py)
and in the test_email management command
(apps/api/plane/db/management/commands/test_email.py), the SMTP port
was passed directly to int(EMAIL_PORT) with no validation. If
EMAIL_PORT was empty or not yet saved, this raised an unhandled
ValueError, causing a 500 Internal Server Error (or an uncaught
exception in the management command) instead of a clear error message.

This PR adds validation that the port is a valid integer between
1 and 65535 before establishing the SMTP connection. If invalid,
the API now returns a 400 Bad Request with a descriptive message,
and the management command raises a CommandError instead of crashing.

Note: this PR is backend-only. The frontend email-config-form.tsx
may have a related UX issue (allowing "Send test email" before the
config is saved) that is out of scope here and can be tracked separately.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • Feature (non-breaking change which adds functionality)
  • Improvement (change that would cause existing functionality to not work as expected)
  • Code refactoring
  • Performance improvements
  • Documentation update

Screenshots and Media (if applicable)

N/A — backend-only fix, no UI changes.

Test Scenarios

  • Attempted to send a test email via EmailCredentialCheckEndpoint
    with EMAIL_PORT unset/empty — previously returned a 500 error,
    now returns a 400 with a clear validation message.
  • Ran the test_email management command with an invalid/missing
    port — previously raised an unhandled exception, now raises a
    CommandError with a clear message.
  • Verified normal flow (valid port configured) still sends the
    test email successfully, unaffected by this change.

References

Fixes #9472

Summary by CodeRabbit

  • Bug Fixes
    • Added validation for SMTP port settings, accepting only numeric values between 1 and 65,535.
    • Improved error messages when email configuration contains an invalid port.
    • Prevented connection attempts when the configured SMTP port is invalid.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

SMTP port parsing and range validation were added to both email testing paths. Invalid or non-integer ports now produce explicit command errors or HTTP 400 responses before SMTP connection creation.

Changes

SMTP port validation

Layer / File(s) Summary
Management command port validation
apps/api/plane/db/management/commands/test_email.py
The test email command validates that EMAIL_PORT is an integer within 1..65535 before creating the SMTP connection, while retaining existing email rendering and output behavior.
Credential check endpoint validation
apps/api/plane/license/api/views/configuration.py
The credential check endpoint validates the configured port, returns HTTP 400 for invalid values, and passes the validated port to the SMTP connection.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: dheeru0198

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title matches the main change: SMTP port validation before connection to avoid crashes.
Description check ✅ Passed The PR description includes the required sections and gives clear change, testing, and reference details.
Linked Issues check ✅ Passed The changes satisfy #9472 by validating SMTP port input and returning a clear 400/CommandError instead of an unhandled crash.
Out of Scope Changes check ✅ Passed The diff stays within the backend SMTP validation fix and does not introduce unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@apps/api/plane/db/management/commands/test_email.py`:
- Around line 29-34: Reject empty or unset SMTP port values in both validation
paths instead of defaulting them to 587. In
apps/api/plane/db/management/commands/test_email.py lines 29-34, parse
EMAIL_PORT unconditionally so empty values reach the existing CommandError
handler; apply the same validation in
apps/api/plane/license/api/views/configuration.py lines 107-115 so empty values
produce HTTP 400.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: b3b86ebe-f4b5-4ee7-801d-cdff668278fa

📥 Commits

Reviewing files that changed from the base of the PR and between a8e53b6 and 75b9159.

📒 Files selected for processing (2)
  • apps/api/plane/db/management/commands/test_email.py
  • apps/api/plane/license/api/views/configuration.py

Comment on lines +29 to +34
try:
port = int(EMAIL_PORT) if EMAIL_PORT else 587
if not (1 <= port <= 65535):
raise ValueError()
except (ValueError, TypeError):
raise CommandError("SMTP port is invalid. Please configure a valid SMTP port.")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Reject empty and unsaved SMTP ports in both paths.

Both implementations treat falsey values as the valid default 587, allowing an empty configuration to proceed instead of producing CommandError or HTTP 400.

  • apps/api/plane/db/management/commands/test_email.py#L29-L34: parse EMAIL_PORT unconditionally so empty values reach the existing error handler.
  • apps/api/plane/license/api/views/configuration.py#L107-L115: apply the same validation so empty values return HTTP 400.
📍 Affects 2 files
  • apps/api/plane/db/management/commands/test_email.py#L29-L34 (this comment)
  • apps/api/plane/license/api/views/configuration.py#L107-L115
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/api/plane/db/management/commands/test_email.py` around lines 29 - 34,
Reject empty or unset SMTP port values in both validation paths instead of
defaulting them to 587. In apps/api/plane/db/management/commands/test_email.py
lines 29-34, parse EMAIL_PORT unconditionally so empty values reach the existing
CommandError handler; apply the same validation in
apps/api/plane/license/api/views/configuration.py lines 107-115 so empty values
produce HTTP 400.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug]: test email not sending while setting up SMTP

2 participants