2023-03-17 12:33:55 +03:00
|
|
|
# frozen_string_literal: true
|
2023-03-17 12:36:42 +03:00
|
|
|
|
2017-10-10 11:58:22 +03: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 21:51:43 +03:00
|
|
|
PERMITTED_CLASSES = %w[
|
2017-10-10 11:58:22 +03:00
|
|
|
Symbol
|
|
|
|
Time
|
|
|
|
Date
|
|
|
|
Gem::Dependency
|
|
|
|
Gem::Platform
|
|
|
|
Gem::Requirement
|
|
|
|
Gem::Specification
|
|
|
|
Gem::Version
|
|
|
|
Gem::Version::Requirement
|
2020-03-24 21:51:43 +03:00
|
|
|
].freeze
|
2017-10-10 11:58:22 +03:00
|
|
|
|
2020-03-24 21:51:43 +03:00
|
|
|
PERMITTED_SYMBOLS = %w[
|
2017-10-10 11:58:22 +03:00
|
|
|
development
|
|
|
|
runtime
|
2020-03-24 21:51:43 +03:00
|
|
|
].freeze
|
2017-10-10 11:58:22 +03:00
|
|
|
|
2024-02-09 21:15:40 +03:00
|
|
|
@aliases_enabled = true
|
2024-02-14 11:50:52 +03:00
|
|
|
def self.aliases_enabled=(value) # :nodoc:
|
2024-02-09 21:15:40 +03:00
|
|
|
@aliases_enabled = !!value
|
|
|
|
end
|
2024-02-07 23:26:31 +03:00
|
|
|
|
2024-02-14 11:50:52 +03:00
|
|
|
def self.aliases_enabled? # :nodoc:
|
|
|
|
@aliases_enabled
|
|
|
|
end
|
|
|
|
|
2023-03-09 09:42:07 +03:00
|
|
|
def self.safe_load(input)
|
2024-02-09 21:15:40 +03:00
|
|
|
::Psych.safe_load(input, permitted_classes: PERMITTED_CLASSES, permitted_symbols: PERMITTED_SYMBOLS, aliases: @aliases_enabled)
|
2023-03-09 09:42:07 +03:00
|
|
|
end
|
2017-10-10 11:58:22 +03:00
|
|
|
|
2023-03-09 09:42:07 +03:00
|
|
|
def self.load(input)
|
|
|
|
::Psych.safe_load(input, permitted_classes: [::Symbol])
|
2017-10-10 11:58:22 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|