|
| 1 | +/** |
| 2 | + * `crypto.subtle` is only available in secure contexts (https / localhost), |
| 3 | + * so the POW captcha (crypto-puzzle -> tiny-encryptor / crypto-sha) breaks |
| 4 | + * when the admin panel is served over plain http. |
| 5 | + * |
| 6 | + * `ensureSubtleCrypto()` installs a minimal pure-JS fallback implementing only |
| 7 | + * the operations crypto-puzzle needs: SHA-256 digest, PBKDF2-HMAC-SHA256 |
| 8 | + * deriveBits and AES-GCM encrypt/decrypt. The asmcrypto.js implementation is |
| 9 | + * loaded dynamically, so secure contexts never download it. |
| 10 | + */ |
| 11 | + |
| 12 | +type FallbackKey = { |
| 13 | + __raw: Uint8Array; |
| 14 | + algorithm: { name: string }; |
| 15 | + usages: string[]; |
| 16 | + type: 'secret'; |
| 17 | + extractable: false; |
| 18 | +}; |
| 19 | + |
| 20 | +function toBytes(data: BufferSource): Uint8Array { |
| 21 | + if (data instanceof Uint8Array) return data; |
| 22 | + if (ArrayBuffer.isView(data)) return new Uint8Array(data.buffer, data.byteOffset, data.byteLength); |
| 23 | + return new Uint8Array(data); |
| 24 | +} |
| 25 | + |
| 26 | +function toBuffer(bytes: Uint8Array): ArrayBuffer { |
| 27 | + return bytes.slice().buffer; |
| 28 | +} |
| 29 | + |
| 30 | +function algoName(algorithm: string | { name: string }): string { |
| 31 | + return (typeof algorithm === 'string' ? algorithm : algorithm.name).toUpperCase(); |
| 32 | +} |
| 33 | + |
| 34 | +let installed: Promise<void> | undefined; |
| 35 | + |
| 36 | +export function ensureSubtleCrypto(): Promise<void> { |
| 37 | + if (typeof crypto === 'undefined' || crypto.subtle) return Promise.resolve(); |
| 38 | + installed ??= installFallback(); |
| 39 | + return installed; |
| 40 | +} |
| 41 | + |
| 42 | +async function installFallback(): Promise<void> { |
| 43 | + const { SHA256, PBKDF2_HMAC_SHA256, AES_GCM } = await import('asmcrypto.js'); |
| 44 | + |
| 45 | + const subtleFallback = { |
| 46 | + async digest(algorithm: string | { name: string }, data: BufferSource): Promise<ArrayBuffer> { |
| 47 | + if (algoName(algorithm) !== 'SHA-256') throw new Error(`Unsupported digest algorithm: ${algoName(algorithm)}`); |
| 48 | + return toBuffer(SHA256.bytes(toBytes(data))); |
| 49 | + }, |
| 50 | + |
| 51 | + async importKey( |
| 52 | + format: string, |
| 53 | + keyData: BufferSource, |
| 54 | + algorithm: string | { name: string }, |
| 55 | + _extractable: boolean, |
| 56 | + usages: string[], |
| 57 | + ): Promise<FallbackKey> { |
| 58 | + if (format !== 'raw') throw new Error(`Unsupported key format: ${format}`); |
| 59 | + return { |
| 60 | + __raw: toBytes(keyData).slice(), |
| 61 | + algorithm: { name: algoName(algorithm) }, |
| 62 | + usages, |
| 63 | + type: 'secret', |
| 64 | + extractable: false, |
| 65 | + }; |
| 66 | + }, |
| 67 | + |
| 68 | + async deriveBits( |
| 69 | + params: { name: string; salt: BufferSource; iterations: number; hash: string | { name: string } }, |
| 70 | + key: FallbackKey, |
| 71 | + length: number, |
| 72 | + ): Promise<ArrayBuffer> { |
| 73 | + if (algoName(params) !== 'PBKDF2' || algoName(params.hash) !== 'SHA-256') { |
| 74 | + throw new Error(`Unsupported deriveBits params: ${algoName(params)}/${algoName(params.hash)}`); |
| 75 | + } |
| 76 | + return toBuffer(PBKDF2_HMAC_SHA256.bytes(key.__raw, toBytes(params.salt), params.iterations, length / 8)); |
| 77 | + }, |
| 78 | + |
| 79 | + async encrypt( |
| 80 | + params: { name: string; iv: BufferSource; tagLength?: number }, |
| 81 | + key: FallbackKey, |
| 82 | + data: BufferSource, |
| 83 | + ): Promise<ArrayBuffer> { |
| 84 | + if (algoName(params) !== 'AES-GCM') throw new Error(`Unsupported encrypt algorithm: ${algoName(params)}`); |
| 85 | + return toBuffer(AES_GCM.encrypt(toBytes(data), key.__raw, toBytes(params.iv), undefined, (params.tagLength ?? 128) / 8)); |
| 86 | + }, |
| 87 | + |
| 88 | + async decrypt( |
| 89 | + params: { name: string; iv: BufferSource; tagLength?: number }, |
| 90 | + key: FallbackKey, |
| 91 | + data: BufferSource, |
| 92 | + ): Promise<ArrayBuffer> { |
| 93 | + if (algoName(params) !== 'AES-GCM') throw new Error(`Unsupported decrypt algorithm: ${algoName(params)}`); |
| 94 | + return toBuffer(AES_GCM.decrypt(toBytes(data), key.__raw, toBytes(params.iv), undefined, (params.tagLength ?? 128) / 8)); |
| 95 | + }, |
| 96 | + }; |
| 97 | + |
| 98 | + Object.defineProperty(crypto, 'subtle', { |
| 99 | + value: subtleFallback, |
| 100 | + configurable: true, |
| 101 | + }); |
| 102 | +} |
0 commit comments