Add encrypted content to mobile push notifications

What does this MR do and why?

Adds encrypted content to mobile push notifications: the to-do content travels sealed between the instance and the device, so Apple and any relay never see it. The instance holds the plaintext and the key, so this is not end-to-end encryption; the API doc has a threat-model section saying what is and is not protected.

Today a full payload carries a to-do's title, subtitle and body in clear text through APNs. With this change a device can register a 32-byte content_key; for such a device the alert is the generic "You have a new to-do" with mutable-content set, and the real content rides sealed under that key (AES-256-GCM) in the gitlab.content envelope, which the GitLab mobile app's notification service extension opens on the phone. Apple never sees to-do content.

  • mobile_device_push_subscriptions.content_key (jsonb, encrypts), the content_key registration parameter (validated as base64 of 32 bytes) and content_key: missing | present in the registration response. The app sends its key only after a missing answer, never on the cold-start heartbeat, so the key is not re-sent on every launch. The parameter is already masked by config.filter_parameters (/key$/).
  • Gitlab::MobilePush::EncryptedContent seals the version-2 envelope {alg: "A256GCM", v: 2, iat, ciphertext} with the additional authenticated data gitlab-mobile-push/2\n<device token>\n<iat>: a captured payload opens neither on another device nor, as the app enforces, after a 3900 s window.
  • A device changing hands drops the previous owner's content_key and payload_mode (RegisterService resets them before applying the registration), so a reassigned device starts with content_key: missing.
  • Key rotation: content_key_registered_at is stamped when the key changes, a key older than 90 days is reported missing so the app replaces it (it keeps sealing pushes until then; re-sending the same key does not renew it), and gitlab:mobile_push:clear_content_keys[username] forgets stored keys for incident response.
  • Payload gains the encrypted mode (MobileDevicePushSubscription#effective_payload_mode: id_only when the user chose identifiers only, encrypted with a key, full otherwise); ApnsClient takes the per-device gitlab object through Payload#gitlab_data_for(subscription). full and id_only payloads are unchanged.

The app side (key exchange, Keychain storage, the extension's AAD and freshness checks) ships in GitLab Mobile. Two app changes follow from the rotation design and land there before the feature flag is enabled: minting a fresh key on every missing answer, and keeping the previous key for the 3900 s envelope window and trying it when the current key does not open an envelope.

References

Screenshots or screen recordings

No UI change.

How to set up and validate locally

  1. In the Rails console enable the flags: Feature.enable(:mobile_push_registration_api); Feature.enable(:mobile_push_notifications_dispatch); Feature.enable(:mobile_push_notifications).
  2. Register a device with a key: curl --request POST --header "PRIVATE-TOKEN: <token>" --data "device_token=$(openssl rand -hex 32)" --data "content_key=$(openssl rand -base64 32)" http://gdk.test:3000/api/v4/user/push_subscriptions — the response carries "content_key": "present"; repeat without content_key and it still reports present.
  3. In the Rails console: sub = Notifications::MobileDevicePushSubscription.last; todo = Todo.last; Gitlab::MobilePush::Payload.new(todo, mode: sub.effective_payload_mode).gitlab_data_for(sub) — the content envelope with alg, v: 2, iat and a base64 ciphertext, and title / body nil on the payload.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Database review

Two migrations on mobile_device_push_subscriptions, a small table: content_key (jsonb, nullable) with the check constraint octet_length(content_key::text) <= 1024, and content_key_registered_at (timestamptz, nullable).

clear_content_keys runs only from the operator Rake task gitlab:mobile_push:clear_content_keys[username]. It batches by id with each_batch (1,000 rows); only the update_all in each batch filters on content_key. Without a username:

SELECT "mobile_device_push_subscriptions"."id" FROM "mobile_device_push_subscriptions" ORDER BY "mobile_device_push_subscriptions"."id" ASC LIMIT 1;
SELECT "mobile_device_push_subscriptions"."id" FROM "mobile_device_push_subscriptions" WHERE "mobile_device_push_subscriptions"."id" >= 1 ORDER BY "mobile_device_push_subscriptions"."id" ASC LIMIT 1 OFFSET 1000;
UPDATE "mobile_device_push_subscriptions" SET "content_key" = NULL, "content_key_registered_at" = NULL WHERE "mobile_device_push_subscriptions"."id" >= 1 AND "mobile_device_push_subscriptions"."id" < 1001 AND "mobile_device_push_subscriptions"."content_key" IS NOT NULL;

With a username, each statement also filters on user_id, which index_mobile_device_push_subscriptions_on_user_id serves:

SELECT "mobile_device_push_subscriptions"."id" FROM "mobile_device_push_subscriptions" WHERE "mobile_device_push_subscriptions"."user_id" = 1 ORDER BY "mobile_device_push_subscriptions"."id" ASC LIMIT 1;
SELECT "mobile_device_push_subscriptions"."id" FROM "mobile_device_push_subscriptions" WHERE "mobile_device_push_subscriptions"."user_id" = 1 AND "mobile_device_push_subscriptions"."id" >= 1 ORDER BY "mobile_device_push_subscriptions"."id" ASC LIMIT 1 OFFSET 1000;
UPDATE "mobile_device_push_subscriptions" SET "content_key" = NULL, "content_key_registered_at" = NULL WHERE "mobile_device_push_subscriptions"."user_id" = 1 AND "mobile_device_push_subscriptions"."id" >= 1 AND "mobile_device_push_subscriptions"."id" < 1001 AND "mobile_device_push_subscriptions"."content_key" IS NOT NULL;
Edited by Marcel van Remmerden

Merge request reports

Loading
Loading