You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
HTTPS-only URL validation bypasses the authority-format guard used by `z.httpUrl() #6766
When normalize: false is used, z.httpUrl() rejects HTTP(S) URLs that do not contain the expected :// separator. However, defining an HTTPS-only schema using z.url({ protocol: /^https$/ }) allows some of those same inputs to pass validation.
This creates an inconsistency between the built-in HTTP URL validation and custom HTTPS-only validation.
When normalize: false is used, it would be useful for custom HTTP/HTTPS protocol schemas to retain the authority-format validation applied by z.httpUrl().
In particular, narrowing the accepted protocol to HTTPS should not cause inputs rejected by the built-in HTTP URL validation to become accepted unexpectedly.
Actual Behavior
The authority-format guard in parseURLObject() is conditional on:
Consequently, a custom protocol regex such as /^https$/ does not activate this guard, even though it validates an HTTP-family URL.
The URL is subsequently accepted through WHATWG URL parsing, and with normalize: false, the original input is returned.
Additional Context
The behavior appears to be an implementation limitation rather than a regression. The documented z.url() API follows WHATWG URL parsing semantics, and .refine() can be used when additional restrictions are required.
However, the difference between the built-in HTTP URL schema and a custom HTTPS-only schema may be surprising to developers who expect consistent validation behavior.
Question
Would maintainers consider generalizing the authority-format guard to custom HTTP/HTTPS protocol schemas when normalize: false is used, or providing a supported way to configure z.httpUrl() for HTTPS-only validation?
I would be interested to know whether this distinction is intentional or whether the guard could be made more consistent.
Since normalize: false hands back the original string, "https:example.com" passes the HTTPS-only schema and then gets read differently by whatever parses it next. I think generalizing the guard makes sense: run it for any custom protocol whenever the input itself starts with an http(s) scheme. Schemes like mailto: are left alone. This is the change I tested in parseURLObject:
The scheme check strips tabs and newlines first because the URL parser ignores them. Without that, "ht\ntps:example.com" would get past the check and still parse as https:. z.httpUrl() behaves exactly as before, since the httpProtocol branch is unchanged. compile.ts calls the same validateURL, so the compiled path is covered too.
After the patch all of the :/://:\ inputs above are rejected by both schemas with note: "Invalid URL format". https://example.com and HTTPS://example.com/a still pass.
Regression test I added to classic/tests/string.test.ts:
test("custom http(s) protocol keeps the :// guard when normalize is off",()=>{consthttpsOnly=z.url({normalize: false,protocol: /^https$/});for(constinputof["https:example.com","https:/example.com","https:\\example.com","ht\ntps:example.com"]){constresult=httpsOnly.safeParse(input);expect(result.success).toBe(false);expect(result.error!.issues[0]).toMatchObject({code: "invalid_format",note: "Invalid URL format"});}expect(httpsOnly.parse("https://example.com")).toBe("https://example.com");expect(httpsOnly.parse("HTTPS://example.com/a")).toBe("HTTPS://example.com/a");expect(httpsOnly.safeParse("http://example.com").success).toBe(false);expect(z.url({normalize: false,protocol: /^mailto$/}).parse("mailto:a@b.com")).toBe("mailto:a@b.com");expect(z.url({normalize: true,protocol: /^https$/}).parse("https:example.com")).toBe("https://example.com/");});
It fails without the patch (zod and compile-mode, expected true to be false) and passes with it. Full vitest run: 8448 passed, 13 skipped. The only failures are the 3 @zod/treeshaking files, which need a built packages/zod and fail the same way on unpatched main here.
Until this lands, z.url({ normalize: false, protocol: /^https$/ }).regex(/^https:\/\//i) works as a workaround (I checked it on unpatched main: rejects https:example.com, accepts https://example.com).
This repo only takes PRs from collaborators, so here is a tested patch on a branch instead: compare (commit 694d8df, 2 files).
It adds a second check in parseURLObject(): when normalize is off and the input starts with http: or https: without //, return URL_BAD_FORMAT if the protocol pattern accepts http or https. That covers /^https$/ and any other custom pattern. /^ftp$/ and normalize: true are untouched, and z.httpUrl() still takes the original branch, so its errors are the same.
The new test fails on main and passes with the patch, in the normal run and with scripts/enable-compile.ts loaded. packages/zod/src/v4/**/*.test.ts: 2110 passed (2109 before). I ran this with a standalone vitest on Windows because nub could not be installed there, so typecheck and the full compile project were not run.
Activity
Add regression tests for narrowed http(s) protocol guard (colinhacks#…
Apply the :// guard to narrowed http(s) protocol regexes (colinhacks#…
hamarshehmhmd commentedon Nov 20, 2026
Reproduced on main (e32e088), in both the runtime and the compile-mode test projects:
Since
normalize: falsehands back the original string,"https:example.com"passes the HTTPS-only schema and then gets read differently by whatever parses it next. I think generalizing the guard makes sense: run it for any customprotocolwhenever the input itself starts with an http(s) scheme. Schemes likemailto:are left alone. This is the change I tested inparseURLObject:The scheme check strips tabs and newlines first because the URL parser ignores them. Without that,
"ht\ntps:example.com"would get past the check and still parse ashttps:.z.httpUrl()behaves exactly as before, since thehttpProtocolbranch is unchanged.compile.tscalls the samevalidateURL, so the compiled path is covered too.After the patch all of the
:/://:\inputs above are rejected by both schemas withnote: "Invalid URL format".https://example.comandHTTPS://example.com/astill pass.Regression test I added to
classic/tests/string.test.ts:It fails without the patch (zod and compile-mode,
expected true to be false) and passes with it. Fullvitest run: 8448 passed, 13 skipped. The only failures are the 3@zod/treeshakingfiles, which need a builtpackages/zodand fail the same way on unpatched main here.Until this lands,
z.url({ normalize: false, protocol: /^https$/ }).regex(/^https:\/\//i)works as a workaround (I checked it on unpatched main: rejectshttps:example.com, acceptshttps://example.com).Manuel-1312 commentedon Nov 21, 2026
This repo only takes PRs from collaborators, so here is a tested patch on a branch instead: compare (commit 694d8df, 2 files).
It adds a second check in
parseURLObject(): whennormalizeis off and the input starts withhttp:orhttps:without//, returnURL_BAD_FORMATif theprotocolpattern acceptshttporhttps. That covers/^https$/and any other custom pattern./^ftp$/andnormalize: trueare untouched, andz.httpUrl()still takes the original branch, so its errors are the same.The new test fails on
mainand passes with the patch, in the normal run and withscripts/enable-compile.tsloaded.packages/zod/src/v4/**/*.test.ts: 2110 passed (2109 before). I ran this with a standalone vitest on Windows becausenubcould not be installed there, so typecheck and the full compile project were not run.Cherry-pick it, or take the idea.