feat(native): push inside the iOS app, through an encrypted relay

The app subscribes to Web Push like a browser, only the app makes the
subscription: it asks for permission, registers with APNs and keeps its own
P-256 key pair and auth secret in a keychain group a notification extension
can share. The endpoint is liketrek's relay for this device's token, so the
server encrypts exactly as for a browser and nobody on the way can read the
notification. The settings card, the test push, sign-in resync and the logout
teardown all take this path where the binary has nativePush; a tapped
notification opens the place it points at, also from a cold start.

The App ID has push enabled, the app carries the aps-environment and
keychain-group entitlements.
This commit is contained in:
Maurice
2026-11-06 13:37:59 +01:00
parent 30158cddc3
commit ad69bbf4a9
10 changed files with 505 additions and 3 deletions
+79
View File
@@ -0,0 +1,79 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { hasNativeFeature } from './capabilities';
import { isNativeIos } from './platform';
import {
NativePushDenied,
forgetNativePush,
nativePushActive,
nativePushEndpoint,
openPushTarget,
registerNativePush,
startNativePush,
} from './push';
import { TrekShell } from './trekShell';
vi.mock('./platform', () => ({ isNativeIos: vi.fn() }));
vi.mock('./capabilities', () => ({ hasNativeFeature: vi.fn() }));
vi.mock('./trekShell', () => ({
TrekShell: {
registerPush: vi.fn(),
takeOpenedPush: vi.fn(),
addListener: vi.fn(async () => ({ remove: async () => {} })),
},
}));
const SUB = {
endpoint: 'https://push.liketrek.com/v1/apns/production/abcd',
keys: { p256dh: 'BPk', auth: 'aa' },
};
beforeEach(() => {
window.localStorage.clear();
vi.mocked(isNativeIos).mockReturnValue(true);
vi.mocked(hasNativeFeature).mockReturnValue(true);
vi.mocked(TrekShell.registerPush).mockReset().mockResolvedValue(SUB);
vi.mocked(TrekShell.takeOpenedPush).mockReset().mockResolvedValue({ url: null });
window.history.replaceState({}, '', '/dashboard');
});
describe('native push', () => {
it('is on only in an iOS binary that has it', () => {
expect(nativePushActive()).toBe(true);
vi.mocked(hasNativeFeature).mockReturnValue(false);
expect(nativePushActive()).toBe(false);
});
it('remembers the relay endpoint it subscribed with, and forgets it again', async () => {
expect(await registerNativePush()).toEqual(SUB);
expect(nativePushEndpoint()).toBe(SUB.endpoint);
forgetNativePush();
expect(nativePushEndpoint()).toBeNull();
});
it('tells a refused permission apart from a failure', async () => {
vi.mocked(TrekShell.registerPush).mockRejectedValue({ code: 'DENIED' });
await expect(registerNativePush()).rejects.toBeInstanceOf(NativePushDenied);
vi.mocked(TrekShell.registerPush).mockRejectedValue(new Error('apns down'));
await expect(registerNativePush()).rejects.toThrow('apns down');
expect(nativePushEndpoint()).toBeNull();
});
it('opens a tapped notification inside the app, and never another origin', () => {
const pops = vi.fn();
window.addEventListener('popstate', pops);
openPushTarget('/trips/12?tab=chat');
expect(window.location.pathname).toBe('/trips/12');
expect(window.location.search).toBe('?tab=chat');
openPushTarget('https://evil.example/phish');
expect(window.location.pathname).toBe('/trips/12');
expect(pops).toHaveBeenCalledTimes(1);
window.removeEventListener('popstate', pops);
});
it('follows the notification the app was opened from', async () => {
vi.mocked(TrekShell.takeOpenedPush).mockResolvedValue({ url: '/trips/3' });
await startNativePush();
expect(window.location.pathname).toBe('/trips/3');
expect(TrekShell.addListener).toHaveBeenCalledWith('pushOpened', expect.any(Function));
});
});