To enable Subresource Integrity (SRI) for enhanced security, you must first call enable_integrity! in config/importmap.rb. This allows the library to automatically calculate hashes for local assets and CDN-loaded packages.
For Propshaft users:
You must explicitly configure the integrity hash algorithm in config/application.rb or your environment files, otherwise integrity will be disabled by default.
# config/application.rb
config.assets.integrity_hash_algorithm = 'sha256' # or 'sha384', 'sha512'
Configuration options in config/importmap.rb:
enable_integrity!: Enables global integrity calculation.pin "name", integrity: "hash": Manually specify a hash.pin "name", integrity: false: Explicitly disable integrity for a specific pin.pin "name", integrity: nil: Explicitly disable integrity for a specific pin.pin_all_from "path", integrity: true: Automatically calculates hashes for all files in a directory.
# config/importmap.rb
enable_integrity!
pin "application" # Auto-calculated
pin "admin", to: "admin.js" # Auto-calculated
pin_all_from "app/javascript/controllers", under: "controllers" # Auto-calculated
pin "cdn_package", integrity: "sha384-abc123..." # Manual
pin "no_integrity_package", integrity: false # Explicitly disabled