1 unstable release
0.1.0 | Aug 20, 2025 |
---|
#1474 in Web programming
161 downloads per month
17KB
314 lines
SRIC - Subresource Integrity Generator
A Rust CLI tool that automatically generates Subresource Integrity (SRI) hashes for HTML files. SRIC parses HTML documents, identifies external resources (scripts and stylesheets), fetches them over HTTP, and adds integrity attributes with SHA-384 hashes.
Features
- Generates SHA-384 SRI hashes for external scripts and stylesheets
- Supports in-place modification or output to new files
- MIME type validation for security
- HTTPS upgrade for HTTP URLs
- Content-Type header verification
- 10MB download limit for safety
- Force override of existing SRI hashes
Installation
cargo install --path .
Usage
# Output to stdout
cargo run -- <html-file>
# Modify file in-place
cargo run -- <html-file> -w
# Write to specific output file
cargo run -- <html-file> -o <output-file>
# Override existing SRI hashes
cargo run -- <html-file> --force
Examples
# Generate SRI hashes and print to stdout
cargo run -- index.html
# Update index.html in-place with SRI hashes
cargo run -- index.html -w
# Save output to a new file
cargo run -- index.html -o index-with-sri.html
Development
Building and Testing
# Build the project
cargo build
# Run tests
cargo test
# Check for compilation errors
cargo check
# Run linter (if installed)
cargo clippy
# Format code (if installed)
cargo fmt
Architecture
main.rs
- CLI argument parsing and orchestrationparse_html.rs
- HTML parsing using html5evergenerate_sri.rs
- Core SRI generation logicwrite_html.rs
- HTML serializationnode_iter.rs
- DOM traversal utilitieselement.rs
- Element operationsmime_ext.rs
- MIME type validationresponse_ext.rs
- HTTP response processing
Dependencies
html5ever
+markup5ever_rcdom
- HTML parsing and DOM manipulationureq
- HTTP client for fetching resourcessha2
+base64
- Cryptographic hashingclap
+clio
- CLI interface and file I/Omime
+mime_guess
- Content type validation
How It Works
- Parse HTML into DOM using html5ever
- Find
<script>
tags withsrc
and<link>
tags withrel="stylesheet|preload|modulepreload"
- Fetch external resources via HTTP with MIME type validation
- Generate SHA-384 hash and encode as base64
- Add or update
integrity
attribute withsha384-<hash>
format - Serialize modified DOM back to HTML
Security
- MIME type validation prevents SRI generation for unexpected content types
- 10MB download limit for safety
- HTTPS upgrade for HTTP URLs
- Content-Type header verification against file extension
Dependencies
~8–24MB
~302K SLoC