Crates.io | resource-rewriter |
lib.rs | resource-rewriter |
version | 0.1.0 |
created_at | 2025-06-01 18:12:27.74278+00 |
updated_at | 2025-06-01 18:12:27.74278+00 |
description | Rewrite inline styles and scripts to separate files |
homepage | |
repository | https://git.legoktm.com/legoktm/resource-rewriter |
max_upload_size | |
id | 1697367 |
size | 47,505 |
Single-file HTML projects that contain inline styles and JavaScript are incredibly convenient to develop with and share, however they constrain some security features, namely the Content Security Policy (CSP).
resource-rewriter
automatically moves any inline styles and scripts to separate files that are loaded by URL so you can set a strict CSP
that bans the use of unsafe inline <style>
and <script>
tags. (Note: no rewriting of inline style/script attributes is attempted.)
This tool is intended to be used at deploy time, so you can develop and share in a single HTML file, and then split it up as you deploy it.
It also adds Subresource Integrity (SRI) hashes to and uses content-based cache invalidation hashes to URLs (optional).
Given an index.html file with both inline styles and scripts and ones already stored in separate files, here's what it looks like in practice:
<head>
<title>Test</title>
- <link href="styles.css" rel="stylesheet">
- <style>
- body {
- background-color: lightblue;
- }
- </style>
+ <link href="styles.css?468d601e37c90f84" rel="stylesheet" integrity="sha384-Kxw/5TXm6N3RHnfs61s9T51oEX9svegYJ+k7D7CEBi+UkiDFKR0UJPd1vCRKkpD4">
+ <link href="styles2.css?14c217a379e00324" rel="stylesheet" integrity="sha384-debHmHz4j3OVP8XwFi6CwbUC8eO7ywH+jHQIZBj5yK8MrBd8ZHzm5nR+CCAbhVMi">
</head>
<body>
<h1>Test</h1>
- <script src="script.js"></script>
- <script>
- console.log("Hello world from an inline script!");
- </script>
+ <script src="script.js?ac5b60349abad073" integrity="sha384-G8jfQwKJiDSJqbfHwdgRD0BnU25phD0D3GvhDkEOrFy0e/6pVvduKyfg3+BwLXeN"></script>
+ <script src="script2.js?8e5cd755d8d444f2" integrity="sha384-iutpq87ZotclarBPMYz6s4nA4xmvaJOs6zucEwXTi0zgwvdmK7xo79OwrZ9OWkoc"></script>
</body>
+
+ body {
+ background-color: lightblue;
+ }
+
+
+ console.log("Hello world from an inline script!");
+
cargo install --git https://git.legoktm.com/legoktm/resource-rewriter
$ resource-rewriter --help
Usage: resource-rewriter [OPTIONS] <PATH>
Arguments:
<PATH> Folder to rewrite resources in
Options:
--diff Print diff for each change
--dry-run Don't make changes, best used with `--diff`
--disable-cache-busting Disable addition of cache busting URL parameters
-h, --help Print help
-V, --version Print version
It'll process all HTML files, move any inline scripts and styles to separate files, calculate and set SRI hashes and cache busting URL parameters.
We use the lol-html crate to rewrite the HTML in three passes.
<script>
and <style>
tags to individual files.<style>
into <link rel="stylesheet">
.The separate passes are mostly to work around limitations in lol-html, namely directly accessing the text content of tags.
No benchmarking or performance optimizations have been performed yet.
Please use caution when using this with untrusted input. While it should work fine, it has not explicitly been tested against adversarial input and may contain bugs.
GPL-3.0-or-later