File tree Expand file tree Collapse file tree
frontend/packages/api-client/src Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -12,16 +12,39 @@ import sha256 from 'crypto-js/sha256'
1212import encHex from 'crypto-js/enc-hex'
1313
1414/**
15- * Check if Web Crypto API is available.
15+ * Check if we're in a secure context where Web Crypto API is available.
1616 *
1717 * crypto.subtle is only available in secure contexts:
1818 * - HTTPS connections
1919 * - localhost (http://localhost, http://127.0.0.1)
2020 *
21+ * We use multiple checks for maximum compatibility:
22+ * 1. globalThis.isSecureContext - the standard way to detect secure context
23+ * 2. Check if crypto.subtle exists and has the digest function
24+ *
2125 * On plain HTTP with non-localhost domains, crypto.subtle is undefined.
2226 */
2327function isWebCryptoAvailable ( ) : boolean {
24- return typeof crypto !== 'undefined' && crypto . subtle !== undefined
28+ try {
29+ // First check: use isSecureContext if available (most reliable)
30+ if ( typeof globalThis !== 'undefined' && 'isSecureContext' in globalThis ) {
31+ if ( ! globalThis . isSecureContext ) {
32+ return false
33+ }
34+ }
35+
36+ // Second check: verify crypto.subtle is actually available and functional
37+ return (
38+ typeof crypto !== 'undefined' &&
39+ crypto !== null &&
40+ typeof crypto . subtle !== 'undefined' &&
41+ crypto . subtle !== null &&
42+ typeof crypto . subtle . digest === 'function'
43+ )
44+ } catch {
45+ // If any check throws, fall back to crypto-js
46+ return false
47+ }
2548}
2649
2750/**
You can’t perform that action at this time.
0 commit comments