*/ private array $files; /** @var callable(string): ?string */ private $read; /** @var array> uid "file:unit" => facts. */ private array $units = []; /** @var array lower-case function name => uid. */ private array $functions = []; /** @var array> lower-case class => lower-case method => uid. */ private array $methods = []; /** @var array lower-case method => uids. */ private array $method_names = []; /** @var array */ private array $classes = []; /** @var array> uid => role. */ private array $roles = []; /** @var array> */ private array $leads = []; private int $skipped = 0; private bool $partial = false; /** * @param array $files PHP and JS files (kind "php" or "js"). * @param callable(string): ?string $read Reads a file, null when it can't. */ private function __construct( array $files, callable $read ) { $this->files = array_values( $files ); $this->read = $read; } /** * @param array $files PHP and JS files (kind "php" or "js"). * @param callable(string): ?string $read Reads a file, null when it can't. * @return array{leads: array>, files: int, skipped: int, partial: bool} */ public static function run( array $files, callable $read ): array { $analyzer = new self( $files, $read ); return $analyzer->analyze(); } /** * @return array{leads: array>, files: int, skipped: int, partial: bool} */ private function analyze(): array { $parsed = $this->first_pass(); $this->resolve_registrations( $parsed['regs'] ); $this->authorization(); $this->abilities(); $this->second_pass( $parsed['php'] ); usort( $this->leads, static function ( array $a, array $b ): int { return [ $a['file'], $a['line'], $a['check'] ] <=> [ $b['file'], $b['line'], $b['check'] ]; } ); $leads = []; $seen = []; foreach ( $this->leads as $lead ) { $key = $lead['check'] . '|' . $lead['kb'] . '|' . $lead['file'] . '|' . $lead['line'] . '|' . $lead['what']; if ( ! isset( $seen[ $key ] ) ) { $seen[ $key ] = true; $leads[] = $lead; } } if ( count( $leads ) > self::MAX_LEADS ) { $leads = array_slice( $leads, 0, self::MAX_LEADS ); $this->partial = true; } return [ 'leads' => $leads, 'files' => count( $parsed['php'] ), 'skipped' => $this->skipped, 'partial' => $this->partial, ]; } /** * Reads each file once: facts and registrations from PHP, secrets from PHP and JS. * * @return array{php: int[], regs: array>} */ private function first_pass(): array { $php = []; $regs = []; $bytes = 0; foreach ( $this->files as $fi => $file ) { if ( count( $php ) >= self::MAX_FILES || $bytes >= self::MAX_BYTES ) { $this->partial = true; break; } $code = ( $this->read )( $file['path'] ); if ( null === $code ) { ++$this->skipped; continue; } $bytes += strlen( $code ); foreach ( Secrets::find( $code ) as $hit ) { $this->add( [ 'check' => 'CODE-008', 'kb' => 'secrets-exposure', 'line' => $hit['line'], 'severity' => 'medium', 'what' => $hit['name'] . ' ' . $hit['masked'], ], $fi, '' ); } if ( 'php' !== $file['kind'] ) { continue; } $source = Source::parse( $code, $file['display'] ); if ( null === $source ) { ++$this->skipped; continue; } $php[] = $fi; $facts = Facts::of( $source ); foreach ( $facts['units'] as $index => $unit ) { $uid = $fi . ':' . $index; $unit['file'] = $fi; $this->units[ $uid ] = $unit; if ( 'function' === $unit['kind'] ) { $this->functions[ $unit['name'] ] = $this->functions[ $unit['name'] ] ?? $uid; } elseif ( 'method' === $unit['kind'] ) { $this->methods[ strtolower( (string) $unit['class'] ) ][ $unit['name'] ] = $uid; $this->method_names[ $unit['name'] ][] = $uid; } } foreach ( $facts['classes'] as $key => $class ) { $this->classes[ $key ] = $class; } foreach ( $facts['leads'] as $lead ) { $this->add( $lead, $fi, (string) $facts['units'][ $lead['unit'] ]['label'] ); } foreach ( $facts['regs'] as $reg ) { $reg['file'] = $fi; $regs[] = $reg; } } return [ 'php' => $php, 'regs' => $regs, ]; } /** * Gives each handler its role, and reports open REST routes (CODE-002) and abilities (CODE-017). * * @param array> $regs The registrations. */ private function resolve_registrations( array $regs ): void { foreach ( $regs as $reg ) { $fi = (int) $reg['file']; $label = (string) ( $this->units[ $fi . ':' . $reg['unit'] ]['label'] ?? '' ); switch ( $reg['type'] ) { case 'hook': foreach ( $this->targets( $reg['callback'], $fi ) as $uid ) { $this->roles[ $uid ]['hooks'][ $reg['kind'] ] = $reg['hook']; if ( 'nopriv' === $reg['kind'] ) { $this->roles[ $uid ]['public'] = true; } } break; case 'shortcode': case 'block': foreach ( $this->targets( $reg['callback'], $fi ) as $uid ) { $this->roles[ $uid ]['attr'] = true; if ( 'block' === $reg['type'] ) { $this->roles[ $uid ]['block'] = true; } } break; case 'rest': foreach ( $reg['endpoints'] as $endpoint ) { $open = $this->permission_open( $endpoint['permission'], $fi ); foreach ( $this->targets( $endpoint['callback'], $fi ) as $uid ) { $this->roles[ $uid ]['rest'] = true; if ( $open ) { $this->roles[ $uid ]['public'] = true; } } if ( $open && true === $endpoint['non_get'] ) { $this->add( [ 'check' => 'CODE-002', 'kb' => 'authz-rest-permission', 'line' => (int) $reg['line'], 'severity' => 'medium', 'what' => 'missing' === $endpoint['permission']['type'] ? 'no permission_callback' : 'permission_callback lets anyone in', 'route' => (string) $reg['route'], ], $fi, $label ); } } break; case 'ability': if ( null !== $reg['permission'] && $this->permission_open( $reg['permission'], $fi ) ) { $this->add( [ 'check' => 'CODE-017', 'kb' => 'abilities-api-permission', 'line' => (int) $reg['line'], 'severity' => $reg['readonly'] ? 'info' : 'medium', 'what' => 'permission_callback lets anyone in', 'route' => (string) $reg['name'], ], $fi, $label ); } break; } } } /** * Whether a permission callback lets anyone in: missing, __return_true, or a function that only * returns true. * * @param array $permission A callback reference. */ private function permission_open( array $permission, int $fi ): bool { if ( in_array( $permission['type'], [ 'missing', 'true' ], true ) ) { return true; } foreach ( $this->targets( $permission, $fi ) as $uid ) { return (bool) $this->units[ $uid ]['returns_true']; } return false; } /** * The functions a callback reference names, in this plugin. * * @param array|null $ref A callback reference from Facts::callback(). * @return string[] uids. */ private function targets( ?array $ref, int $fi ): array { if ( null === $ref ) { return []; } switch ( $ref['type'] ) { case 'unit': return isset( $this->units[ $fi . ':' . $ref['unit'] ] ) ? [ $fi . ':' . $ref['unit'] ] : []; case 'fn': return isset( $this->functions[ $ref['name'] ] ) ? [ $this->functions[ $ref['name'] ] ] : []; case 'method': $uid = $this->method( $ref['class'], (string) $ref['name'] ); return null === $uid ? [] : [ $uid ]; } return []; } /** * A method by class, walking up the parents in this plugin. With no class, the one method of that * name, if there is only one. */ private function method( ?string $owner, string $name ): ?string { if ( null === $owner ) { $named = $this->method_names[ $name ] ?? []; return 1 === count( $named ) ? $named[0] : null; } if ( 0 === strpos( $owner, 'parent:' ) ) { $owner = $this->classes[ strtolower( substr( $owner, 7 ) ) ]['extends'] ?? null; if ( null === $owner ) { return null; } } $key = strtolower( ltrim( $owner, '\\' ) ); // A namespaced name: the classes are recorded by their short name. $short = strrchr( $key, '\\' ); if ( false !== $short && ! isset( $this->classes[ $key ] ) ) { $key = substr( $short, 1 ); } for ( $depth = 0; $depth < 5 && '' !== $key; $depth++ ) { if ( isset( $this->methods[ $key ][ $name ] ) ) { return $this->methods[ $key ][ $name ]; } $parent = $this->classes[ $key ]['extends'] ?? null; $key = null === $parent ? '' : strtolower( $parent ); $short = strrchr( $key, '\\' ); if ( false !== $short ) { $key = substr( $short, 1 ); } } return null; } /** * CODE-001, CODE-010 and CODE-011: handlers that change the site from a request with no * permission check, looking one helper call deep. */ private function authorization(): void { foreach ( $this->units as $uid => $unit ) { $hooks = (array) ( $this->roles[ $uid ]['hooks'] ?? [] ); if ( [] === $hooks && ! $unit['is_admin'] ) { continue; } $cap = (bool) $unit['cap']; $nonce = (bool) $unit['nonce']; $reads = (bool) $unit['reads']; $changes = (array) $unit['changes']; foreach ( $this->helpers( $uid ) as $helper ) { $facts = $this->units[ $helper ]; $cap = $cap || $facts['cap']; $nonce = $nonce || $facts['nonce']; $reads = $reads || $facts['reads']; $changes = array_merge( $changes, (array) $facts['changes'] ); } if ( $cap || ! $reads ) { continue; } $fi = (int) $unit['file']; if ( isset( $hooks['nopriv'] ) ) { $relevant = self::of_kinds( $changes, Rules::NOPRIV_KINDS ); if ( [] !== $relevant ) { $this->add( $this->handler_lead( 'CODE-001', 'authz-nopriv-sensitive', $unit, $relevant, (string) $hooks['nopriv'] ), $fi, (string) $unit['label'] ); } continue; } $relevant = self::of_kinds( $changes, Rules::PRIV_KINDS ); if ( [] === $relevant ) { continue; } $hook = (string) ( $hooks['ajax'] ?? $hooks['admin_init'] ?? '' ); if ( $unit['is_admin'] ) { $this->add( $this->handler_lead( 'CODE-011', 'authz-is-admin', $unit, $relevant, $hook ), $fi, (string) $unit['label'] ); } elseif ( [] !== $hooks ) { $this->add( $this->handler_lead( 'CODE-010', $nonce ? 'authz-nonce-only' : 'authz-ajax-missing-cap', $unit, $relevant, $hook ), $fi, (string) $unit['label'] ); } } } /** * The plugin's own functions a function calls directly. * * @return string[] uids. */ private function helpers( string $uid ): array { $unit = $this->units[ $uid ]; $out = []; foreach ( (array) $unit['calls'] as $call ) { $fi = (int) $unit['file']; foreach ( $this->targets( $call, $fi ) as $helper ) { if ( $helper !== $uid ) { $out[ $helper ] = true; } } } return array_keys( $out ); } /** * @param array $changes Changes. * @param string[] $kinds The kinds that count. * @return array */ private static function of_kinds( array $changes, array $kinds ): array { return array_values( array_filter( $changes, static function ( array $change ) use ( $kinds ): bool { return in_array( $change['kind'], $kinds, true ); } ) ); } /** * @param array $unit The handler's facts. * @param array $changes What it changes. * @return array */ private function handler_lead( string $check, string $kb, array $unit, array $changes, string $hook ): array { $what = []; foreach ( array_slice( $changes, 0, 3 ) as $change ) { $what[] = $change['what']; } return [ 'check' => $check, 'kb' => $kb, 'line' => (int) $unit['line'], 'severity' => 'medium', 'what' => implode( ', ', array_unique( $what ) ), 'hook' => $hook, ]; } /** * CODE-017's ability_class: a WP_Ability subclass whose permission method only says yes. */ private function abilities(): void { foreach ( $this->classes as $key => $class ) { if ( 'wp_ability' !== strtolower( (string) $class['extends'] ) ) { continue; } foreach ( self::ABILITY_METHODS as $name ) { $uid = $this->methods[ $key ][ $name ] ?? null; if ( null !== $uid && $this->units[ $uid ]['returns_true'] ) { $unit = $this->units[ $uid ]; $this->add( [ 'check' => 'CODE-017', 'kb' => 'abilities-api-permission', 'line' => (int) $unit['line'], 'severity' => 'medium', 'what' => 'ability_class ' . $class['name'] . ' lets anyone in', ], (int) $unit['file'], (string) $unit['label'] ); } } } } /** * Reads each PHP file again and follows request data through its functions; fills in the * excerpts. * * @param int[] $php The PHP files' indexes. */ private function second_pass( array $php ): void { $by_file = []; foreach ( $this->leads as $n => $lead ) { $by_file[ $lead['fi'] ][] = $n; } $excerpts = []; foreach ( $php as $fi ) { $file = $this->files[ $fi ]; $code = ( $this->read )( $file['path'] ); if ( null === $code ) { continue; } $source = Source::parse( $code, $file['display'] ); if ( null === $source ) { continue; } $render = 'render.php' === strtolower( basename( $file['path'] ) ); foreach ( $source->units as $index => $unit ) { $role = (array) ( $this->roles[ $fi . ':' . $index ] ?? [] ); unset( $role['hooks'] ); if ( $render && 'file' === $unit['kind'] ) { $role['render_file'] = true; } $label = Facts::label( $unit ); $taint = new Taint( $source, $unit, $role ); foreach ( $taint->run() as $lead ) { $by_file[ $fi ][] = $this->add( $lead, $fi, $label ); } // Nested too deep to follow to the end: counted with the skipped, so the scan says so. if ( $taint->too_deep ) { ++$this->skipped; } } foreach ( $by_file[ $fi ] ?? [] as $n ) { // A secret's lead keeps no excerpt: its name and 4-character mask say enough (R6). if ( 'CODE-008' !== $this->leads[ $n ]['check'] ) { $excerpts[ $n ] = Secrets::mask_all( $source->excerpt( (int) $this->leads[ $n ]['line'] ) ); } } } foreach ( $this->leads as $n => $lead ) { $this->leads[ $n ]['excerpt'] = Str::clean( (string) ( $excerpts[ $n ] ?? '' ), 160 ); $this->leads[ $n ]['file'] = $this->files[ $lead['fi'] ]['display']; unset( $this->leads[ $n ]['fi'], $this->leads[ $n ]['unit'] ); } } /** * @param array $lead A lead. */ private function add( array $lead, int $fi, string $label ): int { $lead['fi'] = $fi; $lead['file'] = ''; $lead['function'] = $label; $this->leads[] = $lead; return count( $this->leads ) - 1; } }