Repository navigation
fix(events): preserve object shapes in event payloads - #13650
Conversation
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
# Conflicts: # src/Appwrite/Platform/Workers/Functions.php
The signature checks re-encoded the decoded request data, which only
matches the delivered bytes while no empty object is involved: `{}`
decodes to an array and comes back as `[]`. Keep the raw body next to the
decoded data and sign that instead.
The webhooks worker re-encoded the payload it decoded from the queue, so an
empty object such as `prefs: {}` was delivered as `[]`. When that round trip
would change the JSON, the message now also carries the original JSON as
`body`, using the same rule as function events, and the worker sends it.
Messages without a body are encoded from the payload as before.
The realtime server decodes published events into arrays, so an empty
object such as `prefs: {}` reached subscribers as `[]`. When decoding
changed the published JSON, the payload is re-read as objects for the
frame, falling back to the arrays if that decode fails.
…ent-json-6360 Webhook::preparePayload() now adds both the eventId from main and the original JSON body, which is only set when decoding the payload into arrays would change its shape. # Conflicts: # src/Appwrite/Event/Webhook.php
Security rulesNo new WARNING or ERROR findings from security rules. 6 existing findings tracked in
|
# Conflicts: # src/Appwrite/Event/Webhook.php # tests/e2e/Services/Functions/FunctionsCustomServerTest.php
|
Review complete. No issues found — approved ✅. This change fixes a JSON round-trip fidelity bug where re-encoding queue messages through `json_decode(..., true)` turned empty objects into arrays, breaking webhook signatures and payload shape for consumers like function logs.
Reviewed commit: 7a137ae |
🔵 Tier A · Mergeable after minor fixes
Preserves JSON object-versus-array shapes through function events, webhook deliveries, and realtime frames while leaving the queue codec unchanged. Updates webhook test helpers to sign delivered bytes and adds a Function event regression test.
Fix with agent prompt### Issue 1
app/realtime.php:831
**Add a realtime regression assertion for empty objects**
This branch can regress to array-shaped payloads without failing the committed tests: the existing realtime tests decode frames associatively and do not check empty-object shape, while the new regression covers only the Function path. Please add an assertion against a raw realtime frame containing empty `prefs` (or decode that frame as objects).
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.📂 Walkthrough · 12
Reviewed |
| // Decoding into arrays turns objects such as `prefs: {}` into `[]`, so when that | ||
| // changed the published JSON, the payload is re-read as objects for the frame. | ||
| if ($receivers !== [] && \json_encode($event) !== $payload) { | ||
| $data['payload'] = \json_decode($payload)->data->payload ?? $data['payload']; |
There was a problem hiding this comment.
Add a realtime regression assertion for empty objects
This branch can regress to array-shaped payloads without failing the committed tests: the existing realtime tests decode frames associatively and do not check empty-object shape, while the new regression covers only the Function path. Please add an assertion against a raw realtime frame containing empty prefs (or decode that frame as objects).
Prompt To Fix With AI
This is a comment left during a code review.
Path: app/realtime.php
Line: 831
Comment:
**Add a realtime regression assertion for empty objects**
This branch can regress to array-shaped payloads without failing the committed tests: the existing realtime tests decode frames associatively and do not check empty-object shape, while the new regression covers only the Function path. Please add an assertion against a raw realtime frame containing empty `prefs` (or decode that frame as objects).
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.🟡 Minor · testing · Reply if this doesn't apply.
What does this PR do?
Fixes #6360. When a user with empty preferences is deleted, the API prepares
prefs: {}, but the queue decodes JSON objects into associative arrays. Every consumer that re-encodes the decoded payload then emitsprefs: [], which breaks typed SDK consumers. This happened in three places: Function event data, webhook bodies and realtime frames. All three now keep JSON object shapes, so{}stays{}.Funckeeps the original JSON in the existing function-messagebodywhen an associative decode/re-encode would change its shape. The worker passes that body as the event data when present.Webhook::preparePayload()adds abodyto the queued message under the same condition. The worker delivers and signs that body when present.Empty lists remain lists, and nested objects and numeric-key objects retain their JSON types.
The queue codec itself is unchanged; nothing under
packages/queueis touched. The fix only adds the original JSON next to the existing payload.Rolling deploys and older messages: the legacy payload stays in every message. Older workers read only events, payload, user and project, so they ignore
bodyand behave as before. New workers fall back to encoding the payload whenbodyis empty, which covers messages queued before the deploy. Only affected payloads carry both representations, which increases their queue size; ordinary payloads keep their existing size.The e2e signature helpers (
Scope,ProjectWebhooksandWebhookstests) now sign the raw delivered body instead of re-encoding the decoded data. Re-encoding turned{}back into[], so the old helpers only matched while the server also sent[]. The same applies to receivers: one that hashes the raw request body is unaffected, but one that re-encodes the decoded JSON before hashing (as these helpers did) will now get a mismatch on payloads with empty objects.Test Plan
All local runs used throwaway containers built from
appwrite-dev:oauth-callbackwith this branch's code mounted, on networkappwrite.--add-host appwrite:127.0.0.1; mountssrc,app,packages,testsandphpunit.xmlread-only; env fromdocker inspect appwrite.php app/worker.php webhooks,functions,executions,builds; env fromdocker inspect appwrite-worker._APP_WEBHOOK_QUEUE_NAME,_APP_FUNCTIONS_QUEUE_NAME,_APP_EXECUTIONS_QUEUE_NAMEand_APP_BUILDS_QUEUE_NAMEset to private names. The worker's startup table and spans confirmed it processed the private webhooks, functions and executions jobs._APP_ALLOWED_INTERNAL_ADDRESSES(new SSRF policy) and a few other new vars that the shared stack lacks. Their values came from the branch's committed.env.tests/resources/functions/timeoutwas overlaid with a scratch copy because the deployment tests writecode.tar.gzthere.Unit tests:
tests/unit/Event: OK, 26 tests / 311 assertions (includesFuncTest, which covers empty objects, empty lists, nested mixed shapes, numeric-key objects, an unchanged 70KB payload and older messages without a body).tests/unit/Platform/Workers/WebhooksTest.php: OK, 3 / 26.tests/unit/Platform/Workers/FunctionsTest.php: OK, 5 / 62.packages/queue/tests/CodecTest.php: OK, 11 / 16.E2E with the fix:
ProjectWebhooks/WebhooksCustomClientTest: 40 tests, 38 pass, 2 errors (testCreateTeamMembership,testUpdateTeamMembership). Same result on a second run.ProjectWebhooks/WebhooksCustomServerTest: 42 tests, 41 pass, 1 error (testCreateTeamMembership). Same result on a second run.getLastEmail()cannot resolve themaildevhost, which does not exist on that network.--display-warningsshowed only that cause.Services/Webhooks(WebhooksConsoleClientTest+WebhooksCustomServerTest): OK, 133 tests / 1005 assertions.FunctionsCustomServerTest --filter testEventTriggerPreservesEmptyPreferences(deletes a user and checks the triggered Function receivesprefs: {}): OK, 1 test / 43 assertions, with the functions and executions jobs going through the branch worker.Same branch code with the old signature helpers:
CustomClientTest:107/176/505/566/628,Base:1449/1488/1576) intestCreateAccount,testDeleteAccount,testUpdateAccountName/Password/Email,testCreateTeam,testUpdateTeam,testDeleteTeam, plus the same 2 maildev errors.CustomServerTest:519/603/641,Base:1449/1488/1576) intestCreateUser,testUpdateUserStatus,testDeleteUser,testCreateTeam/UpdateTeam/DeleteTeam, plus 1 maildev error.Services/Webhooks: 4 failures (testSecretRotationZeroDowntimeandtestCreateWebhookWithCustomSecretin both classes, at the signature asserts atWebhooksBase:815/910).Delivered bodies: request-catcher deliveries from the client-suite run carried
"prefs":{}34 times and"prefs":[]0 times.Realtime: a realtime server running this branch and one running code without this fix were subscribed to the same account event. This branch's frame carried
"prefs":{}; the other carried"prefs":[]. A PHP check of the realtime logic showed that a NUL-prefixed key falls back to the arrays with no warnings, a numeric-key object stays an object, and an unchanged payload skips the re-decode.Static checks: Pint
--test,php -lon all 11 changed PHP files,node --checkon the event-handler fixture andgit diff --checkpass.Earlier, at
0b578fb61f(Functions fix only), GitHub Actions passed the Functions suite (82 tests, 1,909 assertions), along with Unit and Checks. Functions job. The newFuncTestregression fails against the pre-changeFuncclass and passes with this patch.Not verified:
v1-jobsqueue, so they ran on the shared stack's code, not this branch. That path is unchanged by this branch.utopia-php/database7.3.11 while the branch requires^7.4.0. That only affects relationship-delete events, which these suites don't depend on.Funcfix; the earlier CI run above already showed it.0b578fb61fchanges.Related PRs and Issues
Checklist