Skip to content

Commit f2ffcbd

Browse files
Add a new OptionalSafeUrlSchema just for retrocompatibility on existing clients
1 parent 8122c17 commit f2ffcbd

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/shared/auth.test.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { describe, it, expect } from '@jest/globals';
2-
import { SafeUrlSchema, OAuthMetadataSchema, OpenIdProviderMetadataSchema, OAuthClientMetadataSchema } from './auth.js';
2+
import {
3+
SafeUrlSchema,
4+
OAuthMetadataSchema,
5+
OpenIdProviderMetadataSchema,
6+
OAuthClientMetadataSchema,
7+
OptionalSafeUrlSchema
8+
} from './auth.js';
39

410
describe('SafeUrlSchema', () => {
511
it('accepts valid HTTPS URLs', () => {
@@ -18,14 +24,17 @@ describe('SafeUrlSchema', () => {
1824

1925
it('rejects invalid URLs', () => {
2026
expect(() => SafeUrlSchema.parse('not-a-url')).toThrow();
27+
expect(() => SafeUrlSchema.parse('')).toThrow();
2128
});
2229

2330
it('works with safeParse', () => {
2431
expect(() => SafeUrlSchema.safeParse('not-a-url')).not.toThrow();
2532
});
33+
});
2634

27-
it('works with empty string', () => {
28-
expect(() => SafeUrlSchema.parse('')).not.toThrow();
35+
describe('OptionalSafeUrlSchema', () => {
36+
it('accepts empty string and transforms it to undefined', () => {
37+
expect(OptionalSafeUrlSchema.parse('')).toBe(undefined);
2938
});
3039
});
3140

src/shared/auth.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ export const SafeUrlSchema = z
2323
return u.protocol !== 'javascript:' && u.protocol !== 'data:' && u.protocol !== 'vbscript:';
2424
},
2525
{ message: 'URL cannot use javascript:, data:, or vbscript: scheme' }
26-
)
27-
.or(z.literal(''));
26+
);
2827

2928
/**
3029
* RFC 9728 OAuth Protected Resource Metadata
@@ -152,6 +151,11 @@ export const OAuthErrorResponseSchema = z.object({
152151
error_uri: z.string().optional()
153152
});
154153

154+
/**
155+
* Optional version of SafeUrlSchema that allows empty string for retrocompatibility on tos_uri and logo_uri
156+
*/
157+
export const OptionalSafeUrlSchema = SafeUrlSchema.optional().or(z.literal('').transform(() => undefined));
158+
155159
/**
156160
* RFC 7591 OAuth 2.0 Dynamic Client Registration metadata
157161
*/
@@ -163,10 +167,10 @@ export const OAuthClientMetadataSchema = z
163167
response_types: z.array(z.string()).optional(),
164168
client_name: z.string().optional(),
165169
client_uri: SafeUrlSchema.optional(),
166-
logo_uri: SafeUrlSchema.optional(),
170+
logo_uri: OptionalSafeUrlSchema,
167171
scope: z.string().optional(),
168172
contacts: z.array(z.string()).optional(),
169-
tos_uri: SafeUrlSchema.optional(),
173+
tos_uri: OptionalSafeUrlSchema,
170174
policy_uri: z.string().optional(),
171175
jwks_uri: SafeUrlSchema.optional(),
172176
jwks: z.any().optional(),

0 commit comments

Comments
 (0)