mirror of
https://github.com/jimeh/node-base58.git
synced 2026-02-19 07:36:40 +00:00
remove large or negative number vulnerability
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
.DS_Store
|
||||
node_modules
|
||||
node_modules
|
||||
npm-debug.log
|
||||
@@ -8,9 +8,9 @@ var alphabetLookup = alphabet.split('').reduce(function (lookup, char, index) {
|
||||
return lookup;
|
||||
}, {});
|
||||
|
||||
function assertInteger(val) {
|
||||
if (typeof val !== 'number' || isNaN(val) || Math.floor(val) !== val) {
|
||||
throw new Error('Value passed is not an integer.');
|
||||
function assertNonNegativeSafeInteger(val) {
|
||||
if (typeof val !== 'number' || isNaN(val) || val < 0 || val > Number.MAX_SAFE_INTEGER || Math.floor(val) !== val) {
|
||||
throw new Error('Value passed is not a non-negative safe integer.');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ exports.encode = function (num) {
|
||||
|
||||
num = Number(num);
|
||||
|
||||
assertInteger(num);
|
||||
assertNonNegativeSafeInteger(num);
|
||||
|
||||
while (num >= base) {
|
||||
modulus = num % base;
|
||||
|
||||
@@ -49,7 +49,7 @@ describe('Base58', function () {
|
||||
assert.throws(function () {
|
||||
base58.encode('hi');
|
||||
}, function (err) {
|
||||
return err.message === 'Value passed is not an integer.';
|
||||
return err.message === 'Value passed is not a non-negative safe integer.';
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -59,7 +59,7 @@ describe('Base58', function () {
|
||||
assert.throws(function () {
|
||||
base58.encode(3.14);
|
||||
}, function (err) {
|
||||
return err.message === 'Value passed is not an integer.';
|
||||
return err.message === 'Value passed is not a non-negative safe integer.';
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user