Skip to content

feat: subresource integrity hashes for link and script tags - #339

Draft
troglodyne-bot wants to merge 2 commits into
Troglodyne-Internet-Widgets:masterfrom
troglodyne-bot:koan/sri-integrity-hashes
Draft

feat: subresource integrity hashes for link and script tags#339
troglodyne-bot wants to merge 2 commits into
Troglodyne-Internet-Widgets:masterfrom
troglodyne-bot:koan/sri-integrity-hashes

Conversation

@troglodyne-bot

Copy link
Copy Markdown
Contributor

What

Adds integrity="sha384-..." attributes to all <link rel="stylesheet">, <link rel="preload">, and <script> tags in header.tx.

Why

Subresource Integrity (SRI) lets browsers verify that fetched resources haven't been tampered with. Even for same-origin assets, this provides defense-in-depth against injection attacks that might serve a modified JS or CSS file. Closes #301.

How

  • Trog::Renderer::html::sri_hash($url_path) — reads www/$url_path from disk, returns sha384-<base64>. Caches via state %cache (process lifetime). Returns '' for missing files so the integrity attribute is omitted rather than emitted empty.
  • Trog::Renderer::Base::render() gains an extra_functions option that html.pm uses to inject sri_hash into the top-level Xslate renderer.
  • header.tx calls sri_hash() per resource, conditionally emitting the attribute only when the hash is non-empty (so bundled files like reveal.css and obsidian.min.css, not checked in, get no attribute).

Testing

6 new tests in t/Trog-Renderer-html.t: correct SHA-384 for an existing file, caching behavior, empty return for missing/non-absolute/undef inputs.

The hash value is cross-verified against an independent Digest::SHA + MIME::Base64 computation.

Comment thread lib/Trog/Renderer/Base.pm Outdated
@@ -46,6 +46,7 @@ sub render (%options) {
path => $template_dir,
function => {
render_it => $options{child_renderer},

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would very much prefer we simply add render_it as a key to $options{extra_functions} prior to the XSlate::new call here.

Comment thread lib/Trog/Renderer/html.pm Outdated
unless ( -f $file ) {
return $cache{$url_path} = '';
}
open( my $fh, '<:raw', $file ) or return $cache{$url_path} = '';

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More concisely stated as

my $digestor = Digest::SHA->new(384);
$digestor->addfile($file);
return $cache{$url_path} = 'sha384-'.encode_base64($digestor->digest);

@teodesian

Copy link
Copy Markdown
Collaborator

@troglodyne-bot rebase

troglodyne-bot and others added 2 commits June 5, 2026 20:00
Implements subresource integrity (SRI) for all CSS and JS resources
served in HTML pages, as requested in issue Troglodyne-Internet-Widgets#301.

Trog::Renderer::html::sri_hash($url_path) computes a SHA-384 hash of
the file at www/$url_path and returns it as a sri384-<base64> string.
Results are memoized via `state %cache` for the process lifetime.
Returns '' for missing files so the integrity attribute is omitted.

Trog::Renderer::Base::render() accepts an extra_functions hashref to
let subclasses inject custom Xslate template functions.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@troglodyne-bot

Copy link
Copy Markdown
Contributor Author

Rebase with requested adjustments

Branch koan/sri-integrity-hashes was rebased onto master and review feedback was applied.

Changes applied


  • Base.pm: Set $options{extra_functions}{render_it} = $options{child_renderer} before Text::Xslate->new, then pass $options{extra_functions} directly as function. Removes the inline hash merge, per @teodesian's request.
  • html.pm: Replaced slurp + functional sha384() with Digest::SHA->new(384)->addfile($file)->digest OO pattern, per @teodesian's suggestion. Changed use Digest::SHA qw(sha384) to use Digest::SHA since the functional import is no longer needed. Kept '' as second arg to encode_base64 to suppress the trailing newline (required for valid SRI attribute values).

Stats

4 files changed, 90 insertions(+), 17 deletions(-)
Actions performed
  • Already-solved check: negative (confidence=high, reasoning=No SRI-related code (sri_hash, integrity=, sha384) exists anywhere in the master branch.)
  • Rebased koan/sri-integrity-hashes onto upstream/master
  • Applied review feedback
  • Pre-push CI check: no CI runs found
  • Force-pushed koan/sri-integrity-hashes to origin
  • CI check enqueued in ## CI (async)

CI status

CI will be checked asynchronously.


Automated by Kōan

@troglodyne-bot
troglodyne-bot force-pushed the koan/sri-integrity-hashes branch from b6d7ea7 to 2d6765c Compare June 5, 2026 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add means to get SHAs for subresource integrity attrs on link and script tags

2 participants