Skip to content

Fix set_scheme file-scheme checks to match the URL Standard - #1162

Open
Nacl-HCl wants to merge 1 commit into
servo:mainfrom
Nacl-HCl:fix-set-scheme-file-955
Open

Nacl-HCl wants to merge 1 commit into
servo:mainfrom
Nacl-HCl:fix-set-scheme-file-955

Conversation

@Nacl-HCl

Copy link
Copy Markdown

Fixes #955.

Url::set_scheme rejected every change to the file scheme for URLs with an
authority (new_scheme_type.is_file() && self.has_authority()), which does not
match the URL Standard. Under the scheme-state override rules, switching to
file must only be rejected when the URL includes credentials or has a
non-null port, and switching away from file must be rejected when the
file URL's host is empty or null. The no-host guard below the checks also
treated file like the other special schemes, even though file does not
require a host, which kept file URLs without a host from retaining the
file scheme.

With this change:

  • Url::parse("file:///path/to/file.js")?.set_scheme("file") returns Ok(())
    (the case reported in The incorrect implementation of set_scheme for file:// #955).
  • http://localhost/foo (no credentials, no port) can switch to file,
    while URLs with credentials or a port still cannot.
  • A file URL with an empty host still cannot switch to another scheme.

Tests

Added regression tests in url/tests/unit.rs for switching to file with
credentials, switching to file without a port or credentials, setting file
on a file URL, and switching away from file with an empty and a non-empty
host. Verified locally:

  • cargo test -p url — unit (72), WPT (67) and doc tests (67) all pass
  • cargo test --workspace — all pass
  • cargo clippy --workspace --all-targets -- -D warnings — clean
  • cargo fmt --all --check — clean
  • cargo +1.63.0 test -p url --test unit with ci-lockfiles/1.63.Cargo.lock
    — 72 passed (MSRV)

@Nacl-HCl
Nacl-HCl force-pushed the fix-set-scheme-file-955 branch from 3c5cfe2 to 3621d1e Compare November 9, 2026 02:31
Switching to the `file` scheme was rejected for every URL with an
authority. Per the URL Standard it must only be rejected when the URL
includes credentials or has a non-null port, and switching away from
`file` must be rejected when the file URL's host is empty or null.
The no-host guard also no longer treats `file` like the other special
schemes, since `file` does not require a host.

Fixes servo#955.
@Nacl-HCl
Nacl-HCl force-pushed the fix-set-scheme-file-955 branch from 3621d1e to 6fd7eb9 Compare November 12, 2026 10:59

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The incorrect implementation of set_scheme for file://

1 participant