get_routes(); if ( ! is_array( $routes ) ) { return $findings; } foreach ( $routes as $route => $endpoints ) { $route = (string) $route; if ( ! is_array( $endpoints ) ) { continue; } $namespace = $this->namespace_from_route( $route ); $route_random = $this->has_random_component( $route ); $ioc_hits = ACHWPA_IOC::matches( $route ); foreach ( $endpoints as $endpoint ) { if ( ! is_array( $endpoint ) || empty( $endpoint['methods'] ) ) { continue; } $methods = $this->normalize_methods( $endpoint['methods'] ); if ( empty( $methods ) ) { continue; } $permission = isset( $endpoint['permission_callback'] ) ? $endpoint['permission_callback'] : null; $callback = isset( $endpoint['callback'] ) ? $endpoint['callback'] : null; $permission_label = $this->callable_label( $permission ); $callback_label = $this->callable_label( $callback ); $callback_file = $this->callable_file( $callback ); $signals = array(); if ( $route_random ) { $signals[] = 'random-or-hex route component'; } $state_changing = $this->has_state_changing_method( $methods ); $permissive = $this->is_permissive_permission( $permission ); if ( $state_changing && $route_random ) { $signals[] = 'state-changing route with random identifier'; } if ( $state_changing && $permissive ) { $signals[] = 'state-changing route with permissive permission callback'; } $core_namespace_noncore_callback = false; if ( $this->is_core_namespace( $namespace ) && '' !== $callback_file && ! $this->is_core_file( $callback_file ) ) { $signals[] = 'route registered in a core namespace by non-core code'; $core_namespace_noncore_callback = true; } if ( ! empty( $ioc_hits ) ) { $signals[] = 'known indicator'; } if ( empty( $signals ) ) { continue; } $level = 'Info'; $strong_context = $core_namespace_noncore_callback || $route_random || ! empty( $ioc_hits ); if ( $strong_context || count( $signals ) >= 2 && ! ( $state_changing && $permissive && 1 === count( $signals ) ) ) { $level = 'Review'; } $preview_lines = array( 'Route: ' . $route, 'Namespace: ' . ( '' !== $namespace ? $namespace : '[none]' ), 'Methods: ' . implode( ', ', $methods ), 'Callback: ' . ( '' !== $callback_label ? $callback_label : '[not available]' ), 'Permission callback: ' . ( '' !== $permission_label ? $permission_label : '[not available]' ), ); if ( '' !== $callback_file ) { $preview_lines[] = 'Callback source: ' . $this->relative_path( $callback_file ); } $findings[] = new ACHWPA_Finding( array( 'category' => 'REST', 'level' => $level, 'location' => $route, 'indicator' => implode( ', ', array_values( array_unique( array_merge( $signals, $ioc_hits ) ) ) ), 'explanation' => __( 'The registered REST route has characteristics relevant to persistence review. The auditor inspects route metadata only and does not call the endpoint.', 'aiutocomputerhelp-persistence-audit' ), 'preview' => ACHWPA_Preview::sanitize( implode( "\n", $preview_lines ), 1400 ), 'artifacts' => array_merge( array( 'ROUTE:' . $route ), $this->artifacts_from_route( $route ) ), 'identifiers' => $this->extract_identifiers( $route . ' ' . $callback_label ), ) ); } } return $findings; } private function namespace_from_route( $route ) { $trimmed = trim( (string) $route, '/' ); if ( '' === $trimmed ) { return ''; } $parts = explode( '/', $trimmed ); if ( count( $parts ) < 2 ) { return isset( $parts[0] ) ? $parts[0] : ''; } return $parts[0] . '/' . $parts[1]; } private function is_core_namespace( $namespace ) { return in_array( (string) $namespace, $this->core_namespaces, true ); } private function has_random_component( $route ) { $plain = preg_replace( '/\(\?[^)]*\)/', '', (string) $route ); foreach ( explode( '/', trim( $plain, '/' ) ) as $component ) { if ( preg_match( '/^[a-f0-9]{8,32}$/i', $component ) ) { return true; } if ( preg_match( '/(?:^|[-_])[a-f0-9]{8,32}(?:$|[-_])/i', $component ) ) { return true; } } return false; } private function normalize_methods( $methods ) { $out = array(); if ( is_string( $methods ) ) { $methods = preg_split( '/[\s,|]+/', $methods ); foreach ( (array) $methods as $method ) { $method = strtoupper( trim( (string) $method ) ); if ( '' !== $method ) { $out[] = $method; } } } elseif ( is_array( $methods ) ) { foreach ( $methods as $key => $value ) { if ( is_string( $key ) && $value ) { $out[] = strtoupper( $key ); } elseif ( is_string( $value ) ) { $out[] = strtoupper( $value ); } } } return array_values( array_unique( $out ) ); } private function has_state_changing_method( array $methods ) { foreach ( array( 'POST', 'PUT', 'PATCH', 'DELETE' ) as $method ) { if ( in_array( $method, $methods, true ) ) { return true; } } return false; } private function is_permissive_permission( $callback ) { if ( is_string( $callback ) && '__return_true' === $callback ) { return true; } if ( is_array( $callback ) && isset( $callback[1] ) && '__return_true' === $callback[1] ) { return true; } return false; } private function callable_label( $callback ) { if ( is_string( $callback ) ) { return $callback; } if ( $callback instanceof Closure ) { return 'Closure'; } if ( is_array( $callback ) && 2 === count( $callback ) ) { $owner = is_object( $callback[0] ) ? get_class( $callback[0] ) : (string) $callback[0]; return $owner . '::' . (string) $callback[1]; } if ( is_object( $callback ) && method_exists( $callback, '__invoke' ) ) { return get_class( $callback ) . '::__invoke'; } return ''; } private function callable_file( $callback ) { try { if ( is_string( $callback ) && function_exists( $callback ) ) { $reflection = new ReflectionFunction( $callback ); return (string) $reflection->getFileName(); } if ( $callback instanceof Closure ) { $reflection = new ReflectionFunction( $callback ); return (string) $reflection->getFileName(); } if ( is_array( $callback ) && 2 === count( $callback ) && is_callable( $callback ) ) { $reflection = new ReflectionMethod( $callback[0], $callback[1] ); return (string) $reflection->getFileName(); } if ( is_object( $callback ) && method_exists( $callback, '__invoke' ) ) { $reflection = new ReflectionMethod( $callback, '__invoke' ); return (string) $reflection->getFileName(); } } catch ( ReflectionException $e ) { return ''; } return ''; } private function is_core_file( $file ) { $file = wp_normalize_path( (string) $file ); $root = trailingslashit( ACHWPA_Paths::wordpress_root() ); return 0 === strpos( $file, $root . 'wp-includes/' ) || 0 === strpos( $file, $root . 'wp-admin/' ); } private function relative_path( $file ) { $file = wp_normalize_path( (string) $file ); $root = trailingslashit( ACHWPA_Paths::wordpress_root() ); if ( 0 === strpos( $file, $root ) ) { return ltrim( substr( $file, strlen( $root ) ), '/' ); } return basename( $file ); } private function artifacts_from_route( $route ) { $out = array(); if ( preg_match_all( '/[A-Za-z0-9_.-]{3,}\.php\b/', (string) $route, $matches ) ) { foreach ( $matches[0] as $file ) { $out[] = 'FILE:' . basename( $file ); } } if ( preg_match_all( '/(?:_site_transient_|_transient_|wp_cache_|wp_compat_|_wp_)[A-Za-z0-9_.-]{3,}/i', (string) $route, $matches ) ) { foreach ( $matches[0] as $name ) { $out[] = 'OPTION:' . $name; } } return array_values( array_unique( $out ) ); } private function extract_identifiers( $text ) { $ids = array(); if ( preg_match_all( '/\b[a-f0-9]{6,32}\b/i', (string) $text, $matches ) ) { foreach ( $matches[0] as $candidate ) { if ( ! ctype_digit( $candidate ) ) { $ids[] = strtolower( $candidate ); } } } return array_slice( array_values( array_unique( $ids ) ), 0, 30 ); } }