JSON lines from the PHP upload temp file only. Never moves * the file and never writes anything. Never unserializes any value (F8). */ final class Export_File_Reader { const MAX_BYTES = 2097152; const MAX_LINES = 20000; const MAX_LINE_LENGTH = 65536; const JSON_DEPTH = 8; const EXTENSIONS = array( 'txt', 'tsv', 'wld' ); const MIME_TYPES = array( 'text/plain', 'text/tab-separated-values', 'application/octet-stream', '' ); /** * Read an uploaded file array ($_FILES entry). * * @param array $file Upload entry. * @return array{ok:bool, error?:string, entries?:array, rejected?:array} */ public static function read_upload( array $file ) { if ( ! isset( $file['tmp_name'], $file['name'], $file['size'] ) || ! empty( $file['error'] ) ) { return array( 'ok' => false, 'error' => 'upload_error', ); } $tmp = (string) $file['tmp_name']; if ( ! is_uploaded_file( $tmp ) && ! ( defined( 'GATEFORGE_TESTING' ) && GATEFORGE_TESTING ) ) { return array( 'ok' => false, 'error' => 'not_an_upload', ); } $ext = strtolower( pathinfo( (string) $file['name'], PATHINFO_EXTENSION ) ); if ( ! in_array( $ext, self::EXTENSIONS, true ) ) { return array( 'ok' => false, 'error' => 'extension_not_allowed', ); } $type = isset( $file['type'] ) ? strtolower( (string) $file['type'] ) : ''; if ( ! in_array( $type, self::MIME_TYPES, true ) ) { return array( 'ok' => false, 'error' => 'type_not_allowed', ); } if ( (int) $file['size'] > self::MAX_BYTES || filesize( $tmp ) > self::MAX_BYTES ) { return array( 'ok' => false, 'error' => 'too_large', ); } $content = file_get_contents( $tmp ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- local PHP upload temp file, read-only. if ( false === $content ) { return array( 'ok' => false, 'error' => 'unreadable', ); } return self::parse( $content ); } /** * Parse file content. * * @param string $content Content. * @return array */ public static function parse( $content ) { if ( strlen( $content ) > self::MAX_BYTES ) { return array( 'ok' => false, 'error' => 'too_large', ); } if ( 0 === strpos( $content, "\xEF\xBB\xBF" ) ) { $content = substr( $content, 3 ); } $lines = preg_split( '/\r\n|\r|\n/', $content ); if ( count( $lines ) > self::MAX_LINES ) { return array( 'ok' => false, 'error' => 'too_many_lines', ); } $entries = array(); $rejected = array(); foreach ( $lines as $n => $line ) { if ( '' === trim( $line ) || '[START=WIDGET LOGIC OPTIONS]' === trim( $line ) || '[STOP=WIDGET LOGIC OPTIONS]' === trim( $line ) ) { continue; } if ( strlen( $line ) > self::MAX_LINE_LENGTH ) { $rejected[] = array( 'line' => $n + 1, 'reason' => 'line_too_long', ); continue; } $tab = strpos( $line, "\t" ); if ( false === $tab ) { $rejected[] = array( 'line' => $n + 1, 'reason' => 'no_tab', ); continue; } $key = substr( $line, 0, $tab ); $val = json_decode( substr( $line, $tab + 1 ), true, self::JSON_DEPTH ); if ( JSON_ERROR_NONE !== json_last_error() ) { $rejected[] = array( 'line' => $n + 1, 'reason' => 'invalid_json', ); continue; } if ( ! preg_match( '/^[A-Za-z0-9_\-]{1,191}$/', $key ) ) { $rejected[] = array( 'line' => $n + 1, 'reason' => 'bad_key', ); continue; } // R-2: check the decoded type here, so nothing is skipped silently later. // Widget entries must be strings; Widget Logic's own option lines // (widget_logic-options-*) and its "msg" line may hold any scalar. $own = 0 === strpos( $key, 'widget_logic-options-' ) || 'msg' === $key; if ( ! is_string( $val ) && ! ( $own && ( null === $val || is_scalar( $val ) ) ) ) { $rejected[] = array( 'line' => $n + 1, 'key' => $key, 'reason' => 'value_not_string', 'type' => gettype( $val ), ); continue; } $entries[ $key ] = $val; } return array( 'ok' => true, 'entries' => $entries, 'rejected' => $rejected, ); } }