From 5086fdc76d934881c1d25bf61857926d6df76dd3 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 31 May 2018 17:02:27 +1000 Subject: [PATCH] FIX: add protection for scss removal during upgrade In some cases plugins would remove scss files or change them, but CSS was still calculated based off stale data in old instance cache --- lib/stylesheet/manager.rb | 13 +++++++++++- spec/components/stylesheet/manager_spec.rb | 24 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/stylesheet/manager.rb b/lib/stylesheet/manager.rb index ea7de4dbe0f..2d5f3614d10 100644 --- a/lib/stylesheet/manager.rb +++ b/lib/stylesheet/manager.rb @@ -256,7 +256,18 @@ class Stylesheet::Manager raise "attempting to look up theme digest for invalid field" end - Digest::SHA1.hexdigest(scss.to_s + color_scheme_digest.to_s + settings_digest) + Digest::SHA1.hexdigest(scss.to_s + color_scheme_digest.to_s + settings_digest + plugins_digest) + end + + # this protects us from situations where new versions of a plugin removed a file + # old instances may still be serving CSS and not aware of the change + # so we could end up poisoning the cache with a bad file that can not be removed + def plugins_digest + assets = [] + assets += DiscoursePluginRegistry.stylesheets.to_a + assets += DiscoursePluginRegistry.mobile_stylesheets.to_a + assets += DiscoursePluginRegistry.desktop_stylesheets.to_a + Digest::SHA1.hexdigest(assets.sort.join) end def settings_digest diff --git a/spec/components/stylesheet/manager_spec.rb b/spec/components/stylesheet/manager_spec.rb index 07571887fa3..9e5b6ff3475 100644 --- a/spec/components/stylesheet/manager_spec.rb +++ b/spec/components/stylesheet/manager_spec.rb @@ -65,6 +65,30 @@ describe Stylesheet::Manager do expect(new_link).to include("/stylesheets/desktop_theme_#{theme.id}_") end + describe 'digest' do + after do + DiscoursePluginRegistry.stylesheets.delete "fake_file" + end + + it 'can correctly account for plugins in digest' do + + theme = Theme.create!( + name: 'parent', + user_id: -1 + ) + + manager = Stylesheet::Manager.new(:desktop_theme, theme.key) + digest1 = manager.digest + + DiscoursePluginRegistry.stylesheets.add "fake_file" + + manager = Stylesheet::Manager.new(:desktop_theme, theme.key) + digest2 = manager.digest + + expect(digest1).not_to eq(digest2) + end + end + describe 'color_scheme_digest' do it "changes with category background image" do theme = Theme.new(