# Bug OP-20819 - attachments deleted from S3 when ClamAV is enabled

Project: OpenProject
Status: closed
URL: https://community.openproject.org/work_packages/OP-20819

## Open points

_No work packages_

## Details

| Attribute | Value |
| --- | --- |
| Date |  |
| Priority | Immediate |
| Module | Modules and project features / Attachments |
| Target versions | 18.0.1, 17.9.10 |
| Observed in versions | 17.3.0 |
| Sprint |  |
| Backlog bucket |  |
| Bug found in version (deprecated) |  |
| Found in mobile app version |  |
| Follow-up feedback e-mail |  |
| Requires doc change | No |
| Accessibility | No |
| Pull preview |  |
| List |  |
| Labels |  |

## People

| Attribute | Value |
| --- | --- |
| Assignee | Arthur Bonic |
| Accountable | OP Dev Titan Team |
| Product manager |  |
| Developers | Arthur Bonic |
| Designer |  |
| Ordered by |  |
| Signed off by |  |
| Interest by |  |

## Planning

| Attribute | Value |
| --- | --- |
| Relevance |  |

## Costs

| Attribute | Value |
| --- | --- |
| Votes |  |
| Work |  |
| Labor costs |  |

## Description

###   

### Steps to reproduce

_Please write down the steps to reproduce. Try to write down all necessary preconditions (what permissions do you have, are other users involved?). Example:_

1.  Enable S3 Storage in Openproject
    
2.  Enable Clamav in openproject
    
3.  Upload file to S3 Storage in openproject
    
    <br>
    

### What is the buggy behavior?

_Please describe the bug in as much detail as possible. Example:_

*   When using S3 (fog) attachment storage with ClamAV virus scanning enabled, uploaded files are correctly stored in S3 initially but are deleted after the background virus scan completes. Subsequent attempts to download the attachment return `NoSuchKey`.
    

### What is the expected behavior?

_Describe how the application should behave like. Example:_

1.  The application should invite the new user with the selected role
    
2.  An email should have been sent to that user
    

###   

### **Logs**

_&lt;Error&gt;  
&lt;Code&gt;NoSuchKey&lt;/Code&gt;  
&lt;Message&gt;The specified key does not exist.&lt;/Message&gt;  
&lt;Key&gt;  
uploads/attachment/file/8/XXX-2026-08-25T10h29m04Z.txt  
&lt;/Key&gt;  
&lt;BucketName&gt;openproject&lt;/BucketName&gt;  
&lt;Resource&gt;  
/openproject/uploads/attachment/file/8/XXXXXX-2026-08-25T10h29m04Z.txt  
&lt;/Resource&gt;  
&lt;RequestId&gt;18E924A4E991A773&lt;/RequestId&gt;  
&lt;HostId&gt;  
dd9025bab4ad464b049177c95eb6ebf374d3b3fd1af9251148b658df7ac2e3e8  
&lt;/HostId&gt;  
&lt;/Error&gt;  
_  

<br>

<br>

### Screenshots and other files

<br>

## **The sequence**

### **1\. Upload**

The file is stored in S3 at `uploads/attachment/file/<id>/<filename>` and is accessible. The attachment gets `status: uploaded`, and `Attachments::VirusScanJob` is queued (`app/models/attachment.rb`, `enqueue_jobs`).

### **2\. Scan downloads the file and caches the uploader**

`Attachments::ClamAVService#scan` calls `attachment.diskfile`:

```ruby
# app/services/attachments/clamav_service.rb
def scan(attachment)
  file = attachment.diskfile
  clamav_client.execute(ClamAV::Commands::InstreamCommand.new(file))
end
```

`diskfile` calls `FogFileUploader#local_file`, which calls CarrierWave&#x27;s `cache_stored_file!`. In CarrierWave 2.2.7 that method is just `cache!` with no argument. OpenProject overrides `cache!`:

```ruby
# app/uploaders/fog_file_uploader.rb
def cache!(new_file = file)   # new_file = the REMOTE S3 file object
  @move_new_file_to_cache = new_file.is_a?(MovableSource)
  super
end

# app/uploaders/file_uploader.rb
def cache!(new_file = sanitized_file)
  super
  @old_tmp_file = new_file     # remembers the S3 object as a "temp file"
end
```

Downloading the file for scanning leaves the uploader in a &quot;cached&quot; state (`@cache_id` set), and `@old_tmp_file` now points at the real S3 object.

### **3\. Status update stores the file again**

The scan comes back clean, and the job runs:

```ruby
# app/workers/attachments/virus_scan_job.rb
def handle_success_response(attachment)
  attachment.update!(status: :scanned)
end
```

Because `update!` runs callbacks, CarrierWave&#x27;s `after_commit :store_file!, on: [:create, :update]` fires (`carrierwave/orm/activerecord.rb`). The uploader is cached, so `Uploader::Store#store!` really runs: it uploads the cached local copy again to the same key and deletes the local cache copy (`delete_tmp_file_after_storage`).

### **4\. Cleanup callback deletes the S3 object**

`FogFileUploader` registers `after :store, :delete_old_tmp_file`:

```ruby
# app/uploaders/file_uploader.rb
def delete_old_tmp_file(_dummy)
  @old_tmp_file.try :delete      # Fog file -> S3 DELETE on the same key
end
```

`@old_tmp_file` is the S3 object from step 2, so this sends a DELETE for `uploads/attachment/file/<id>/<filename>`, the key that was just uploaded again. The object is gone, and the next download returns `NoSuchKey`.

### **Affected paths**

*   Clean scan: `update!(status: :scanned)`
    
*   Virus found, action &quot;quarantine&quot;: `update!(status: :quarantined)`
    
*   Rescan job (`Attachments::VirusRescanJob`), which uses the same `scan_attachment`
    

Without ClamAV nothing calls `diskfile` and then saves the record. Fulltext extraction also downloads the file, but it writes with `update_all`, which runs no callbacks, so it is not affected.

## **Recommendations**

### **Immediate**

Turn virus scanning off until a fix is in place. Otherwise every new upload is lost after the scan.

### **Recovering lost files**

Only possible if **versioning is enabled on the S3 bucket**. In that case the object is hidden behind a delete marker, and removing the marker restores it. Without versioning the files cannot be recovered, because the local cache copy is also deleted in step 3.

### **Code fix (any one of these resolves it)**

1.  **Root cause.** In `FileUploader#cache!`, don&#x27;t remember a remote object as a temp file:
    

```ruby
@old_tmp_file = new_file unless new_file.is_a?(CarrierWave::Storage::Fog::File)
```

1.  This also protects any other code path that calls `diskfile` and then saves the attachment.
    
2.  **Scan job only.** In `VirusScanJob`, write the status with `attachment.update_columns(status: ...)` instead of `update!`, so no CarrierWave callbacks run. The underlying problem in the uploader remains.
    
3.  **Scan service only.** In `ClamAVService#scan`, copy the file into a `Tempfile` with `attachment.file.stream_to(tempfile)` and scan that, so the uploader is never cached.
    

### Environment information

_Please check and fill out the following details to help us identify in what versions and distributions of OpenProject the error occurs_

**OpenProject installation type**

*   [ ] Cloud edition
*   [x] On-Premise
*   [ ] Packaged installation
*   [ ] Docker All-in-one container
*   [ ] Docker-compose installation
*   [x] installation from opendesk

**OpenProject version**

_v17.9.7_

<br>

**Browser**

*   [x] Chrome
*   [x] Firefox
*   [ ] Safari
*   [ ] Mobile Safari
*   [ ] Other (please specify)

**Operating System**

*   [x] Windows
*   [ ] Mac OS X
*   [ ] Mobile iOS
*   [ ] Mobile Android
*   [ ] Linux (please specify distro)
*   [ ] Chrome OS
*   [ ] Other (please specify)

**Language**

*   [ ] English
*   [x] German
*   [ ] Other:
