,checked?:int,error?:string,zip_slip?:list} */ public static function validate( string $zip_path ): array { if ( '' === $zip_path || ! is_readable( $zip_path ) ) { return array( 'ok' => false, 'error' => __( 'Artifact zip is missing or unreadable.', '007aj-backup-restore-verifier' ), ); } // Synchronous wrapper around the chunked checker (used outside job ticks). $dir = trailingslashit( Working_Dir::root() ) . 'check-' . wp_generate_password( 10, false, false ); try { Working_Dir::ensure(); $check = new Archive_Check( $dir ); $st = array(); do { $st = $check->step( $zip_path, $st, microtime( true ) + 3600 ); } while ( empty( $st['done'] ) ); return array( 'ok' => true, 'manifest' => $check->manifest(), 'checked' => (int) ( $st['ok'] ?? 0 ), ); } catch ( \Throwable $e ) { $out = array( 'ok' => false, 'error' => $e->getMessage(), ); if ( \brverify_str_contains( $e->getMessage(), 'Zip-slip' ) ) { $out['zip_slip'] = array( $e->getMessage() ); } return $out; } finally { Working_Dir::rrmdir( $dir ); } } /** * Scan zip member names for path traversal / absolute paths. * * @param ZipArchive $zip Open archive. * @return list Unsafe names (empty = clean). */ public static function scan_zip_slip( ZipArchive $zip ): array { $bad = array(); $n = $zip->numFiles; for ( $i = 0; $i < $n; $i++ ) { $stat = $zip->statIndex( $i ); if ( ! is_array( $stat ) || empty( $stat['name'] ) ) { continue; } $name = (string) $stat['name']; if ( ! self::is_safe_zip_entry( $name ) ) { $bad[] = $name; } } return $bad; } /** * Whether a zip entry name is safe (relative, no .., no absolute). * * @param string $name Entry name. */ public static function is_safe_zip_entry( string $name ): bool { $normalized = str_replace( '\\', '/', $name ); if ( '' === $normalized || \brverify_str_ends_with( $normalized, '/' ) ) { // Directory entries OK if the path itself is safe. $normalized = rtrim( $normalized, '/' ); if ( '' === $normalized ) { return true; } } if ( ! Path_Guard::is_safe_relative( $normalized ) ) { return false; } // Also reject null bytes and drive-letter style already covered by Path_Guard. if ( \brverify_str_contains( $normalized, "\0" ) ) { return false; } return true; } /** * Resolve an extract destination and require it stay under $root. * * @param string $root Absolute extract root (must exist). * @param string $entry Zip entry relative name. * @return string|null Absolute destination or null if unsafe. */ public static function jail_destination( string $root, string $entry ): ?string { if ( ! self::is_safe_zip_entry( $entry ) ) { return null; } $root_real = realpath( $root ); if ( false === $root_real ) { return null; } $root_real = rtrim( $root_real, '/\\' ); $normalized = ltrim( str_replace( '\\', '/', $entry ), '/' ); $candidate = $root_real . DIRECTORY_SEPARATOR . str_replace( '/', DIRECTORY_SEPARATOR, $normalized ); // Normalize .. that somehow slipped (should already be rejected). $parts = array(); foreach ( explode( DIRECTORY_SEPARATOR, str_replace( '\\', '/', $candidate ) ) as $part ) { if ( '' === $part || '.' === $part ) { continue; } if ( '..' === $part ) { return null; } $parts[] = $part; } // Rebuild — on Unix leading slash needed. $rebuilt = ( \brverify_str_starts_with( $root_real, '/' ) ? '/' : '' ) . implode( '/', $parts ); // Prefer string containment against root_real before file exists. $check = str_replace( '\\', '/', $rebuilt ); $root_n = str_replace( '\\', '/', $root_real ); if ( $check !== $root_n && ! \brverify_str_starts_with( $check, $root_n . '/' ) ) { return null; } return $candidate; } /** * Parse checksums.sha256 body into lines. * * @param string $body File body. * @return list */ public static function parse_checksum_lines( string $body ): array { $lines = array(); foreach ( preg_split( '/\r\n|\r|\n/', $body ) as $line ) { $line = trim( (string) $line ); if ( '' === $line ) { continue; } if ( ! preg_match( '/^[a-f0-9]{64}\s+.+$/', $line ) ) { continue; } $lines[] = $line; } return $lines; } }