Make WordPress Core


Ignore:
Timestamp:
05/02/2018 02:32:57 AM (7 years ago)
Author:
SergeyBiryukov
Message:

Privacy: Add cron to delete expired export files to protect privacy.

The primary means of protecting the files is the CSPRN appended to the filename, but there is no reason to keep the files after the data subject has downloaded them, so deleting them provides an additional layer of protection. Previously this was done from wp_privacy_generate_personal_data_export_file(), but that does not guarantee that it will be run regularly, and on smaller sites that could result in export files being exposed for much longer than necessary.

wp_privacy_delete_old_export_files() was moved to a front end file, so that it can be called from cron.php.

This introduces the wp_privacy_export_expiration filter, which allows plugins to customize how long the exports are kept before being deleted.

index.html was added to the $exclusions parameter of list_files() to make sure that it isn't deleted. If it were, then poorly-configured servers would allow the directory to be traversed, exposing all of the exported files.

Props iandunn, desrosj.
Merges [43046] to the 4.9 branch.
See #43546.

Location:
branches/4.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

  • branches/4.9/src/wp-includes/functions.php

    r43082 r43095  
    59355935    return apply_filters( 'wp_privacy_anonymize_data', $anonymous, $type, $data );
    59365936}
     5937
     5938/**
     5939 * Schedule a `WP_Cron` job to delete expired export files.
     5940 *
     5941 * @since 4.9.6
     5942 */
     5943function wp_schedule_delete_old_privacy_export_files() {
     5944    if ( ! wp_next_scheduled( 'wp_privacy_delete_old_export_files' ) ) {
     5945        wp_schedule_event( time(), 'hourly', 'wp_privacy_delete_old_export_files' );
     5946    }
     5947}
     5948
     5949/**
     5950 * Cleans up export files older than three days old.
     5951 *
     5952 * The export files are stored in `wp-content/uploads`, and are therefore publicly
     5953 * accessible. A CSPRN is appended to the filename to mitigate the risk of an
     5954 * unauthorized person downloading the file, but it is still possible. Deleting
     5955 * the file after the data subject has had a chance to delete it adds an additional
     5956 * layer of protection.
     5957 *
     5958 * @since 4.9.6
     5959 */
     5960function wp_privacy_delete_old_export_files() {
     5961    $upload_dir   = wp_upload_dir();
     5962    $exports_dir  = trailingslashit( $upload_dir['basedir'] . '/exports' );
     5963    $export_files = list_files( $exports_dir, 100, array( 'index.html' ) );
     5964
     5965    /**
     5966     * Filters the lifetime, in seconds, of a personal data export file.
     5967     *
     5968     * By default, the lifetime is 3 days. Once the file reaches that age, it will automatically
     5969     * be deleted by a cron job.
     5970     *
     5971     * @since 4.9.6
     5972     *
     5973     * @param int $expiration The expiration age of the export, in seconds.
     5974     */
     5975    $expiration = apply_filters( 'wp_privacy_export_expiration', 3 * DAY_IN_SECONDS );
     5976
     5977    foreach ( (array) $export_files as $export_file ) {
     5978        $file_age_in_seconds = time() - filemtime( $export_file );
     5979
     5980        if ( $expiration < $file_age_in_seconds ) {
     5981            unlink( $export_file );
     5982        }
     5983    }
     5984}
Note: See TracChangeset for help on using the changeset viewer.