Skip to content

Commit b1445c2

Browse files
committed
Document SRI feature in the README.md file
1 parent 6e6d36e commit b1445c2

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

README.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ With Rails 8, Propshaft is the default asset pipeline for new applications. With
1616

1717
## Usage
1818

19-
Propshaft makes all the assets from all the paths it's been configured with through `config.assets.paths` available for serving and will copy all of them into `public/assets` when precompiling. This is unlike Sprockets, which did not copy over assets that hadn't been explicitly included in one of the bundled assets.
19+
Propshaft makes all the assets from all the paths it's been configured with through `config.assets.paths` available for serving and will copy all of them into `public/assets` when precompiling. This is unlike Sprockets, which did not copy over assets that hadn't been explicitly included in one of the bundled assets.
2020

2121
You can however exempt directories that have been added through the `config.assets.excluded_paths`. This is useful if you're for example using `app/assets/stylesheets` exclusively as a set of inputs to a compiler like Dart Sass for Rails, and you don't want these input files to be part of the load path. (Remember you need to add full paths, like `Rails.root.join("app/assets/stylesheets")`).
2222

@@ -50,9 +50,55 @@ export default class extends Controller {
5050

5151
If you need to put multiple files that refer to each other through Propshaft, like a JavaScript file and its source map, you have to digest these files in advance to retain stable file names. Propshaft looks for the specific pattern of `-[digest].digested.js` as the postfix to any asset file as an indication that the file has already been digested.
5252

53+
## Subresource Integrity (SRI)
54+
55+
Propshaft supports Subresource Integrity (SRI) to help protect against malicious modifications of assets. SRI allows browsers to verify that resources fetched from CDNs or other sources haven't been tampered with by checking cryptographic hashes.
56+
57+
### Enabling SRI
58+
59+
To enable SRI support, configure the hash algorithm in your Rails application:
60+
61+
```ruby
62+
config.assets.integrity_hash_algorithm = "sha384"
63+
```
64+
65+
Valid hash algorithms include:
66+
- `"sha256"` - SHA-256 (most common)
67+
- `"sha384"` - SHA-384 (recommended for enhanced security)
68+
- `"sha512"` - SHA-512 (strongest)
69+
70+
### Using SRI in your views
71+
72+
Once configured, you can enable SRI by passing the `integrity: true` option to asset helpers:
73+
74+
```erb
75+
<%= stylesheet_link_tag "application", integrity: true %>
76+
<%= javascript_include_tag "application", integrity: true %>
77+
```
78+
79+
This generates HTML with integrity hashes:
80+
81+
```html
82+
<link rel="stylesheet" href="/assets/application-abc123.css"
83+
integrity="sha384-xyz789...">
84+
<script src="/assets/application-def456.js"
85+
integrity="sha384-uvw012..."></script>
86+
```
87+
88+
**Important**: SRI only works in secure contexts (HTTPS) or during local development. The integrity hashes are automatically omitted when serving over HTTP in production for security reasons.
89+
90+
### Bulk stylesheet inclusion with SRI
91+
92+
Propshaft extends `stylesheet_link_tag` with special symbols for bulk inclusion:
93+
94+
```erb
95+
<%= stylesheet_link_tag :all, integrity: true %> <!-- All stylesheets -->
96+
<%= stylesheet_link_tag :app, integrity: true %> <!-- Only app/assets stylesheets -->
97+
```
98+
5399
## Improving performance in development
54100

55-
Before every request Propshaft checks if any asset was updated to decide if a cache sweep is needed. This verification is done using the application's configured file watcher which, by default, is `ActiveSupport::FileUpdateChecker`.
101+
Before every request Propshaft checks if any asset was updated to decide if a cache sweep is needed. This verification is done using the application's configured file watcher which, by default, is `ActiveSupport::FileUpdateChecker`.
56102

57103
If you have a lot of assets in your project, you can improve performance by adding the `listen` gem to the development group in your Gemfile, and this line to the `development.rb` environment file:
58104

0 commit comments

Comments
 (0)