Sha256: f475fd0f3fd242f6a0b4a426ac462ffb0bf192bc8be036ff8a0711a9bfd01022

Contents?: true

Size: 947 Bytes

Versions: 3

Compression:

Stored size: 947 Bytes

Contents

# frozen_string_literal: true

module PlatformosCheck
  class FormAuthenticityToken < HtmlCheck
    severity :error
    categories :html
    doc docs_url(__FILE__)

    AUTHENTICITY_TOKEN_VALUE = /\A\s*{{\s*context\.authenticity_token\s*}}\s*\z/

    def on_form(node)
      authenticity_toke_inputs = node.children.select { |c| c.name == 'input' && c.attributes['name'] == 'authenticity_token' && c.attributes['value']&.match?(AUTHENTICITY_TOKEN_VALUE) }
      return if authenticity_toke_inputs.size == 1
      return add_offense('Duplicated authenticity_token inputs', node:) if authenticity_toke_inputs.size > 1

      add_offense('Missing authenticity_token input <input type="hidden" name="authenticity_token" value="{{ context.authenticity_token }}">', node:) do |corrector|
        corrector.insert_after(node, "\n<input type=\"hidden\" name=\"authenticity_token\" value=\"{{ context.authenticity_token }}\">")
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
platformos-check-0.4.8 lib/platformos_check/checks/form_authenticity_token.rb
platformos-check-0.4.7 lib/platformos_check/checks/form_authenticity_token.rb
platformos-check-0.4.6 lib/platformos_check/checks/form_authenticity_token.rb