Sha256: b2fb3be7a4f0c58b41a96ffeafbd03065a9ca2b181925595cde91279e7ce6cc5

Contents?: true

Size: 1.57 KB

Versions: 188

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Security
      # Checks for the use of YAML class methods which have
      # potential security issues leading to remote code execution when
      # loading from an untrusted source.
      #
      # NOTE: Ruby 3.1+ (Psych 4) uses `Psych.load` as `Psych.safe_load` by default.
      #
      # @safety
      #   The behavior of the code might change depending on what was
      #   in the YAML payload, since `YAML.safe_load` is more restrictive.
      #
      # @example
      #   # bad
      #   YAML.load("--- !ruby/object:Foo {}") # Psych 3 is unsafe by default
      #
      #   # good
      #   YAML.safe_load("--- !ruby/object:Foo {}", [Foo])                    # Ruby 2.5  (Psych 3)
      #   YAML.safe_load("--- !ruby/object:Foo {}", permitted_classes: [Foo]) # Ruby 3.0- (Psych 3)
      #   YAML.load("--- !ruby/object:Foo {}", permitted_classes: [Foo])      # Ruby 3.1+ (Psych 4)
      #   YAML.dump(foo)
      #
      class YAMLLoad < Base
        extend AutoCorrector

        MSG = 'Prefer using `YAML.safe_load` over `YAML.load`.'
        RESTRICT_ON_SEND = %i[load].freeze

        # @!method yaml_load(node)
        def_node_matcher :yaml_load, <<~PATTERN
          (send (const {nil? cbase} :YAML) :load ...)
        PATTERN

        def on_send(node)
          return if target_ruby_version >= 3.1

          yaml_load(node) do
            add_offense(node.loc.selector) do |corrector|
              corrector.replace(node.loc.selector, 'safe_load')
            end
          end
        end
      end
    end
  end
end

Version data entries

188 entries across 181 versions & 18 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/security/yaml_load.rb
rubocop-1.69.0 lib/rubocop/cop/security/yaml_load.rb
rubocop-1.68.0 lib/rubocop/cop/security/yaml_load.rb
rubocop-1.67.0 lib/rubocop/cop/security/yaml_load.rb
rubocop-1.66.1 lib/rubocop/cop/security/yaml_load.rb
rubocop-1.66.0 lib/rubocop/cop/security/yaml_load.rb
rubocop-1.65.1 lib/rubocop/cop/security/yaml_load.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/security/yaml_load.rb
rubocop-1.65.0 lib/rubocop/cop/security/yaml_load.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/security/yaml_load.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/security/yaml_load.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/security/yaml_load.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/security/yaml_load.rb
rubocop-1.64.1 lib/rubocop/cop/security/yaml_load.rb
rubocop-1.63.4 lib/rubocop/cop/security/yaml_load.rb
rubocop-1.63.3 lib/rubocop/cop/security/yaml_load.rb
rubocop-1.63.2 lib/rubocop/cop/security/yaml_load.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/security/yaml_load.rb
rubocop-1.63.1 lib/rubocop/cop/security/yaml_load.rb
rubocop-1.63.0 lib/rubocop/cop/security/yaml_load.rb