I have a content type with a file field that has been set to "download link".

This works fine for users who are logged in, but anonymous users get the access denied page. Accessing the raw file URL is possible for anonymous users though. I went through all the permissions for anonymous users and all the relevant ones were enabled. After that I just enabled all permissions for anonymous users just to check and that did not work either.

The formatter makes the link become something like this: http://example.com/file/980/download?token=OBVqG5D1va06NDH2_BcgF0vqwVhhN...

Another weird thing is that this issue is not consistently repeatable. I have tested the download link across a number of PCs and it works for some and not for others. I cannot pinpoint the common denominator for those PCs whereby the download links did not work.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

csedax90’s picture

same problem and actually no solutions... please someone check this bug...

TaraRowell’s picture

I am having the identical issue right now - please help!

update:
It seems that the failure originates from here in the the file_entity.pages.inc file:

/**
 * Menu callback; download a single file entity.
 */
function file_entity_download_page($file) {
  // Ensure there is a valid token to download this file.
  if (!variable_get('file_entity_allow_insecure_download', FALSE)) {
    if (!isset($_GET['token']) || $_GET['token'] !== file_entity_get_download_token($file)) {
      return MENU_ACCESS_DENIED;
    }
  }

  // If the file does not exist it can cause problems with file_transfer().
  if (!is_file($file->uri)) {
    return MENU_NOT_FOUND;
  }

  $headers = array(
    'Content-Type' => 'force-download',
    'Content-Disposition' => 'attachment; filename="' . $file->filename . '"',
    'Content-Length' => $file->filesize,
    'Content-Transfer-Encoding' => 'binary',
    'Pragma' => 'no-cache',
    'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
    'Expires' => '0',
    'Accept-Ranges' => 'bytes',
  );

  // Let other modules alter the download headers.
  drupal_alter('file_download_headers', $headers, $file);

  file_transfer($file->uri, $headers);
}

When I do this I get nothing back:

<?php
   $token = variable_get('file_entity_allow_insecure_download', FALSE);
   dpm($token);
   dpm(get_defined_vars());
TaraRowell’s picture

Priority: Normal » Major

update #2

Adding this to settings.php seems to be making an impact:

$conf['file_entity_allow_insecure_download'] = TRUE;

Maybe the variable is just not set anywhere in the module?

Darren Oh’s picture

Status: Active » Needs review
FileSize
816 bytes

file_entity_access() allowed anonymous users to view files but not download them. Attached patch applies view permissions to download as well.

Status: Needs review » Needs work

The last submitted patch, 4: file_entity-anonymous-download-2215247.patch, failed testing.

Dave Reid’s picture

Status: Needs work » Needs review
FileSize
733 bytes

@Darren Oh: This is why there are permissions for downloading the files.

The real problem here is our use of ip_address() for the anonymous token. I realized why do we even care if anonymous users share a download like to a file? If one anonymous user can download, they all should pretty much be able to. The condition we really care about is logged-in users sharing download links between other users than may not have access to those files (or anonymous users). This case should be covered already.

So here's my fix to the token generation, which is to remove the identifier for anonymous users, and add the filesize and filemtime, which means the token would be regenerated for anonymous users only if a file has changed.

Status: Needs review » Needs work

The last submitted patch, 6: 2215247-file-anonymous-download-token-fix.patch, failed testing.

Darren Oh’s picture

Status: Needs work » Needs review
FileSize
887 bytes
628 bytes

Testing requires the token to be generated from a plain string.

Darren Oh’s picture

Fixed test to use modification time and file size.

TaraRowell’s picture

Thank you @Darren Oh! We are testing this patch on a live site now and I will report back.

Anonymous’s picture

Thanks @Darren Oh . @TaraRowell any news?

Proteo’s picture

I've applied the patch from #9 to a live site a and it absolutely fixed the problem, many thanks @Darren Oh.

Anonymous’s picture

Thanks @Proteo, I'm applying it now

TaraRowell’s picture

Sorry for the delay - yes - it works just great!

Anonymous’s picture

Status: Needs review » Patch (to be ported)
Darren Oh’s picture

Status: Patch (to be ported) » Reviewed & tested by the community

Set correct status.

  • Commit d23fcf8 on 7.x-2.x by aaron:
    Issue #2215247 by Darren Oh, Dave Reid: Access denied for anonymous...
aaron’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

stephenplatz’s picture

My field is simply not showing for annon users, even after applying #9. I wonder if this issue is related, checked permissions but still can't get the field to render if I'm not logged-in.

Status: Closed (fixed) » Needs work

The last submitted patch, 9: file_entity-anonymous-token-2215247-8.patch, failed testing.

museumboy’s picture

Solution #2 works for me, but I worry that it leaves some giant security hole in my drupal install.

Dave Reid’s picture

Status: Needs work » Fixed
huijing’s picture

I can verify that the latest version of file_entity with this patch works.

huijing’s picture

Status: Fixed » Closed (fixed)
tomdisher’s picture

I don't see the changes in the latest 7.x 2.x-dev on 20141203.

Edit: Cancel that, I found this issue: https://www.drupal.org/node/2267483