Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion web/app/api/device-tokens/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
MAX_PUSH_REQUEST_BYTES,
normalizeApnsBundle,
readBoundedJsonObject,
registrationApnsBundle,
} from "../../../services/apns/routePolicy";
import {
AccountDeletionMutationBlockedError,
Expand Down Expand Up @@ -321,7 +322,7 @@ function parseRegistrationInput(
const installationId = typeof body.installationId === "string" ? body.installationId.trim() : "";
const pushKeyId = typeof body.pushKeyId === "string" ? body.pushKeyId.trim() : "";
const pushPublicKey = typeof body.pushPublicKey === "string" ? body.pushPublicKey.trim() : "";
const bundle = normalizeApnsBundle(bundleId);
const bundle = registrationApnsBundle(normalizeApnsBundle(bundleId), body.environment);
if (!HEX_TOKEN.test(deviceToken)) return { ok: false, response: jsonResponse({ error: "invalid_device_token" }, 400) };
if (!bundle) return { ok: false, response: jsonResponse({ error: "invalid_bundle_id" }, 400) };
if (!/^[A-Za-z0-9._:-]{1,255}$/.test(clientNamespace) || (clientNamespace !== "legacy" && clientNamespace !== bundle.bundleId)) {
Expand Down
17 changes: 17 additions & 0 deletions web/services/apns/routePolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@ function boundedString(value: unknown, maxChars: number): string | null {
return text;
}

/**
* The APNs environment to store for one registration. A production bundle
* defaults to the production host, but a Simulator or development-signed
* install of that bundle only receives sandbox tokens and declares
* `environment: "sandbox"`; sending those to the production host fails with
* BadDeviceToken and prunes the row. Development bundles stay sandbox-only.
*/
export function registrationApnsBundle(
bundle: ApnsBundlePolicy | null,
requestedEnvironment: unknown,
): ApnsBundlePolicy | null {
if (bundle?.environment === "production" && requestedEnvironment === "sandbox") {
return { bundleId: bundle.bundleId, environment: "sandbox" };
}
return bundle;
}

export function normalizeApnsBundle(bundleId: string): ApnsBundlePolicy | null {
const normalized = bundleId.trim();
if (PROD_BUNDLE_IDS.has(normalized)) {
Expand Down
16 changes: 16 additions & 0 deletions web/tests/apns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
normalizeApnsBundle,
parsePushPayload,
readBoundedJsonObject,
registrationApnsBundle,
} from "../services/apns/routePolicy";

describe("apns payload", () => {
Expand Down Expand Up @@ -495,6 +496,21 @@ describe("apns route policy", () => {
expect(normalizeApnsBundle("dev.cmux.ios.-bad")).toBeNull();
});

test("stores sandbox for a production bundle only when the install declares it", () => {
const official = normalizeApnsBundle("com.cmux.app")!;
// A Simulator or development-signed install only has sandbox tokens.
expect(registrationApnsBundle(official, "sandbox")).toEqual({
bundleId: "com.cmux.app",
environment: "sandbox",
});
expect(registrationApnsBundle(official, "production")).toEqual(official);
expect(registrationApnsBundle(official, undefined)).toEqual(official);
expect(registrationApnsBundle(official, "SANDBOX")).toEqual(official);
const dev = normalizeApnsBundle("dev.cmux.ios.push1")!;
expect(registrationApnsBundle(dev, "production")).toEqual(dev);
expect(registrationApnsBundle(null, "sandbox")).toBeNull();
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.

test("allows the internal TestFlight bundle id as a production APNs topic", () => {
// The scheduled internal TestFlight lane ships dev.cmux.app.internal
// (.github/workflows/ios-testflight.yml); TestFlight uses the production
Expand Down
Loading