Cannot programmatically download Attachment

Created on 6 November 2023, 8 months ago
Updated 13 May 2024, about 1 month ago

First, thank-you for an amazing module. Second, I apologize if I've selected the incorrect category; I don't know if this is a Feature request, a Support request, or a Bug.

Issue setup

  • I have a webform containing sensitive information.
  • I am processing the webform submissions via a custom Webform Handler
  • Submissions should either not be saved or only visible to admins while being processed and then purged. I am doing the latter as it seems I cannot access the files for processing unless I save them.
  • Submissions include fields, pdf file uploads, and Attachments
  • I can access and process the pdf file uploads that are saved to the private files folder.
  • I cannot access and process the pdf Attachments via code.

Issues:

  • If I use Attachment URL, the url is always accessed as an Anonymous user via code and is denied access because permissions are set so that submissions are only accessible to Admins.
  • Even if I Switch Accounts programmatically to an Admin user, the url is still always accessed as an Anonymous user via code and is denied access.
  • If I use either Attachment PDF or Attachment URL, and check 'Force users to download the attachment', the Attachment is not downloaded on submissions as I would have expected. I thought this would automatically download the file, or save it to the Public or Private files folders, but it does not. Not sure if this is a bug, or my understanding is incorrect, or I am doing something incorrectly. I have attached the export of a simple webform that generates a PDF attachment with 'Force users to download the attachment' that doesn't download the Attachment as I would expect on submission.

Having read through the issue queues and documentation, I believe this is all working as expected (except perhaps the automatic download).

Summary

  • It seems that the Publicly available Attachment URL conflicts in intent with Private/Restricted access submissions in certain circumstances. I do see this comment: "Private files can not be attached to Webform. The URL https://xxx/sites/default/files/private/ca_xxxx_interactif.pdf must be accessible when you are logged out." Why is this? I don't think this is an unusual use case. See linked related issues.
  • I do not understand how the "Force users to download the attachment" is intended to work for Attachments. Can I automatically or programmatically save/download the Attachment on submission? How do I do that?

Ultimately, I want to download the Attachment and process programmatically, and I'm unable to do this unless permissions to submissions is enabled for Anonymous users, which I can't have.

๐Ÿ’ฌ Support request
Status

Fixed

Version

6.2

Component

Code

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States sassafrass

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @sassafrass
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States sassafrass

    I still have not been able to resolve this. Could someone perhaps guide me on how I could fix this? I am willing to try to write a patch or a custom module, but I'm not sure where to begin.

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada Liam Morland Ontario, CA ๐Ÿ‡จ๐Ÿ‡ฆ

    Is this happening on 6.2.x? 6.1.x is not supported.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States sassafrass

    It was on 6.1, but I'll update to 6.2 and re-test.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States sassafrass

    I have updated my environment to the following:
    Drupal core 10.1.6
    Webform 6.2.0

    I have imported the attached webform and tested a submission. Submission works as expected. Attachment is generated as expected and can be accessed via the submission ui. However, it is not automatically downloaded as expected according to the config (see screenshot).

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada Liam Morland Ontario, CA ๐Ÿ‡จ๐Ÿ‡ฆ
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States cilefen

    When you say โ€œprogrammaticallyโ€ is the custom program a key fixture in the bug reproduction, or is the bug about the inability to download the attachment by any means?

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States sassafrass

    The "programatically" aspect of this ticket is key to me. :-) I have tried the following to access the attachment link from a custom Webform Handler:

    $account = User::load(1);
    $this->accountSwitcher->switchTo($account);
    
    // Save and get the submission results.  This is all successful except when trying to download the attachment with:
    
    file_put_contents($destination, file_get_contents($attachment_url));  // Works if Anonymous users have "Administer webforms" permission.  Doesn't work otherwise.
    
    $this->httpClient->get($source, $this->configuration['guzzle_options']); // Denied access unless Anonymous users have "Administer webforms" permission.
    
    curl url/to/attachment/path  // Administrator has access to download but the file is malformed (the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).
    
    $this->fileSystem->saveData($attachment_url, $destination, FileSystemInterface::EXISTS_REPLACE);  // Administrator has access to download but the file is malformed (the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States sassafrass

    Actually I just tried curl again, and for some reason that worked this time. I don't know what's different.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States cilefen

    In what context is that custom code executing? If it is on the Drupal site, it shouldnโ€™t be necessary to make HTTP calls to obtain webform files.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States sassafrass

    I am writing a custom module with a custom Web Handler on a Drupal site.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States cilefen

    Perhaps if we could โ€œzoom outโ€ and understand succinctly what your requirements are.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States sassafrass

    Thank-you for your patience. :-)

    I want to programmatically download a Webform attachment without having to open up the permissions to access the Webform attachment to anonymous users.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States cilefen

    Why do you need to download attachments when your code is, I think, running on the Drupal instance that has the attachments? You could directly access those files in that context. Where are you intending to download them from or to?

    I am setting this to a support request until we understand this is a bug.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States sassafrass

    I have a client requirement to upload that attachment to their BOX client via an API.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States cilefen

    When you load a submission object, you have access to its files, and I am saying this as someone who has written a relatively big Webform integration module that handles files. I don't see any way you would have to "download" the files to work on them in a submission handler. The files are already in Drupal's file system.

    I hope this makes sense.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States sassafrass

    I can see that when I inspect the webform submission using Xdebug and by looking in Drupal's file system that what you say is true for any files uploaded to the webform by the submitter. However, fields of type attachment that generate a pdf of the submitted form (please see the example form and screenshot), are generated AFTER the form is submitted and does not seem to be available in the file system. I can only access them via the url generated by Webform after the file is generated.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States sassafrass

    I believe this might be related: https://www.drupal.org/project/webform/issues/3419954 ๐Ÿ’ฌ 'Attachment URL' element with tokens gives 'Connection refused for URI' error Active

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States sassafrass

    I can get to this to work using the access token from webform submission. This can be closed.

  • Status changed to Fixed about 2 months ago
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada Liam Morland Ontario, CA ๐Ÿ‡จ๐Ÿ‡ฆ
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024