mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 08:00:06 +00:00
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:
@@ -145,6 +145,9 @@ export const PUBLIC_ROUTE_ALLOW_LIST: string[] = [
|
||||
// The native app redeems a one-time code from the system browser here. The
|
||||
// code is only good together with the verifier the app kept to itself.
|
||||
'NativeAuthController.exchange',
|
||||
// Where the app's sign-in in the system browser starts: a page that forwards
|
||||
// to the handoff page and carries no credential.
|
||||
'NativeAuthController.start',
|
||||
'OauthPublicController.revoke',
|
||||
'OauthPublicController.token',
|
||||
'OauthPublicController.userinfo',
|
||||
|
||||
@@ -10,7 +10,7 @@ import { ResponseContract } from '../common/response-contract';
|
||||
import { sessionClientFrom } from '../sessions/sessions.service';
|
||||
import { NativeExchangeDto, NativeHandoffDto } from './native-auth.dto';
|
||||
import { NativeAuthService } from './native-auth.service';
|
||||
import { Body, Controller, HttpCode, HttpException, Post, Req, Res, UseGuards } from '@nestjs/common';
|
||||
import { Body, Controller, Get, HttpCode, HttpException, Post, Query, Req, Res, UseGuards } from '@nestjs/common';
|
||||
import {
|
||||
nativeHandoffResponseSchema,
|
||||
successResponseSchema,
|
||||
@@ -23,6 +23,36 @@ import type { Request, Response } from 'express';
|
||||
const EXCHANGE_WINDOW_MS = 15 * 60 * 1000;
|
||||
const EXCHANGE_MAX = 20;
|
||||
|
||||
const CHALLENGE = /^[0-9a-f]{64}$/;
|
||||
const START = new Set(['oidc', 'passkey']);
|
||||
const INVITE = /^[A-Za-z0-9_-]{1,200}$/;
|
||||
|
||||
/** `&`, `<`, `>` and `"` as entities, for a value inside a double-quoted attribute. */
|
||||
function attr(value: string): string {
|
||||
return value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
||||
}
|
||||
|
||||
/**
|
||||
* The page /api/auth/native/start answers with: it takes a service worker of an
|
||||
* older TREK off the browser, then opens the handoff page (public/native-handoff.js).
|
||||
* A browser that ran TREK before 5.1.1 keeps that version's app in its service
|
||||
* worker, and the worker answers /native-handoff from its cache with an app that
|
||||
* has no such page: the sign-in ended on the dashboard in the browser instead of
|
||||
* in the app. Every TREK service worker lets /api through to the server, so this
|
||||
* page always comes from the running version.
|
||||
*/
|
||||
export function nativeStartPage(target: string): string {
|
||||
const href = attr(target);
|
||||
return [
|
||||
'<!doctype html>',
|
||||
'<html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">',
|
||||
'<meta name="robots" content="noindex"><title>TREK</title>',
|
||||
`<noscript><meta http-equiv="refresh" content="0;url=${href}"></noscript>`,
|
||||
`<script src="/native-handoff.js" data-target="${href}"></script>`,
|
||||
'</head><body></body></html>',
|
||||
].join('');
|
||||
}
|
||||
|
||||
/**
|
||||
* /api/auth/native: the sign-in hand-off between the system browser and the
|
||||
* native app. See the contract in @trek/shared for the whole round trip.
|
||||
@@ -47,6 +77,34 @@ export class NativeAuthController {
|
||||
return { code: await this.nativeAuth.issueCode(user.id, body.challenge) };
|
||||
}
|
||||
|
||||
/**
|
||||
* Where the app's sign-in in the system browser starts. Answers a small page
|
||||
* instead of the handoff page itself (see nativeStartPage); the query is the
|
||||
* one the handoff page takes, checked here so nothing else rides along.
|
||||
*/
|
||||
// response-contract-exempt: an HTML page that forwards the browser, no body to check
|
||||
@Get('start')
|
||||
@Public('opens the handoff page in the browser; it carries no credential and shows nothing')
|
||||
start(
|
||||
@Query('challenge') challenge: unknown,
|
||||
@Query('start') start: unknown,
|
||||
@Query('invite') invite: unknown,
|
||||
@Res() res: Response,
|
||||
): void {
|
||||
if (typeof challenge !== 'string' || !CHALLENGE.test(challenge)) {
|
||||
res.status(400).type('text/plain').send('Invalid sign-in link.');
|
||||
return;
|
||||
}
|
||||
const params = new URLSearchParams({ challenge });
|
||||
if (typeof start === 'string' && START.has(start)) params.set('start', start);
|
||||
if (typeof invite === 'string' && INVITE.test(invite)) params.set('invite', invite);
|
||||
res
|
||||
.status(200)
|
||||
.set('Cache-Control', 'no-store')
|
||||
.type('text/html')
|
||||
.send(nativeStartPage(`/native-handoff?${params.toString()}`));
|
||||
}
|
||||
|
||||
@Post('exchange')
|
||||
@Public('the one-time code together with its verifier is the credential')
|
||||
@HttpCode(200)
|
||||
|
||||
@@ -132,4 +132,14 @@ describe('Native sign-in hand-off e2e', () => {
|
||||
const blocked = await exchange({ code: 'nope', verifier });
|
||||
expect(blocked.status).toBe(429);
|
||||
});
|
||||
|
||||
it('NATIVE-E2E-006: the browser starts at a page under /api that forwards to the handoff page', async () => {
|
||||
const res = await request(server).get(`/api/auth/native/start?challenge=${challenge}&start=oidc`);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers['content-type']).toMatch(/text\/html/);
|
||||
expect(res.headers['cache-control']).toBe('no-store');
|
||||
expect(res.text).toContain(`data-target="/native-handoff?challenge=${challenge}&start=oidc"`);
|
||||
|
||||
expect((await request(server).get('/api/auth/native/start?challenge=nope')).status).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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&start=oidc"></script>');
|
||||
expect(page).toContain('<meta http-equiv="refresh" content="0;url=/native-handoff?challenge=a&start=oidc">');
|
||||
expect(nativeStartPage('/native-handoff?x="<>')).not.toMatch(/"<>/);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user