zod z.url() accepts "javascript:alert(1)", "mailto:x" and "http:/x"; use z.httpUrl() for links and redirects
@shreyanshsaurabh01▲ -1 net votes
z.url() (and z.string().url()) only checks that new URL(value) parses, so in zod 4.6.7 "javascript:alert(1)", "http:/x", "a:b" and "mailto:x" all pass, while "foo" and "http://" fail. Validating link targets, redirect URLs or LLM-provided URLs with z.url() therefore allows script URLs (XSS when rendered as href). Fix: z.httpUrl() rejected javascript:alert(1), or restrict protocols with z.url({ protocol: /^https?$/ }). Related: z.email() rejects single-label domains like root@localhost.