Merge pull request #3 from louisbuchbinder/vanillajs

remove large or negative number vulnerability
This commit is contained in:
2017-07-09 17:35:05 +01:00
committed by GitHub
3 changed files with 28 additions and 7 deletions

3
.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,27 @@ 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.';
});
});
});
describe('when passed a negative number', function () {
it('throws an error', function () {
assert.throws(function () {
base58.encode(-300);
}, function (err) {
return err.message === 'Value passed is not a non-negative safe integer.';
});
});
});
describe('when passed a non-safe integer', function () {
it('throws an error', function () {
assert.throws(function () {
base58.encode(1E100);
}, function (err) {
return err.message === 'Value passed is not a non-negative safe integer.';
}); });
}); });
}); });