DEV: Ensure stylesheet sourcemap URLs include __ws hostname param (#15934)

Without this parameter, requests for sourcemaps on shared-CDN multisites will not be routed to the correct database, resulting in a 404.

The stylesheet content now depends on the site hostname, so the hostname has been added to the digest.
This commit is contained in:
David Taylor 2022-02-14 19:53:39 +00:00 committed by GitHub
parent 3405f66b7e
commit b8526c6d75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 17 deletions

View File

@ -225,7 +225,7 @@ class Stylesheet::Manager
next if builder.theme&.component && !scss_checker.has_scss(theme_id)
builder.compile unless File.exist?(builder.stylesheet_fullpath)
href = builder.stylesheet_path(current_hostname)
href = builder.stylesheet_absolute_url
data[:new_href] = href
stylesheets << data
@ -243,7 +243,7 @@ class Stylesheet::Manager
else
builder = Builder.new(target: target, manager: self)
builder.compile unless File.exist?(builder.stylesheet_fullpath)
href = builder.stylesheet_path(current_hostname)
href = builder.stylesheet_absolute_url
data = { target: target, new_href: href }
stylesheets << data
@ -287,7 +287,7 @@ class Stylesheet::Manager
builder.compile unless File.exist?(builder.stylesheet_fullpath)
href = builder.stylesheet_path(current_hostname)
href = builder.stylesheet_absolute_url
stylesheet[:new_href] = href
cache.defer_set(cache_key, stylesheet.freeze)
stylesheet

View File

@ -36,7 +36,7 @@ class Stylesheet::Manager::Builder
rtl: rtl,
theme_id: theme&.id,
theme_variables: theme&.scss_variables.to_s,
source_map_file: source_map_filename,
source_map_file: source_map_url_relative_from_stylesheet,
color_scheme_id: @color_scheme&.id,
load_paths: load_paths
)
@ -73,6 +73,10 @@ class Stylesheet::Manager::Builder
css
end
def current_hostname
Discourse.current_hostname
end
def cache_fullpath
Stylesheet::Manager.cache_fullpath
end
@ -89,16 +93,16 @@ class Stylesheet::Manager::Builder
"#{stylesheet_filename}.map"
end
def source_map_url_relative_from_stylesheet
"#{source_map_filename}?__ws=#{current_hostname}"
end
def stylesheet_fullpath_no_digest
"#{cache_fullpath}/#{stylesheet_filename_no_digest}"
end
def stylesheet_cdnpath(hostname)
"#{GlobalSetting.cdn_url}#{stylesheet_relpath}?__ws=#{hostname}"
end
def stylesheet_path(hostname)
stylesheet_cdnpath(hostname)
def stylesheet_absolute_url
"#{GlobalSetting.cdn_url}#{stylesheet_relpath}?__ws=#{current_hostname}"
end
def root_path
@ -177,7 +181,7 @@ class Stylesheet::Manager::Builder
end
def theme_digest
Digest::SHA1.hexdigest(scss_digest.to_s + color_scheme_digest.to_s + settings_digest + uploads_digest)
Digest::SHA1.hexdigest(scss_digest.to_s + color_scheme_digest.to_s + settings_digest + uploads_digest + current_hostname)
end
# this protects us from situations where new versions of a plugin removed a file
@ -225,7 +229,7 @@ class Stylesheet::Manager::Builder
end
def default_digest
Digest::SHA1.hexdigest "default-#{Stylesheet::Manager.last_file_updated}-#{plugins_digest}"
Digest::SHA1.hexdigest "default-#{Stylesheet::Manager.last_file_updated}-#{plugins_digest}-#{current_hostname}"
end
def color_scheme_digest
@ -241,19 +245,19 @@ class Stylesheet::Manager::Builder
fonts = "#{SiteSetting.base_font}-#{SiteSetting.heading_font}"
digest_string = "#{current_hostname}-"
if cs || categories_updated > 0
theme_color_defs = resolve_baked_field(:common, :color_definitions)
Digest::SHA1.hexdigest "#{RailsMultisite::ConnectionManagement.current_db}-#{cs&.id}-#{cs&.version}-#{theme_color_defs}-#{Stylesheet::Manager.last_file_updated}-#{categories_updated}-#{fonts}"
digest_string += "#{RailsMultisite::ConnectionManagement.current_db}-#{cs&.id}-#{cs&.version}-#{theme_color_defs}-#{Stylesheet::Manager.last_file_updated}-#{categories_updated}-#{fonts}"
else
digest_string = "defaults-#{Stylesheet::Manager.last_file_updated}-#{fonts}"
digest_string += "defaults-#{Stylesheet::Manager.last_file_updated}-#{fonts}"
if cdn_url = GlobalSetting.cdn_url
digest_string = "#{digest_string}-#{cdn_url}"
digest_string += "-#{cdn_url}"
end
end
Digest::SHA1.hexdigest digest_string
end
end
def resolve_baked_field(target, name)
theme_ids =

View File

@ -366,6 +366,24 @@ describe Stylesheet::Manager do
builder = Stylesheet::Manager::Builder.new(target: :desktop, manager: manager)
expect(builder.digest).to eq(builder.default_digest)
end
it 'returns different digest based on hostname' do
theme = Fabricate(:theme)
SiteSetting.force_hostname = "host1.example.com"
initial_theme_digest = Stylesheet::Manager::Builder.new(target: :desktop_theme, theme: theme, manager: manager).digest
initial_color_scheme_digest = Stylesheet::Manager::Builder.new(target: :color_definitions, manager: manager).digest
initial_default_digest = Stylesheet::Manager::Builder.new(target: :desktop, manager: manager).digest
SiteSetting.force_hostname = "host2.example.com"
new_theme_digest = Stylesheet::Manager::Builder.new(target: :desktop_theme, theme: theme, manager: manager).digest
new_color_scheme_digest = Stylesheet::Manager::Builder.new(target: :color_definitions, manager: manager).digest
new_default_digest = Stylesheet::Manager::Builder.new(target: :desktop, manager: manager).digest
expect(initial_theme_digest).not_to eq(new_theme_digest)
expect(initial_color_scheme_digest).not_to eq(new_color_scheme_digest)
expect(initial_default_digest).not_to eq(new_default_digest)
end
end
describe 'color_scheme_digest' do
@ -788,6 +806,10 @@ describe Stylesheet::Manager do
expect(results).to include("color_definitions_#{scheme1.name}_#{scheme1.id}_#{user_theme.id}")
expect(results).to include("color_definitions_#{scheme2.name}_#{scheme2.id}_#{default_theme.id}")
# Check that sourceMappingURL includes __ws parameter
content = StylesheetCache.last.content
expect(content).to match(/# sourceMappingURL=[^\/]+\.css\.map\?__ws=test\.localhost/)
end
end
end