Skip to content

Mqtt subscriber cleanup revoke - #14952

Merged
fogelito merged 7 commits into
mainfrom
security/mqtt-subscriber-cleanup-revoke
Nov 2, 2026
Merged

fogelito merged 7 commits into
mainfrom
security/mqtt-subscriber-cleanup-revoke

Conversation

@fogelito

Copy link
Copy Markdown
Contributor

What does this PR do?

(Provide a description of what this PR does and why it's needed.)

Test Plan

(Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work. Screenshots may also be helpful.)

Related PRs and Issues

  • (Related PR or issue)

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?

@luxor-code-review

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

Copy link
Copy Markdown

Review complete. 🟡 1 medium

💬 Inline comments (1)

  • 🟡 Per-subscriber recheck findOne runs for every provider type — Targets.php:66
🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 Dedupe repeated revokeTopic publishes in deleteTopic (Deletes.php:564) — The callback in deleteTopic (src/Appwrite/Platform/Workers/Deletes.php:564) calls revokeTopic once per subscriber row without deduplication.

This PR integrates MQTT revocation into the delete pipeline: Targets and the Deletes worker now accept an optional Mqtt adapter and, when deleting push-target/subscriber rows, check whether the affected user still has rows for the topic before publishing a revokeTopic to the broker. All worker call sites pass the new parameters consistently, and unit tests cover the new recheck and revocation behavior.

Files Change
src/Appwrite/Deletes/Targets.php Adds optional $mqtt/projectId params and a per-row recheck gate that publishes revokeTopic when a user's last subscriber row for a topic is removed
src/Appwrite/Platform/Workers/Deletes.php Threads the mqtt adapter and project id into target/subscriber deletion paths, including topic deletion
tests/unit/Deletes/TargetsTest.php, tests/unit/Platform/Workers/DeletesTest.php Unit coverage for the recheck gate and MQTT revocation across the updated delete paths

Reviewed commit: 9422936

@ghost

ghost commented Oct 26, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

Cleanup revokes live MQTT topic subscriptions when target deletion removes a user's final push subscription, with bulk cleanup deduplicating and batching the remaining-membership checks. Account and team claim flows revoke credentials and invalidate sessions while preserving MFA requirements, and user deletion also clears related presence, usage, and monitor state. Regression tests cover MQTT cleanup and worker deletion setup.

Latest changes: The latest commits replace per-pair subscriber lookups with chunked, paginated checks, capture the MQTT adapter in the team-claim transaction, and add coverage for cross-pair matches and later-page rows.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 8
File Change
app/controllers/api/account.php Threads realtime through session creation and emits invalidations for claim and recovery flows while preserving MFA requirements.
src/Appwrite/Deletes/Targets.php Deduplicates MQTT topic revocations and checks remaining subscriber rows in bounded, paginated batches.
src/Appwrite/Platform/Modules/Account/Http/Account/Sessions/IdToken/Create.php Invalidates MQTT and realtime sessions when a verified OAuth email claims an unverified account.
src/Appwrite/Platform/Modules/Teams/Http/Memberships/Status/Update.php Supports email- and phone-based invitation claims, preserving MFA and invalidating existing sessions.
src/Appwrite/Platform/Workers/Deletes.php Revokes user MQTT access and removes presence, usage, and bucket-scoped monitor state during deletion.
src/Appwrite/Utopia/Database/Documents/User.php Revokes credentials, unproven addresses, and targets when an unverified account is claimed.
tests/unit/Deletes/TargetsTest.php Covers final-topic unsubscribe behavior, deduplication, cross-pair matches, and paginated remaining-row checks.
tests/unit/Platform/Workers/DeletesTest.php Updates worker deletion test setup to provide the MQTT adapter.

Reviewed the commits since ed2c986 · 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 · 1 blocking finding to address. Summary

Comment thread src/Appwrite/Deletes/Targets.php Resolved
@github-actions

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.

@luxor-code-review luxor-code-review Bot 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.

The PR threads an MQTT adapter through the Deletes worker so subscriber-row deletions revoke MQTT topic subscriptions, but it adds a per-row findOne recheck for every provider type and repeats revokeTopic publishes per subscriber row.

Key findings

  • 🟡 Per-subscriber recheck findOne runs for every provider type — Targets.php:66

Comment thread src/Appwrite/Deletes/Targets.php Resolved
@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → security/mqtt-subscriber-cleanup-revoke (after).

Metric Before After Change
🚀 Requests/sec 201.6 201.85 ⚪ +0.1%
⏱️ Latency P50 85.22 ms 86.63 ms ⚪ +1.7%
⏱️ Latency P95 197.41 ms 194.63 ms ⚪ -1.4%
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 86.63 194.63 12,597 201.85 -2.79
Account 158 283.99 663 10.99 -4.08
TablesDB 85.09 159.63 6,851 111.11 +1.45
Storage 77.74 180.82 3,315 55.73 -15.01
Functions 119.45 221.32 1,768 30.49 -6.65

Top API waits (after)

API request Max wait (ms)
account.name.update 482.33
account.prefs.update 372.95
storage.buckets.create 360.43
tablesdb.rows.increment 349.45
functions.create 349.07

@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

…rity/mqtt-subscriber-cleanup-revoke

# Conflicts:
#	app/controllers/api/account.php
#	src/Appwrite/Platform/Modules/Account/Http/Account/Sessions/IdToken/Create.php
#	src/Appwrite/Utopia/Database/Documents/User.php
Comment thread src/Appwrite/Deletes/Targets.php Outdated
Comment on lines +109 to +135
foreach ($toRevoke as [$userId, $topicInternalId, $topicId]) {
self::revokeIfNoLongerSubscribed($database, $mqtt, $projectId, $userId, $topicInternalId, $topicId);
}
}

/**
* Drop the user's live MQTT subscription once they hold no other subscriber row for the topic,
* matching the subscribe gate (membership is per user, not per target). Mirrors the explicit
* subscriber-delete endpoint's own notification, which this cleanup path otherwise lacks.
*/
private static function revokeIfNoLongerSubscribed(Database $database, Mqtt $mqtt, string $projectId, string $userId, string $topicInternalId, string $topicId): void
{
$stillSubscribed = $database->skipFilters(fn () => $database->findOne('subscribers', [
Query::equal('topicInternalId', [$topicInternalId]),
Query::equal('userId', [$userId]),
]), APP_TOPICS_SUBQUERIES);

if (!$stillSubscribed->isEmpty()) {
return;
}

try {
$mqtt->revokeTopic($projectId, $userId, $topicId);
} catch (\Throwable $error) {
Console::error('Failed to unsubscribe MQTT connection: ' . $error->getMessage());
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

cant we bulk fetch from db and send the events instead of n+1 findone

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good call, done!

@fogelito
fogelito merged commit 17b93f9 into main Nov 2, 2026
68 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.

2 participants