size( $path ); /* * Avoid loading unexpectedly huge files into memory. The audit already * treats metadata and placement as evidence; content inspection is bounded. */ if ( false !== $size && $size > max( self::ANALYSIS_LIMIT * 4, $limit * 4 ) ) { return ''; } $data = $wp_filesystem->get_contents( $path ); if ( ! is_string( $data ) ) { return ''; } return strlen( $data ) > $limit ? substr( $data, 0, $limit ) : $data; } public static function sanitize( $value, $limit = self::DEFAULT_LIMIT, $wp_config_mode = false ) { $text = is_scalar( $value ) ? (string) $value : wp_json_encode( $value ); if ( ! is_string( $text ) ) { $text = ''; } $text = self::strip_control_chars( $text ); $text = self::redact_secrets( $text, (bool) $wp_config_mode ); if ( strlen( $text ) > $limit ) { $text = substr( $text, 0, $limit ) . "\n[preview truncated]"; } return $text; } public static function contextual( $text, array $needles, $limit = self::DEFAULT_LIMIT, $wp_config_mode = false ) { $text = is_string( $text ) ? $text : ''; if ( '' === $text ) { return ''; } $positions = array(); foreach ( $needles as $needle ) { $needle = trim( (string) $needle ); if ( '' === $needle ) { continue; } $position = stripos( $text, $needle ); if ( false !== $position ) { $positions[] = (int) $position; } } if ( empty( $positions ) ) { return self::sanitize( $text, $limit, $wp_config_mode ); } sort( $positions, SORT_NUMERIC ); $first = $positions[0]; $last = $positions[ count( $positions ) - 1 ]; $start = max( 0, $first - 240 ); $span = max( 700, ( $last - $start ) + 520 ); $span = min( $span, max( $limit + 600, 1200 ) ); $slice = substr( $text, $start, $span ); if ( $start > 0 ) { $slice = "[context omitted]\n" . $slice; } if ( $start + $span < strlen( $text ) ) { $slice .= "\n[context omitted]"; } return self::sanitize( $slice, $limit, $wp_config_mode ); } public static function redact_secrets( $text, $wp_config_mode = false ) { $patterns = array( '/(DB_PASSWORD\s*[\'\"]\s*,\s*[\'\"])[^\'\"]+/i' => '$1[REDACTED]', '/(DB_USER\s*[\'\"]\s*,\s*[\'\"])[^\'\"]+/i' => '$1[REDACTED]', '/(DB_HOST\s*[\'\"]\s*,\s*[\'\"])[^\'\"]+/i' => '$1[REDACTED]', '/((?:AUTH_KEY|SECURE_AUTH_KEY|LOGGED_IN_KEY|NONCE_KEY|AUTH_SALT|SECURE_AUTH_SALT|LOGGED_IN_SALT|NONCE_SALT)\s*[\'\"]\s*,\s*[\'\"])[^\'\"]+/i' => '$1[REDACTED]', '/((?:password|passwd|pwd|secret|token|api[_-]?key)\s*[=:]\s*)[^\s,;]+/i' => '$1[REDACTED]', '/(Authorization:\s*(?:Bearer|Basic)\s+)[A-Za-z0-9+\/=._-]+/i' => '$1[REDACTED]', ); $redacted = preg_replace( array_keys( $patterns ), array_values( $patterns ), $text ); if ( ! is_string( $redacted ) ) { $redacted = ''; } if ( $wp_config_mode ) { $redacted = preg_replace( '/define\(\s*[\'\"](DB_PASSWORD|DB_USER|DB_HOST|AUTH_KEY|SECURE_AUTH_KEY|LOGGED_IN_KEY|NONCE_KEY|AUTH_SALT|SECURE_AUTH_SALT|LOGGED_IN_SALT|NONCE_SALT)[\'\"]\s*,\s*[\'\"][^\'\"]*[\'\"]\s*\)\s*;/i', "define('$1', '[REDACTED]');", $redacted ); } return is_string( $redacted ) ? $redacted : ''; } private static function strip_control_chars( $text ) { $text = str_replace( array( "\r\n", "\r" ), "\n", $text ); return preg_replace( '/[^\P{C}\n\t]/u', '', $text ); } }