diff --git a/.changeset/pin-templates-v0-23-0.md b/.changeset/pin-templates-v0-23-0.md new file mode 100644 index 0000000..6e0a8d7 --- /dev/null +++ b/.changeset/pin-templates-v0-23-0.md @@ -0,0 +1,11 @@ +--- +"seamless-cli": patch +--- + +New projects scaffold auth API v0.24.3 and the starter templates v0.23.0. + +The auth API release brings the v0.24 security fixes: magic links need the code from the link fragment, step-up and factor enrollment are stricter, refresh token reuse is detected at any age, pre-auth endpoints answer the same whether or not an account exists, and the OpenID Provider contract is version 4, so a scaffolded stack can run `@seamless-auth/oidc-provider` 0.5.x. + +The templates' API starters use the adapter releases that default the auth cookies to `SameSite=Lax` and refuse a state-changing request from an `Origin` the API does not allow (`@seamless-auth/express` 0.27.0, `@seamless-auth/fastify` 0.18.0, and `seamless-auth` 0.7.0 for Go, Rust and Python). The web starters send security headers and a Content-Security-Policy and read the magic link code from the fragment, the Docker builds default to the production stage, and the Express and Fastify starters run in production mode from `npm start`. Existing projects keep the versions they were scaffolded with. + +On auth API 0.24 a step-up only accepts an authenticator app enrolled before the session signed in. When the app is newer, the CLI now says to sign in again and keeps the session, where it used to refresh the session, retry, and then clear it with a message that the session was rejected. diff --git a/scripts/cli-commands-e2e.sh b/scripts/cli-commands-e2e.sh index 27885b1..9884888 100755 --- a/scripts/cli-commands-e2e.sh +++ b/scripts/cli-commands-e2e.sh @@ -112,9 +112,12 @@ seamless users list --json | jq -e --arg e "$OWNER" 'any(.[]; .email == $e)' >/d # with a code from the user's authenticator app. Give the owner one, check that a # write without a code is refused with the step-up message, then let the first write # step up from SEAMLESS_TOTP_CODE. The step-up then covers the rest of the run. +# From v0.24 a step-up only accepts an app enrolled before the session signed in, so +# the owner signs in again after enrolling, as a person would. echo "==> admin step-up with an authenticator app" TOTP_STATE="$WORKDIR/totp.json" node "$REPO_ROOT/scripts/e2e-authenticator.mjs" enroll "$TOTP_STATE" +seamless profile login "$PROFILE" "$OWNER" --local if refused="$(seamless org create "No Step-up" --slug no-step-up 2>&1 { expect((await getTokens(profile))?.refreshToken).toBe("refresh"); }); + it("returns a step-up refused for a too-new app without refreshing or clearing the session", async () => { + const calls = mockFetch([() => json({ error: "step_up_unavailable" }, 401)]); + + const client = await createAuthClient({ stepUp: vi.fn() }); + const res = await client.post("/totp/verify-mfa", { code: "123456" }); + + expect(res.status).toBe(401); + expect(calls).toHaveLength(1); + expect((await getTokens(profile))?.refreshToken).toBe("refresh"); + }); + it("steps up with the authenticator app by default", async () => { vi.stubEnv("SEAMLESS_TOTP_CODE", "123456"); const calls = mockFetch([ diff --git a/src/core/authClient.ts b/src/core/authClient.ts index 949ebaf..6a38617 100644 --- a/src/core/authClient.ts +++ b/src/core/authClient.ts @@ -17,6 +17,7 @@ import { sessionStepUp } from "./providerStepUp.js"; import { isFactorRejection, isStepUpRequired, + isStepUpUnavailable, type StepUpHandler, type StepUpSession, } from "./stepUp.js"; @@ -249,7 +250,12 @@ async function createClientForProfile( const sentWith = session.accessToken; let res = await apiRequest(url, build()); - if (res.status === 401 && session.refreshToken && !isFactorRejection(res)) { + if ( + res.status === 401 && + session.refreshToken && + !isFactorRejection(res) && + !isStepUpUnavailable(res) + ) { // Only refresh if nothing else already did while this request was in flight. // Otherwise a burst of 401s against one stale token would each rotate again. if (session.accessToken === sentWith) { diff --git a/src/core/images.ts b/src/core/images.ts index d4dc204..ee70dd4 100644 --- a/src/core/images.ts +++ b/src/core/images.ts @@ -7,12 +7,12 @@ export const POSTGRES_IMAGE = "postgres:18"; // bumped, because a tag can be moved and what it names is then built and run on the // developer's machine. Bump them together: `npm run check:templates-pin` (run in CI) // checks every pin in this file against its tag and fails when one disagrees. -export const SEAMLESS_AUTH_API_VERSION = "v0.23.0"; +export const SEAMLESS_AUTH_API_VERSION = "v0.24.3"; -export const SEAMLESS_AUTH_API_COMMIT = "afd4495c2af3f2be709d4f8802124870382a7716"; +export const SEAMLESS_AUTH_API_COMMIT = "f05dc338f0f62a89e516e7dfb977b7e4bb5fdc2a"; export const SEAMLESS_AUTH_API_IMAGE_DIGEST = - "sha256:c32b2e84a32b2b5a2fd69112453090f62a89226e81f86cf2d06d49ec9fafc2c9"; + "sha256:95fb964f0430215f75310aa311950ba024e166bdb7dfb6b8673c6230d3d6c999"; export const SEAMLESS_AUTH_API_IMAGE = `ghcr.io/fells-code/seamless-auth-api:${SEAMLESS_AUTH_API_VERSION}@${SEAMLESS_AUTH_API_IMAGE_DIGEST}`; @@ -41,6 +41,6 @@ export const SEAMLESS_AUTH_ADMIN_DASHBOARD_COMMIT = "df2a8b78f7a8eb7adeec67e900d // SEAMLESS_TEMPLATES_DIR. export const SEAMLESS_TEMPLATES_REPO = "fells-code/seamless-templates"; -export const SEAMLESS_TEMPLATES_REF = "v0.22.1"; +export const SEAMLESS_TEMPLATES_REF = "v0.23.0"; -export const SEAMLESS_TEMPLATES_COMMIT = "0c0618e4c8f13af9ffd68b1129719bd375e5dd25"; +export const SEAMLESS_TEMPLATES_COMMIT = "a9945875b172aacbfbe17de724e8f1ba46edabec"; diff --git a/src/core/stepUp.test.ts b/src/core/stepUp.test.ts index 8cb17fa..127bea1 100644 --- a/src/core/stepUp.test.ts +++ b/src/core/stepUp.test.ts @@ -17,10 +17,12 @@ import { forbidden, isFactorRejection, isStepUpRequired, + isStepUpUnavailable, MAX_CODE_ATTEMPTS, NO_AUTHENTICATOR_MESSAGE, STEP_UP_REQUIRED_MESSAGE, StepUpError, + STEP_UP_UNAVAILABLE_MESSAGE, TOTP_CODE_ENV, } from "./stepUp.js"; import { isInteractive } from "./tty.js"; @@ -110,6 +112,14 @@ describe("response classification", () => { expect(plain.message).toMatch(/requires an admin role/); }); + it("tells an authenticator app too new for step-up apart from a wrong code", () => { + const tooNew = response(401, { error: "step_up_unavailable" }); + expect(isStepUpUnavailable(tooNew)).toBe(true); + expect(isFactorRejection(tooNew)).toBe(false); + expect(isStepUpUnavailable(wrongCode)).toBe(false); + expect(isStepUpUnavailable(response(403, { error: "step_up_unavailable" }))).toBe(false); + }); + it("treats only a rejected code as a factor rejection", () => { expect(isFactorRejection(wrongCode)).toBe(true); expect(isFactorRejection(response(401, { error: "unauthorized" }))).toBe(false); @@ -353,6 +363,12 @@ describe("authenticatorAppStepUp: verify errors", () => { ); }); + it("says to sign in again when the app was enrolled after this session signed in", async () => { + const err = await failWith(response(401, { error: "step_up_unavailable" })); + expect(err).toBeInstanceOf(StepUpError); + expect(err.message).toBe(STEP_UP_UNAVAILABLE_MESSAGE); + }); + it("reports any other failure with its status", async () => { const err = await failWith(response(500, { error: "boom" })); expect(err).toBeInstanceOf(StepUpError); diff --git a/src/core/stepUp.ts b/src/core/stepUp.ts index 593683f..9e28fe1 100644 --- a/src/core/stepUp.ts +++ b/src/core/stepUp.ts @@ -60,6 +60,17 @@ export function isFactorRejection(res: Pick): bo return res.status === 401 && errorCode(res.data) === "totp_verification_failed"; } +// From auth API 0.24.0 a step-up only accepts an authenticator app enrolled before +// the session signed in, and refuses a newer one with this 401. It is an answer about +// the factor, not the access token: refreshing cannot change it, and another code from +// the same app gets the same answer. +export function isStepUpUnavailable(res: Pick): boolean { + return res.status === 401 && errorCode(res.data) === "step_up_unavailable"; +} + +export const STEP_UP_UNAVAILABLE_MESSAGE = + "Your authenticator app was added after this session signed in, and a step-up only accepts one from before it. Sign in again, then run this command again."; + export function forbidden(res: Pick): PermissionError { return isStepUpRequired(res) ? new StepUpError(STEP_UP_REQUIRED_MESSAGE) @@ -176,6 +187,10 @@ export function authenticatorAppStepUp( if (res.ok) return; + if (isStepUpUnavailable(res)) { + throw new StepUpError(STEP_UP_UNAVAILABLE_MESSAGE); + } + if (isFactorRejection(res)) { if (attempt >= MAX_CODE_ATTEMPTS) { throw new StepUpError(