Add invite universal-link landing for app deep-link

Encode the admin invite QR as a clean /invite/<hlv_token> universal-link
that the iOS app intercepts via the AASA wildcard matcher. Add the
matching web route as a thin, shape-validated redirect onto the existing
register flow, and open the path on the public allowlist so an
unauthenticated scanner reaches it. A malformed token segment is
indistinguishable from no invite — no database hit, no enumeration
oracle. The token is never logged.
This commit is contained in:
Marc-André Bombeck committed 2026-06-14 19:32:19 +02:00
1 parent e561f8d893
commit c1eb618dcc
8 files changed
+235 -5

No files matched your search

@@ -0,0 +1,73 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
/**
* v1.17.0 — invite universal-link landing (iOS #16).
*
* `/invite/<hlv_token>` is a thin, server-side redirect: it validates the
* `hlv_` shape, then bounces a browser onto the existing
* `/auth/register?invite=<token>` flow. These tests pin the security
* contract — the token shape gate, the carry-through to register, and the
* non-oracle behaviour where a malformed segment is indistinguishable
* from "no invite". The route never touches the database.
*/
const redirectMock = vi.fn((href: string) => {
// next/navigation `redirect()` throws a sentinel inside the
// server-component renderer to short-circuit rendering. Mimic it by
// throwing an error tagged with the href so each test can assert the
// exact redirect target.
const err = new Error(`__redirect__:${href}`);
(err as Error & { __redirect__: string }).__redirect__ = href;
throw err;
});
vi.mock("next/navigation", () => ({
redirect: (href: string) => redirectMock(href),
}));
import InviteLandingPage from "../page";
beforeEach(() => {
redirectMock.mockClear();
});
const VALID_TOKEN = `hlv_${"a".repeat(64)}`;
async function runRedirect(token: string): Promise<string> {
try {
await InviteLandingPage({ params: Promise.resolve({ token }) });
} catch (e) {
const tagged = e as Error & { __redirect__?: string };
if (tagged.__redirect__) return tagged.__redirect__;
throw e;
}
throw new Error("expected redirect, none thrown");
}
describe("<InviteLandingPage> universal-link landing", () => {
it("carries a well-formed token through to the register flow", async () => {
const href = await runRedirect(VALID_TOKEN);
expect(href).toBe(`/auth/register?invite=${VALID_TOKEN}`);
});
it("encodes the token segment into the register query string", async () => {
const href = await runRedirect(VALID_TOKEN);
const url = new URL(href, "https://h.example.com");
expect(url.pathname).toBe("/auth/register");
expect(url.searchParams.get("invite")).toBe(VALID_TOKEN);
});
it("drops a malformed token to plain register (no enumeration oracle)", async () => {
// Wrong prefix, wrong length, and outright garbage all land on the
// identical target — the page never reveals whether a token exists.
for (const bad of [
"not-a-token",
`hlk_${"a".repeat(64)}`,
`hlv_${"a".repeat(63)}`,
`hlv_${"G".repeat(64)}`,
]) {
redirectMock.mockClear();
const href = await runRedirect(bad);
expect(href).toBe("/auth/register");
}
});
});
+47
View File
@@ -0,0 +1,47 @@
import { redirect } from "next/navigation";
import { looksLikeInviteToken } from "@/lib/auth/invite-token";
/**
* v1.17.0 — invite universal-link landing (iOS #16).
*
* `https://<host>/invite/<hlv_token>` is the URL encoded in the admin
* invite QR and the path the AASA `["*"]` matcher hands to the iOS app:
* - On an installed iOS app the scene delegate intercepts the
* Universal Link before this page ever renders and starts onboarding
* registration with the token prefilled. This server component is the
* BROWSER fallback only.
* - In a browser, the page is a thin, safe redirect onto the existing
* `/auth/register?invite=<token>` flow, where the invite banner shows
* and the token rides the signup POST exactly as before.
*
* Security:
* - The `hlv_` shape is validated before the token is ever reflected
* into the redirect target — a malformed segment lands on plain
* `/auth/register` with no `?invite`, so the page can never echo
* attacker-controlled text into the URL.
* - The route does NOT touch the database and is NOT an enumeration
* oracle: a well-formed-but-unknown token and a well-formed-and-valid
* token redirect to the identical target. Whether a token is real is
* only ever decided at `POST /api/auth/register`, which keeps its
* existing uniform error semantics and rate limit.
* - The token is never logged here; the redirect is the only effect.
*
* The route is listed in `PUBLIC_PATHS` (`/invite/`) so an unauthenticated
* visitor reaches it without the auth-gate bounce to `/auth/login`.
*/
export default async function InviteLandingPage({
params,
}: {
params: Promise<{ token: string }>;
}) {
const { token } = await params;
if (looksLikeInviteToken(token)) {
redirect(`/auth/register?invite=${encodeURIComponent(token)}`);
}
// Malformed token: behave exactly like a visitor with no invite. No DB
// hit, no leak of whether any token exists.
redirect("/auth/register");
}