From 71dda575ca855ec5b2d027c65a961b7873ce28c5 Mon Sep 17 00:00:00 2001 From: MAEDA Go Date: Mon, 23 Nov 2026 17:13:26 +0900 Subject: [PATCH] Fix exception when normalizing an email address with a too long IDN domain EmailAddress normalizes the address before validation and converts the domain to Punycode with Addressable::IDNA.to_ascii. If the Punycode of a label would be longer than 256 characters, the conversion raised Addressable::IDNA::PunycodeBigOutput, and saving the address resulted in an internal error. This can happen even with an address shorter than User::MAIL_LENGTH_LIMIT, for example with a label of about 220 Cyrillic letters. Rescue the exception and leave the address unconverted, so that the validation rejects it. Also skip the conversion if the address is already longer than User::MAIL_LENGTH_LIMIT as entered, and leave it to the length validation. Co-Authored-By: Claude Opus 5.5 --- app/models/email_address.rb | 13 +++++++++++-- test/unit/email_address_test.rb | 7 +++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/models/email_address.rb b/app/models/email_address.rb index 9d3d42562..c6a97be84 100644 --- a/app/models/email_address.rb +++ b/app/models/email_address.rb @@ -38,12 +38,21 @@ class EmailAddress < ApplicationRecord normalizes :address, with: lambda { |address| normalized_address = address.to_s.strip + # An address that is longer than the limit as entered is invalid. + # Leave it to the length validation without converting it. + return normalized_address if normalized_address.length > User::MAIL_LENGTH_LIMIT + local_part, _at, domain = normalized_address.partition('@') if domain.present? # Convert internationalized domain name (IDN) to Punycode # e.g. 'marie@société.example' => 'marie@xn--socit-esab.example' - ascii_domain = Addressable::IDNA.to_ascii(domain) - normalized_address = "#{local_part}@#{ascii_domain}" + begin + ascii_domain = Addressable::IDNA.to_ascii(domain) + normalized_address = "#{local_part}@#{ascii_domain}" + rescue Addressable::IDNA::PunycodeBigOutput + # The domain has a label that is too long to convert. Leave the + # address as it is, so that the validation rejects it. + end end normalized_address } diff --git a/test/unit/email_address_test.rb b/test/unit/email_address_test.rb index c5b2375f4..c51b69f14 100644 --- a/test/unit/email_address_test.rb +++ b/test/unit/email_address_test.rb @@ -124,4 +124,11 @@ class EmailAddressTest < ActiveSupport::TestCase assert_equal 'marie@xn--socit-esab.example', email.address assert email.valid? end + + def test_should_reject_idn_email_address_with_too_long_label + # Addressable::IDNA.to_ascii raises Addressable::IDNA::PunycodeBigOutput + # for this address + email = EmailAddress.new(address: "marie@#{'абвгдежзийклмнопрстуфхцчшщъыьэюя' * 7}.example") + assert_not email.valid? + end end -- 2.55.0