2015-08-25 19:57:23 +00:00
|
|
|
<?php
|
2017-04-17 14:40:11 +00:00
|
|
|
|
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
2015-08-25 19:57:23 +00:00
|
|
|
/**
|
|
|
|
* Tokenizer for AbuseFilter rules.
|
|
|
|
*/
|
|
|
|
class AbuseFilterTokenizer {
|
|
|
|
/** @var int Tokenizer cache version. Increment this when changing the syntax. **/
|
|
|
|
const CACHE_VERSION = 1;
|
|
|
|
const COMMENT_START_RE = '/\s*\/\*/A';
|
|
|
|
const ID_SYMBOL_RE = '/[0-9A-Za-z_]+/A';
|
2016-04-09 13:35:35 +00:00
|
|
|
const OPERATOR_RE =
|
|
|
|
'/(\!\=\=|\!\=|\!|\*\*|\*|\/|\+|\-|%|&|\||\^|\:\=|\?|\:|\<\=|\<|\>\=|\>|\=\=\=|\=\=|\=)/A';
|
2015-08-25 19:57:23 +00:00
|
|
|
const RADIX_RE = '/([0-9A-Fa-f]+(?:\.\d*)?|\.\d+)([bxo])?/Au';
|
|
|
|
const WHITESPACE = "\011\012\013\014\015\040";
|
|
|
|
|
|
|
|
// Order is important. The punctuation-matching regex requires that
|
2017-07-08 18:49:13 +00:00
|
|
|
// ** comes before *, etc. They are sorted to make it easy to spot
|
|
|
|
// such errors.
|
2017-06-15 14:23:34 +00:00
|
|
|
public static $operators = [
|
2018-04-04 21:14:25 +00:00
|
|
|
// Inequality
|
|
|
|
'!==', '!=', '!',
|
|
|
|
// Multiplication/exponentiation
|
|
|
|
'**', '*',
|
|
|
|
// Other arithmetic
|
|
|
|
'/', '+', '-', '%',
|
|
|
|
// Logic
|
|
|
|
'&', '|', '^',
|
|
|
|
// Setting
|
|
|
|
':=',
|
|
|
|
// Ternary
|
|
|
|
'?', ':',
|
|
|
|
// Less than
|
|
|
|
'<=', '<',
|
|
|
|
// Greater than
|
|
|
|
'>=', '>',
|
|
|
|
// Equality
|
|
|
|
'===', '==', '=',
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
2015-08-25 19:57:23 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
public static $punctuation = [
|
2015-09-28 18:03:35 +00:00
|
|
|
',' => AFPToken::TCOMMA,
|
|
|
|
'(' => AFPToken::TBRACE,
|
|
|
|
')' => AFPToken::TBRACE,
|
|
|
|
'[' => AFPToken::TSQUAREBRACKET,
|
|
|
|
']' => AFPToken::TSQUAREBRACKET,
|
|
|
|
';' => AFPToken::TSTATEMENTSEPARATOR,
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
2015-08-25 19:57:23 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
public static $bases = [
|
2015-08-25 19:57:23 +00:00
|
|
|
'b' => 2,
|
|
|
|
'x' => 16,
|
|
|
|
'o' => 8
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
2015-08-25 19:57:23 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
public static $baseCharsRe = [
|
2015-08-25 19:57:23 +00:00
|
|
|
2 => '/^[01]+$/',
|
|
|
|
8 => '/^[0-8]+$/',
|
|
|
|
16 => '/^[0-9A-Fa-f]+$/',
|
|
|
|
10 => '/^[0-9.]+$/',
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
2015-08-25 19:57:23 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
public static $keywords = [
|
2015-08-25 19:57:23 +00:00
|
|
|
'in', 'like', 'true', 'false', 'null', 'contains', 'matches',
|
|
|
|
'rlike', 'irlike', 'regex', 'if', 'then', 'else', 'end',
|
2017-06-15 14:23:34 +00:00
|
|
|
];
|
2015-08-25 19:57:23 +00:00
|
|
|
|
|
|
|
/**
|
2015-08-25 21:15:13 +00:00
|
|
|
* @param string $code
|
2015-08-25 19:57:23 +00:00
|
|
|
* @return array
|
|
|
|
* @throws AFPException
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public static function tokenize( $code ) {
|
2015-08-25 19:57:23 +00:00
|
|
|
static $tokenizerCache = null;
|
|
|
|
|
|
|
|
if ( !$tokenizerCache ) {
|
2016-10-11 21:35:26 +00:00
|
|
|
$tokenizerCache = ObjectCache::getLocalServerInstance( 'hash' );
|
2015-08-25 19:57:23 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 20:29:53 +00:00
|
|
|
static $stats = null;
|
|
|
|
|
|
|
|
if ( !$stats ) {
|
2017-04-17 14:40:11 +00:00
|
|
|
$stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
|
2015-10-22 20:29:53 +00:00
|
|
|
}
|
|
|
|
|
2015-08-25 19:57:23 +00:00
|
|
|
$cacheKey = wfGlobalCacheKey( __CLASS__, self::CACHE_VERSION, crc32( $code ) );
|
2015-10-22 20:29:53 +00:00
|
|
|
|
2015-08-25 19:57:23 +00:00
|
|
|
$tokens = $tokenizerCache->get( $cacheKey );
|
|
|
|
|
2015-10-22 20:29:53 +00:00
|
|
|
if ( $tokens ) {
|
|
|
|
$stats->increment( 'AbuseFilter.tokenizerCache.hit' );
|
|
|
|
return $tokens;
|
|
|
|
}
|
2015-08-25 19:57:23 +00:00
|
|
|
|
2015-10-22 20:29:53 +00:00
|
|
|
$stats->increment( 'AbuseFilter.tokenizerCache.miss' );
|
2017-06-15 14:23:34 +00:00
|
|
|
$tokens = [];
|
2015-10-22 20:29:53 +00:00
|
|
|
$curPos = 0;
|
2015-08-25 19:57:23 +00:00
|
|
|
|
2015-10-22 20:29:53 +00:00
|
|
|
do {
|
|
|
|
$prevPos = $curPos;
|
|
|
|
$token = self::nextToken( $code, $curPos );
|
2017-06-15 14:23:34 +00:00
|
|
|
$tokens[ $token->pos ] = [ $token, $curPos ];
|
2015-10-22 20:29:53 +00:00
|
|
|
} while ( $curPos !== $prevPos );
|
|
|
|
|
2015-10-24 17:27:14 +00:00
|
|
|
$tokenizerCache->set( $cacheKey, $tokens, 60 * 60 * 24 );
|
2015-08-25 19:57:23 +00:00
|
|
|
|
|
|
|
return $tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-25 21:15:13 +00:00
|
|
|
* @param string $code
|
2017-08-04 23:14:10 +00:00
|
|
|
* @param int &$offset
|
2015-08-25 19:57:23 +00:00
|
|
|
* @return AFPToken
|
|
|
|
* @throws AFPException
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
|
|
|
protected static function nextToken( $code, &$offset ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
$matches = [];
|
2015-08-25 19:57:23 +00:00
|
|
|
$start = $offset;
|
|
|
|
|
|
|
|
// Read past comments
|
|
|
|
while ( preg_match( self::COMMENT_START_RE, $code, $matches, 0, $offset ) ) {
|
2018-04-10 17:26:02 +00:00
|
|
|
if ( strpos( $code, '*/', $offset ) === false ) {
|
|
|
|
throw new AFPUserVisibleException(
|
|
|
|
'unclosedcomment', $offset, [] );
|
|
|
|
}
|
2015-08-25 19:57:23 +00:00
|
|
|
$offset = strpos( $code, '*/', $offset ) + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Spaces
|
|
|
|
$offset += strspn( $code, self::WHITESPACE, $offset );
|
|
|
|
if ( $offset >= strlen( $code ) ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPToken( AFPToken::TNONE, '', $start );
|
2015-08-25 19:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$chr = $code[$offset];
|
|
|
|
|
|
|
|
// Punctuation
|
|
|
|
if ( isset( self::$punctuation[$chr] ) ) {
|
|
|
|
$offset++;
|
|
|
|
return new AFPToken( self::$punctuation[$chr], $chr, $start );
|
|
|
|
}
|
|
|
|
|
|
|
|
// String literal
|
|
|
|
if ( $chr === '"' || $chr === "'" ) {
|
|
|
|
return self::readStringLiteral( $code, $offset, $start );
|
|
|
|
}
|
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
$matches = [];
|
2015-08-25 19:57:23 +00:00
|
|
|
|
|
|
|
// Operators
|
|
|
|
if ( preg_match( self::OPERATOR_RE, $code, $matches, 0, $offset ) ) {
|
|
|
|
$token = $matches[0];
|
|
|
|
$offset += strlen( $token );
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPToken( AFPToken::TOP, $token, $start );
|
2015-08-25 19:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Numbers
|
|
|
|
if ( preg_match( self::RADIX_RE, $code, $matches, 0, $offset ) ) {
|
|
|
|
$token = $matches[0];
|
|
|
|
$input = $matches[1];
|
2015-10-21 23:52:14 +00:00
|
|
|
$baseChar = isset( $matches[2] ) ? $matches[2] : null;
|
2015-08-25 19:57:23 +00:00
|
|
|
// Sometimes the base char gets mixed in with the rest of it because
|
|
|
|
// the regex targets hex, too.
|
|
|
|
// This mostly happens with binary
|
|
|
|
if ( !$baseChar && !empty( self::$bases[ substr( $input, - 1 ) ] ) ) {
|
|
|
|
$baseChar = substr( $input, - 1, 1 );
|
|
|
|
$input = substr( $input, 0, - 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
$base = $baseChar ? self::$bases[$baseChar] : 10;
|
|
|
|
|
|
|
|
// Check against the appropriate character class for input validation
|
|
|
|
|
|
|
|
if ( preg_match( self::$baseCharsRe[$base], $input ) ) {
|
|
|
|
$num = $base !== 10 ? base_convert( $input, $base, 10 ) : $input;
|
|
|
|
$offset += strlen( $token );
|
|
|
|
return ( strpos( $input, '.' ) !== false )
|
2015-09-28 18:03:35 +00:00
|
|
|
? new AFPToken( AFPToken::TFLOAT, floatval( $num ), $start )
|
|
|
|
: new AFPToken( AFPToken::TINT, intval( $num ), $start );
|
2015-08-25 19:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IDs / Keywords
|
|
|
|
|
|
|
|
if ( preg_match( self::ID_SYMBOL_RE, $code, $matches, 0, $offset ) ) {
|
|
|
|
$token = $matches[0];
|
|
|
|
$offset += strlen( $token );
|
|
|
|
$type = in_array( $token, self::$keywords )
|
2015-09-28 18:03:35 +00:00
|
|
|
? AFPToken::TKEYWORD
|
2015-08-25 19:57:23 +00:00
|
|
|
: AFPToken::TID;
|
|
|
|
return new AFPToken( $type, $token, $start );
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new AFPUserVisibleException(
|
2017-06-15 14:23:34 +00:00
|
|
|
'unrecognisedtoken', $start, [ substr( $code, $start ) ] );
|
2015-08-25 19:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-25 21:15:13 +00:00
|
|
|
* @param string $code
|
|
|
|
* @param int &$offset
|
|
|
|
* @param int $start
|
|
|
|
* @return AFPToken
|
2015-08-25 19:57:23 +00:00
|
|
|
* @throws AFPException
|
|
|
|
* @throws AFPUserVisibleException
|
|
|
|
*/
|
|
|
|
protected static function readStringLiteral( $code, &$offset, $start ) {
|
|
|
|
$type = $code[$offset];
|
|
|
|
$offset++;
|
|
|
|
$length = strlen( $code );
|
|
|
|
$token = '';
|
|
|
|
while ( $offset < $length ) {
|
|
|
|
if ( $code[$offset] === $type ) {
|
|
|
|
$offset++;
|
2015-09-28 18:03:35 +00:00
|
|
|
return new AFPToken( AFPToken::TSTRING, $token, $start );
|
2015-08-25 19:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Performance: Use a PHP function (implemented in C)
|
|
|
|
// to scan ahead.
|
|
|
|
$addLength = strcspn( $code, $type . "\\", $offset );
|
|
|
|
if ( $addLength ) {
|
|
|
|
$token .= substr( $code, $offset, $addLength );
|
|
|
|
$offset += $addLength;
|
|
|
|
} elseif ( $code[$offset] == '\\' ) {
|
2015-09-28 18:03:35 +00:00
|
|
|
switch ( $code[$offset + 1] ) {
|
2015-08-25 19:57:23 +00:00
|
|
|
case '\\':
|
|
|
|
$token .= '\\';
|
|
|
|
break;
|
|
|
|
case $type:
|
|
|
|
$token .= $type;
|
|
|
|
break;
|
|
|
|
case 'n';
|
|
|
|
$token .= "\n";
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
$token .= "\r";
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
$token .= "\t";
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
$chr = substr( $code, $offset + 2, 2 );
|
|
|
|
|
|
|
|
if ( preg_match( '/^[0-9A-Fa-f]{2}$/', $chr ) ) {
|
|
|
|
$chr = base_convert( $chr, 16, 10 );
|
|
|
|
$token .= chr( $chr );
|
2018-04-04 21:14:25 +00:00
|
|
|
// \xXX -- 2 done later
|
|
|
|
$offset += 2;
|
2015-08-25 19:57:23 +00:00
|
|
|
} else {
|
|
|
|
$token .= 'x';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$token .= "\\" . $code[$offset + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
$offset += 2;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$token .= $code[$offset];
|
|
|
|
$offset++;
|
|
|
|
}
|
|
|
|
}
|
2017-06-15 14:23:34 +00:00
|
|
|
throw new AFPUserVisibleException( 'unclosedstring', $offset, [] );
|
2015-08-25 19:57:23 +00:00
|
|
|
}
|
|
|
|
}
|