forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ManageIQ#22698 from jrafanie/ruby31
Support Ruby 3.1
- Loading branch information
Showing
7 changed files
with
80 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module YamlLoadAliases | ||
# Psych 4 aliases load as safe_load. Some loads happen early, like reading the database.yml so we don't want to load our | ||
# constants at that time, such as MiqExpression, Ruport, so we have two sets of permitted classes. | ||
def safe_load(yaml, permitted_classes: [], aliases: false, **kwargs) | ||
# permitted_classes kwarg is provided because rails 6.1.7.x expects it as a defined kwarg. See: https://github.com/rails/rails/blob/9ab33753b6bab1809fc73d35b98a5c1d0c96ba1b/activerecord/lib/active_record/coders/yaml_column.rb#L52 | ||
permitted_classes += YamlPermittedClasses.permitted_classes | ||
super(yaml, permitted_classes: permitted_classes, aliases: true, **kwargs) | ||
rescue Psych::DisallowedClass => err | ||
# Temporary hack to fallback to psych 3 behavior to go back to unsafe load if it's a disallowed class. | ||
# See: https://stackoverflow.com/questions/71191685/visit-psych-nodes-alias-unknown-alias-default-psychbadalias/71192990#71192990 | ||
# The alternative is to enumerate all the classes we will allow to be loaded from YAML, such as many of the various models. | ||
raise unless Rails.env.production? | ||
|
||
warn "WARNING: Using fallback to unsafe_load due to DisallowedClass: #{err}" | ||
unsafe_load(yaml, **kwargs.except(:aliases, :permitted_classes, :permitted_symbols)) | ||
end | ||
end | ||
|
||
if Psych::VERSION >= "3.1" | ||
require 'yaml' | ||
YAML.singleton_class.prepend(YamlLoadAliases) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class YamlPermittedClasses | ||
DEFAULT_PERMITTED_CLASSES = [ | ||
ActiveSupport::Duration, | ||
ActiveSupport::HashWithIndifferentAccess, | ||
ActiveSupport::TimeWithZone, | ||
ActiveSupport::TimeZone, | ||
Date, | ||
DateTime, | ||
Object, | ||
Range, | ||
Regexp, | ||
Symbol, | ||
Time | ||
].freeze | ||
def self.app_yaml_permitted_classes | ||
@app_yaml_permitted_classes ||= DEFAULT_PERMITTED_CLASSES + [MiqExpression] | ||
end | ||
|
||
def self.app_yaml_permitted_classes=(classes) | ||
@app_yaml_permitted_classes = Array(classes) | ||
end | ||
|
||
def self.default_permitted_classes | ||
@default_permitted_classes ||= DEFAULT_PERMITTED_CLASSES | ||
end | ||
|
||
def self.permitted_classes | ||
initialized? ? app_yaml_permitted_classes : default_permitted_classes | ||
end | ||
|
||
def self.initialize_app_yaml_permitted_classes | ||
@initialize_app_yaml_permitted_classes ||= begin | ||
ActiveRecord::Base.yaml_column_permitted_classes = YamlPermittedClasses.app_yaml_permitted_classes | ||
true | ||
end | ||
end | ||
|
||
def self.initialized? | ||
!!@initialize_app_yaml_permitted_classes | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters