Skip to content

fix(auth): keep created token and session secrets out of realtime events - #14520

Merged
HarshMN2345 merged 11 commits into
mainfrom
fix-users-token-session-event-secret
Oct 28, 2026
Merged

HarshMN2345 merged 11 commits into
mainfrom
fix-users-token-session-event-secret

Conversation

@HarshMN2345

@HarshMN2345 HarshMN2345 commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

POST /v1/users/:userId/tokens and POST /v1/users/:userId/sessions put the plaintext secret in the event payload without marking it sensitive, so realtime delivered it on the account channel. They now pass sensitive: ['secret'], like the account token endpoints.

Account session creation leaked the same way when called with an API key (email/password, anonymous, sessions/token with the magic URL and phone routes that share it, and ID token). Each now sets its payload explicitly with sensitive: ['secret'].

MFA recovery codes (users and account, create and update) and the TOTP secret/uri from account authenticator create also reached realtime through the response fallback; they are now marked sensitive the same way.

Webhooks still carry the secret on purpose: the Token model documents it as part of the webhook payload, and the recovery/verification webhook tests expect it.

Verified: vendor/bin/pint --test and php -l on the changed files; a local script confirmed Realtime::getRealtimePayload() drops a sensitive key while the webhook payload keeps it. E2E left to CI.

Closes #14415

@luxor-code-review

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

Copy link
Copy Markdown

Review complete. No blocking issues — 1 nitpick below.

🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 OAuth2 callback session event misses sensitive: ['secret'] (account.php:2348) — This PR marks the session secret as sensitive on five session/token creation events, but the OAuth2 callback session-creation flow (Create OAuth2 callback) still calls setPayload with no sensitive list at app/controllers/api/account.php:2348, even though it creates the session document with a secret attribute just above.

This PR threads sensitive: ['secret'] through setPayload() calls on every session/token creation and deletion event so the secret attribute is stripped before realtime distribution.

Files Change
app/controllers/api/account.php, Account/Sessions/IdToken/Create.php Session create/delete event payloads now mark secret sensitive for realtime filtering.
Users/Sessions/Create.php, Users/Tokens/Create.php Users-side session and token creation events get the same sensitive: ['secret'] treatment.

One sibling path — the OAuth2 callback session creation — was left unredacted, so realtime subscribers there still receive the session secret attribute; aligning it with the patched paths would complete the fix.

Reviewed commit: 11e8d2b

@ghost

ghost commented Oct 15, 2026 •

Copy link
Copy Markdown

🟡 Tier B · Needs changes before merging

The webhook filter breaks existing verification webhook behavior and also strips non-secret relationship fields, while the open MFA realtime regression-coverage finding remains unresolved.

The pull request marks token and session credentials, MFA recovery codes, and authenticator provisioning details as sensitive in event payloads, and explicitly sets payloads for account session flows. The latest change also filters sensitive keys from webhook deliveries, in addition to realtime payloads.

Latest changes: The latest commit filters keys marked sensitive out of webhook queue payloads before delivery.

Verdict New comments Fixed Still open
💬 Commented 2 1 1

Note

Not approving while a bug finding is open: Preserve usable verification webhook behavior

Finding Where
🟡 Preserve usable verification webhook behavior src/Appwrite/Event/Webhook.php:35
🟡 Do not strip relationship fields from database webhooks src/Appwrite/Event/Webhook.php:35
Fix with agent prompt
### Issue 1
src/Appwrite/Event/Webhook.php:35
**Preserve usable verification webhook behavior**

This drops the secret from account-verification webhook events as well as token and session events. `WebhooksCustomClientTest::testCreateAccountVerification()` still asserts that the webhook contains `secret`, and `testUpdateAccountVerification()` uses that value to complete verification, so these existing webhook flows and tests break.

### Issue 2
src/Appwrite/Event/Webhook.php:35
**Do not strip relationship fields from database webhooks**

The sensitive list also contains non-secret relationship attribute names: document actions such as `Databases/Http/Databases/Collections/Documents/Create.php` pass those names to `setPayload()`, and `Event::from()` copies them into this webhook. This now silently removes relationship values from database webhook payloads, breaking consumers that rely on those fields.

### Issue 3
src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Authenticators/Create.php:141
**Add regression coverage for MFA realtime redaction**

The existing MFA E2E tests assert the secret and recovery codes in HTTP responses but do not inspect realtime deliveries, so removing or mistyping these sensitive keys would pass CI while exposing them again. Please add observable realtime assertions for the new authenticator and recovery-code redactions.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
📂 Walkthrough · 12
File Change
app/controllers/api/account.php Marks account session and token credentials sensitive in legacy account event payloads.
src/Appwrite/Event/Webhook.php Filters sensitive keys from outbound webhook payloads.
src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Authenticators/Create.php Marks authenticator TOTP secret and URI sensitive.
src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Challenges/Update.php Sets the completed session payload explicitly and marks session credentials sensitive.
src/Appwrite/Platform/Modules/Account/Http/Account/MFA/RecoveryCodes/Create.php Sets the recovery-code event payload and marks the codes sensitive.
src/Appwrite/Platform/Modules/Account/Http/Account/MFA/RecoveryCodes/Update.php Sets the regenerated recovery-code event payload and marks the codes sensitive.
src/Appwrite/Platform/Modules/Account/Http/Account/Sessions/IdToken/Create.php Marks session and provider credentials sensitive in ID-token session events.
src/Appwrite/Platform/Modules/Users/Http/Users/MFA/RecoveryCodes/Create.php Sets the recovery-code event payload and marks the codes sensitive.
src/Appwrite/Platform/Modules/Users/Http/Users/MFA/RecoveryCodes/Update.php Sets the regenerated recovery-code event payload and marks the codes sensitive.
src/Appwrite/Platform/Modules/Users/Http/Users/Sessions/Create.php Marks session and provider credentials sensitive in user session events.
src/Appwrite/Platform/Modules/Users/Http/Users/Sessions/Delete.php Marks session and provider credentials sensitive in session deletion events.
src/Appwrite/Platform/Modules/Users/Http/Users/Tokens/Create.php Marks the created token secret sensitive.
⏳ Still open from earlier reviews · 1
✅ Fixed since the last review · 1
  • Webhook events still contain the plaintext secret · src/Appwrite/Platform/Modules/Users/Http/Users/Tokens/Create.php:98

Reviewed the commits since d057ec2 · 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 B · See the inline comments. Summary

@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 B · See the inline comments. Summary

$queueForEvents->setParam('userId', $user->getId());
$queueForEvents
->setParam('userId', $user->getId())
->setPayload($response->output($model, Response::MODEL_MFA_TYPE), sensitive: ['secret', 'uri']);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Add regression coverage for MFA realtime redaction

The existing MFA E2E tests assert the secret and recovery codes in HTTP responses but do not inspect realtime deliveries, so removing or mistyping these sensitive keys would pass CI while exposing them again. Please add observable realtime assertions for the new authenticator and recovery-code redactions.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Authenticators/Create.php
Line: 141

Comment:
**Add regression coverage for MFA realtime redaction**

The existing MFA E2E tests assert the secret and recovery codes in HTTP responses but do not inspect realtime deliveries, so removing or mistyping these sensitive keys would pass CI while exposing them again. Please add observable realtime assertions for the new authenticator and recovery-code redactions.

---

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.

@github-actions

github-actions Bot commented Oct 15, 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 Oct 15, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix-users-token-session-event-secret (after).

Metric Before After Change
🚀 Requests/sec 206.11 205.45 ⚪ -0.3%
⏱️ Latency P50 81.17 ms 85.34 ms 🔴 +5.1%
⏱️ Latency P95 196.64 ms 192.66 ms ⚪ -2%
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 85.34 192.66 12,654 205.45 -3.97
Account 157.45 281.82 666 11.04 -46.96
TablesDB 83.42 154.39 6,882 113.06 -2.86
Storage 77.33 184.33 3,330 57.13 +11.31
Functions 117.25 212.51 1,776 31.2 -19.05

Top API waits (after)

API request Max wait (ms)
storage.buckets.create 387.87
account.prefs.update 381.31
functions.variables.update 378.93
account.name.update 361.55
functions.create 350.76

@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 B · See the inline comments. Summary

Comment thread src/Appwrite/Event/Webhook.php Outdated
protected function preparePayload(): array
{
return \array_merge(parent::preparePayload(), [
'payload' => \array_diff_key($this->payload, $this->sensitive),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Preserve usable verification webhook behavior

This drops the secret from account-verification webhook events as well as token and session events. WebhooksCustomClientTest::testCreateAccountVerification() still asserts that the webhook contains secret, and testUpdateAccountVerification() uses that value to complete verification, so these existing webhook flows and tests break.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Appwrite/Event/Webhook.php
Line: 35

Comment:
**Preserve usable verification webhook behavior**

This drops the secret from account-verification webhook events as well as token and session events. `WebhooksCustomClientTest::testCreateAccountVerification()` still asserts that the webhook contains `secret`, and `testUpdateAccountVerification()` uses that value to complete verification, so these existing webhook flows and tests break.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

🟡 Minor · bug · Reply if this doesn't apply.

Comment thread src/Appwrite/Event/Webhook.php Outdated
protected function preparePayload(): array
{
return \array_merge(parent::preparePayload(), [
'payload' => \array_diff_key($this->payload, $this->sensitive),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do not strip relationship fields from database webhooks

The sensitive list also contains non-secret relationship attribute names: document actions such as Databases/Http/Databases/Collections/Documents/Create.php pass those names to setPayload(), and Event::from() copies them into this webhook. This now silently removes relationship values from database webhook payloads, breaking consumers that rely on those fields.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Appwrite/Event/Webhook.php
Line: 35

Comment:
**Do not strip relationship fields from database webhooks**

The sensitive list also contains non-secret relationship attribute names: document actions such as `Databases/Http/Databases/Collections/Documents/Create.php` pass those names to `setPayload()`, and `Event::from()` copies them into this webhook. This now silently removes relationship values from database webhook payloads, breaking consumers that rely on those fields.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

🟡 Minor · bug · Reply if this doesn't apply.

HarshMN2345 and others added 4 commits October 23, 2026 16:47
Webhooks go to endpoints the project owner configures; verification and
recovery flows rely on the secret, and the sensitive list also names
relationship attributes on document events.
@HarshMN2345

Copy link
Copy Markdown
Member Author

@hansi-codes re-review

@ghost

ghost commented Oct 28, 2026

Copy link
Copy Markdown

One issue remains: add an E2E assertion that the realtime event omits the newly redacted MFA values. The existing MFA test checks that the HTTP response contains the authenticator secret and recovery codes, but does not verify what realtime subscribers receive (test). Without a realtime assertion, a future regression in these sensitive-key lists could pass CI. The changed filtering is at Authenticators/Create.php.

The OAuth2 callback finding is fixed: its session payload now marks the secret and provider tokens sensitive (account.php). I also don’t find the previously reported webhook regressions in the current code: the sensitive-key filter is applied by realtime (Realtime.php); the event queue payload does not serialize the sensitive list (Event.php).

@HarshMN2345
HarshMN2345 merged commit e92067b into main Oct 28, 2026
37 checks passed
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.

🐛 Bug Report: Server-side token and session events carry the plaintext secret to realtime and webhooks

2 participants