summaryrefslogtreecommitdiff
path: root/lib/gitlab/utils/email.rb
blob: c65d7165263fdfed8617d65e9b0f6ac6130b02f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# frozen_string_literal: true

module Gitlab
  module Utils
    module Email
      extend self

      # Replaces most visible characters with * to obfuscate an email address
      # deform adds a fix number of * to ensure the address cannot be guessed. Also obfuscates TLD with **
      def obfuscated_email(email, deform: false)
        regex = ::Gitlab::UntrustedRegexp.new('^(..?)(.*)(@.?)(.*)(\..+)$')
        match = regex.match(email)
        return email unless match

        if deform
          # Ensure we can show two characters for the username, even if the username has
          # only one character. Boring solution is to just duplicate the character.
          email_start = match[1]
          email_start += email_start if email_start.length == 1

          email_start + '*' * 5 + match[3] + '*' * 5 + "#{match[5][0..1]}**"
        else
          match[1] + '*' * (match[2] || '').length + match[3] + '*' * (match[4] || '').length + match[5]
        end
      end
    end
  end
end