Skip to content

Commit 0d679c6

Browse files
committed
fix with url parsing being fatal
1 parent 0a67d32 commit 0d679c6

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/shared/auth.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ describe('SafeUrlSchema', () => {
2525
expect(() => SafeUrlSchema.parse('not-a-url')).toThrow();
2626
expect(() => SafeUrlSchema.parse('')).toThrow();
2727
});
28+
29+
it('works with safeParse', () => {
30+
expect(() => SafeUrlSchema.safeParse('not-a-url')).not.toThrow();
31+
});
2832
});
2933

3034
describe('OAuthMetadataSchema', () => {

src/shared/auth.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@ import { z } from "zod";
44
* Reusable URL validation that disallows javascript: scheme
55
*/
66
export const SafeUrlSchema = z.string().url()
7-
.refine(
8-
(url) => URL.canParse(url),
9-
{message: "URL must be parseable"}
10-
).refine(
7+
.superRefine((val, ctx) => {
8+
if (!URL.canParse(val)) {
9+
ctx.addIssue({
10+
code: z.ZodIssueCode.custom,
11+
message: "URL must be parseable",
12+
fatal: true,
13+
});
14+
15+
return z.NEVER;
16+
}
17+
}).refine(
1118
(url) => {
1219
const u = new URL(url);
1320
return u.protocol !== 'javascript:' && u.protocol !== 'data:' && u.protocol !== 'vbscript:';

0 commit comments

Comments
 (0)