When I switch the schema of an existing file from public to private, or vice versa, a new download token is generated, breaking any existing links to the file that use the previous token. This removes a major benefit of using the download link in content instead of a direct link to the file. I am not sure if this was intentional, but I could definitely see how it could also be unintentional. Is there any reason NOT to preserve this token? Would doing so allow users to download files that they should no longer be able to once a previously public file becomes private?

CommentFileSizeAuthor
#4 file_token_alter-2667278-4.patch1.25 KBfrodri
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

chrisgross created an issue. See original summary.

mstrelan’s picture

Title: Switching file schema breaks download links » Changing file URI breaks download links
Related issues: +#2267483: Remove user ID in download token generation and allow file download URLs to work across all users

This is not limited to file schema. If you replace the file attached to a file entity, and the new file has a different name, it will also have a new URI. The token generation uses the file URI to generate a hash.

substr(drupal_hmac_base64("file/$file->fid/download:" . $file->uri, drupal_get_private_key() . drupal_get_hash_salt()), 0, 8)

Would it be secure enough to remove the file uri from this key?

mstrelan’s picture

FWIW if uuid module was a dependency you could use that instead of the file uri. Or perhaps file_entity_get_download_token() should have an alter hook. Then a contrib module could exist to solve this by relying on uuid.

But my vote would still be to remove the file uri from the hash data. The token exists to prevent people from iterating through every integer and downloading each file by fid. AFAIK a valid token can't be generated without knowing the sites private key and hash salt.

frodri’s picture

This patch adds an alter hook to file_entity_get_download_token(), as suggested by mstrelan.