From de4f1c9046cba0f2dc6d33e92d68d787eed00aa5 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Fri, 16 Feb 2018 16:53:48 +0100 Subject: [PATCH] luci-base: rework client side IP validation types and support "hostid" type Signed-off-by: Jo-Philipp Wich --- .../luci-base/htdocs/luci-static/resources/cbi.js | 168 ++++++++++----------- 1 file changed, 77 insertions(+), 91 deletions(-) diff --git a/modules/luci-base/htdocs/luci-static/resources/cbi.js b/modules/luci-base/htdocs/luci-static/resources/cbi.js index b334703bd..d40ec34bc 100644 --- a/modules/luci-base/htdocs/luci-static/resources/cbi.js +++ b/modules/luci-base/htdocs/luci-static/resources/cbi.js @@ -23,6 +23,62 @@ function Dec(x) { return (/^-?\d+(?:\.\d+)?$/.test(x) ? +x : NaN); } +function IPv4(x) { + if (!x.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)) + return null; + + if (RegExp.$1 > 255 || RegExp.$2 > 255 || RegExp.$3 > 255 || RegExp.$4 > 255) + return null; + + return [ +RegExp.$1, +RegExp.$2, +RegExp.$3, +RegExp.$4 ]; +} + +function IPv6(x) { + if (x.match(/^([a-fA-F0-9:]+):(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/)) { + var v6 = RegExp.$1, v4 = IPv4(RegExp.$2); + + if (!v4) + return null; + + x = v6 + ':' + (v4[0] * 256 + v4[1]).toString(16) + + ':' + (v4[2] * 256 + v4[3]).toString(16); + } + + if (!x.match(/^[a-fA-F0-9:]+$/)) + return null; + + var prefix_suffix = x.split(/::/); + + if (prefix_suffix.length > 2) + return null; + + var prefix = (prefix_suffix[0] || '0').split(/:/); + var suffix = prefix_suffix.length > 1 ? (prefix_suffix[1] || '0').split(/:/) : []; + + if (suffix.length ? (prefix.length + suffix.length > 7) : (prefix.length > 8)) + return null; + + var i, word; + var words = []; + + for (i = 0, word = parseInt(prefix[0], 16); i < prefix.length; word = parseInt(prefix[++i], 16)) + if (prefix[i].length <= 4 && !isNaN(word) && word <= 0xFFFF) + words.push(word); + else + return null; + + for (i = 0; i < (8 - prefix.length - suffix.length); i++) + words.push(0); + + for (i = 0, word = parseInt(suffix[0], 16); i < suffix.length; word = parseInt(suffix[++i], 16)) + if (suffix[i].length <= 4 && !isNaN(word) && word <= 0xFFFF) + words.push(word); + else + return null; + + return words; +} + var cbi_validators = { 'integer': function() @@ -53,69 +109,14 @@ var cbi_validators = { 'ip4addr': function() { - if (this.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})(\/(\S+))?$/)) - { - return (RegExp.$1 >= 0) && (RegExp.$1 <= 255) && - (RegExp.$2 >= 0) && (RegExp.$2 <= 255) && - (RegExp.$3 >= 0) && (RegExp.$3 <= 255) && - (RegExp.$4 >= 0) && (RegExp.$4 <= 255) && - ((RegExp.$6.indexOf('.') < 0) - ? ((RegExp.$6 >= 0) && (RegExp.$6 <= 32)) - : (cbi_validators.ip4addr.apply(RegExp.$6))) - ; - } - - return false; + var m = this.match(/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?:\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|\/(\d{1,2}))?$/); + return !!(m && IPv4(m[1]) && (m[2] ? IPv4(m[2]) : (m[3] ? cbi_validators.ip4prefix.apply(m[3]) : true))); }, 'ip6addr': function() { - if( this.match(/^([a-fA-F0-9:.]+)(\/(\d+))?$/) ) - { - if( !RegExp.$2 || ((RegExp.$3 >= 0) && (RegExp.$3 <= 128)) ) - { - var addr = RegExp.$1; - - if( addr == '::' ) - { - return true; - } - - if( addr.indexOf('.') > 0 ) - { - var off = addr.lastIndexOf(':'); - - if( !(off && cbi_validators.ip4addr.apply(addr.substr(off+1))) ) - return false; - - addr = addr.substr(0, off) + ':0:0'; - } - - if( addr.indexOf('::') >= 0 ) - { - var colons = 0; - var fill = '0'; - - for( var i = 1; i < (addr.length-1); i++ ) - if( addr.charAt(i) == ':' ) - colons++; - - if( colons > 7 ) - return false; - - for( var i = 0; i < (7 - colons); i++ ) - fill += ':0'; - - if (addr.match(/^(.*?)::(.*?)$/)) - addr = (RegExp.$1 ? RegExp.$1 + ':' : '') + fill + - (RegExp.$2 ? ':' + RegExp.$2 : ''); - } - - return (addr.match(/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/) != null); - } - } - - return false; + var m = this.match(/^([0-9a-fA-F:.]+)(?:\/(\d{1,3}))?$/); + return !!(m && IPv6(m[1]) && (m[2] ? cbi_validators.ip6prefix.apply(m[2]) : true)); }, 'ip4prefix': function() @@ -136,50 +137,35 @@ var cbi_validators = { 'cidr4': function() { - if (this.match(/^(\S+)\/(\S+)$/)) - { - ip = RegExp.$1; - mask = RegExp.$2; - return cbi_validators.ip4addr.apply(ip) && - cbi_validators.ip4prefix.apply(mask); - } - return false; + var m = this.match(/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(\d{1,2})$/); + return !!(m && IPv4(m[1]) && cbi_validators.ip4prefix.apply(m[2])); }, 'cidr6': function() { - if (this.match(/^(\S+)\/(\S+)$/)) - { - ip = RegExp.$1; - mask = RegExp.$2; - return cbi_validators.ip6addr.apply(ip) && - cbi_validators.ip6prefix.apply(mask); - } - return false; + var m = this.match(/^([0-9a-fA-F:.]+)\/(\d{1,3})$/); + return !!(m && IPv6(m[1]) && cbi_validators.ip6prefix.apply(m[2])); }, 'ipnet4': function() { - if (this.match(/^(\S+)\/(\S+)$/)) - { - ip = RegExp.$1; - net = RegExp.$2; - return cbi_validators.ip4addr.apply(ip) && - cbi_validators.ip4addr.apply(net); - } - return false; + var m = this.match(/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/); + return !!(m && IPv4(m[1]) && IPv4(m[2])); }, 'ipnet6': function() { - if (this.match(/^(\S+)\/(\S+)$/)) - { - ip = RegExp.$1; - net = RegExp.$2; - return cbi_validators.ip6addr.apply(ip) && - cbi_validators.ip6addr.apply(net); - } - return false; + var m = this.match(/^([0-9a-fA-F:.]+)\/([0-9a-fA-F:.]+)$/); + return !!(m && IPv6(m[1]) && IPv6(m[2])); + }, + + 'ip6hostid': function() + { + if (this == "eui64" || this == "random") + return true; + + var v6 = IPv6(this); + return !(!v6 || v6[0] || v6[1] || v6[2] || v6[3]); }, 'ipmask': function() -- 2.11.0