Skip to content

fix(messaging): require files.read to attach images to push messages (appwrite:appwrite:alert#4625) - #15182

Closed
claudear wants to merge 1 commit into
mainfrom
fix/push-image-requires-files-read
Closed

claudear wants to merge 1 commit into
mainfrom
fix/push-image-requires-files-read

Conversation

@claudear

@claudear claudear commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

What

POST /v1/messaging/messages/push and PATCH /v1/messaging/messages/push/:messageId now reject an image when the caller's scopes do not include files.read, mirroring the email attachments gate added in 90deb7e.

Why

Code-scanning alert #4625.

The image parameter takes an arbitrary <BUCKET_ID>:<FILE_ID>, looks the bucket and file up directly, validates only that the mime type is PNG or JPEG, and then signs a JWT for the public-scoped push-file endpoint. The resulting URL is stored in the message's data and returned verbatim in the create/update response (and through messages.read).

A draft is enough — no targets, topics, users or provider are required — and project API keys and console admin sessions run with authorization disabled (app/controllers/shared/api.php), so document permissions were never consulted. A key limited to messages.write could therefore read any PNG or JPEG in any bucket of the project through an unauthenticated URL, bypassing files.read, bucket fileSecurity and per-file permissions.

The email attachments path was fixed for exactly this; the push image path is the second call site and was missed.

Changes

  • Push/Create.php — throw GENERAL_UNAUTHORIZED_SCOPE when image is supplied without files.read.
  • Push/Update.php — same check, and it also fires when the stored message already carries an image, because the handler re-signs the URL (and refreshes its expiry from the current schedule) on every update.
  • MessagingCustomServerTest::testPushImageRequiresFilesRead — e2e regression test alongside the existing testEmailAttachmentsRequireFilesRead.

Testing

docker compose exec appwrite test tests/e2e/Services/Messaging
# OK (176 tests, 2555 assertions, 24 skipped)

The new test fails on main (the restricted key gets 201 and a signed image URL) and passes with the fix. pint, phpstan and rector --dry-run are clean.

Deliberately not changed

The alert also suggests re-checking file readability under the caller's authorization and shortening the token lifetime. Neither is addressed here:

  • A readability check would be a no-op for the only actors this endpoint accepts (AuthType::ADMIN, AuthType::KEY), since both run with Authorization disabled; the scope is the real control, and the email fix takes the same approach.
  • APP_PUSH_IMAGE_TOKEN_DURATION (15 days from delivery) is deliberate — devices fetch the image after the notification lands — and its semantics are asserted by MessagingBase::testCreateDraftPushWithImage. Shortening it, or deferring minting to the messaging worker, is a product change worth doing separately.

🤖 Generated with Claude Code

The push image is served through a signed, public URL stored in the
message data, so attaching a file needs the same scope as reading it.
The email attachment path already requires files.read; the push image
path was missed.

Co-Authored-By: Claude Opus 5 <[email protected]>
@luxor-code-review

luxor-code-review Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Review complete. No blocking issues — approved ✅; 3 nitpicks below.

🧹 Nitpicks (3) — 🟢 3 low
  • 🟢 MIME allowlist omits bmp promised by parameter docs (Create.php:204) — The image parameter description promises jpeg, png, or bmp images, but the enforcement allowlist only accepts image/png and image/jpeg, so valid bmp files are rejected with STORAGE_FILE_TYPE_UNSUPPORTED (src/Appwrite/Platform/Modules/Messaging/Http/Messages/Push/Create.php:204).
  • 🟢 CompoundUID accepts more than two segments (CompoundUID.php:28) — CompoundUID::isValid only rejects fewer than 2 segments, so bucketId:fileId:extra passes validation while CompoundUID::parse returns 3 elements (src/Appwrite/Utopia/Database/Validator/CompoundUID.php:28).
  • 🟢 Push-image cache lifetime exceeds JWT validity window (Get.php:158) — The push-image JWT expires at delivery + APP_PUSH_IMAGE_TOKEN_DURATION (15 days, app/init/constants.php:370), but the push endpoint responds with Cache-Control: private, max-age=3888000 — 45 days (src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Push/Get.php:158).

The PR lets push messages carry an image: on create and update, a bucketId/fileId compound UID is validated, checked against the files.read scope when an API key is used, and turned into a short-lived JWT-signed storage URL embedded in the message data. E2E tests cover the console flow. A few low-severity gaps remain around the documented MIME allowlist and JWT/URL lifetime coupling.

Files Change
src/Appwrite/Platform/Modules/Messaging/Http/Messages/Push/{Create,Update}.php Parse image compound UID, enforce files.read scope, build signed URL into message data
tests/e2e/Services/Messaging/MessagingCustomServerTest.php E2E coverage for push messages with images
src/Appwrite/Utopia/Database/Validator/CompoundUID.php, src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Push/Get.php Supporting validator and signed push-image serving endpoint touched by the flow

Reviewed commit: 210c522

@ghost

ghost commented Nov 2, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The new scope checks cover both image attachment and update-time URL re-signing, with regression coverage for the denied and allowed cases.

Push message creation and updates now require the files.read scope when an image is attached or an existing image is re-signed. The Messaging E2E suite adds coverage for allowed and denied create/update requests and verifies a rejected update leaves the draft unchanged.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 3
File Change
src/Appwrite/Platform/Modules/Messaging/Http/Messages/Push/Create.php Injects the request and checks for files.read before looking up and signing an attached image.
src/Appwrite/Platform/Modules/Messaging/Http/Messages/Push/Update.php Injects the request and checks for files.read when the requested or stored message data contains an image.
tests/e2e/Services/Messaging/MessagingCustomServerTest.php Adds E2E coverage for image scope checks on push message creation and updates.

Reviewed 210c522 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

6 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (3)
  • php.appwrite.secret-compare-timing (1)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/push-image-requires-files-read (after).

Metric Before After Change
🚀 Requests/sec 183.94 187.62 ⚪ +2%
⏱️ Latency P50 92.71 ms 91.44 ms ⚪ -1.4%
⏱️ Latency P95 224.2 ms 218.35 ms ⚪ -2.6%
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 91.44 218.35 11,628 187.62 -5.85
Account 171.2 313.58 612 10.16 -55.13
TablesDB 88.43 172.42 6,324 103.89 +5.94
Storage 82.78 208.26 3,060 52.5 +15
Functions 127.89 256.51 1,632 28.78 -53.36

Top API waits (after)

API request Max wait (ms)
account.name.update 520.66
tablesdb.rows.create 429.94
functions.create 426.32
functions.variables.update 414.82
account.prefs.update 409

@ChiragAgg5k

Copy link
Copy Markdown
Member

Superseded: the files.read gate for push images is already on main.

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