Sha256: e487584f5d954ce09d904c24bb60a242d890a10decc238e756c9a383554379be

Contents?: true

Size: 936 Bytes

Versions: 41

Compression:

Stored size: 936 Bytes

Contents

# frozen_string_literal: true

class MiniDefender::Rules::MacAddress < MiniDefender::Rule
  MODES = %w[any local universal]

  def initialize(mode = 'any')
    raise ArgumentError, 'Invalid mode' unless MODES.include?(mode)

    @mode = mode
  end

  def self.signature
    'mac'
  end

  def self.make(args)
    new(args[0] || 'any')
  end

  def passes?(attribute, value, validator)
    clean = value.to_s.gsub(/[-:]/)
    clean.match?(/[0-9A-F]{12}/i) && (
      @mode == 'any' ||
      @mode == 'local' && (clean.hex & 0x020000000000 > 0) ||
      @mode == 'universal' && (clean.hex & 0x020000000000 == 0)
    )
  end

  def message(attribute, value, validator)
    case @mode
    when 'local'
      "The value must be a locally administrated MAC address."
    when 'universal'
      "The value must be a universally administrated MAC address."
    else
      "The value must be a properly formatted mac address."
    end
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
mini_defender-0.6.8 lib/mini_defender/rules/mac_address.rb
mini_defender-0.6.7 lib/mini_defender/rules/mac_address.rb
mini_defender-0.6.5 lib/mini_defender/rules/mac_address.rb
mini_defender-0.6.4 lib/mini_defender/rules/mac_address.rb
mini_defender-0.6.3 lib/mini_defender/rules/mac_address.rb
mini_defender-0.6.2 lib/mini_defender/rules/mac_address.rb
mini_defender-0.6.1 lib/mini_defender/rules/mac_address.rb
mini_defender-0.6.0 lib/mini_defender/rules/mac_address.rb
mini_defender-0.5.8 lib/mini_defender/rules/mac_address.rb
mini_defender-0.5.7 lib/mini_defender/rules/mac_address.rb
mini_defender-0.5.6 lib/mini_defender/rules/mac_address.rb
mini_defender-0.5.5 lib/mini_defender/rules/mac_address.rb
mini_defender-0.5.4 lib/mini_defender/rules/mac_address.rb
mini_defender-0.5.3 lib/mini_defender/rules/mac_address.rb
mini_defender-0.5.2 lib/mini_defender/rules/mac_address.rb
mini_defender-0.5.1 lib/mini_defender/rules/mac_address.rb
mini_defender-0.5.0 lib/mini_defender/rules/mac_address.rb
mini_defender-0.4.1 lib/mini_defender/rules/mac_address.rb
mini_defender-0.4.0 lib/mini_defender/rules/mac_address.rb
mini_defender-0.3.14 lib/mini_defender/rules/mac_address.rb