Skip to content

fix(dashproof-lab): clear error when crypto.subtle is unavailable#78

Merged
thephez merged 2 commits into
mainfrom
fix/dashproof-secure-context-error
May 12, 2026
Merged

fix(dashproof-lab): clear error when crypto.subtle is unavailable#78
thephez merged 2 commits into
mainfrom
fix/dashproof-secure-context-error

Conversation

@thephez
Copy link
Copy Markdown
Collaborator

@thephez thephez commented May 12, 2026

Summary

  • Loading the app over plain http:// (e.g. bare-IP hosting, non-HTTPS IPFS gateways) crashed with Cannot read properties of undefined (reading 'digest') because crypto.subtle is only exposed in Secure Contexts.
  • hashFile now checks for crypto.subtle up front and throws a clear message pointing the user to HTTPS or http://localhost. Applied to both the React app (src/lib/hash.ts) and the zero-build companion (public/dashproof-lite.html).
  • Existing callers in AnchorForm and VerifyPanel already pipe thrown errors through errorMessage(err) into the status UI, so the new message surfaces inline instead of crashing.
  • Added a regression test that stubs crypto.subtle = undefined and asserts the exact error string, so any future reword fails the test rather than silently degrading the diagnostic.

Test plan

  • npm run test — 138/138 pass (20 files)
  • npm run format — clean
  • npm run build — typecheck + bundle clean
  • Manually load the built app over http:// and confirm the inline error replaces the cryptic crash
  • Manually load over https:// / http://localhost and confirm hashing still works

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved error handling for non-secure contexts. Users now receive clear instructions to use HTTPS or http://localhost instead of encountering cryptic failures during file operations.
  • Tests

    • Added regression test for secure context validation.

Review Change Stack

thephez and others added 2 commits May 12, 2026 12:04
Hashing requires a Secure Context, so plain http:// origins (e.g. bare-IP
hosting, non-HTTPS IPFS gateways) crashed with "Cannot read properties of
undefined (reading 'digest')". Detect the missing crypto.subtle up front
and throw a message pointing to HTTPS or localhost. Applied to both the
React app and the dashproof-lite single-file companion.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Asserts the exact error message so an accidental rewording would fail the
test instead of silently degrading the user-facing diagnostic.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 12, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: de2f587b-c0e9-40db-bb81-40f101a8d943

📥 Commits

Reviewing files that changed from the base of the PR and between 26e631e and 87322ce.

📒 Files selected for processing (3)
  • example-apps/dashproof-lab/public/dashproof-lite.html
  • example-apps/dashproof-lab/src/lib/hash.ts
  • example-apps/dashproof-lab/test/hashFile.test.ts

📝 Walkthrough

Walkthrough

This PR adds an early precondition check to hashFile() ensuring crypto.subtle is available before attempting SHA-256 hashing. When unavailable (non-secure contexts), it throws a clear error guiding users to use HTTPS or http://localhost instead of failing silently during computation.

Changes

Secure Context Check for Hash Operations

Layer / File(s) Summary
Secure context guard in hashFile
src/lib/hash.ts, public/dashproof-lite.html
Both the HTML inline and TypeScript versions of hashFile now validate that crypto.subtle is available before proceeding with SHA-256 hashing, throwing a descriptive error with guidance about HTTPS or localhost when the check fails.
Test setup and regression test for secure context
test/hashFile.test.ts
Test suite adds an afterEach hook to preserve and restore globalThis.crypto.subtle across tests, preventing cross-test mutation, and includes a new regression test verifying hashFile rejects with a specific Secure Context error message when crypto.subtle is undefined.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A check before the hash begins its way,

Ensures the crypto context lights the day,

No silent fails in insecure land,

Just clear guidance—HTTPS hand in hand! 🔐

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and directly describes the main fix: adding error handling for when crypto.subtle is unavailable in non-secure contexts.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/dashproof-secure-context-error

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@thephez thephez merged commit ee6e71a into main May 12, 2026
3 checks passed
@thephez thephez deleted the fix/dashproof-secure-context-error branch May 12, 2026 17:56
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.

1 participant