Skip to content

Commit ffba6e2

Browse files
committed
fix(spec): say what the app-scheme rule enforces; refuse an empty redirect_uri
Contract-review should-fixes: the scheme prose now states the regex (two or more non-empty labels joined by single dots), and redirect_uri takes min(1) and is described as an absolute URI. Claude-Session: https://claude.ai/code/session_01JQwxbXghRkYmSR5dVh8PFG Co-authored-by: Claude <noreply@anthropic.com>
1 parent b73cfb2 commit ffba6e2

5 files changed

Lines changed: 15 additions & 8 deletions

File tree

‎.changeset/29736-mobile-sign-in-handoff-spec.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ Metadata-protocol: added
99

1010
Some identity providers refuse embedded WebViews, so a mobile app signs in through the system browser and needs the session handed back to its WebView. This declares the contract the auth plugin serves for that, before the plugin enables it:
1111

12-
- **`AuthEndpointPaths.oneTimeTokenHandoff`** (`GET /one-time-token/handoff`) with `OneTimeTokenHandoffQuerySchema`: a closed query of `redirect_uri`, an RFC 7636 S256 `code_challenge` and an optional `code_challenge_method` (only `S256`). A signed-in session is redirected to `redirect_uri` with a single-use `token` that expires after one minute. An unregistered scheme, an unknown or repeated parameter, or a bad challenge is refused with `400 VALIDATION_ERROR` and no redirect.
12+
- **`AuthEndpointPaths.oneTimeTokenHandoff`** (`GET /one-time-token/handoff`) with `OneTimeTokenHandoffQuerySchema`: a closed query of `redirect_uri` (an absolute URI), an RFC 7636 S256 `code_challenge` and an optional `code_challenge_method` (only `S256`). A signed-in session is redirected to `redirect_uri` with a single-use `token` that expires after one minute. An unregistered scheme, an unknown or repeated parameter, or a bad challenge is refused with `400 VALIDATION_ERROR` and no redirect.
1313
- **`AuthEndpointPaths.oneTimeTokenVerify`** (`POST /one-time-token/verify`, better-auth's own route) with `OneTimeTokenVerifyRequestSchema` and `OneTimeTokenVerifyResponseSchema`. The body is `{ token }`, where the token is followed by a dot and the code verifier. A callback intercepted by another app cannot be redeemed without the verifier. The response is better-auth's unwrapped `{ session, user }` with the session cookie. better-auth's `GET /one-time-token/generate` refuses client requests, so it is not declared.
14-
- **`AuthConfig.trustedAppSchemes`** (new authorable key, beside `trustedOrigins`): the native-app schemes the hand-off may redirect to. Each entry is a `TrustedAppSchemeSchema`: a lowercase reverse-domain scheme written bare, with at least one dot (RFC 8252 section 7.1), such as `com.example.app`. A malformed entry is refused, not dropped or rewritten. That includes a `://` suffix, an uppercase letter, a dotless name such as `myapp`, and every web scheme such as `https`. If the list is absent or empty, the hand-off is not offered.
14+
- **`AuthConfig.trustedAppSchemes`** (new authorable key, beside `trustedOrigins`): the native-app schemes the hand-off may redirect to. Each entry is a `TrustedAppSchemeSchema`: a lowercase reverse-domain scheme written bare: two or more non-empty labels joined by single dots, the first starting with a letter (RFC 8252 section 7.1), such as `com.example.app`. A malformed entry is refused, not dropped or rewritten. That includes a `://` suffix, an uppercase letter, a dotless name such as `myapp`, and every web scheme such as `https`. If the list is absent or empty, the hand-off is not offered.
1515

1616
No `/auth/config` feature flag is added, because nothing reads one yet.
1717

‎packages/spec/src/api/auth-endpoints.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ describe('mobile sign-in hand-off (one-time token)', () => {
389389
expect(result.error!.issues[0].code).toBe('unrecognized_keys');
390390
});
391391

392-
it('refuses the plain method, a malformed challenge and a missing challenge', () => {
392+
it('refuses the plain method, a malformed challenge, a missing challenge and an empty redirect_uri', () => {
393393
const base = { redirect_uri: 'com.example.app://cb', code_challenge: challenge };
394394
const plain = OneTimeTokenHandoffQuerySchema.safeParse({ ...base, code_challenge_method: 'plain' });
395395
expect(plain.success).toBe(false);
@@ -399,6 +399,9 @@ describe('mobile sign-in hand-off (one-time token)', () => {
399399
expect(short.error!.issues[0].path).toEqual(['code_challenge']);
400400
const padded = OneTimeTokenHandoffQuerySchema.safeParse({ ...base, code_challenge: `${challenge.slice(1)}=` });
401401
expect(padded.success).toBe(false);
402+
const emptyUri = OneTimeTokenHandoffQuerySchema.safeParse({ ...base, redirect_uri: '' });
403+
expect(emptyUri.success).toBe(false);
404+
expect(emptyUri.error!.issues[0].path).toEqual(['redirect_uri']);
402405
const missing = OneTimeTokenHandoffQuerySchema.safeParse({ redirect_uri: base.redirect_uri });
403406
expect(missing.success).toBe(false);
404407
expect(missing.error!.issues[0].path).toEqual(['code_challenge']);

‎packages/spec/src/api/auth-endpoints.zod.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,8 @@ export type DeviceTokenResponse = z.input<typeof DeviceTokenResponseSchema>;
438438
* The parameter names are the OAuth 2.0 / RFC 7636 wire names, kept verbatim.
439439
*/
440440
export const OneTimeTokenHandoffQuerySchema = lazySchema(() => z.object({
441-
redirect_uri: z.string().describe(
442-
'Where to send the token: a URI whose scheme is registered in AuthConfig.trustedAppSchemes '
441+
redirect_uri: z.string().min(1).describe(
442+
'Where to send the token: an absolute URI whose scheme is registered in AuthConfig.trustedAppSchemes '
443443
+ '(e.g. com.example.app://auth/callback); any other scheme is refused with 400 and no redirect',
444444
),
445445
code_challenge: z.string().regex(/^[A-Za-z0-9_-]{43}$/).describe(

‎packages/spec/src/system/auth-config.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ describe('AuthConfigSchema', () => {
162162
['an uppercase scheme', 'Com.Example.App'],
163163
['an empty label', 'com..app'],
164164
['a trailing dot', 'com.example.'],
165+
['a leading dot', '.com.example'],
165166
])('refuses %s in trustedAppSchemes (%s), never dropping or rewriting it', (_label, scheme) => {
166167
expect(TrustedAppSchemeSchema.safeParse(scheme).success).toBe(false);
167168
const result = AuthConfigSchema.safeParse({ trustedAppSchemes: ['com.example.app', scheme] });

‎packages/spec/src/system/auth-config.zod.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,9 @@ export const AdvancedAuthConfigSchema = lazySchema(() => z.object({
547547
* (`GET /api/v1/auth/one-time-token/handoff`, see `OneTimeTokenHandoffQuerySchema`).
548548
*
549549
* Strict, never normalised: a lowercase RFC 3986 scheme written bare (no `:` or
550-
* `://` suffix) that contains at least one dot. The dot is RFC 8252 section 7.1's
550+
* `://` suffix) made of two or more non-empty labels joined by single dots, the
551+
* first starting with a letter (so `com..app`, `.com.app` and `com.example.` are
552+
* refused). The dot is RFC 8252 section 7.1's
551553
* rule that a private-use scheme be a reverse domain name the app's owner
552554
* controls (`com.example.app`), which is also what keeps two apps on one device
553555
* from claiming the same scheme and intercepting the token. The rule refuses, by
@@ -558,10 +560,11 @@ export const AdvancedAuthConfigSchema = lazySchema(() => z.object({
558560
export const TrustedAppSchemeSchema = lazySchema(() => z.string().regex(/^[a-z][a-z0-9+-]*(\.[a-z0-9+-]+)+$/, {
559561
message:
560562
'An app scheme is a lowercase reverse-domain URI scheme written bare, e.g. "com.example.app": '
561-
+ 'no ":" or "://" suffix, no uppercase, and at least one dot (RFC 8252 section 7.1). '
563+
+ 'no ":" or "://" suffix, no uppercase, and two or more non-empty labels joined by single dots, '
564+
+ 'the first starting with a letter (RFC 8252 section 7.1). '
562565
+ 'http, https and other web schemes are never app schemes.',
563566
}).describe(
564-
'A registered native-app URI scheme: lowercase, reverse-domain with at least one dot, written bare (e.g. com.example.app)',
567+
'A registered native-app URI scheme: lowercase, reverse-domain (two or more non-empty labels joined by single dots, the first starting with a letter), written bare (e.g. com.example.app)',
565568
));
566569

567570
export const AuthConfigSchema = lazySchema(() => z.object({

0 commit comments

Comments
 (0)