Skip to content

Commit aa36a56

Browse files
1 parent 3a8d21e commit aa36a56

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

lib/aes.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,17 @@ class AesGcm extends Aes {
149149

150150
const decipher = crypto.createDecipheriv(
151151
this.ALGO_AES_256_GCM, key, iv
152-
).setAuthTag(tag).setAAD(Buffer.from(aad))
152+
)
153+
154+
// Restrict valid GCM tag length, patches for Node < 11.0.0
155+
// more @see https://github.com/nodejs/node/pull/20039
156+
const tagLen = tag.length
157+
if (tagLen > 16 || (tagLen < 12 && tagLen != 8 && tagLen != 4)) {
158+
let backport = new TypeError(`Invalid authentication tag length: ${tagLen}`)
159+
backport.code = 'ERR_CRYPTO_INVALID_AUTH_TAG'
160+
throw backport
161+
}
162+
decipher.setAuthTag(tag).setAAD(Buffer.from(aad))
153163

154164
return Buffer.concat([
155165
decipher.update(payload, this.hex),

tests/lib/aes.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ describe('lib/aes', () => {
226226
should(() => {
227227
aes.decrypt(mockupIv, mockupKey, '')
228228
}).throw(Error, {
229+
code: 'ERR_CRYPTO_INVALID_AUTH_TAG',
229230
message: 'Invalid authentication tag length: 0',
230-
stack: /at Decipheriv\.setAuthTag/,
231231
})
232232
})
233233

0 commit comments

Comments
 (0)