-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Subresource Integrity #323
Comments
@Gargron Will take a look at this 👍 |
Any new on this ? ^-^ |
This ticket is three days old. Gaurav is doing an excellent job
investigating the many requests put forward here, but let's not burn him
out with incessant pestering on issues that were just opened but have not
yet progressed. If you want to contribute a PR with a proposed solution,
that's awesome. But otherwise please have some patience. Thanks!
…On Sun, May 7, 2017 at 5:01 PM, Technowix ***@***.***> wrote:
Any new on this ? ^-^
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#323 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAAKtdEzZp6Tj5JWYawOuFWtnAH92CFQks5r3dzhgaJpZM4NQFuj>
.
|
Just to update everyone here, this feature is on hold for now as I couldn't find any straightforward solution to this problem (something that doesn't make things complicated). Feel free to leave any suggestions/ideas here though 👍 |
I just looked at my production Sprockets manifest and I can see that it contains the SRI digest for my packs: {
"files": {
"manifest-d0ff5974b6aa52cf562bea5921840c032a860a91a3512f7fe8f768f6bbe005f6.js": {
"logical_path": "manifest.js",
"mtime": "2017-03-23T17:25:57+00:00",
"size": 0,
"digest": "d0ff5974b6aa52cf562bea5921840c032a860a91a3512f7fe8f768f6bbe005f6",
"integrity": "sha256-0P9ZdLaqUs9WK+pZIYQMAyqGCpGjUS9/6Pdo9rvgBfY="
},
} I know that Webpacker does not depend on Sprockets, but this seems the way to go. I am not sure why this is generated, but I think we should use the same mechanism: put the SRI digests (and in the future other informations?) in the manifest generated by Webpack. The current Some Webpack plugins already store the SRI attributes in Webpack's I wont have time to work on this, but hopefully here are some ideas! |
A new Webpacker could ship with a |
I would like to +1 @renchap's suggestion to use the new feature of With a simple change of the structure of {
"application.js": "/packs/application-[DIGEST].js"
} .. to: {
"application.js": {
"path": "/packs/application-[DIGEST].js"
}
} .. it immediately becomes possible to add all kinds of metadata into the manifest that could be appended by Integrity hashes is just one piece of data that could be passed in here. Other data that comes to mind includes:
I can probably think of more, (and I'm sure there are many custom situations) but you get the jist. |
@gauravtiwari what are your thoughts on my comment above? I might be interested in making a PR (at least to preliminarily change the format of |
Bump here. I think this would be awesome to first use the new structure for the manifest file, and then add others features based on it, starting with SRI :) |
The new manifest plugin used in Webpacker 4 (https://github.com/webdeveric/webpack-assets-manifest) supports an |
Yep, that's why used it so we can get this feature too at some point. Initially, I added the option but then it changes the manifest schema, which means rethinking the way the manifest class is structured in the gem and it's responsibilities (integration with the helper module) so this feature is slightly more involved. |
@gauravtiwari, would a PR be accepted that implements this feature? Or does more discussion need to take place first (regarding the helper module, etc -- perhaps with the Rails team)? If the latter, where would be the best place to start it? |
Any updates? |
@jebenois in environment.js add next lines:
and i created the new helper to replace
and use this methoud instead of webpacker methods. But currently, it generated incorrect SHA sum :-( |
@gauravtiwari any news on making SRI functional with webpacker? i am willing to help with a PR if you can point to what needs addressing. |
fwiw, we are using this somewhat hacky monkeypatch to make it work: app/helpers/asset_helper.rb: module AssetHelper
# this monkeypatch is needed to set the integrity attr to html tags for SRI
class Webpacker::Manifest
unless Webpacker::Manifest.instance_methods(false).include?(:original_lookup)
alias original_lookup lookup
end
def lookup(name, pack_type = {})
asset = original_lookup(name, pack_type)
(asset.respond_to?(:dig) && asset.dig('src')) || asset # rubocop:disable Style/SingleArgumentDig
end
def lookup_integrity(name, pack_type = {})
asset = original_lookup(name, pack_type)
(asset.respond_to?(:dig) && asset.dig('integrity')) || nil # rubocop:disable Style/SingleArgumentDig
end
end
module Webpacker::Helper
def javascript_pack_tag(*names, **options)
generic_packs_with_chunks_tag(:javascript, false, *names, **options)
end
def javascript_packs_with_chunks_tag(*names, **options)
generic_packs_with_chunks_tag(:javascript, true, *names, **options)
end
def stylesheet_pack_tag(*names, **options)
generic_packs_with_chunks_tag(:stylesheet, false, *names, **options)
end
def stylesheet_packs_with_chunks_tag(*names, **options)
generic_packs_with_chunks_tag(:stylesheet, true, *names, **options)
end
private
def generic_packs_with_chunks_tag(type, chunks, *names, **options)
return if type == :stylesheet && !current_webpacker_instance.config.extract_css?
entries = if chunks
sources_from_manifest_entrypoints(names, type: type)
else
sources_from_manifest_entries(names, type: type)
end
entries.map do |entry|
name = entry.match(%r{\A.+/([^/]+)(?:-[a-z0-9]{8,20})(?:\.chunk)?\.(?:js|css)\z}) && Regexp.last_match(1)
if name
integrity = Webpacker.manifest.lookup_integrity(name, type: type)
if integrity
options = options.merge(integrity: integrity, crossorigin: 'anonymous')
end
else
logger.error { "ERROR failed to find #{entry} in manifest" }
end
case type
when :stylesheet
stylesheet_link_tag(entry, **options)
when :javascript
javascript_include_tag(entry, **options)
else
raise "unknown type: #{type}"
end
end.join("\n").html_safe # rubocop:disable Rails/OutputSafety
end
end
end config/webpack/environment.js: // Webpacker uses this plugin to generate its manifest but
// at present it does not generate the integrity for each asset.
const manifestPlugin = environment.plugins.get('Manifest');
manifestPlugin.options.integrity = true;
manifestPlugin.options.integrityHashes = ['sha256']; |
@gsar, thanks for your solution. Unfortunately, it wasn't working for me. I'm on Rails 6.0.2.1 Any plans to officially support this? |
@cirdes i can confirm the monkeypatch i showed above has been working for a couple of years now. we are currently on webpacker 5.4.0 and rails 6.1.4. official support would be great to see! |
@gsar, I'm running with webpacker 6.0.0.beta.7 and rails 6.1.4. I will try to apply the monkeypatch again. Thanks! |
A word of caution: if all you do is to set the (I'm the principal maintainer of webpack-subresource-integrity.) I also recommend reading our list of pitfalls which we've distilled from years worth of feedback. Most of them would also apply here. Cache settings in particular are worth paying close attention to. |
Disregard |
Sprockets supported subresource integrity out of the box. It would be nice if
worked the same. There is a webpack plugin but I am not sure how to hook it up to Rails to put the integrity hashes into the template.
The text was updated successfully, but these errors were encountered: