Sha256: d63c4af904f314c0a72ea6693ffa482fd25f5bb03ce189c3acc5d4306aa66971

Contents?: true

Size: 1.4 KB

Versions: 13

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Security
      # This cop checks for the use of JSON class methods which have potential
      # security issues.
      #
      # Autocorrect is disabled by default because it's potentially dangerous.
      # If using a stream, like `JSON.load(open('file'))`, it will need to call
      # `#read` manually, like `JSON.parse(open('file').read)`.
      # If reading single values (rather than proper JSON objects), like
      # `JSON.load('false')`, it will need to pass the `quirks_mode: true`
      # option, like `JSON.parse('false', quirks_mode: true)`.
      # Other similar issues may apply.
      #
      # @example
      #   # always offense
      #   JSON.load("{}")
      #   JSON.restore("{}")
      #
      #   # no offense
      #   JSON.parse("{}")
      #
      class JSONLoad < Cop
        MSG = 'Prefer `JSON.parse` over `JSON.%<method>s`.'.freeze

        def_node_matcher :json_load, <<-PATTERN
          (send (const {nil? cbase} :JSON) ${:load :restore} ...)
        PATTERN

        def on_send(node)
          json_load(node) do |method|
            add_offense(node,
                        location: :selector,
                        message: format(MSG, method: method))
          end
        end

        def autocorrect(node)
          ->(corrector) { corrector.replace(node.loc.selector, 'parse') }
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
rubocop-0.58.2 lib/rubocop/cop/security/json_load.rb
rubocop-0.58.1 lib/rubocop/cop/security/json_load.rb
rubocop-0.58.0 lib/rubocop/cop/security/json_load.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/security/json_load.rb
rubocop-0.57.2 lib/rubocop/cop/security/json_load.rb
rubocop-0.57.1 lib/rubocop/cop/security/json_load.rb
rubocop-0.57.0 lib/rubocop/cop/security/json_load.rb
rubocop-0.56.0 lib/rubocop/cop/security/json_load.rb
rubocop-0.55.0 lib/rubocop/cop/security/json_load.rb
rubocop-0.54.0 lib/rubocop/cop/security/json_load.rb
rubocop-0.53.0 lib/rubocop/cop/security/json_load.rb
rubocop-0.52.1 lib/rubocop/cop/security/json_load.rb
rubocop-0.52.0 lib/rubocop/cop/security/json_load.rb