remove large or negative number vulnerability

This commit is contained in:
louisbuchbinder
2017-07-09 09:14:58 -07:00
parent 786abf351e
commit 280b2a7461
3 changed files with 8 additions and 7 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
.DS_Store .DS_Store
node_modules node_modules
npm-debug.log

View File

@@ -8,9 +8,9 @@ var alphabetLookup = alphabet.split('').reduce(function (lookup, char, index) {
return lookup; return lookup;
}, {}); }, {});
function assertInteger(val) { function assertNonNegativeSafeInteger(val) {
if (typeof val !== 'number' || isNaN(val) || Math.floor(val) !== 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 an integer.'); throw new Error('Value passed is not a non-negative safe integer.');
} }
} }
@@ -33,7 +33,7 @@ exports.encode = function (num) {
num = Number(num); num = Number(num);
assertInteger(num); assertNonNegativeSafeInteger(num);
while (num >= base) { while (num >= base) {
modulus = num % base; modulus = num % base;

View File

@@ -49,7 +49,7 @@ describe('Base58', function () {
assert.throws(function () { assert.throws(function () {
base58.encode('hi'); base58.encode('hi');
}, function (err) { }, 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 () { assert.throws(function () {
base58.encode(3.14); base58.encode(3.14);
}, function (err) { }, function (err) {
return err.message === 'Value passed is not an integer.'; return err.message === 'Value passed is not a non-negative safe integer.';
}); });
}); });
}); });