Skip to content

Commit c42a2e7

Browse files
authored
Merge pull request #61 from LeslieLeung/hotfix/crypto
2 parents aa00653 + 4a72d2e commit c42a2e7

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

frontend/packages/api-client/src/crypto.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,39 @@ import sha256 from 'crypto-js/sha256'
1212
import 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
*/
2327
function 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
/**

0 commit comments

Comments
 (0)