Skip to content

fix(storage): stop rejecting clean uploads the scanner could not scan - #13600

Merged
ChiragAgg5k merged 2 commits into
mainfrom
fix/clamav-scan-failure-main
Sep 10, 2026
Merged

ChiragAgg5k merged 2 commits into
mainfrom
fix/clamav-scan-failure-main

Conversation

@abnegate

Copy link
Copy Markdown
Member

Tests / E2E / Antivirus fails 2 runs in 19 on main with

StorageCustomServerTest::testCreateBucketFileChunkedUploadWithAntivirus
Failed asserting that 403 matches expected 201.

and, in the container log, INSTREAM: Size limit reached, (requested: 247036) — a length read out of file bytes rather than a real size.

Why

appwrite/php-clamav 2.0.0 sizes every INSTREAM frame at the fixed chunk length rather than the bytes actually read, and loops on !feof(), which only becomes true after a read has hit the end. The trailing read returns '' and pack('Na8192', 8192, '') still declares a full chunk:

file bytes sent to ClamAV
1,000 2,000
8,192 16,384
9,192 16,384

The framing then desyncs, ClamAV reads file content as a 4-byte length prefix, and answers with an error instead of a verdict. 2.0.1 fixed the framing; 2.0.2 added a result type. Both are taken here.

The half that matters even once the framing is right

fileScanInStream() returned a bare bool, so a scan that could not happen answered exactly like a detection:

clamd reply before
stream: OK true
stream: Test.Sig FOUND false
INSTREAM size limit exceeded. ERROR false

Create.php read that false, deleted the uploaded file and returned 403 STORAGE_INVALID_FILE — "The uploaded file is invalid". Someone whose file was fine was told it was not, and the file was gone.

Now a detection keeps its 403 and its delete, and a scan that failed returns 500 with the daemon's own reply and leaves the upload alone.

Establishing the trigger

Two plausible triggers were checked and are not it, so they are not worth re-investigating:

  • A mid-run database reload does not cause it. 120 scans of a clean file taken across repeated clamdscan --reload: 120 true, zero failures, zero throws.
  • The startup window does not either. On a cold clamav/clamav:1.4-debian, ping() and a clean scan go green at the same moment (14.8 s).

What produces it is any reply that is neither OK nor ... FOUND.

Verified

Against clamd 1.4, using a custom signature rather than EICAR (developer machines delete EICAR files), with StreamMaxLength lowered to force a refusal:

case                               status     signature
clean file                         clean      -
detected (custom signature)        infected   Appwrite.Test.Marker.UNOFFICIAL
oversized (StreamMaxLength 20K)    failed     -

Not verified

No E2E covers the failed-scan branch. Reaching a reply-level refusal needs StreamMaxLength below APP_LIMIT_ANTIVIRUS, and they default to 25M and 20M — so the only way to trigger it in the Antivirus lane would be to lower the shared daemon's limit, which would stop that lane testing the configuration we ship. The classification itself is covered upstream in php-clamav, where the tests fail on the previous semantics with the size refusal reporting infected where failed is expected.

Note also that with stock configuration the size-limit path is not reachable — 20M sits under the 25M default. What made it reachable was the doubling, which is fixed here. It returns if anyone lowers StreamMaxLength below 20M or raises APP_LIMIT_ANTIVIRUS.

🤖 Generated with Claude Code

php-clamav 2.0.0 sizes every INSTREAM frame at the fixed chunk length
rather than the bytes actually read, and loops on !feof(), which is only
true after a read has hit the end. A file is therefore sent roughly
doubled and NUL-padded, and the framing desyncs: ClamAV reads file
content as a length prefix and answers with an error rather than a
verdict.

  file 1000 bytes -> 2000 bytes sent
  file 8192 bytes -> 16384 bytes sent
  file 9192 bytes -> 16384 bytes sent

2.0.1 fixed the framing, 2.0.2 added a result type. Both are taken here.

The second half matters on its own. fileScanInStream() answered false for
a detection and for a scan that never happened alike, and this acted on
the second as the first: it deleted the upload and returned 403 "The
uploaded file is invalid". Someone whose file was fine was told it was
not, and the file was gone. A detection now keeps its 403 and its delete;
a scan that failed returns 500 with the daemon's own reply and leaves the
upload alone.

This is live on main: `Tests / E2E / Antivirus` fails 2 runs in 19 with
`INSTREAM: Size limit reached, (requested: 247036)` -- a length read out
of file bytes -- surfacing as `403 matches expected 201`.

Verified against clamd 1.4 with a custom signature rather than EICAR, and
StreamMaxLength lowered to force a refusal:

  clean file                     clean      -
  detected (custom signature)    infected   Appwrite.Test.Marker.UNOFFICIAL
  oversized                      failed     -

No E2E covers the failed-scan branch. Reaching a reply-level refusal
needs StreamMaxLength below APP_LIMIT_ANTIVIRUS, and they default to 25M
and 20M, so the only way to trigger it in that lane would be to
reconfigure the shared daemon and stop it testing the configuration we
ship. The classification itself is covered in php-clamav, where the tests
fail on the previous semantics with the size refusal reporting 'infected'
where 'failed' is expected.

Co-Authored-By: Claude Opus 5 <[email protected]>
@greptile-apps

greptile-apps Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge, with the previously reported orphaned-upload issue resolved and no new actionable defects identified.

Summary

  • Clean uploads are accepted when ClamAV returns a clean verdict.
  • Infected uploads are deleted and rejected as invalid.
  • Failed scans delete the unrecorded finalized upload and return a server error containing the daemon reply.
  • The follow-up change resolves the previously reported orphaned-upload behavior.

Reviews (2) · Last reviewed commit: "fix(storage): delete finalized uploads w..."

@github-actions

github-actions Bot commented Sep 10, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/clamav-scan-failure-main (after).

Metric Before After Change
🚀 Requests/sec 214.1 213.19 ⚪ -0.4%
⏱️ Latency P50 82.55 ms 81.59 ms ⚪ -1.2%
⏱️ Latency P95 188.79 ms 191.69 ms ⚪ +1.5%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 81.59 191.69 13,566 213.19 +2.9
Account 162.11 296.97 714 11.81 +17.94
TablesDB 78.68 148.41 7,378 118.69 +0.78
Storage 75.66 162.26 3,570 59.01 -4.2
Functions 120.84 229.8 1,904 32.17 +13.62

Top API waits (after)

API request Max wait (ms)
account.name.update 449.13
account.prefs.update 427.84
tablesdb.rows.delete 390.61
functions.delete 360.76
functions.create 343.13

@ChiragAgg5k
ChiragAgg5k merged commit f34c1c1 into main Sep 10, 2026
45 of 46 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/clamav-scan-failure-main branch September 10, 2026 11:47
@ChiragAgg5k ChiragAgg5k mentioned this pull request Sep 10, 2026
2 of 4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants