2017-10-10 04:58:22 -04:00
|
|
|
module Gem
|
|
|
|
|
|
|
|
###
|
|
|
|
# This module is used for safely loading YAML specs from a gem. The
|
|
|
|
# `safe_load` method defined on this module is specifically designed for
|
|
|
|
# loading Gem specifications. For loading other YAML safely, please see
|
|
|
|
# Psych.safe_load
|
|
|
|
|
|
|
|
module SafeYAML
|
2020-03-24 14:51:43 -04:00
|
|
|
PERMITTED_CLASSES = %w[
|
2017-10-10 04:58:22 -04:00
|
|
|
Symbol
|
|
|
|
Time
|
|
|
|
Date
|
|
|
|
Gem::Dependency
|
|
|
|
Gem::Platform
|
|
|
|
Gem::Requirement
|
|
|
|
Gem::Specification
|
|
|
|
Gem::Version
|
|
|
|
Gem::Version::Requirement
|
2020-03-24 14:51:43 -04:00
|
|
|
].freeze
|
2017-10-10 04:58:22 -04:00
|
|
|
|
2020-03-24 14:51:43 -04:00
|
|
|
PERMITTED_SYMBOLS = %w[
|
2017-10-10 04:58:22 -04:00
|
|
|
development
|
|
|
|
runtime
|
2020-03-24 14:51:43 -04:00
|
|
|
].freeze
|
2017-10-10 04:58:22 -04:00
|
|
|
|
2022-01-18 23:28:23 -05:00
|
|
|
if ::Psych.respond_to? :safe_load
|
2018-11-21 05:20:47 -05:00
|
|
|
def self.safe_load(input)
|
2018-10-19 22:51:09 -04:00
|
|
|
if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0.pre1')
|
2022-01-18 23:28:23 -05:00
|
|
|
::Psych.safe_load(input, permitted_classes: PERMITTED_CLASSES, permitted_symbols: PERMITTED_SYMBOLS, aliases: true)
|
2018-10-19 22:51:09 -04:00
|
|
|
else
|
2022-01-18 23:28:23 -05:00
|
|
|
::Psych.safe_load(input, PERMITTED_CLASSES, PERMITTED_SYMBOLS, true)
|
2018-10-19 22:51:09 -04:00
|
|
|
end
|
2017-10-10 04:58:22 -04:00
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def self.load(input)
|
2018-10-19 22:51:09 -04:00
|
|
|
if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0.pre1')
|
2022-01-18 23:28:23 -05:00
|
|
|
::Psych.safe_load(input, permitted_classes: [::Symbol])
|
2018-10-19 22:51:09 -04:00
|
|
|
else
|
2022-01-18 23:28:23 -05:00
|
|
|
::Psych.safe_load(input, [::Symbol])
|
2018-10-19 22:51:09 -04:00
|
|
|
end
|
2017-10-10 04:58:22 -04:00
|
|
|
end
|
|
|
|
else
|
2017-11-02 00:25:37 -04:00
|
|
|
unless Gem::Deprecate.skip
|
2022-01-18 23:28:23 -05:00
|
|
|
warn "Psych safe loading is not available. Please upgrade psych to a version that supports safe loading (>= 2.0)."
|
2017-11-02 00:25:37 -04:00
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def self.safe_load(input, *args)
|
2022-01-18 23:28:23 -05:00
|
|
|
::Psych.load input
|
2017-10-10 04:58:22 -04:00
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def self.load(input)
|
2022-01-18 23:28:23 -05:00
|
|
|
::Psych.load input
|
2017-10-10 04:58:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|