Skip to content

fix(account): route APNs push targets by bundle ID and environment - #14589

Merged
ArnabChatterjee20k merged 3 commits into
mainfrom
fix/apns-target-provider-routing
Oct 16, 2026
Merged

ArnabChatterjee20k merged 3 commits into
mainfrom
fix/apns-target-provider-routing

Conversation

@ArnabChatterjee20k

@ArnabChatterjee20k ArnabChatterjee20k commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

The Apple SDK (sdk-for-apple 21.0.0) registers its APNs device token with:

  • POST /v1/account/targets/push and body { targetId, identifier, environment, bundleId }, with no providerId;
  • on 409, PUT /v1/account/targets/:targetId/push and body { identifier, environment, bundleId }.

Neither endpoint declared bundleId or environment, so the router dropped them and the target was saved with providerId: null. At send time a providerless target uses $default, which is findOne(providers, enabled, type = push) with no ordering. In the setup that appwrite init apns and the Appwrite Push docs produce (Appwrite push + APNs production + APNs sandbox), the token lands on any of these:

Default provider Result for the iOS device
Appwrite push nothing reaches APNs, so the background wake-up is dead
APNs production debug builds' sandbox tokens are rejected
APNs sandbox App Store and TestFlight tokens are rejected
FCM rejected

Two iOS apps with different bundle IDs in one project couldn't be told apart either.

Fix

  • Create: optional bundleId and environment (sandbox | production), declared last. When providerId is empty and bundleId is set, the target goes to the oldest enabled APNs provider whose credentials.bundleId matches and whose options.sandbox matches the environment. With no environment, the bundle ID alone decides.

    • The existing same-session token-rotation check also adopts the install's providerless target from before routing, and moves it onto the resolved provider. Otherwise a rotated token would leave it live beside a new target, and an unchanged token would hit the unique identifier index with a 409.
    • An explicit providerId still wins.
    • No match keeps today's behaviour, providerId: null, so nothing that works now breaks.
  • Update: the same params. A target on no provider or on an APNs provider is moved to the matching APNs provider when it differs, for example the same install moving from a debug build to TestFlight. Targets on other providers are never moved, and no match leaves the current provider in place.

  • The worker is unchanged. Apps already in the field heal on their next launch: the SDK re-registers every launch through POST, then PUT on 409.

  • APNs provider update fix (found in review): Messaging/Http/Providers/Apns/Update.php wrote the bundle ID to credentials.bundle. Create, the enable check, the APNs adapter and this routing all read credentials.bundleId, so a bundle ID changed through update never took effect for delivery or routing. It now writes bundleId and drops the stale key.

Credentials are encrypted, so providers are loaded with the indexed provider, type and enabled filters and matched in PHP.

The new params are public and optional on account.createPushTarget and account.updatePushTarget in the client SDKs, appended to app/config/sdks/parameters.lock.json.

Test Plan

E2E in tests/e2e/Services/Account/AccountCustomClientTest.php. Each test uses unique bundle IDs, with placeholder APNs credentials, so Apple is never contacted:

  • testCreatePushTargetRoutesApnsByBundleAndEnvironment, alongside an Appwrite push provider:

    • sandbox routes to the oldest enabled sandbox provider. A disabled one created first and a newer duplicate are both skipped.
    • production routes to the production provider.
    • An explicit providerId wins.
    • An unknown bundle stays on providerId: null.
    • environment: development returns 400.
  • testUpdatePushTargetReroutesApnsEnvironment:

    • PUT moves the target from sandbox to production.
    • No match keeps the current provider.
    • A target on an Appwrite push provider isn't moved.
    • An invalid environment returns 400.
  • testCreatePushTargetAdoptsLegacyProviderlessTarget:

    • A rotated token with a fresh target ID reuses and reroutes the legacy providerless target, leaving one live target.
    • An unchanged token with a fresh target ID resolves to it instead of returning 409.
  • testCreatePushTargetRoutesApnsByUpdatedBundleId: after a provider's bundle ID is updated, the new ID routes to it and the old one no longer does.

The routing tests each use a fresh user and session, standing in for one device install.

Locally I ran php -l, Pint and PHPStan (no new errors on changed lines; the rest is the stale local vendor). E2E and the ParametersTest lock check run in CI.

Related PRs and Issues

🤖 Generated with Claude Code

The Apple SDK registers its APNs token with bundleId and environment but
no providerId. Both were dropped, so the target fell back to the default
push provider: an arbitrary enabled one, which could be Appwrite push, the
wrong APNs environment or FCM.

Accept optional bundleId and environment on create and update push target.
Without a providerId, a target is routed to the oldest enabled APNs
provider whose bundle ID and sandbox flag match. No match keeps today's
behaviour. Update re-routes an APNs or providerless target, so an install
moving from a debug build to TestFlight follows, and leaves other
providers alone.

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

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

Copy link
Copy Markdown

Review complete. 🟡 2 medium

💬 Inline comments (1)

📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to

🟡 Medium — Sibling query misses targets on other providers after routing · account.php:5491–5496 · unchanged line

// app/controllers/api/account.php
5491	                empty($providerId)
5492	                    ? Query::isNull('providerId')
5493	                    : Query::equal('providerId', [$providerId]),
5494	                Query::orderAsc('$sequence'),
5495	                Query::limit(APP_LIMIT_SUBQUERY),
5496	            ]));

The new bundleId routing resolves a provider and sets $providerId before the sibling dedup query runs, so the query filters Query::equal('providerId', [$providerId]) (or Query::isNull when none resolves) and cannot see same-session push targets registered under a different provider, including ones stored with providerId null (app/controllers/api/account.php:5491-5493). Missed siblings are never expired in the loop at app/controllers/api/account.php:5544-5548, so superseded tokens stay live and messages arrive twice — the regression the sibling-expiry code exists to prevent. If the client re-registers the same identifier after routing changes, the unseen sibling collides with the project-wide unique _key_identifier index on identifier (app/config/collections/common.php:467) and the create branch surfaces USER_TARGET_ALREADY_EXISTS instead of reusing the target.

🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 Missing enum: metadata on environment params (account.php:5433) — The new environment params are declared with a bare new WhiteList(['sandbox', 'production'], true) and no enum: metadata (app/controllers/api/account.php:5433, repeated at app/controllers/api/account.php:5585).

This PR lets client apps route push targets to a specific APNs provider by bundle ID and register against the sandbox or production APNs gateway. The account API's create and update push-target routes accept two new optional params, resolve a matching messaging provider when no explicit provider is given, and expire superseded sibling targets for the same device. The SDK parameter lock and e2e suite are updated to cover the new surface.

Files Change
app/controllers/api/account.php Adds optional bundleId and environment params to push-target create/update, with provider resolution and sibling-target expiry logic
app/config/sdks/parameters.lock.json Locks the two new optional params for account.createPushTarget and account.updatePushTarget (console and client sections)
tests/e2e/Services/Account/AccountCustomClientTest.php Adds e2e coverage for bundle-id routing and environment whitelist validation

Review found three issues: the sibling-target dedup query can miss targets registered under a different provider after routing, the APNs provider update endpoint writes the bundle ID under a key the new routing does not read, and the new environment params lack enum: metadata for typed SDK codegen.

Reviewed commit: d3f2f03

@ArnabChatterjee20k ArnabChatterjee20k mentioned this pull request Oct 16, 2026
4 tasks done
@ghost

ghost commented Oct 16, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

Adds optional bundle ID and APNs environment routing for push-target creation and updates, selecting the oldest enabled matching APNs provider while preserving explicit-provider behavior and adopting legacy providerless targets. It also exposes the parameters in the SDK lock and corrects APNs provider updates to persist bundle IDs under the key used by routing and delivery.

Latest changes: The newest commit fixes APNs provider updates to store bundle IDs as credentials.bundleId and adds an end-to-end regression test for routing after that update.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 4
File Change
app/config/sdks/parameters.lock.json Locks the appended optional bundle ID and environment parameters for push-target create and update.
app/controllers/api/account.php Adds APNs bundle/environment routing and providerless-target adoption to push-target registration, plus provider-aware rerouting on updates.
src/Appwrite/Platform/Modules/Messaging/Http/Providers/Apns/Update.php Persists updated APNs bundle IDs under credentials.bundleId and removes the stale bundle key.
tests/e2e/Services/Account/AccountCustomClientTest.php Covers push-target routing, environment updates, legacy-target adoption, and routing after an APNs provider bundle ID is changed.

Reviewed the commits since 4083cf3 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@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.

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

Comment thread app/controllers/api/account.php Resolved

@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.

Adds APNs bundleId and environment routing parameters to push target create/update in the account API, with matching parameter-lock and e2e test updates.

Key findings

  • 🟡 Sibling query misses targets on other providers after routing — account.php:5491
  • 🟡 Provider update writes bundle ID under wrong key — account.php:5460

Comment thread app/controllers/api/account.php Resolved
…g it

A routed registration looked for same-session siblings on the resolved
APNs provider only, so an install that registered before routing kept its
providerless target. A rotated token left that target live beside the new
one, and an unchanged token hit the unique identifier index with a 409.

When the provider comes from bundleId and environment, include
providerless siblings in the lookup, and move the adopted target onto the
resolved provider. Routing tests now use their own session, so they don't
adopt the shared session's targets.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/apns-target-provider-routing (after).

Metric Before After Change
🚀 Requests/sec 194.68 217.78 🟢 +11.9%
⏱️ Latency P50 88.34 ms 80.48 ms 🟢 -8.9%
⏱️ Latency P95 206.2 ms 186.42 ms 🟢 -9.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.48 186.42 13,680 217.78 -19.78
Account 146.16 278.06 720 12.15 -15.89
TablesDB 77.87 143.13 7,440 121.38 -24.24
Storage 74.97 173.37 3,600 60.52 -17.73
Functions 110.71 217.01 1,920 32.91 -28.23

Top API waits (after)

API request Max wait (ms)
functions.variables.update 499.74
tablesdb.rows.create 422.89
account.name.update 400.83
tablesdb.rows.get 360.26
storage.buckets.create 355.02

@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

…dleId

The APNs provider update endpoint wrote the bundle ID to
credentials.bundle, while create, the enable check, the APNs adapter and
push target routing all read credentials.bundleId. A bundle ID changed
through update never took effect for delivery or routing. Write
bundleId and drop the stale key.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ArnabChatterjee20k
ArnabChatterjee20k merged commit 3f08f01 into main Oct 16, 2026
61 of 66 checks passed
ArnabChatterjee20k added a commit to ArnabChatterjee20k/appwrite that referenced this pull request Oct 16, 2026
….bundle key

Until appwrite#14589, updating an APNs provider's bundle ID wrote it to
credentials.bundle and left credentials.bundleId on the value from
creation. Those rows still deliver and route with the old bundle ID, and
the API shows it, until the provider is updated with a bundle ID again.

Prefer a legacy `bundle` value wherever APNs credentials are read: the
APNs adapter, push target routing and the Provider response model. Any
update of the provider now promotes it into bundleId, so the row repairs
itself on the next save.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@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.

1 participant