Skip to content

Commit

Permalink
fix: unhandled rejections for invalid base64
Browse files Browse the repository at this point in the history
  • Loading branch information
frytg committed Dec 17, 2024
1 parent 1e71ba6 commit 7268e4a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
8 changes: 5 additions & 3 deletions crypto/hmac-buffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test('bufferFromBase64 - converts base64 strings to Buffer correctly', () => {
}
})

test('bufferFromBase64 - validates base64 strings correctly', () => {
test('bufferFromBase64 - validates valid base64 strings correctly', () => {
// Valid base64 strings should work
const validBase64 = [
'aGVsbG8=', // normal case
Expand All @@ -46,10 +46,12 @@ test('bufferFromBase64 - validates base64 strings correctly', () => {
assertEquals(
typeof bufferFromBase64(base64),
'object',
`bufferFromBase64 should accept valid base64 string "${base64}"`,
`bufferFromBase64 should accept valid base64 string "${base64}".`,
)
}
})

test('bufferFromBase64 - validates invalid base64 strings correctly', () => {
// Invalid base64 strings should throw
const invalidBase64 = [
'!@#$', // invalid characters
Expand All @@ -62,7 +64,7 @@ test('bufferFromBase64 - validates base64 strings correctly', () => {
() => bufferFromBase64(base64),
Error,
'base64', // error message should include
`bufferFromBase64 should reject invalid base64 string "${base64}"`,
`bufferFromBase64 should reject invalid base64 string "${base64}".`,
)
}
})
Expand Down
16 changes: 11 additions & 5 deletions crypto/hmac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
*/

// load packages
import { Buffer } from 'node:buffer'
import { Buffer, atob } from 'node:buffer'
import { createHmac } from 'node:crypto'

const BASE64_ENCODING = 'base64'
const BASE64_REGEX = /^[0-9a-zA-Z+/=]*$/
const HEX_ENCODING = 'hex'
const HEX_REGEX = /^[0-9a-fA-F]*$/
Expand Down Expand Up @@ -67,11 +66,18 @@ export const hmacSha512 = (str: string | Buffer, key: string | Buffer): string =
* hmacSha512('hello world', bufferFromBase64('MDEyMzQ1Njc4OWFiY2RlZg=='))
* ```
*/
export const bufferFromBase64 = (base64: string): Buffer => {
export const bufferFromBase64 = (base64: string, preferNativeError = false): Buffer => {
// check if base64 string is valid
if (!BASE64_REGEX.test(base64)) throw new Error('Invalid base64 string')
if (!BASE64_REGEX.test(base64)) throw new Error('Invalid base64 characters')

return Buffer.from(base64, BASE64_ENCODING)
try {
// we're using atob because Buffer.from(base64, 'base64') throws unhandled rejections on some systems
return Buffer.from(atob(base64))
} catch (error) {
// the native error varies between runtimes, so the default is to throw our own error
if (preferNativeError) throw error
throw new Error('Invalid base64 string')
}
}

/**
Expand Down

0 comments on commit 7268e4a

Please sign in to comment.