Site Health Check for Publicly Exposed Sensitive Files
Adds a Site Health test that checks whether files such as .env, debug.log, .git/HEAD and wp-config.php backups can be downloaded from your site by anyone, and flags them as critical.
Opens WPIDE on
Code
// Paths (relative to the site root) that should never be public.
$wpide_sensitive_paths = array( '.env', '.git/HEAD', 'wp-content/debug.log', 'wp-config.php.bak', 'wp-config.php.old', 'wp-config.php~', 'wp-config.bak', 'backup.sql', 'database.sql', '.htpasswd', 'phpinfo.php' );
add_filter( 'site_status_tests', function ( $tests ) use ( $wpide_sensitive_paths ) {
$tests['direct']['wpide_exposed_files'] = array(
'label' => 'Exposed sensitive files',
'test' => function () use ( $wpide_sensitive_paths ) {
$exposed = get_transient( 'wpide_exposed_files' );
if ( false === $exposed ) {
$exposed = array();
foreach ( $wpide_sensitive_paths as $path ) {
$response = wp_remote_get( site_url( '/' . $path ), array(
'timeout' => 4,
'redirection' => 0,
'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
'headers' => array( 'Range' => 'bytes=0-255' ),
) );
$code = (int) wp_remote_retrieve_response_code( $response );
$type = (string) wp_remote_retrieve_header( $response, 'content-type' );
if ( in_array( $code, array( 200, 206 ), true ) && false === stripos( $type, 'text/html' ) && '' !== wp_remote_retrieve_body( $response ) ) {
$exposed[] = $path;
}
}
set_transient( 'wpide_exposed_files', $exposed, 12 * HOUR_IN_SECONDS );
}
return array(
'label' => $exposed ? 'Sensitive files can be downloaded publicly' : 'No sensitive files are exposed',
'status' => $exposed ? 'critical' : 'good',
'badge' => array( 'label' => 'Security', 'color' => $exposed ? 'red' : 'blue' ),
'description' => $exposed
? '<p>' . esc_html( 'Anyone can download: ' . implode( ', ', $exposed ) . '. Delete these files or block access to them on your server.' ) . '</p>'
: '<p>None of the common sensitive files could be downloaded.</p>',
'test' => 'wpide_exposed_files',
);
},
);
return $tests;
} );
Notes
Fix problems by deleting the files or blocking them in your server configuration; results are cached for 12 hours.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.