Class: Aikido::Zen::Request::HeuristicRouter::SecretMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/aikido/zen/request/heuristic_router.rb

Constant Summary collapse

MIN_LENGTH =
10
SECRET_THRESHOLD =
0.75
LOWER =
/[[:lower:]]/
UPPER =
/[[:upper:]]/
DIGIT =
/[[:digit:]]/
SPECIAL =
/[!#\$%^&*|;:<>]/
SEPARATORS =
/[[:space:]]|-/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.===(candidate) ⇒ Boolean

Decides if a given string looks random enough to be a “secret”.

Parameters:

  • candidate (String)

Returns:

  • (Boolean)


78
79
80
# File 'lib/aikido/zen/request/heuristic_router.rb', line 78

def self.===(candidate)
  new(candidate).matches?
end

Instance Method Details

#matches?Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/aikido/zen/request/heuristic_router.rb', line 86

def matches?
  return false if @string.size <= MIN_LENGTH
  return false if SEPARATORS === @string
  return false unless DIGIT === @string
  return false if [LOWER, UPPER, SPECIAL].none? { |pattern| pattern === @string }

  ratios = @string.chars.each_cons(MIN_LENGTH).map do |window|
    window.to_set.size / MIN_LENGTH.to_f
  end

  ratios.sum / ratios.size > SECRET_THRESHOLD
end