Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .changeset/magic-link-fragment-code.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'seamless-auth-api': minor
---

Magic links now need a code from the link fragment to sign in, and a requested `redirectUri` must match exactly (#594).

Breaking, in two places:

- `redirectUri` on `POST /magic-link` is no longer accepted on its origin alone. With `magic_link_redirect_uris` empty, the only accepted value is the `/verify-magiclink` page on `frontend_url` or a configured origin, spelled exactly with no query. Any other path, such as a mobile landing page, has to be listed in `magic_link_redirect_uris`.
- The link is now `...?token=<token>#code=<code>`. `GET /magic-link/verify/:token` no longer consumes the link, even on the device that requested it: it always answers `409 confirmation_required`, with a new `sameDevice` boolean. `POST /magic-link/verify/:token/confirm` now requires a JSON body `{ "code": "<code>" }` and answers `400` without it. A wrong code spends the link. Links mailed before the upgrade carry no code and can no longer be completed, so a user with one in flight requests another.

Clients need to read `code` from the fragment, confirm with it, and confirm without asking when `sameDevice` is true. Release a client that does this before upgrading the API: it keeps working against older API versions, which ignore the body. A caller using external delivery that builds its own URL from `delivery.token` has to switch to `delivery.magicLinkUrl`, which carries the code.

Run the database migrations: this adds `magic_link_tokens.code_hash`.
50 changes: 32 additions & 18 deletions docs/api-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,37 @@ protocol in [ecosystem.md](./ecosystem.md).
link lands. Omit it and the link keeps the tenant-wide destination, `frontend_url` falling back
to the first configured origin, which is what every existing caller gets.

A supplied value is validated against the configured `origins` the same way
`resolveOAuthRedirectUri` validates an OAuth redirect, and a value outside them answers `400`.
That is what lets one tenant serve a web client and a mobile client without them sharing a
single destination. The token is set as a `token` query parameter on the target, replacing one
of that name the caller had already put there.

A destination that cannot be expressed as an origin, a custom scheme such as `myapp://auth` or a
universal link on a host that is not a WebAuthn origin, goes in the `magic_link_redirect_uris`
system config key instead. Entries there are matched exactly, which is what makes a custom
scheme safe to accept: there is no origin to compare, so only a literal an operator listed is
allowed. The key defaults to `[]`, and an empty list falls back to comparing against `origins`,
so a deployment that sets nothing behaves as described above.

Setting it narrows rather than widens. Once the list is non-empty it is the whole allowlist, and
a destination on a configured origin that is not listed is refused. That is deliberate: a tenant
that needs a mobile scheme should not have to widen `origins`, which gates passkey ceremonies, to
get it.
A supplied value must match an entry of the `magic_link_redirect_uris` system config key
exactly. While that key is empty (the default), the only accepted values are the
`/verify-magiclink` page on `frontend_url` or on one of the configured `origins`, spelled
exactly, with no query of its own. Anything else answers `400`. Matching on origin alone used to
let a link land on any page of the site, including one that forwards its URL to a third party.

A destination that is not that page, a custom scheme such as `myapp://auth`, a universal link on
a host that is not a WebAuthn origin, or a second path for a mobile client, goes in
`magic_link_redirect_uris`. Once the list is non-empty it is the whole allowlist, and the
default verify page is refused unless it is listed too. A tenant that needs a mobile scheme
should not have to widen `origins`, which gates passkey ceremonies, to get it.

### Magic link token and code

The link carries two values: `?token=` in the query and `#code=` in the fragment. Both replace
any of that name the target already had. The token finds the link; the code proves the person
reading the email is the one completing it. A fragment is never sent to a server and never
appears in a `Referer`, so a page that leaks its query string through a third-party request, an
analytics beacon or an access log hands out a token that cannot sign anyone in.

- `GET /magic-link/verify/:token` describes the link and never consumes it. It always answers
`409 confirmation_required` with `requestedFrom` and `sameDevice`, which is true when the link
was opened by the attempt that requested it (the forwarded pre-auth bearer, or the IP and user
agent a trusted adapter forwards). A client may confirm without asking when it is true.
- `POST /magic-link/verify/:token/confirm` takes `{ "code": "<value from the fragment>" }` and
consumes the link. A request with no code answers `400` from validation; a wrong code answers
`400` and spends the link. The requesting device collects the session from
`GET /magic-link/check` as before.

The landing page should read both values and drop them from the address bar
(`history.replaceState`) before it loads anything else.

### Native and mobile clients

Expand All @@ -113,7 +127,7 @@ IP forwarding in the path while the app holds the tokens. What a deployment has
- **Magic links.** A universal link on the web origin needs nothing extra. A custom scheme goes
in `magic_link_redirect_uris` (exact match, see above). The app can also skip the link entirely
and poll `GET /magic-link/check` with the ephemeral token until the session arrives, since
`/magic-link/verify/:token` completes the link from whichever device opened it.
`/magic-link/verify/:token/confirm` completes the link from whichever device opened it.
- **OAuth.** A custom-scheme `redirectUri` is accepted when it is on the provider's
`redirectUris` allowlist. Some providers refuse custom schemes on a web client type; a
universal link on the web origin's `/oauth/callback` sidesteps that.
Expand Down
2 changes: 1 addition & 1 deletion docs/security-posture.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ A decoy responder that only reproduces the success path is not finished. Any `40
account can be made to answer is an oracle if a decoy answers `200` to the same request,
and the ones that matter are the ones a caller can trigger on purpose:

- `/magic-link?redirectUri=` pointing somewhere the configured origins do not allow
- `/magic-link?redirectUri=` pointing somewhere the magic link allowlist does not allow
answers `400 Redirect URI is not allowed`. A caller picks that value, so this is one
deliberately bad request away. The decoy responder runs the same `resolveMagicLinkUrl`
validation.
Expand Down
137 changes: 17 additions & 120 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -17190,128 +17190,12 @@
"/magic-link/verify/{token}": {
"get": {
"summary": "Verify magic link token",
"description": "Consumes the link when it is opened on the device that requested it, proven by the requesting attempt's ephemeral bearer token when the adapter forwards it, otherwise by the IP address and user agent a trusted adapter forwards. Opened anywhere else, answers 409 without consuming it; confirm with POST /magic-link/verify/{token}/confirm.",
"description": "Describes the link without consuming it, always answering 409 confirmation_required. `sameDevice` is true when it was opened by the attempt that requested it, proven by that attempt's ephemeral bearer token when the adapter forwards it, otherwise by the IP address and user agent a trusted adapter forwards. Consume it with POST /magic-link/verify/{token}/confirm and the code from the link fragment.",
"tags": ["MagicLinks"],
"parameters": [
{ "schema": { "type": "string" }, "required": true, "name": "token", "in": "path" }
],
"responses": {
"200": {
"description": "HTTP 200",
"content": {
"application/json": {
"example": { "message": "string", "token": "string", "delivery": null },
"schema": {
"type": "object",
"properties": {
"message": { "type": "string" },
"token": { "type": "string" },
"delivery": {
"oneOf": [
{
"type": "object",
"properties": {
"kind": { "type": "string", "enum": ["otp_email"] },
"to": { "type": "string" },
"token": { "type": "string" },
"purpose": { "type": "string", "enum": ["account_recovery"] }
},
"required": ["kind", "to", "token"]
},
{
"type": "object",
"properties": {
"kind": { "type": "string", "enum": ["otp_sms"] },
"to": { "type": "string" },
"token": { "anyOf": [{ "type": "string" }, { "type": "number" }] },
"purpose": { "type": "string", "enum": ["account_recovery"] }
},
"required": ["kind", "to", "token"]
},
{
"type": "object",
"properties": {
"kind": { "type": "string", "enum": ["magic_link_email"] },
"to": { "type": "string" },
"token": { "type": "string" },
"magicLinkUrl": { "type": "string" }
},
"required": ["kind", "to", "magicLinkUrl"]
},
{
"type": "object",
"properties": {
"kind": { "type": "string", "enum": ["enrollment_invite_email"] },
"to": { "type": "string" },
"signInUrl": { "type": "string" }
},
"required": ["kind", "to", "signInUrl"]
},
{
"type": "object",
"properties": {
"kind": { "type": "string", "enum": ["account_notice_email"] },
"notice": {
"type": "string",
"enum": [
"account_recovery_opened",
"account_recovery_completed",
"recovery_code_used",
"identity_changed",
"application_notice"
]
},
"to": { "type": "string" },
"subject": { "type": "string" },
"text": { "type": "string" },
"cancelUrl": { "type": "string" },
"application": {
"type": "object",
"properties": {
"clientId": { "type": "string" },
"name": { "type": "string" }
},
"required": ["clientId", "name"]
}
},
"required": ["kind", "notice", "to", "subject", "text"]
},
{
"type": "object",
"properties": {
"kind": { "type": "string", "enum": ["account_notice_sms"] },
"notice": {
"type": "string",
"enum": [
"account_recovery_opened",
"account_recovery_completed",
"recovery_code_used",
"identity_changed",
"application_notice"
]
},
"to": { "type": "string" },
"text": { "type": "string" },
"cancelUrl": { "type": "string" },
"application": {
"type": "object",
"properties": {
"clientId": { "type": "string" },
"name": { "type": "string" }
},
"required": ["clientId", "name"]
}
},
"required": ["kind", "notice", "to", "text"]
}
]
}
},
"required": ["message"]
}
}
}
},
"400": {
"description": "HTTP 400",
"content": {
Expand Down Expand Up @@ -17373,13 +17257,15 @@
"example": {
"error": null,
"message": "string",
"sameDevice": true,
"requestedFrom": { "deviceClass": null, "requestedAt": "string" }
},
"schema": {
"type": "object",
"properties": {
"error": { "type": "string", "enum": ["confirmation_required"] },
"message": { "type": "string" },
"sameDevice": { "type": "boolean" },
"requestedFrom": {
"type": "object",
"properties": {
Expand All @@ -17401,7 +17287,7 @@
"required": ["deviceClass", "requestedAt"]
}
},
"required": ["error", "message", "requestedFrom"]
"required": ["error", "message", "sameDevice", "requestedFrom"]
}
}
}
Expand Down Expand Up @@ -17437,12 +17323,23 @@
},
"/magic-link/verify/{token}/confirm": {
"post": {
"summary": "Confirm a magic link opened on another device",
"description": "Consumes the link after the person who opened it has approved signing in the device that requested it.",
"summary": "Confirm a magic link",
"description": "Consumes the link, signing in the device that requested it. Takes the `code` the link carries in its fragment (`#code=`), which never appears in a query string or a Referer. A wrong code spends the link.",
"tags": ["MagicLinks"],
"parameters": [
{ "schema": { "type": "string" }, "required": true, "name": "token", "in": "path" }
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": { "code": { "type": "string", "minLength": 1, "maxLength": 256 } },
"required": ["code"]
}
}
}
},
"responses": {
"200": {
"description": "HTTP 200",
Expand Down
6 changes: 2 additions & 4 deletions src/controllers/decoyResponders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,8 @@ export const decoyRequestMagicLink = async (req: Request, res: Response) => {
// of one deliberately bad request. The URL the first one builds goes nowhere, since
// nothing is mailed; the call is made for its refusal.
try {
await resolveMagicLinkUrl(
decoyCredentialIdFor(decoySubject(req)),
req.query.redirectUri as string | undefined,
);
const decoyId = decoyCredentialIdFor(decoySubject(req));
await resolveMagicLinkUrl(decoyId, decoyId, req.query.redirectUri as string | undefined);
} catch (error) {
if (error instanceof MagicLinkRedirectNotAllowedError) {
return res.status(400).json({ error: 'Redirect URI is not allowed' });
Expand Down
Loading
Loading