Skip to content

fix(audits): send only the user fields audit workers read - #14509

Merged
levivannoort merged 1 commit into
mainfrom
fix/lean-audit-message
Oct 15, 2026
Merged

levivannoort merged 1 commit into
mainfrom
fix/lean-audit-message

Conversation

@levivannoort

Copy link
Copy Markdown
Member

What does this PR do?

Audit messages on the v1-audits queue carried data no consumer needs: sensitive user attributes, and response or document bodies in payload. That queue is replicated and keeps dead letters for days, so those copies lived well beyond the request.

#14477 already narrowed user and impersonatorUser to UserContext when the message is serialised. This PR finishes the job:

  • payload is no longer queued. No consumer reads it. CE has had no audits worker since the usage/audit workers were removed, and Audit publishing is a no-op on self-hosted. The one consumer is the Cloud audits worker, which writes data => '' and never reads payload. No route in CE or Cloud sets the audits.payload label, so the block in app/controllers/shared/api.php that copied the response body was dead, and this PR removes it. Cloud's billing tasks pass whole invoice, payment and credit documents as payload directly. Those no longer reach the queue.
  • fromArray() narrows users as well. A message published before this change still holds the full user document. It now gets reduced to the UserContext fields when it is read, so a worker or a retry/replay of an old message never passes the extra attributes on.

What stays on the message, and why:

Field Reader
user.{$id,$sequence,name,email,type} Cloud audits worker (actorId, actorInternalId, actorName, actorEmail, actorType)
impersonatorUser.{$id,$sequence,name,email,type} No reader today. Kept at the same five fields so the impersonation trail survives
project (ProjectContext), event, resource, mode, ip, userAgent, hostname, sdk, sdkVersion Cloud audits worker

Compatibility

  • Old messages in the queue (full user documents, payload, no sdk fields) still parse. Extra fields get dropped, missing ones default to empty. testReadsMessagesQueuedBeforeTheChange and testReadsMessagesWithMissingFields cover both cases.
  • New messages read by an old consumer have no payload key. The old fromArray() already defaulted it to [].
  • Producers can still pass payload: because the constructor parameter stays (now optional, default []). It just isn't serialised. That way the Cloud billing producers and their tests keep compiling after the next server-ce bump. They can stop passing it in a follow-up, and then the parameter can go.

Test Plan

tests/unit/Event/Message/AuditTest.php builds the message from a user document filled in the way a stored one is: password hash, hashOptions, mfa, mfaRecoveryCodes, authenticators, sessions, tokens, memberships, targets, prefs. It also includes a response body. The tests check that:

  • the serialised user and impersonatorUser are exactly the five context fields, and none of the sensitive keys is present;
  • the JSON message contains no payload and none of the secret values;
  • a JSON round trip through fromArray() still gives the worker everything it reads;
  • a legacy message, and one with missing fields, still parse, and re-serialising a legacy message does not carry the old attributes forward.

Seen red: on main, 3 of the 5 tests fail (payload is queued and read back). With Audit.php from before #14477, 4 of 5 fail (the user-field test fails too). All pass with this change.

  • vendor/bin/phpunit --testsuite unit in appwrite/base:2.1.0: 1825 tests pass. The only error is KeyTest::testDecode, which needs _APP_OPENSSL_KEY_V1, and it passes when that is set.
  • Pint, PHPStan (repo config) and Rector --dry-run are clean on the changed files.

There is no E2E test for the producer-to-queue-to-Cloud-worker path in this repo.

Related PRs and Issues

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs? (n/a)

@luxor-code-review

luxor-code-review Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Review complete. No blocking issues — approved ✅; 1 nitpick below.

🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 Dead payload plumbing still fed by producers (Audit.php) — Audit::toArray() no longer serializes payload, yet Notifications/Logos/Appwrite/Get.php still builds a structured notification-view payload that can never reach the queue.

This change stops queueing potentially sensitive data with audit events: response bodies are no longer attached via the audits.payload route label, and the Audit message serializes only a narrow user projection instead of the full user document (which could include password hashes, tokens, and MFA recovery codes). Legacy queued messages are still parsed and re-sanitized by fromArray, so retry/dead-letter replay does not reintroduce the dropped fields, and a new unit test locks this contract. One producer (notifications.view audit) still passes structured payload data that the message now silently drops, which is flagged below. The codebase no longer references the removed label, and consumers in this repository never read the removed fields.

Reviewed commit: 7b68c82

@ghost

ghost commented Oct 15, 2026

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The changed message serialization and legacy parsing paths are covered, and the repository’s audit-message call sites remain compatible.

Audit queue messages now omit payload and normalize both user contexts to the fields consumed by audit processing. The response-body copying path has been removed, and unit tests cover serialization, round trips, and legacy messages.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 3
File Change
app/controllers/shared/api.php Removes copying response bodies into audit context payloads.
src/Appwrite/Event/Message/Audit.php Stops serializing payloads and narrows deserialized user documents to UserContext fields.
tests/unit/Event/Message/AuditTest.php Covers sensitive-field and payload omission, round trips, and legacy/missing-field parsing.

Reviewed 7b68c82 · 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

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

19 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (12)
  • php.appwrite.secret-compare-timing (5)
  • 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

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/lean-audit-message (after).

Metric Before After Change
🚀 Requests/sec 219.6 216.94 ⚪ -1.2%
⏱️ Latency P50 77.26 ms 80.39 ms ⚪ +4.1%
⏱️ Latency P95 189.36 ms 184.48 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 80.39 184.48 13,680 216.94 -4.88
Account 147.2 270.62 720 12.07 -20.94
TablesDB 77.66 146.26 7,440 120.42 +5.73
Storage 75.46 166.78 3,600 59.83 +3.14
Functions 111.61 212.54 1,920 32.72 -65.24

Top API waits (after)

API request Max wait (ms)
account.name.update 454.1
account.prefs.update 399.37
functions.create 380.71
tablesdb.rows.decrement 354.18
functions.variables.create 349.9

@levivannoort
levivannoort merged commit 128cef7 into main Oct 15, 2026
67 checks passed
@ChiragAgg5k ChiragAgg5k mentioned this pull request Oct 21, 2026
6 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