From f1fccaca0605a5d183f78b2c39d2e09a54753787 Mon Sep 17 00:00:00 2001 From: Artur Neumann Date: Mon, 27 Feb 2017 12:34:15 +0545 Subject: [PATCH 1/2] better quota validation this fixes #3634 1. fixed computerFileSize to be more picky about incorrect values 2. more tests for computerFileSize 3. use computerFileSize to validate user quota Signed-off-by: Artur Neumann --- core/js/js.js | 20 +++++++++++--------- core/js/tests/specs/coreSpec.js | 17 +++++++++++++++-- settings/js/users/users.js | 2 +- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/core/js/js.js b/core/js/js.js index 3651635541a..ba831e4251d 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -1678,16 +1678,12 @@ OC.Util = { * */ computerFileSize: function (string) { - if (typeof string != 'string') { + if (typeof string !== 'string') { return null; } var s = string.toLowerCase(); - var bytes = parseFloat(s) - - if (!isNaN(bytes) && isFinite(s)) { - return bytes; - } + var bytes = null; var bytesArray = { 'b' : 1, @@ -1703,12 +1699,18 @@ OC.Util = { 'p' : 1024 * 1024 * 1024 * 1024 * 1024 }; - var matches = s.match(/([kmgtp]?b?)$/i); - if (matches[1]) { - bytes = bytes * bytesArray[matches[1]]; + var matches = s.match(/^[\s+]?([0-9]*)(\.([0-9]+))?( +)?([kmgtp]?b?)$/i); + if (matches !== null) { + bytes = parseFloat(s); + if (!isFinite(bytes)) { + return null; + } } else { return null; } + if (matches[5]) { + bytes = bytes * bytesArray[matches[5]]; + } bytes = Math.round(bytes); return bytes; diff --git a/core/js/tests/specs/coreSpec.js b/core/js/tests/specs/coreSpec.js index d83c0cd9a38..faafb4797be 100644 --- a/core/js/tests/specs/coreSpec.js +++ b/core/js/tests/specs/coreSpec.js @@ -594,8 +594,14 @@ describe('Core base tests', function() { it('correctly parses file sizes from a human readable formated string', function() { var data = [ ['125', 125], - ['125.25', 125.25], + ['125.25', 125], + ['125.25B', 125], + ['125.25 B', 125], ['0 B', 0], + ['99999999999999999999999999999999999999999999 B', 99999999999999999999999999999999999999999999], + ['0 MB', 0], + ['0 kB', 0], + ['0kB', 0], ['125 B', 125], ['125b', 125], ['125 KB', 128000], @@ -605,7 +611,14 @@ describe('Core base tests', function() { ['119.2 GB', 127990025421], ['119.2gb', 127990025421], ['116.4 TB', 127983153473126], - ['116.4tb', 127983153473126] + ['116.4tb', 127983153473126], + ['8776656778888777655.4tb', 9.650036181387265e+30], + [1234, null], + [-1234, null], + ['-1234 B', null], + ['B', null], + ['40/0', null], + ['40,30 kb', null], ]; for (var i = 0; i < data.length; i++) { expect(OC.Util.computerFileSize(data[i][0])).toEqual(data[i][1]); diff --git a/settings/js/users/users.js b/settings/js/users/users.js index a2ccc059f15..d23bd553246 100644 --- a/settings/js/users/users.js +++ b/settings/js/users/users.js @@ -533,7 +533,7 @@ var UserList = { if (quota === 'other') { return; } - if ((quota !== 'default' && quota !=="none") && (isNaN(parseInt(quota, 10)) || parseInt(quota, 10) < 0)) { + if ((quota !== 'default' && quota !=="none") && (!OC.Util.computerFileSize(quota))) { // the select component has added the bogus value, delete it again $select.find('option[selected]').remove(); OC.Notification.showTemporary(t('core', 'Invalid quota value "{val}"', {val: quota})); From 9790fe7f5d349df4f56c427cf3559d004067eae5 Mon Sep 17 00:00:00 2001 From: Artur Neumann Date: Tue, 28 Feb 2017 08:25:34 +0545 Subject: [PATCH 2/2] make values with white spaces possible a user might enter values with white spaces, and that should be possible and valid Signed-off-by: Artur Neumann --- core/js/js.js | 2 +- core/js/tests/specs/coreSpec.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/core/js/js.js b/core/js/js.js index ba831e4251d..44204f438c5 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -1682,7 +1682,7 @@ OC.Util = { return null; } - var s = string.toLowerCase(); + var s = string.toLowerCase().trim(); var bytes = null; var bytesArray = { diff --git a/core/js/tests/specs/coreSpec.js b/core/js/tests/specs/coreSpec.js index faafb4797be..c9e8a60f2f1 100644 --- a/core/js/tests/specs/coreSpec.js +++ b/core/js/tests/specs/coreSpec.js @@ -619,6 +619,13 @@ describe('Core base tests', function() { ['B', null], ['40/0', null], ['40,30 kb', null], + [' 122.1 MB ', 128031130], + ['122.1 MB ', 128031130], + [' 122.1 MB ', 128031130], + [' 122.1 MB ', 128031130], + ['122.1 MB ', 128031130], + [' 125', 125], + [' 125 ', 125], ]; for (var i = 0; i < data.length; i++) { expect(OC.Util.computerFileSize(data[i][0])).toEqual(data[i][1]);