WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Lock down media files (6 posts)

  1. Patrick Johanneson
    Member
    Posted 1 year ago #

    Hi,

    I've got a "closed" site set up in a Multisite install, where users have to be at least Subscribers in order to see content. What I've found is that any content posted into the Media Library is available if they have the URL -- so, for instance, http://example.com/locked-site/files/secret-document.pdf can be downloaded by someone that's not signed in. I'd like to prevent this.

    I'd like to be able to write a filter, something along the lines of:

    add_filter( '???', 'lock_media' );
    function lock_media( $media ) {
        if( ! current_user_can( 'read' ) ) {
            wp_die( 'Nice try!' );
        }
        return $media;
    }

    Is there a way to filter the content returned by the functions that handle handing files to the user? I took a look in wp-includes/ms-files.php and didn't see any action or filter hooks, but maybe I'm just missing it.

  2. bcworkz
    Member
    Posted 1 year ago #

    The thing is, if someone has the direct url to a file, they are not interacting with WP or even PHP, just the server itself. A filter will not help. So you need to lock down the media folder so the public does not have access to it, but server apps still do, such as WP.

    Then, the only way to access the files is by using a server app or WP. Since using WP is not possible in your case unless the user is logged in, this should suffice.

    I'm no expert in this sort of thing, so there may be some nuance I'm missing, but I'm confident the general intent of my statements are correct. With that disclaimer, try setting the folder and file permissions to 600 and see how that works. If all else fails, you can also block public access to files via .htaccess files.

  3. Patrick Johanneson
    Member
    Posted 1 year ago #

    I hoped I'd be able to affect it somehow since the .htaccess file points requests to '/files/.*' to the ms-files.php file (I think; right now I don't have access to the actual .htaccess file since it's behind a firewall). But I didn't see any action or filter hooks in the ms-files.php file; I'd hoped I'd just missed one.

    I will certainly try setting the files directories to 600 and see if that works.

    Thanks!

  4. vjpo
    Member
    Posted 1 year ago #

  5. bcworkz
    Member
    Posted 1 year ago #

    I will certainly try setting the files directories to 600 and see if that works.

    No, that won't work at all. My head was completely in the wrong place. (It was really warm and dark in there) My apologies. The only realistic solution lies in .htaccess. Checking for a cookie is probably the best approach, as in vjpo's first link. Using the referer field as suggested in the second link is a little too easy to circumvent IMO.

    Even the cookie can be circumvented since .htaccess cannot check the fingerprint that makes it secure, but at least circumvention is obscure enough to make it unlikely to be attempted by nearly all users.

  6. Patrick Johanneson
    Member
    Posted 1 year ago #

    OK, I got it working (at least as far as my userbase goes):

    I added this to the .htaccess file:

    # BEGIN file lockdowns
    RewriteCond %{HTTP_COOKIE} !^.*wordpress_logged_in.*$ [NC]
    RewriteRule ^sitename/files/.*\.(pdf|docx?)$ /sitename/not-allowed [R=302,L]
    # END file lockdowns

    (as seen in the top-frog.com posting from vjpo above)

    -- and then made a page at /sitename/not-allowed that asks the user to please log in.

    This won't block people from going after the /wp-content/blogs.dir/[blog_id]/[filename].pdf file, but it's sufficient to keep accidental viewing of private files locked down.

Topic Closed

This topic has been closed to new replies.

About this Topic