Enable Subresource Integrity

This commit is contained in:
Sebastian Serth
2022-09-03 03:22:44 +02:00
parent 51e9daf930
commit 9e08f3a6a8
7 changed files with 93 additions and 7 deletions

View File

@ -0,0 +1,27 @@
# frozen_string_literal: true
module Webpacker::SriHelperExtensions
def stylesheet_link_tag(*sources, **options)
tags = sources.map do |stylesheet|
if stylesheet.is_a?(Hash)
super(stylesheet[:src], options.merge(integrity: stylesheet[:integrity]))
else
super(stylesheet, options)
end
end
safe_join(tags)
end
def javascript_include_tag(*sources, **options)
tags = sources.map do |javascript|
if javascript.is_a?(Hash)
super(javascript[:src], options.merge(integrity: javascript[:integrity]))
else
super(javascript, options)
end
end
safe_join(tags)
end
end
Sprockets::Rails::Helper.prepend(Webpacker::SriHelperExtensions)

View File

@ -0,0 +1,29 @@
# frozen_string_literal: true
module Webpacker::SriManifestExtensions
def lookup(name, pack_type = {})
asset = super
augment_with_integrity asset, pack_type
end
def lookup_pack_with_chunks(name, pack_type = {})
assets = super
assets.map do |asset|
augment_with_integrity asset, pack_type
end
end
def augment_with_integrity(asset, _pack_type = {})
if asset.respond_to?(:dig) && asset['integrity']
{src: asset['src'], integrity: asset['integrity']}
elsif asset.respond_to?(:dig)
asset['src']
else
asset
end
end
end
Webpacker::Manifest.prepend(Webpacker::SriManifestExtensions)