fix(native): the app's browser sign-in no longer lands on the dashboard when the browser still holds an older TREK

A browser that ran TREK before 5.1.1 keeps that version's app in its service
worker. When the app opened /native-handoff, the worker answered it from its
cache with an app that has no such page, which sent the user to /, and the
sign-in ended on the dashboard in the browser instead of in the app.

The app now opens /api/auth/native/start, which every TREK service worker
passes to the server. That page takes the workers off the browser and
forwards to the handoff page. Until the sign-in is handed back, the web app
registers no service worker and does not reload for a new version: a tab
still open on the older TREK keeps its worker alive, and a registration or a
reload in the middle of the sign-in brought it back to answer the next page.
This commit is contained in:
Maurice
2026-11-09 22:21:09 +01:00
parent 5420077b98
commit 034cfb539a
14 changed files with 286 additions and 7 deletions
@@ -8,7 +8,7 @@ import type { AuditService } from '../../../src/nest/audit/audit.service';
import { DomainError } from '../../../src/nest/common/domain-error';
import type { RateLimitService } from '../../../src/nest/common/rate-limit.service';
import { RESPONSE_CONTRACT_KEY } from '../../../src/nest/common/response-contract';
import { NativeAuthController } from '../../../src/nest/native-auth/native-auth.controller';
import { NativeAuthController, nativeStartPage } from '../../../src/nest/native-auth/native-auth.controller';
import type { NativeAuthService } from '../../../src/nest/native-auth/native-auth.service';
import type { User } from '../../../src/types';
import { nativeHandoffResponseSchema, successResponseSchema } from '@trek/shared';
@@ -100,3 +100,48 @@ describe('NativeAuthController.exchange', () => {
expect(Reflect.getMetadata(RESPONSE_CONTRACT_KEY, proto.exchange)).toBe(successResponseSchema);
});
});
describe('NativeAuthController.start', () => {
function htmlRes() {
const res = {
status: vi.fn(),
set: vi.fn(),
type: vi.fn(),
send: vi.fn(),
};
for (const fn of [res.status, res.set, res.type]) fn.mockReturnValue(res);
return res;
}
it('NATIVE-CTL-007: answers the page that opens the handoff page with the same challenge, start and invite', () => {
const { controller } = build();
const res = htmlRes();
controller.start(challenge, 'oidc', 'inv_A-1', res as unknown as Response);
expect(res.status).toHaveBeenCalledWith(200);
expect(res.set).toHaveBeenCalledWith('Cache-Control', 'no-store');
expect(res.type).toHaveBeenCalledWith('text/html');
expect(res.send).toHaveBeenCalledWith(
nativeStartPage(`/native-handoff?challenge=${challenge}&start=oidc&invite=inv_A-1`),
);
});
it('NATIVE-CTL-008: refuses a malformed challenge and drops a start or invite it does not know', () => {
const { controller } = build();
const bad = htmlRes();
controller.start('not-a-hash', 'oidc', undefined, bad as unknown as Response);
expect(bad.status).toHaveBeenCalledWith(400);
const res = htmlRes();
controller.start(challenge, 'evil', '"><script>', res as unknown as Response);
expect(res.send).toHaveBeenCalledWith(nativeStartPage(`/native-handoff?challenge=${challenge}`));
});
it('NATIVE-CTL-009: the page loads the forwarding script from the server and escapes the target', () => {
const page = nativeStartPage('/native-handoff?challenge=a&start=oidc');
expect(page).toContain('<script src="/native-handoff.js" data-target="/native-handoff?challenge=a&amp;start=oidc"></script>');
expect(page).toContain('<meta http-equiv="refresh" content="0;url=/native-handoff?challenge=a&amp;start=oidc">');
expect(nativeStartPage('/native-handoff?x="<>')).not.toMatch(/"<>/);
});
});