@@ -31,6 +31,33 @@ function getNative(): Pbkdf2 {
3131 return native ;
3232}
3333
34+ const MAX_INT32 = 2147483647 ;
35+
36+ function validateParameters ( iterations : number , keylen : number ) : void {
37+ if ( typeof iterations !== 'number' ) {
38+ throw new TypeError ( 'Iterations not a number' ) ;
39+ }
40+ if ( typeof keylen !== 'number' ) {
41+ throw new TypeError ( 'Key length not a number' ) ;
42+ }
43+ if (
44+ iterations < 1 ||
45+ ! Number . isFinite ( iterations ) ||
46+ ! Number . isInteger ( iterations ) ||
47+ iterations > MAX_INT32
48+ ) {
49+ throw new TypeError ( 'Bad iterations' ) ;
50+ }
51+ if (
52+ keylen < 0 ||
53+ ! Number . isFinite ( keylen ) ||
54+ ! Number . isInteger ( keylen ) ||
55+ keylen > MAX_INT32
56+ ) {
57+ throw new TypeError ( 'Bad key length' ) ;
58+ }
59+ }
60+
3461function sanitizeInput ( input : BinaryLike , errorMsg : string ) : ArrayBuffer {
3562 try {
3663 return binaryLikeToArrayBuffer ( input ) ;
@@ -51,6 +78,7 @@ export function pbkdf2(
5178 if ( callback === undefined || typeof callback !== 'function' ) {
5279 throw new Error ( 'No callback provided to pbkdf2' ) ;
5380 }
81+ validateParameters ( iterations , keylen ) ;
5482 const sanitizedPassword = sanitizeInput ( password , WRONG_PASS ) ;
5583 const sanitizedSalt = sanitizeInput ( salt , WRONG_SALT ) ;
5684 const normalizedDigest = normalizeHashName ( digest , HashContext . Node ) ;
@@ -81,6 +109,7 @@ export function pbkdf2Sync(
81109 keylen : number ,
82110 digest : string ,
83111) : Buffer {
112+ validateParameters ( iterations , keylen ) ;
84113 const sanitizedPassword = sanitizeInput ( password , WRONG_PASS ) ;
85114 const sanitizedSalt = sanitizeInput ( salt , WRONG_SALT ) ;
86115
0 commit comments