Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot create empty file - breaks WooCommerce Admin compatibility #24

Open
marsjaninzmarsa opened this issue Mar 25, 2022 · 0 comments
Open

Comments

@marsjaninzmarsa
Copy link

marsjaninzmarsa commented Mar 25, 2022

Hi, there is a compatibility issue between your plugin and WooCommerce Admin reports export functionality. Basically, your stream wrapper, even when running on filesystem locally, using fopen and whole rest of the team, doesn't preserve its behavior when it comes to handling empty files, preventing it from being created at all and silently breaking whole export functionality, which depends on handling previously created, empty files to write to.

Steps to reproduce:

  1. Have existing running WooCommerce store with WooCommerce Admin and some transactions history for export (more than 25/one page) to trigger multipass export flow
  2. Go to Statistics, click "export"
  3. Observe empty uploads/woocommerce_uploads/reports/index.html not being created (serious security risk) and .csvs not being generated at all, besides of properly generated and saved, not-empty .htaccess, and link to non-existing file being emailed to your user email.

or, if you don't have a shop running, just run this code from your functions/mu-plugin to test just it:

<?php
$upload_dir = wp_upload_dir();
$reports_dir =  trailingslashit( $upload_dir['basedir'] ) . 'woocommerce_uploads/reports/';
$files = array(
    array(
        'base'    => $reports_dir,
        'file'    => '.htaccess',
        'content' => 'DirectoryIndex index.php index.html' . PHP_EOL . 'deny from all',
    ),
    array(
        'base'    => $reports_dir,
        'file'    => 'index.html',
        'content' => '',
    ),
);


foreach ( $files as $file ) {
    if ( ! file_exists( trailingslashit( $file['base'] ) ) ) {
        wp_mkdir_p( $file['base'] );
    }
    if ( ! file_exists( trailingslashit( $file['base'] ) . $file['file'] ) ) {
        $file_handle = @fopen( trailingslashit( $file['base'] ) . $file['file'], 'wb' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_read_fopen
        if ( $file_handle ) {
            fwrite( $file_handle, $file['content'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fwrite
            fclose( $file_handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
        }
    }
}

(based on woocommerce/packages/woocommerce-admin/src/ReportCSVExporter.php:maybe_create_directory, entry point for export functionality)

Tested on v0.5.3 and v0.7.0, breaks just the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant