x in the same function: that is the AI * audit's job. * * @package ShieldXen */ declare(strict_types=1); namespace ShieldXen\Code; defined( 'ABSPATH' ) || exit; final class Taint { private const ASSIGN = [ T_CONCAT_EQUAL, T_COALESCE_EQUAL ]; private const BOOL_OPS = [ T_IS_EQUAL, T_IS_IDENTICAL, T_IS_NOT_EQUAL, T_IS_NOT_IDENTICAL, T_IS_SMALLER_OR_EQUAL, T_IS_GREATER_OR_EQUAL, T_SPACESHIP, T_BOOLEAN_AND, T_BOOLEAN_OR, T_LOGICAL_AND, T_LOGICAL_OR, T_LOGICAL_XOR, T_INSTANCEOF ]; private const KILL_CASTS = [ T_INT_CAST, T_DOUBLE_CAST, T_BOOL_CAST ]; private const NOT_A_CALL = [ T_FUNCTION, T_OBJECT_OPERATOR, T_DOUBLE_COLON, T_NEW, T_CONST ]; private Source $s; /** @var array{kind: string, name: string, class: ?string, params: array, decl: int, start: int, end: int, line: int} */ private array $unit; /** @var array{rest?: bool, public?: bool, attr?: bool, block?: bool} */ private array $role; /** @var array Variable (or $this->prop) => its request bits. */ private array $taint = []; /** @var array REST request variables. */ private array $requests = []; /** How deep mask() may nest: far past any real code. */ private const MAX_DEPTH = 200; private int $depth = 0; /** Whether an expression nested past MAX_DEPTH, so the function wasn't followed to the end. */ public bool $too_deep = false; /** @var array Expressions the function checks against known values. */ private array $checked = []; /** @var array Variables holding an array whose 'role' item carries request bits (CODE-012). */ private array $roles = []; /** @var array Variables holding a whole request array ($_POST). */ private array $whole = []; private bool $password = false; private bool $contained = false; private bool $json = false; /** @var array */ private array $leads = []; private int $nullsafe; /** * @param array{kind: string, name: string, class: ?string, params: array, decl: int, start: int, end: int, line: int} $unit The function. * @param array{rest?: bool, public?: bool, attr?: bool, block?: bool, render_file?: bool} $role What calls it: a REST route, a shortcode or block render, a public handler. */ public function __construct( Source $s, array $unit, array $role ) { $this->s = $s; $this->unit = $unit; $this->role = $role; $this->nullsafe = defined( 'T_NULLSAFE_OBJECT_OPERATOR' ) ? (int) constant( 'T_NULLSAFE_OBJECT_OPERATOR' ) : -2; } /** * @return array */ public function run(): array { $this->prepare(); $s = $this->s; $depth = 0; $end = $this->unit['end']; if ( 'arrow' === $this->unit['kind'] && ! empty( $this->role['attr'] ) ) { // fn( $atts ) => '' . $atts['x'] . '': the expression is what it returns. $this->page( $this->unit['start'], $end, $this->unit['start'], 'return' ); } for ( $i = $this->unit['start']; $i < $end; $i++ ) { if ( isset( $s->skip[ $i ] ) ) { $i = $s->skip[ $i ] - 1; continue; } $id = $s->id( $i ); if ( $id === $this->nullsafe ) { $id = T_OBJECT_OPERATOR; } if ( Source::CHAR === $id ) { if ( $s->is( $i, '{' ) ) { ++$depth; } elseif ( $s->is( $i, '}' ) && $s->is( $s->close( $i ), '{' ) ) { --$depth; } continue; } switch ( $id ) { case T_VARIABLE: $this->variable( $i, $depth, $end ); break; case T_LIST: $this->destructure( $i, $end ); break; case T_FOREACH: $this->each( $i ); break; case T_ECHO: case T_PRINT: case T_OPEN_TAG_WITH_ECHO: $this->echoed( $i, $end ); break; case T_EXIT: if ( $s->is( $i + 1, '(' ) ) { $this->page( $i + 2, $s->close( $i + 1 ), $i, 'exit' ); } break; case T_RETURN: if ( ! empty( $this->role['attr'] ) ) { $this->page( $i + 1, $s->expr_end( $i + 1, $end ), $i, 'return' ); } break; case T_EVAL: if ( $s->is( $i + 1, '(' ) && ( $this->mask( $i + 2, $s->close( $i + 1 ) ) & Rules::CODE ) ) { $this->lead( 'CODE-004', 'code-exec-dynamic', $i, 'eval()' ); } break; case T_INCLUDE: case T_INCLUDE_ONCE: case T_REQUIRE: case T_REQUIRE_ONCE: $this->path( $i + 1, $s->expr_end( $i + 1, $end ), $i, strtolower( $s->text( $i ) ), 'CODE-007', 'path-traversal-include' ); break; case T_STRING: if ( $s->is( $i + 1, '(' ) && ! in_array( $s->id( $i - 1 ), self::NOT_A_CALL, true ) && $s->id( $i - 1 ) !== $this->nullsafe ) { $this->call( $i, strtolower( $s->text( $i ) ), $i + 1 ); } break; case T_OBJECT_OPERATOR: if ( T_STRING === $s->id( $i + 1 ) && $s->is( $i + 2, '(' ) ) { $this->method( $i, strtolower( $s->text( $i + 1 ) ), $i + 2 ); } elseif ( T_VARIABLE === $s->id( $i + 1 ) && $s->is( $i + 2, '(' ) && ( $this->var_bits( $s->text( $i + 1 ) ) & Rules::CODE ) ) { $this->lead( 'CODE-004', 'code-exec-dynamic', $i, '->' . $s->text( $i + 1 ) . '()' ); } break; } } return $this->leads; } /** * Parameters, and what the function checks anywhere in its body. */ private function prepare(): void { $s = $this->s; $params = $this->unit['params']; foreach ( $params as $param ) { if ( 'wp_rest_request' === strtolower( ltrim( $param['type'], '\\' ) ) ) { $this->requests[ $param['name'] ] = true; } } if ( isset( $params[0] ) ) { if ( ! empty( $this->role['rest'] ) ) { $this->requests[ $params[0]['name'] ] = true; } if ( ! empty( $this->role['attr'] ) ) { $this->taint[ $params[0]['name'] ] = Rules::ATTR; } } if ( ! empty( $this->role['render_file'] ) ) { $this->taint['$attributes'] = Rules::ATTR; $this->role['attr'] = true; $this->role['block'] = true; } $realpath = false; $prefix = false; for ( $i = $this->unit['start']; $i < $this->unit['end']; $i++ ) { if ( isset( $s->skip[ $i ] ) ) { $i = $s->skip[ $i ] - 1; continue; } $id = $s->id( $i ); if ( T_SWITCH === $id && $s->is( $i + 1, '(' ) ) { $this->checked[ $s->norm( $i + 2, $s->close( $i + 1 ) ) ] = true; continue; } if ( T_ISSET === $id && $s->is( $i + 1, '(' ) ) { // isset( $allowed[ $x ] ): $x is one of the array's keys. $close = $s->close( $i + 1 ); for ( $k = $i + 2; $k < $close; $k++ ) { if ( $s->is( $k, '[' ) ) { if ( T_VARIABLE === $s->id( $k + 1 ) ) { $this->checked[ $s->norm( $k + 1, $s->close( $k ) ) ] = true; } $k = $s->close( $k ); } } continue; } if ( T_STRING !== $id || ! $s->is( $i + 1, '(' ) ) { if ( T_CONSTANT_ENCAPSED_STRING === $id && false !== stripos( $s->text( $i ), 'application/json' ) ) { $this->json = true; } continue; } $fn = strtolower( $s->text( $i ) ); $args = $s->args( $i + 1 ); if ( in_array( $fn, Rules::VALIDATORS, true ) && isset( $args[0] ) ) { $this->checked[ $s->norm( $args[0][0], $args[0][1] ) ] = true; } elseif ( 'preg_match' === $fn && isset( $args[1] ) ) { $pattern = $s->only_literal( $args[0] ); if ( null !== $pattern && false !== strpos( $pattern, '^' ) && false !== strpos( $pattern, '$' ) ) { $this->checked[ $s->norm( $args[1][0], $args[1][1] ) ] = true; } } elseif ( in_array( $fn, Rules::PASSWORD_CHECKS, true ) ) { $this->password = true; } elseif ( 'realpath' === $fn ) { $realpath = true; } elseif ( in_array( $fn, [ 'strpos', 'stripos', 'str_starts_with', 'strncmp', 'substr' ], true ) ) { $prefix = true; } elseif ( in_array( $fn, [ 'wp_send_json', 'wp_send_json_success', 'wp_send_json_error' ], true ) ) { $this->json = true; } } $this->contained = $realpath && $prefix; } /** * A variable: an assignment to it, or a call through it. */ private function variable( int $i, int $depth, int $end ): void { $s = $this->s; $name = $s->text( $i ); $prev = $s->id( $i - 1 ); if ( T_DOUBLE_COLON === $prev || T_OBJECT_OPERATOR === $prev || $prev === $this->nullsafe || $s->is( $i - 1, '$' ) ) { return; } if ( $s->is( $i + 1, '(' ) ) { if ( ( $this->var_bits( $name ) & Rules::CODE ) && ! isset( $this->checked[ $name ] ) ) { $this->lead( 'CODE-004', 'code-exec-dynamic', $i, $name . '()' ); } return; } // What is assigned to: a variable, one of its items or properties, or a property of $this. $key = $name; $indexed = false; $index = null; $j = $i + 1; while ( $j < $end ) { if ( $s->is( $j, '[' ) ) { if ( null === $index ) { $index = $s->only_literal( [ $j + 1, $s->close( $j ) ] ); } $indexed = true; $j = $s->close( $j ) + 1; continue; } if ( ( T_OBJECT_OPERATOR === $s->id( $j ) || $s->id( $j ) === $this->nullsafe ) && T_STRING === $s->id( $j + 1 ) && ! $s->is( $j + 2, '(' ) ) { if ( '$this' === $name && $key === $name && ! $indexed ) { $key .= '->' . $s->text( $j + 1 ); } else { $indexed = true; } $j += 2; continue; } break; } if ( '$this' === $key ) { return; } $op = $s->id( $j ); if ( ! $s->is( $j, '=' ) && ! in_array( $op, self::ASSIGN, true ) ) { return; } $from = $j + 1; if ( $s->is( $from, '&' ) ) { ++$from; } $to = $s->expr_end( $from, $end ); $bits = $this->mask( $from, $to ); if ( $s->is( $j, '=' ) && ! $indexed && 0 === $depth ) { $this->set( $key, $bits ); unset( $this->roles[ $key ], $this->whole[ $key ] ); } else { $this->set( $key, $this->var_bits( $key ) | $bits ); } // What CODE-012 needs about arrays given to wp_update_user() and wp_insert_user(). if ( 'role' === $index ) { $this->roles[ $key ] = ( $this->roles[ $key ] ?? 0 ) | $bits; } elseif ( ! $indexed ) { $open = $s->array_open( $from ); if ( null !== $open && $s->close( $open ) + 1 === $to ) { foreach ( $s->items( $open ) as $item ) { if ( 'role' === $item['key'] ) { $this->roles[ $key ] = $this->mask( $item['value'][0], $item['value'][1] ); } } } if ( $this->whole_request( $from, $to ) ) { $this->whole[ $key ] = true; } } } private function set( string $key, int $bits ): void { if ( 0 === $bits ) { unset( $this->taint[ $key ] ); return; } $this->taint[ $key ] = $bits; } private function var_bits( string $key ): int { return isset( $this->checked[ $key ] ) ? 0 : ( $this->taint[ $key ] ?? 0 ); } /** * list( $a, $b ) = … */ private function destructure( int $i, int $end ): void { $s = $this->s; if ( ! $s->is( $i + 1, '(' ) || ! $s->is( $s->close( $i + 1 ) + 1, '=' ) ) { return; } $close = $s->close( $i + 1 ); $from = $close + 2; $bits = $this->mask( $from, $s->expr_end( $from, $end ) ); for ( $k = $i + 2; $k < $close; $k++ ) { if ( T_VARIABLE === $s->id( $k ) ) { $this->set( $s->text( $k ), $this->var_bits( $s->text( $k ) ) | $bits ); } } } /** * foreach ( … as $k => $v ): both carry what the array carries. */ private function each( int $i ): void { $s = $this->s; if ( ! $s->is( $i + 1, '(' ) ) { return; } $close = $s->close( $i + 1 ); $as = $s->expr_end( $i + 2, $close ); if ( T_AS !== $s->id( $as ) ) { return; } $bits = $this->mask( $i + 2, $as ); if ( 0 === $bits ) { return; } for ( $k = $as + 1; $k < $close; $k++ ) { if ( T_VARIABLE === $s->id( $k ) && T_OBJECT_OPERATOR !== $s->id( $k - 1 ) ) { $this->set( $s->text( $k ), $this->var_bits( $s->text( $k ) ) | $bits ); } } } private function echoed( int $i, int $end ): void { $s = $this->s; $start = $i + 1; while ( $start < $end ) { $stop = $s->expr_end( $start, $end ); $this->page( $start, $stop, $i, T_PRINT === $s->id( $i ) ? 'print' : 'echo' ); if ( T_PRINT === $s->id( $i ) || ! $s->is( $stop, ',' ) ) { break; } $start = $stop + 1; } } /** * Something printed into the page: CODE-015 for request data, CODE-018 for an attribute. */ private function page( int $a, int $b, int $at, string $what ): void { if ( ! empty( $this->role['rest'] ) || $this->json ) { return; } $bits = $this->mask( $a, $b ); if ( $bits & Rules::XSS ) { $this->lead( 'CODE-015', 'xss-reflected', $at, $what ); } if ( ( $bits & Rules::ATTR ) && ! empty( $this->role['attr'] ) ) { $this->lead( 'CODE-018', empty( $this->role['block'] ) ? 'xss-shortcode-attr' : 'xss-block-attr', $at, $what ); } } /** * A file path: medium when it can climb folders, low when basename() kept it in one. */ private function path( int $a, int $b, int $at, string $what, string $check, string $kb ): void { if ( $this->contained ) { return; } $bits = $this->mask( $a, $b ); if ( $bits & Rules::PATH ) { $this->lead( $check, $kb, $at, $what ); } elseif ( $bits & Rules::PATHLOW ) { $this->lead( $check, $kb, $at, $what, 'low' ); } } private function call( int $i, string $callee, int $open ): void { $s = $this->s; $args = $s->args( $open ); $arg0 = $args[0] ?? null; $what = $callee . '()'; switch ( $callee ) { case 'unserialize': case 'maybe_unserialize': if ( null !== $arg0 && ( $this->mask( $arg0[0], $arg0[1] ) & Rules::UNSER ) ) { $this->lead( 'CODE-003', 'php-object-injection', $i, $what ); } return; case 'assert': case 'call_user_func': case 'call_user_func_array': if ( null !== $arg0 && ( $this->mask( $arg0[0], $arg0[1] ) & Rules::CODE ) ) { $this->lead( 'CODE-004', 'code-exec-dynamic', $i, $what ); } return; case 'create_function': if ( $this->args_mask( $args ) & Rules::CODE ) { $this->lead( 'CODE-004', 'code-exec-dynamic', $i, $what ); } return; case 'printf': case 'vprintf': $this->page( $open + 1, $s->close( $open ), $i, $what ); return; case '_e': case 'wp_die': if ( null !== $arg0 ) { $this->page( $arg0[0], $arg0[1], $i, $what ); } return; case 'print_r': if ( null !== $arg0 && 1 === count( $args ) ) { $this->page( $arg0[0], $arg0[1], $i, $what ); } return; case 'update_option': case 'update_site_option': if ( null !== $arg0 && ( $this->mask( $arg0[0], $arg0[1] ) & Rules::OPTION ) ) { $this->lead( 'CODE-012', 'option-update-arbitrary', $i, $what ); } return; case 'update_user_meta': case 'add_user_meta': case 'update_user_option': if ( isset( $args[1] ) && ( $this->mask( $args[1][0], $args[1][1] ) & Rules::OPTION ) ) { $this->lead( 'CODE-012', 'privesc-user-role', $i, $what ); } return; case 'wp_update_user': case 'wp_insert_user': if ( null !== $arg0 && $this->role_from_request( $arg0 ) ) { $this->lead( 'CODE-012', 'privesc-user-role', $i, $what ); } return; case 'file_get_contents': if ( null === $arg0 ) { return; } if ( $this->is_url( $arg0 ) ) { $this->fetch( $arg0, $i, $what ); return; } $this->path( $arg0[0], $arg0[1], $i, $what, 'CODE-013', 'file-read-delete' ); return; case 'unlink': case 'wp_delete_file': case 'readfile': case 'fopen': case 'file': case 'fpassthru': if ( null !== $arg0 ) { $this->path( $arg0[0], $arg0[1], $i, $what, 'CODE-013', 'file-read-delete' ); } return; case 'wp_remote_get': case 'wp_remote_post': case 'wp_remote_head': case 'wp_remote_request': case 'download_url': case 'curl_init': if ( null !== $arg0 ) { $this->fetch( $arg0, $i, $what ); } return; case 'curl_setopt': if ( isset( $args[2] ) && 'CURLOPT_URL' === $s->norm( $args[1][0], $args[1][1] ) ) { $this->fetch( $args[2], $i, 'curl_setopt( CURLOPT_URL )' ); } return; case 'wp_set_auth_cookie': case 'wp_set_current_user': if ( null !== $arg0 && ! $this->password && ( $this->mask( $arg0[0], $arg0[1] ) & Rules::AUTH ) ) { $this->lead( 'CODE-016', 'apis/passwords-auth', $i, $what ); } return; case 'parse_str': if ( null !== $arg0 && isset( $args[1] ) && T_VARIABLE === $s->id( $args[1][0] ) ) { $key = $s->text( $args[1][0] ); $this->set( $key, $this->var_bits( $key ) | $this->mask( $arg0[0], $arg0[1] ) ); } return; } } /** * $obj->name( … ): $wpdb's queries, $wp_filesystem's reads, and role changes. */ private function method( int $arrow, string $name, int $open ): void { $s = $this->s; $args = $s->args( $open ); $arg0 = $args[0] ?? null; if ( null === $arg0 ) { return; } $object = $s->text( $arrow - 1 ); $wpdb = '$wpdb' === $object || ( 'wpdb' === $object && T_OBJECT_OPERATOR === $s->id( $arrow - 2 ) ); if ( $wpdb && in_array( $name, [ 'query', 'get_results', 'get_var', 'get_row', 'get_col' ], true ) ) { // $wpdb->get_results( $wpdb->prepare( … ) ) is the prepare() call's to report. if ( ! $this->starts_with_prepare( $arg0 ) ) { $this->sql( $arg0, $arrow, '$wpdb->' . $name . '()', false ); } return; } if ( $wpdb && 'prepare' === $name ) { $this->sql( $arg0, $arrow, '$wpdb->prepare()', true ); return; } if ( '$wp_filesystem' === $object && in_array( $name, [ 'delete', 'get_contents', 'get_contents_array' ], true ) ) { $this->path( $arg0[0], $arg0[1], $arrow, '$wp_filesystem->' . $name . '()', 'CODE-013', 'file-read-delete' ); return; } if ( in_array( $name, [ 'set_role', 'add_role', 'add_cap' ], true ) && ( $this->mask( $arg0[0], $arg0[1] ) & Rules::OPTION ) ) { $this->lead( 'CODE-012', 'privesc-user-role', $arrow, '->' . $name . '()' ); } } /** * @param array{0: int, 1: int} $range The SQL argument. */ private function sql( array $range, int $at, string $what, bool $prepare ): void { $bits = $this->mask( $range[0], $range[1] ); if ( $bits & Rules::SQL ) { $this->lead( 'CODE-005', $prepare ? 'sqli-orderby-in' : 'sqli-unprepared', $at, $what ); } elseif ( ( $bits & Rules::SQLRAW ) && $this->raw_sql( $range[0], $range[1] ) ) { $this->lead( 'CODE-005', 'sqli-orderby-in', $at, $what ); } } /** * Whether escaped request data sits outside quotes in the SQL: after a comparison, ORDER BY, IN (, * LIMIT and the like. */ private function raw_sql( int $a, int $b ): bool { $s = $this->s; $before = ''; for ( $k = $a; $k < $b; $k++ ) { $id = $s->id( $k ); if ( T_CONSTANT_ENCAPSED_STRING === $id ) { $before = (string) $s->literal( $k ); continue; } if ( T_ENCAPSED_AND_WHITESPACE === $id ) { $before = $s->text( $k ); continue; } if ( T_VARIABLE === $id || ( T_STRING === $id && $s->is( $k + 1, '(' ) ) ) { $stop = $this->term_end( $k, $b ); if ( ( $this->mask( $k, $stop ) & Rules::SQLRAW ) && Rules::raw_sql_context( $before ) ) { return true; } $before = ''; $k = $stop - 1; } } return false; } /** * @param array{0: int, 1: int} $range The URL argument. */ private function fetch( array $range, int $at, string $what ): void { if ( $this->mask( $range[0], $range[1] ) & Rules::URL ) { $this->lead( 'CODE-014', 'ssrf-remote-request', $at, $what, empty( $this->role['public'] ) ? 'low' : 'medium' ); } } /** * @param array{0: int, 1: int} $range An argument. */ private function is_url( array $range ): bool { $s = $this->s; $first = $s->literal( $range[0] ); if ( null === $first && T_ENCAPSED_AND_WHITESPACE === $s->id( $range[0] + 1 ) && $s->is( $range[0], '"' ) ) { $first = $s->text( $range[0] + 1 ); } return null !== $first && 1 === preg_match( '#^(?:https?:)?//#i', $first ); } /** * @param array{0: int, 1: int} $range The user data argument. */ private function role_from_request( array $range ): bool { $s = $this->s; $open = $s->array_open( $range[0] ); if ( null !== $open && $s->close( $open ) + 1 === $range[1] ) { foreach ( $s->items( $open ) as $item ) { if ( 'role' === $item['key'] && ( $this->mask( $item['value'][0], $item['value'][1] ) & Rules::OPTION ) ) { return true; } } return false; } if ( 1 === $range[1] - $range[0] && T_VARIABLE === $s->id( $range[0] ) ) { $var = $s->text( $range[0] ); return ( ( $this->roles[ $var ] ?? 0 ) & Rules::OPTION ) || isset( $this->whole[ $var ] ); } return $this->whole_request( $range[0], $range[1] ); } /** * Whether an expression passes a whole request array on ($_POST, not $_POST['x']). */ private function whole_request( int $a, int $b ): bool { $s = $this->s; for ( $k = $a; $k < $b; $k++ ) { if ( T_VARIABLE === $s->id( $k ) && in_array( $s->text( $k ), Rules::SUPERGLOBALS, true ) && ! $s->is( $k + 1, '[' ) ) { return true; } } return false; } /** * @param array{0: int, 1: int} $range An argument. */ private function starts_with_prepare( array $range ): bool { $s = $this->s; for ( $k = $range[0]; $k < $range[1]; $k++ ) { $id = $s->id( $k ); if ( T_OBJECT_OPERATOR === $id && 'prepare' === strtolower( $s->text( $k + 1 ) ) ) { return true; } if ( T_VARIABLE !== $id && T_STRING !== $id && T_OBJECT_OPERATOR !== $id ) { return false; } } return false; } /** * The request bits an expression [a, b) carries. Nesting deeper than MAX_DEPTH stops the walk and * marks the function too deep, so a file of a hundred thousand brackets can't use up the memory of * every scan tick that reads it (R13). */ public function mask( int $a, int $b ): int { if ( $this->depth >= self::MAX_DEPTH ) { $this->too_deep = true; return 0; } ++$this->depth; try { return $this->mask_at( $a, $b ); } finally { --$this->depth; } } private function mask_at( int $a, int $b ): int { $s = $this->s; $question = null; $colon = null; $coalesce = null; $bool = false; $arith = false; $concat = false; for ( $k = $a; $k < $b; $k++ ) { if ( isset( $s->match[ $k ] ) && $s->match[ $k ] > $k ) { $k = $s->match[ $k ]; continue; } if ( isset( $s->skip[ $k ] ) ) { $k = $s->skip[ $k ] - 1; continue; } $id = $s->id( $k ); if ( Source::CHAR === $id ) { $char = $s->text( $k ); if ( '?' === $char && null === $question ) { $question = $k; } elseif ( ':' === $char && null !== $question && null === $colon ) { $colon = $k; } elseif ( '.' === $char ) { $concat = true; } elseif ( in_array( $char, [ '+', '-', '*', '/', '%', '&', '|', '^' ], true ) ) { $arith = true; } elseif ( in_array( $char, [ '<', '>', '!' ], true ) ) { $bool = true; } continue; } if ( T_COALESCE === $id && null === $coalesce ) { $coalesce = $k; } elseif ( in_array( $id, self::BOOL_OPS, true ) ) { $bool = true; } elseif ( in_array( $id, [ T_POW, T_SL, T_SR ], true ) ) { $arith = true; } } if ( null !== $question && null !== $colon ) { if ( $colon === $question + 1 ) { return $this->mask( $a, $question ) | $this->mask( $colon + 1, $b ); } $then = $this->mask( $question + 1, $colon ); if ( 0 !== $then && $this->guarded( $a, $question, $s->norm( $question + 1, $colon ) ) ) { $then = 0; } return $then | $this->mask( $colon + 1, $b ); } if ( null !== $coalesce ) { return $this->mask( $a, $coalesce ) | $this->mask( $coalesce + 1, $b ); } if ( $bool || ( $arith && ! $concat ) ) { return 0; } return $this->terms( $a, $b ); } /** * Whether a ternary's condition limits the value its true branch uses: in_array( $x, … ) ? $x : …. */ private function guarded( int $a, int $b, string $value ): bool { $s = $this->s; for ( $k = $a; $k < $b; $k++ ) { if ( T_STRING === $s->id( $k ) && $s->is( $k + 1, '(' ) && in_array( strtolower( $s->text( $k ) ), Rules::VALIDATORS, true ) ) { $args = $s->args( $k + 1 ); if ( isset( $args[0] ) && $s->norm( $args[0][0], $args[0][1] ) === $value ) { return true; } } if ( T_ISSET === $s->id( $k ) && false !== strpos( $s->norm( $k, $s->close( $k + 1 ) ), '[' . $value . ']' ) ) { return true; } } return false; } /** * The bits of a concatenation, a single value, or a call. */ private function terms( int $a, int $b ): int { $s = $this->s; $bits = 0; for ( $k = $a; $k < $b; $k++ ) { if ( isset( $s->skip[ $k ] ) ) { $k = $s->skip[ $k ] - 1; continue; } $id = $s->id( $k ); if ( T_VARIABLE === $id ) { $stop = $this->chain_end( $k, $b ); $bits |= $this->variable_bits( $k, $stop ); $k = $this->index_end( $k, $b ) - 1; continue; } if ( ( T_OBJECT_OPERATOR === $id || $id === $this->nullsafe ) && T_STRING === $s->id( $k + 1 ) && $s->is( $k + 2, '(' ) ) { $method = strtolower( $s->text( $k + 1 ) ); if ( in_array( $method, Rules::REST_METHODS, true ) ) { $bits |= in_array( $method, [ 'get_header', 'get_headers' ], true ) ? Rules::HEADERS : Rules::REQUEST; } elseif ( 'esc_like' === $method ) { $bits |= $this->args_mask( $s->args( $k + 2 ) ); } $k = $s->close( $k + 2 ); continue; } if ( T_STRING === $id && $s->is( $k + 1, '(' ) && ! in_array( $s->id( $k - 1 ), self::NOT_A_CALL, true ) ) { $bits |= $this->call_bits( strtolower( $s->text( $k ) ), $k + 1 ); $k = $s->close( $k + 1 ); continue; } if ( T_STRING === $id && T_DOUBLE_COLON === $s->id( $k + 1 ) ) { $k += 2; if ( $s->is( $k + 1, '(' ) ) { $k = $s->close( $k + 1 ); } continue; } if ( in_array( $id, self::KILL_CASTS, true ) ) { $k = $this->term_end( $k + 1, $b ) - 1; continue; } if ( ( T_ISSET === $id || T_EMPTY === $id ) && $s->is( $k + 1, '(' ) ) { $k = $s->close( $k + 1 ); continue; } if ( T_NEW === $id ) { $k = isset( $s->skip[ $k + 1 ] ) ? $s->skip[ $k + 1 ] - 1 : $this->term_end( $k + 1, $b ) - 1; continue; } if ( $s->is( $k, '(' ) ) { $bits |= $this->mask( $k + 1, $s->close( $k ) ); $k = $s->close( $k ); continue; } $open = $s->array_open( $k ); if ( null !== $open ) { foreach ( $s->items( $open ) as $item ) { $bits |= $this->mask( $item['value'][0], $item['value'][1] ); } $k = $s->close( $open ); } } return $bits & ~$this->prefix_clears( $a, $b ); } /** * A concatenation that starts with a fixed text: an option name with a fixed prefix can't name * another option (CODE-012), and a URL with a fixed host can't reach another (CODE-014). */ private function prefix_clears( int $a, int $b ): int { $s = $this->s; $first = null; if ( $s->is( $a + 1, '.' ) ) { $first = $s->literal( $a ); } elseif ( $s->is( $a, '"' ) && T_ENCAPSED_AND_WHITESPACE === $s->id( $a + 1 ) ) { $first = $s->text( $a + 1 ); } elseif ( T_START_HEREDOC === $s->id( $a ) && T_ENCAPSED_AND_WHITESPACE === $s->id( $a + 1 ) ) { $first = $s->text( $a + 1 ); } if ( null === $first || '' === $first || $b - $a < 2 ) { return 0; } return Rules::OPTION | ( Rules::fixed_host( $first ) ? Rules::URL : 0 ); } /** * A variable and what follows it ([…], ->prop), as one value. */ private function variable_bits( int $k, int $stop ): int { $s = $this->s; $name = $s->text( $k ); if ( isset( $this->checked[ $s->norm( $k, $this->index_end( $k, $stop ) ) ] ) || isset( $this->checked[ $name ] ) ) { return 0; } if ( in_array( $name, Rules::SUPERGLOBALS, true ) || '$_SERVER' === $name ) { return $this->superglobal_bits( $k ); } if ( '$this' === $name ) { if ( T_OBJECT_OPERATOR === $s->id( $k + 1 ) && T_STRING === $s->id( $k + 2 ) && ! $s->is( $k + 3, '(' ) ) { return $this->var_bits( '$this->' . $s->text( $k + 2 ) ); } return 0; } if ( isset( $this->requests[ $name ] ) && $s->is( $k + 1, '[' ) ) { return Rules::REQUEST; } return $this->var_bits( $name ); } private function superglobal_bits( int $k ): int { $s = $this->s; $name = $s->text( $k ); if ( '$_SERVER' === $name ) { $key = $s->is( $k + 1, '[' ) ? $s->only_literal( [ $k + 2, $s->close( $k + 1 ) ] ) : null; return null !== $key && Rules::server_key( $key ) ? Rules::HEADERS : 0; } if ( '$_FILES' === $name && $s->is( $k + 1, '[' ) ) { $second = $s->close( $k + 1 ) + 1; if ( $s->is( $second, '[' ) && in_array( $s->only_literal( [ $second + 1, $s->close( $second ) ] ), Rules::FILES_SAFE, true ) ) { return 0; } } return Rules::REQUEST; } /** * What a call returns, from what goes in. */ private function call_bits( string $callee, int $open ): int { $s = $this->s; $args = $s->args( $open ); switch ( $callee ) { case 'filter_input': return isset( $args[2] ) && 1 === preg_match( '/FILTER_(?:VALIDATE_(?:INT|FLOAT|BOOL)|SANITIZE_NUMBER)/', $s->norm( $args[2][0], $args[2][1] ) ) ? 0 : Rules::REQUEST; case 'filter_input_array': return Rules::REQUEST; case 'getallheaders': case 'apache_request_headers': return Rules::HEADERS; case 'file_get_contents': return isset( $args[0] ) && 'php://input' === $s->only_literal( $args[0] ) ? Rules::REQUEST : 0; case 'array_map': if ( ! isset( $args[1] ) ) { return 0; } $callback = $s->only_literal( $args[0] ); $flow = null === $callback ? null : Rules::flow( strtolower( $callback ) ); return null === $flow ? 0 : $this->args_mask( array_slice( $args, 1 ) ) & ~$flow; case 'filter_var': $filter = isset( $args[1] ) ? $s->norm( $args[1][0], $args[1][1] ) : ''; if ( 1 === preg_match( '/FILTER_(?:VALIDATE_(?:INT|FLOAT|BOOL)|SANITIZE_NUMBER)/', $filter ) ) { return 0; } $bits = isset( $args[0] ) ? $this->mask( $args[0][0], $args[0][1] ) : 0; if ( 1 === preg_match( '/FILTER_(?:VALIDATE_EMAIL|SANITIZE_(?:FULL_)?SPECIAL_CHARS)/', $filter ) ) { $bits &= ~( Rules::XSS | Rules::ATTR | Rules::SQL ); } return $bits; case 'sprintf': case 'vsprintf': $bits = $this->args_mask( array_slice( $args, 1 ) ); $format = isset( $args[0] ) ? $s->only_literal( $args[0] ) : null; if ( null !== $format && '' !== $format && '%' !== $format[0] ) { $bits &= ~( Rules::OPTION | ( Rules::fixed_host( $format ) ? Rules::URL : 0 ) ); } return $bits | ( isset( $args[0] ) ? $this->mask( $args[0][0], $args[0][1] ) : 0 ); } $flow = Rules::flow( $callee ); if ( null === $flow ) { return 0; } return $this->args_mask( $args ) & ~$flow; } /** * @param array $args Argument ranges. */ private function args_mask( array $args ): int { $bits = 0; foreach ( $args as $range ) { $bits |= $this->mask( $range[0], $range[1] ); } return $bits; } /** * After a variable's [ … ] indexes. */ private function index_end( int $k, int $b ): int { $j = $k + 1; while ( $j < $b && $this->s->is( $j, '[' ) ) { $j = $this->s->close( $j ) + 1; } return $j; } /** * After a variable with its indexes and ->props. */ private function chain_end( int $k, int $b ): int { $s = $this->s; $j = $k + 1; while ( $j < $b ) { if ( $s->is( $j, '[' ) ) { $j = $s->close( $j ) + 1; } elseif ( ( T_OBJECT_OPERATOR === $s->id( $j ) || $s->id( $j ) === $this->nullsafe ) && T_STRING === $s->id( $j + 1 ) && ! $s->is( $j + 2, '(' ) ) { $j += 2; } else { break; } } return $j; } /** * After one operand: a variable with its chain and calls, a call, a bracket, or one token. */ private function term_end( int $k, int $b ): int { $s = $this->s; if ( T_STRING === $s->id( $k ) && T_DOUBLE_COLON === $s->id( $k + 1 ) ) { $k += 2; } if ( T_VARIABLE === $s->id( $k ) || T_STRING === $s->id( $k ) ) { $j = $k + 1; while ( $j < $b ) { if ( $s->is( $j, '[' ) || $s->is( $j, '(' ) ) { $j = $s->close( $j ) + 1; } elseif ( ( T_OBJECT_OPERATOR === $s->id( $j ) || $s->id( $j ) === $this->nullsafe || T_DOUBLE_COLON === $s->id( $j ) ) && $j + 1 < $b ) { $j += 2; } else { break; } } return $j; } if ( $s->is( $k, '(' ) || $s->is( $k, '[' ) ) { return $s->close( $k ) + 1; } return $k + 1; } private function lead( string $check, string $kb, int $at, string $what, string $severity = 'medium' ): void { $this->leads[] = [ 'check' => $check, 'kb' => $kb, 'line' => $this->s->line( $at ), 'severity' => $severity, 'what' => $what, ]; } }