Impedisce combinazioni SMTP errate tra porta e crittografia.
CI / scan_ruby (push) Failing after 14m26s
CI / lint (push) Has been cancelled
CI / scan_js (push) Has been cancelled

Evita l'errore SSL wrong version number quando SSL/TLS è impostato sulla porta 587 invece che sulla 465.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-18 00:09:25 +02:00
co-authored by Cursor
parent 98dcdbbf82
commit 765ee51eeb
3 changed files with 66 additions and 0 deletions
+13
View File
@@ -15,6 +15,7 @@ class MailIdentity < ApplicationRecord
validates :smtp_port, numericality: { in: 1..65535 } validates :smtp_port, numericality: { in: 1..65535 }
validates :encryption, inclusion: { in: ENCRYPTIONS } validates :encryption, inclusion: { in: ENCRYPTIONS }
validates :smtp_authentication, inclusion: { in: Catalog::MAIL_AUTH_METHODS.keys } validates :smtp_authentication, inclusion: { in: Catalog::MAIL_AUTH_METHODS.keys }
validate :encryption_matches_port
scope :active, -> { where(active: true) } scope :active, -> { where(active: true) }
scope :ordered, -> { order(:name) } scope :ordered, -> { order(:name) }
@@ -38,4 +39,16 @@ class MailIdentity < ApplicationRecord
end end
settings settings
end end
private
def encryption_matches_port
return if smtp_port.blank? || encryption.blank?
if encryption == "tls" && smtp_port == 587
errors.add(:smtp_port, "con SSL/TLS va usata la porta 465 (es. smtps.aruba.it)")
elsif encryption == "starttls" && smtp_port == 465
errors.add(:smtp_port, "con STARTTLS va usata la porta 587 (es. smtp.aruba.it)")
end
end
end end
+1
View File
@@ -25,6 +25,7 @@
<div> <div>
<label class="mb-1 block text-sm font-medium">Porta</label> <label class="mb-1 block text-sm font-medium">Porta</label>
<%= f.number_field :smtp_port, required: true, min: 1, max: 65535, class: input_class %> <%= f.number_field :smtp_port, required: true, min: 1, max: 65535, class: input_class %>
<p class="mt-1 text-xs text-zinc-500">587 con STARTTLS · 465 con SSL/TLS (Aruba: smtp.aruba.it:587 oppure smtps.aruba.it:465)</p>
</div> </div>
<div> <div>
<label class="mb-1 block text-sm font-medium">Crittografia</label> <label class="mb-1 block text-sm font-medium">Crittografia</label>
+52
View File
@@ -0,0 +1,52 @@
require "test_helper"
class MailIdentityTest < ActiveSupport::TestCase
test "smtp settings use starttls on port 587" do
identity = MailIdentity.new(
name: "Test",
from_name: "Test",
from_email: "hello@example.com",
smtp_host: "smtp.example.com",
smtp_port: 587,
encryption: "starttls",
smtp_authentication: "plain",
active: true
)
assert identity.valid?
settings = identity.smtp_settings
assert settings[:enable_starttls_auto]
assert_not settings[:ssl]
end
test "smtp settings use ssl on port 465" do
identity = MailIdentity.new(
name: "Test",
from_name: "Test",
from_email: "hello@example.com",
smtp_host: "smtps.example.com",
smtp_port: 465,
encryption: "tls",
smtp_authentication: "plain",
active: true
)
assert identity.valid?
settings = identity.smtp_settings
assert settings[:ssl]
assert_not settings[:enable_starttls_auto]
end
test "rejects ssl encryption on port 587" do
identity = MailIdentity.new(
name: "Test",
from_name: "Test",
from_email: "hello@example.com",
smtp_host: "smtps.example.com",
smtp_port: 587,
encryption: "tls",
smtp_authentication: "plain",
active: true
)
assert_not identity.valid?
assert_includes identity.errors[:smtp_port], "con SSL/TLS va usata la porta 465 (es. smtps.aruba.it)"
end
end