Skip to content

Commit 7f4d58e

Browse files
committed
Permit yaml safe_load of aliases in automate ruby methods
Psych 4 defaults to safe_load, which defaults to not permitting aliases or classes not in an approved list. This is similar to what we do in the core application here: https://github.com/ManageIQ/manageiq/blob/46c992aaee664ea79713020e60c0342f703a8bc6/lib/extensions/yaml_load_aliases.rb#L9 The difference is we don't want to pull in application models/classes as permitted classes, at least until we know why we need them. Also, automate's ruby invocation is somewhat isolated from the application and doesn't really pull much into the remote ruby process beyond active support and some minor changes. We're instead, just extending YAML.safe_load to permit aliases in this change. We can add more later or find a better way to share code if that is needed.
1 parent 0afae73 commit 7f4d58e

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/miq_automation_engine/engine/miq_ae_engine/drb_remote_invoker.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ class AutomateMethodException < StandardError
110110
require 'drb'
111111
require 'yaml'
112112
113+
YAML.singleton_class.prepend(
114+
Module.new do
115+
def safe_load(yaml, aliases: false, **kwargs)
116+
super(yaml, aliases: true, **kwargs)
117+
end
118+
end
119+
)
120+
113121
Time.zone = 'UTC'
114122
115123
MIQ_OK = 0

spec/engine/miq_ae_method_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,38 @@ def root
4747
end
4848
end
4949

50+
context "with a script that tries to YAML.load with aliases" do
51+
let(:script) do
52+
<<-RUBY
53+
YAML.load("---\na: &a\n b: true \n\ndevelopment:\n <<: *a\n c: false\n\n")
54+
RUBY
55+
end
56+
57+
it "logs and returns the correct exit status" do
58+
allow($miq_ae_logger).to receive(:info).and_call_original
59+
expect($miq_ae_logger).to receive(:info).with("Method exited with rc=MIQ_OK", :resource_id => 123).at_least(:once)
60+
expect($miq_ae_logger).to_not receive(:error)
61+
62+
expect(subject).to eq(0)
63+
end
64+
end
65+
66+
context "with a script that tries to YAML.safe_load with aliases" do
67+
let(:script) do
68+
<<-RUBY
69+
YAML.safe_load("---\na: &a\n b: true \n\ndevelopment:\n <<: *a\n c: false\n\n")
70+
RUBY
71+
end
72+
73+
it "logs and returns the correct exit status" do
74+
allow($miq_ae_logger).to receive(:info).and_call_original
75+
expect($miq_ae_logger).to receive(:info).with("Method exited with rc=MIQ_OK", :resource_id => 123).at_least(:once)
76+
expect($miq_ae_logger).to_not receive(:error)
77+
78+
expect(subject).to eq(0)
79+
end
80+
end
81+
5082
context "with a script that raises" do
5183
let(:script) do
5284
<<-RUBY

0 commit comments

Comments
 (0)