r51179 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r51178‎ | r51179 | r51180 >
Date:06:55, 30 May 2009
Author:aaron
Status:ok
Tags:
Comment:
(bug 17018) Made IP validation more robust. Also fixed bug that were no range was given if the last ip-like item was invalid.
Modified paths:
  • /trunk/extensions/CheckUser/checkuser.js (modified) (history)

Diff [purge]

Index: trunk/extensions/CheckUser/checkuser.js
@@ -27,33 +27,39 @@
2828 var bin_prefix = 0;
2929 var prefix_cidr = 0;
3030 var prefix = new String( "" );
31 - // Go through each IP in the list, get it's binary form, and track
32 - // the largest binary prefix among them
 31+ // Go through each IP in the list, get it's binary form, and
 32+ // track the largest binary prefix among them...
3333 for( var i=0; i<ips.length; i++ ) {
 34+ var invalid = false;
3435 // ...in the spirit of block.js, call this "addy"
3536 var addy = ips[i];
3637 // Match the first IP in each list (ignore other garbage)
37 - var ipV4 = addy.match(/(^|\b)(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/\d+)?\b/);
 38+ var ipV4 = addy.match(/(^|\b)(\d+\.\d+\.\d+\.\d+)(\/\d+)?\b/);
3839 var ipV6 = addy.match(/(^|\b)(:(:[0-9A-Fa-f]{1,4}){1,7}|[0-9A-Fa-f]{1,4}(:{1,2}[0-9A-Fa-f]{1,4}|::$){1,7})(\/\d+)?\b/);
3940 // Binary form
4041 var bin = new String( "" );
41 - // Rebuilt formatted bin_prefix for each IP
42 - if( ipV4 || ipV6 ) prefix = '';
4342 // Convert the IP to binary form: IPv4
4443 if( ipV4 ) {
4544 var ip = ipV4[2];
4645 var cidr = ipV4[3]; // CIDR, if it exists
4746 // Get each quad integer
4847 var blocs = ip.split('.');
 48+ // IANA 1.0.0.0/8, 2.0.0.0/8
 49+ if( blocs[0] < 3 ) continue;
4950 for( var x=0; x<blocs.length; x++ ) {
5051 bloc = parseInt( blocs[x], 10 );
51 - if( bloc > 255 ) continue; // bad IP!
 52+ if( bloc > 255 ) {
 53+ invalid = true; // bad IP!
 54+ break; // bad IP!
 55+ }
5256 bin_block = bloc.toString(2); // concat bin with binary form of bloc
5357 while( bin_block.length < 8 ) {
5458 bin_block = "0" + bin_block; // pad out as needed
5559 }
5660 bin += bin_block;
5761 }
 62+ if( invalid ) continue; // move to next IP
 63+ prefix = ''; // Rebuild formatted bin_prefix for each IP
5864 // Apply any valid CIDRs
5965 if( cidr ) {
6066 cidr = cidr.match( /\d+$/ )[0]; // get rid of slash
@@ -119,7 +125,10 @@
120126 var blocs = ip.split(':');
121127 for( var x=0; x<=7; x++ ) {
122128 bloc = blocs[x] ? blocs[x] : "0";
123 - if( bloc > "ffff" ) continue; // bad IP!
 129+ if( bloc > "ffff" ) {
 130+ invalid = true; // bad IP!
 131+ break; // bad IP!
 132+ }
124133 int_block = hex2int( bloc ); // convert hex -> int
125134 bin_block = int_block.toString(2); // concat bin with binary form of bloc
126135 while( bin_block.length < 16 ) {
@@ -127,6 +136,8 @@
128137 }
129138 bin += bin_block;
130139 }
 140+ if( invalid ) continue; // move to next IP
 141+ prefix = ''; // Rebuild formatted bin_prefix for each IP
131142 // Apply any valid CIDRs
132143 if( cidr ) {
133144 cidr = cidr.match( /\d+$/ )[0]; // get rid of slash

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r47226(bug 17018) Automated smallest-CIDR calculation from within Checkuser UIaaron19:47, 13 February 2009

Status & tagging log