Class: Gitlab::SecretDetection::Utils::Masker

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/secret_detection/utils/masker.rb

Constant Summary collapse

DEFAULT_VISIBLE_CHAR_COUNT =
3
DEFAULT_MASK_CHAR_COUNT =
5
DEFAULT_MASK_CHAR =
'*'

Class Method Summary collapse

Class Method Details

.mask_secret(raw_secret_value, mask_char: DEFAULT_MASK_CHAR, visible_chars_count: DEFAULT_VISIBLE_CHAR_COUNT, mask_chars_count: DEFAULT_MASK_CHAR_COUNT) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gitlab/secret_detection/utils/masker.rb', line 12

def mask_secret(
  raw_secret_value,
  mask_char: DEFAULT_MASK_CHAR,
  visible_chars_count: DEFAULT_VISIBLE_CHAR_COUNT,
  mask_chars_count: DEFAULT_MASK_CHAR_COUNT
)
  return '' if raw_secret_value.nil? || raw_secret_value.empty?
  return raw_secret_value if raw_secret_value.length <= visible_chars_count # Too short to mask

  chars = raw_secret_value.chars
  position = 0

  while position < chars.length
    # Show 'visible_chars_count' characters
    position += visible_chars_count

    # Mask next 'mask_chars' characters if available
    mask_chars_count.times do
      break if position >= chars.length

      chars[position] = mask_char
      position += 1
    end
  end

  chars.join
end