Skip to content

Commit 24d1660

Browse files
Fix infinite loop in ERBTracker
When we nest strings inside ERBTracker our earlier regex only finds a partial match on the string and then it's possible to have a mismatched unmber of brackets. scan_until does not advance the string when it does not find a match. Ultimately, we can't parse Ruby via regex, so this is a best effort. We do need to make sure to fail safely. Co-authored-by: Breno Gazzola <[email protected]>
1 parent f903206 commit 24d1660

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

actionview/lib/action_view/dependency_tracker/erb_tracker.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ def add_static_dependency(dependencies, dependency, quote_type)
127127
wildcard_dependency << scanner.pre_match
128128

129129
while unmatched_brackets > 0 && !scanner.eos?
130-
scanner.scan_until(/[{}]/)
130+
found = scanner.scan_until(/[{}]/)
131+
return unless found
131132

132133
case scanner.matched
133134
when "{"

actionview/test/template/dependency_tracker_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,19 @@ def test_dependencies_with_interpolation_non_trailing
247247

248248
assert_equal [ "*/comments" ], tracker.dependencies
249249
end
250+
251+
def test_dependencies_with_interpolation_expr
252+
view_paths = ActionView::PathSet.new([File.expand_path("../fixtures/digestor", __dir__)])
253+
254+
template = FakeTemplate.new(%q{
255+
<%= render "orders/#{variable || "default"}" %>
256+
}, :erb)
257+
258+
tracker = make_tracker("interpolation/_string", template, view_paths)
259+
260+
# unsupported
261+
assert_equal [], tracker.dependencies
262+
end
250263
end
251264

252265
class ERBTrackerTest < ActiveSupport::TestCase

0 commit comments

Comments
 (0)