From 5cff9da997a2467c9e41b3eb790221ae4c601955 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 17 May 2024 16:53:17 -0700 Subject: [PATCH] Protect against cyclical references --- lib/propshaft/compiler/css_asset_urls.rb | 18 +++++++++--------- lib/propshaft/load_path.rb | 2 +- .../fixtures/assets/first_path/dependent/c.css | 2 ++ 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/propshaft/compiler/css_asset_urls.rb b/lib/propshaft/compiler/css_asset_urls.rb index a5fe850..de8a950 100644 --- a/lib/propshaft/compiler/css_asset_urls.rb +++ b/lib/propshaft/compiler/css_asset_urls.rb @@ -9,17 +9,17 @@ def compile(asset, input) input.gsub(ASSET_URL_PATTERN) { asset_url resolve_path(asset.logical_path.dirname, $1), asset.logical_path, $2, $1 } end - def referenced_by(asset) - Set.new.tap do |references| - asset.content.scan(ASSET_URL_PATTERN).each do |referenced_asset_url, _| - referenced_asset = load_path.find(resolve_path(asset.logical_path.dirname, referenced_asset_url)) - - unless references.include?(referenced_asset) - references << referenced_asset - references.merge referenced_by(referenced_asset) - end + def referenced_by(asset, references: Set.new) + asset.content.scan(ASSET_URL_PATTERN).each do |referenced_asset_url, _| + referenced_asset = load_path.find(resolve_path(asset.logical_path.dirname, referenced_asset_url)) + + unless references.include?(referenced_asset) + references << referenced_asset + references.merge referenced_by(referenced_asset, references: references) end end + + references end private diff --git a/lib/propshaft/load_path.rb b/lib/propshaft/load_path.rb index aac184a..6abdbe2 100644 --- a/lib/propshaft/load_path.rb +++ b/lib/propshaft/load_path.rb @@ -12,7 +12,7 @@ def find(asset_name) end def find_referenced_by(asset) - compilers.referenced_by(asset) + compilers.referenced_by(asset).delete(self) end def assets(content_types: nil) diff --git a/test/fixtures/assets/first_path/dependent/c.css b/test/fixtures/assets/first_path/dependent/c.css index e21e2c2..8b990bb 100644 --- a/test/fixtures/assets/first_path/dependent/c.css +++ b/test/fixtures/assets/first_path/dependent/c.css @@ -1,3 +1,5 @@ +@import url('a.css') + p { color: red; } \ No newline at end of file