Skip to content

Commit 6744b81

Browse files
authored
fix: prevent infinite loops in context variable injection (#716)
close #714
1 parent 7c57bd0 commit 6744b81

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

app/services/forest_liana/utils/context_variables_injector.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ class ContextVariablesInjector
44

55
def self.inject_context_in_value(value, context_variables)
66
inject_context_in_value_custom(value) do |context_variable_key|
7-
context_variables.get_value(context_variable_key).to_s
7+
value = context_variables.get_value(context_variable_key)
8+
raise "Unknown context variable: #{context_variable_key}, please check the query for any typos" if value.nil?
9+
value.to_s
810
end
911
end
1012

@@ -18,12 +20,10 @@ def self.inject_context_in_value_custom(value)
1820
while (match = regex.match(value_with_context_variables_injected))
1921
context_variable_key = match[1]
2022

21-
unless encountered_variables.include?(context_variable_key)
22-
value_with_context_variables_injected.gsub!(
23-
/{{#{context_variable_key}}}/,
24-
yield(context_variable_key)
25-
)
26-
end
23+
value_with_context_variables_injected.gsub!(
24+
/{{#{context_variable_key}}}/,
25+
yield(context_variable_key)
26+
)
2727

2828
encountered_variables.push(context_variable_key)
2929
end

spec/services/forest_liana/utils/context_variables_injector_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ module Utils
101101
).to eq(expected_value.to_s)
102102
end
103103
end
104+
105+
it 'raises an error when the variable is not found' do
106+
expect {
107+
described_class.inject_context_in_value("{{siths.selectedRecord.evilString}}", context_variables)
108+
}.to raise_error('Unknown context variable: siths.selectedRecord.evilString, please check the query for any typos')
109+
end
104110
end
105111
end
106112
end

0 commit comments

Comments
 (0)