From 882c68117122d9a86f0dc61870ec4752fb6fb608 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Thu, 30 Nov 2017 14:07:31 +1100 Subject: [PATCH 1/3] check unexpected high bits for invalid characters --- lib/bn.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/bn.js b/lib/bn.js index 57c5bb9..944259b 100644 --- a/lib/bn.js +++ b/lib/bn.js @@ -186,6 +186,7 @@ function parseHex (str, start, end) { var r = 0; var len = Math.min(str.length, end); + var z = 0; for (var i = start; i < len; i++) { var c = str.charCodeAt(i) - 48; @@ -206,9 +207,11 @@ b = c; } - assert(c >= 0 && b <= 0xf, 'Invalid character'); r |= b; + z |= b; } + + assert(!(z & 0xf0), 'Invalid character in ' + str); return r; } From 8f9f971e21f7f428d1928a918cc8ceb8c7fbcd64 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Thu, 30 Nov 2017 14:16:13 +1100 Subject: [PATCH 2/3] add more invalid hex tests --- test/constructor-test.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/constructor-test.js b/test/constructor-test.js index 9f27203..385634b 100644 --- a/test/constructor-test.js +++ b/test/constructor-test.js @@ -108,6 +108,22 @@ describe('BN.js/Constructor', function () { res; }, /Invalid character/); }); + + it('should not accept non-hex characters', function () { + [ + '0000000z', + '000000gg', + '0000gg00', + 'ffffggff', + 'ffffggff', + 'hexadecimal' + ].forEach(function (str) { + assert.throws(function () { + var res = new BN(str, 16); + res; + }, /Invalid character in /); + }); + }) }); describe('with Array input', function () { From 4b7109dc4b69a917fafe4f10a9f8eb48bf693ef2 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Thu, 30 Nov 2017 14:34:38 +1100 Subject: [PATCH 3/3] add ./- tests --- test/constructor-test.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/constructor-test.js b/test/constructor-test.js index 385634b..0356b79 100644 --- a/test/constructor-test.js +++ b/test/constructor-test.js @@ -114,8 +114,10 @@ describe('BN.js/Constructor', function () { '0000000z', '000000gg', '0000gg00', - 'ffffggff', - 'ffffggff', + 'fffggfff', + '/0000000', + '0-000000', // if -, is first, that is OK + 'ff.fffff', 'hexadecimal' ].forEach(function (str) { assert.throws(function () { @@ -123,7 +125,7 @@ describe('BN.js/Constructor', function () { res; }, /Invalid character in /); }); - }) + }); }); describe('with Array input', function () {