Make the measurement-token mint cookie-only

The mint used requireAuth() with no scope, which refuses a narrow token
but still admits a cookie-equivalent one. The reach is not the problem —
a wildcard token can already do everything this endpoint does. The
lifetimes are: a native access token lives a day, and what it mints here
lives a year. So a credential that leaked for an afternoon could leave
behind one that outlives revoking it, and the revocation would look like
it had worked.

requireCookieAuth() closes that. It is the helper the passkey
registration and trusted-device routes already use for the same reason,
and the sharing guard recognises it as cookie-only.

The prose said more than the code did, in three places: the route header
and the OpenAPI description both claimed the endpoint "cannot mint
another token", which held only for narrow tokens, and the 403 arm
described a scope refusal that a Bearer never reaches. All three now say
what is true — no Bearer credential reaches this endpoint at any scope,
and a Bearer is refused 401 before the operator switch is read.

Tests follow the status change: the integration case asserting a scoped
token cannot mint now expects 401 rather than 403, since the refusal is
authentication and not scope, and a companion case pins the wildcard —
the credential class this change is actually about — and asserts no
token row is created.
This commit is contained in:
Antiheld86 committed 2026-08-31 11:38:16 +00:00
1 parent 8f61e0bc6a
commit 56d6fb180a
5 files changed
+57 -16

No files matched your search

@@ -13,7 +13,7 @@ import { describe, it, expect, vi, beforeEach } from "vitest";
vi.mock("@/lib/api-handler", () => ({
apiHandler: (fn: unknown) => fn,
requireAuth: vi.fn(),
requireCookieAuth: vi.fn(),
}));
vi.mock("@/lib/auth/issue-token", () => ({
@@ -38,7 +38,7 @@ vi.mock("@/lib/logging/context", () => ({
}));
import { POST } from "../route";
import { requireAuth } from "@/lib/api-handler";
import { requireCookieAuth } from "@/lib/api-handler";
import { issueApiToken } from "@/lib/auth/issue-token";
import { isApiGloballyEnabled } from "@/lib/app-settings";
import { checkRateLimit } from "@/lib/rate-limit";
@@ -58,7 +58,7 @@ function req(body: unknown): Request {
beforeEach(() => {
vi.resetAllMocks();
vi.mocked(requireAuth).mockResolvedValue({ user: USER } as never);
vi.mocked(requireCookieAuth).mockResolvedValue({ user: USER } as never);
vi.mocked(isApiGloballyEnabled).mockResolvedValue(true);
vi.mocked(checkRateLimit).mockResolvedValue({ allowed: true } as never);
vi.mocked(auditLog).mockResolvedValue(undefined as never);
+17 -10
View File
@@ -15,9 +15,8 @@
* request shape reaches it; the scope it names is accepted by exactly two
* routes, both writes, both on the holder's own record. It cannot read a
* reading back, cannot edit or delete one, cannot reach the export, and cannot
* mint another token — the `requireAuth()` below names no scope, so a
* measurements token presenting itself here is refused like any other narrow
* credential. That last property is why the mint is worth its own file: the
* mint another token — no Bearer credential reaches this endpoint at all, at
* any scope. That last property is why the mint is worth its own file: the
* blast radius of a credential pasted into a container the user does not
* operate should not include making more credentials.
*
@@ -31,7 +30,7 @@
*/
import { NextRequest } from "next/server";
import { apiHandler, requireAuth } from "@/lib/api-handler";
import { apiHandler, requireCookieAuth } from "@/lib/api-handler";
import {
apiError,
apiSuccess,
@@ -64,12 +63,20 @@ const MINT_RATE_LIMIT_MAX = 10;
const MINT_RATE_LIMIT_WINDOW_MS = 60 * 1000;
export const POST = apiHandler(async (request: NextRequest) => {
// No scope argument, and that is load-bearing rather than incidental: it
// keeps the fail-closed default, so the only credentials that reach the mint
// are a cookie session and a cookie-equivalent token. It also refuses any
// acting-account carrier, so a delegate cannot mint a credential against the
// record they are helping with.
const { user } = await requireAuth();
// Cookie-only, and the reason is lifetime rather than reach.
//
// `requireAuth()` would refuse a narrow token — a measurements credential
// cannot mint its own successor — but it still admits a cookie-EQUIVALENT
// one, and that is the case worth closing. A native access token lives a
// day; what it could mint here lives a year. So a credential that leaked for
// an afternoon would leave behind one that outlives revoking it, and the
// revocation would look like it had worked.
//
// A session cannot do that: it is held by a browser the person is sitting in
// front of, and it is the surface this endpoint is reached from anyway. The
// same argument the passkey-registration and trusted-device routes make, and
// `requireCookieAuth` is the helper they share.
const { user } = await requireCookieAuth();
annotate({ action: { name: "tokens.measurements.create" } });
if (!(await isApiGloballyEnabled())) {