Skip to content

Update the regex for received fields to improve performance

Nick Malcolm requested to merge improve-email-regex into master

What does this MR do and why?

Update the regex for received fields to improve performance.

Addresses https://gitlab.com/gitlab-org/gitlab/-/issues/416610

Before

If you add the spec, but leave out the code change:

Failures:

  1) Gitlab::Email::Receiver when the received field is malformed mail_metadata has no ReDos issue
     Failure/Error: received.filter_map { |header| header.value[RECEIVED_HEADER_REGEX, 1] }

     Timeout::Error:
       execution expired
     # ./lib/gitlab/email/receiver.rb:180:in `[]'
     # ./lib/gitlab/email/receiver.rb:180:in `block (2 levels) in recipients_from_received_headers'
     # ./lib/gitlab/email/receiver.rb:180:in `each'
     # ./lib/gitlab/email/receiver.rb:180:in `filter_map'

After

All the specs in spec/lib/gitlab/email/receiver_spec.rb are back to passing.

Screenshots or screen recordings

Screenshots are required for UI changes, and strongly recommended for all other merge requests.

Before After

How to set up and validate locally

  1. Check out master
  2. Perform https://gitlab.com/gitlab-org/gitlab/-/issues/416610#poc
  3. Check out this branch
  4. Observe that the regex now performs well

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Nick Malcolm

Merge request reports