jose/src/runtime/browser/base64url.ts
codedust ed32b0d46e fix: workaround for RangeError in browser runtime base64url
Fixes RangeError in base64url.ts when encrypting large Uint8Arrays

String.fromCharCode.apply causes a RangeError for large Uint8Arrays
(> ~500kB). This happens, e.g., when encrypting larger files.

See this gist to reproduce the bug (select a large file and see the
browser console):
https://gist.github.com/codedust/88c8af3b2acd782e72ffbe0c3c8bf5af

Error message in Firefox:
```
Uncaught (in promise) RangeError: too many arguments provided for a
function call (in base64url.js:8:62)
    encode http://localhost:8000/jose/runtime/base64url.js:8
    encrypt http://localhost:8000/jose/jwe/flattened/encrypt.js:143
```

Error message in Chromium:
```
Uncaught (in promise) RangeError: Maximum call stack size exceeded
    at encode (base64url.js:8)
    at FlattenedEncrypt.encrypt (encrypt.js:143)
    at async CompactEncrypt.encrypt (encrypt.js:23)
    at async jwe_test ((index):55)
```

Solution: Apply String.fromCharCode.apply in chunks of 32768 bytes,
see https://stackoverflow.com/a/12713326
2021-01-10 14:33:30 +01:00

35 lines
1.1 KiB
TypeScript

import { encoder, decoder } from '../../lib/buffer_utils.js'
import globalThis from './global.js'
export const encode = (input: Uint8Array | string) => {
let unencoded = input
if (typeof unencoded === 'string') {
unencoded = encoder.encode(unencoded)
}
const CHUNK_SIZE = 0x8000
const arr = []
for (let i = 0; i < unencoded.length; i += CHUNK_SIZE) {
// @ts-expect-error
arr.push(String.fromCharCode.apply(null, unencoded.subarray(i, i + CHUNK_SIZE)))
}
const base64string = globalThis.btoa(arr.join(''))
return base64string.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')
}
export const decode = (input: Uint8Array | string) => {
let encoded = input
if (encoded instanceof Uint8Array) {
encoded = decoder.decode(encoded)
}
encoded = encoded.replace(/-/g, '+').replace(/_/g, '/').replace(/\s/g, '')
try {
return new Uint8Array(
globalThis
.atob(encoded)
.split('')
.map((c) => c.charCodeAt(0)),
)
} catch {
throw new TypeError('The input to be decoded is not correctly encoded.')
}
}