Skip to content

Commit

Permalink
Fix TypeError when invalid initialization vector (#3416)
Browse files Browse the repository at this point in the history
This can happen for example when `encrypted` has less than 32 bytes
of hex content.
  • Loading branch information
juliangruber authored Feb 1, 2022
1 parent a74ae35 commit c5f8f15
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/@uppy/companion/src/server/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,18 @@ module.exports.decrypt = (encrypted, secret) => {

const iv = Buffer.from(encrypted.slice(0, 32), 'hex')
const encryptionWithoutIv = encrypted.slice(32)
const decipher = crypto.createDecipheriv('aes256', createSecret(secret), iv)

let decipher
try {
decipher = crypto.createDecipheriv('aes256', createSecret(secret), iv)
} catch (err) {
if (err.code === 'ERR_CRYPTO_INVALID_IV') {
throw new Error('Invalid initialization vector')
} else {
throw err
}
}

let decrypted = decipher.update(urlDecode(encryptionWithoutIv), 'base64', 'utf8')
decrypted += decipher.final('utf8')
return decrypted
Expand Down

0 comments on commit c5f8f15

Please sign in to comment.