Skip to content

HTTPS-only URL validation bypasses the authority-format guard used by `z.httpUrl() #6766

Open

Description

@hrishikeshpathak1009

Description

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.

Reproduction

import { z } from "zod";

const httpUrlSchema = z.httpUrl({
  normalize: false,
});

const httpsOnlySchema = z.url({
  normalize: false,
  protocol: /^https$/,
});

const input = "https:example.com";

console.log(httpUrlSchema.safeParse(input).success);
// false

console.log(httpsOnlySchema.safeParse(input).success);
// true

The same behavior can be observed with:

"https:/example.com"
"https:\\example.com"

Expected Behavior

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:

def.protocol?.source === regexes.httpProtocol.source

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.

Activity

  1. hamarshehmhmd commented on Nov 20, 2026

    @hamarshehmhmd

    Reproduced on main (e32e088), in both the runtime and the compile-mode test projects:

    input                   httpUrl({normalize:false})   url({normalize:false, protocol:/^https$/})
    https:example.com       false                        true
    https:/example.com      false                        true
    https:\example.com      false                        true
    https://example.com     true                         true
    

    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:

    -  if (!def.normalize && def.protocol?.source === regexes.httpProtocol.source && !/^https?:\/\//i.test(trimmed)) {
    +  if (
    +    !def.normalize &&
    +    def.protocol &&
    +    !/^https?:\/\//i.test(trimmed) &&
    +    (def.protocol.source === regexes.httpProtocol.source || /^https?:/i.test(stripTabAndNewline(trimmed)))
    +  ) {
         return URL_BAD_FORMAT;
       }

    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", () => {
      const httpsOnly = z.url({ normalize: false, protocol: /^https$/ });
      for (const input of ["https:example.com", "https:/example.com", "https:\\example.com", "ht\ntps:example.com"]) {
        const result = 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).

  2. Manuel-1312 commented on Nov 21, 2026

    @Manuel-1312

    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.

    Cherry-pick it, or take the idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions