Skip to content

Commit b9bf57a

Browse files
PiquelChipsclaude
andcommitted
fix(sdk): a union-typed scope proves nothing
`require` and `has` proved every member of a union-typed argument, though the runtime checked only the value it held. They now take one scope per argument, so such a call does not compile. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ab36ae6 commit b9bf57a

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎packages/auth/src/token.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ export type Subject = { kind: 'user'; id: string } | { kind: 'client'; id: strin
116116
/** Names the scopes an {@link AccessToken} is proven to hold; exists only in types. */
117117
declare const proven: unique symbol;
118118

119+
/**
120+
* `S` when it names one scope, `never` when it is a union: a value typed
121+
* `'notes:read' | 'notes:write'` is checked as one of them, so it proves neither.
122+
*/
123+
type Single<S, All = S> = S extends unknown ? ([All] extends [S] ? S : never) : never;
124+
119125
/**
120126
* A validated access token. `Declared` are the scopes its resource declared, and
121127
* `Proven` those it is proven to hold, so a function can ask for
@@ -155,18 +161,20 @@ export class AccessToken<
155161
}
156162

157163
/** Whether the token holds `scope`; narrows its type when it does. */
158-
has<S extends Declared>(scope: S): this is AccessToken<Declared, Proven | S> {
164+
has<S extends Declared>(scope: S & Single<S>): this is AccessToken<Declared, Proven | S> {
159165
return this.scopes.has(scope);
160166
}
161167

162168
/**
163169
* Requires every scope in `scopes`, returning the token with their proof, or throws
164170
* `insufficient_scope`, whose challenge names every scope required.
165171
*/
166-
require<S extends Declared>(...scopes: [S, ...S[]]): AccessToken<Declared, Proven | S> {
172+
require<const T extends readonly [Declared, ...Declared[]]>(
173+
...scopes: T & { [K in keyof T]: Single<T[K]> }
174+
): AccessToken<Declared, Proven | T[number]> {
167175
// The proof exists only in types, and the scopes were just checked.
168176
if (scopes.every((scope) => this.scopes.has(scope))) {
169-
return this as unknown as AccessToken<Declared, Proven | S>;
177+
return this as unknown as AccessToken<Declared, Proven | T[number]>;
170178
}
171179
throw new AuthError(
172180
{ code: 'insufficient_scope', scope: Scopes.of(scopes).toString() },

‎packages/auth/test/types.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,12 @@ export async function checks(issuer: Issuer, compact: string) {
2323
// @ts-expect-error a token proving another scope is refused
2424
write(token.require('notes:read'));
2525
if (token.has('notes:write')) write(token);
26+
// A scope typed as a union is checked as one of its members, so it proves none.
27+
const either = notes.all[0] as Notes;
28+
// @ts-expect-error a union-typed scope proves nothing
29+
write(token.require(either));
30+
// @ts-expect-error a union-typed scope proves nothing
31+
token.require('notes:read', either);
32+
// @ts-expect-error a union-typed scope proves nothing
33+
if (token.has(either)) write(token);
2634
}

0 commit comments

Comments
 (0)