Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(shared): one-letter link schemes no longer become file links on Windows #24006

MergedZacharon(GitHub profile, opens in a new tab)merged 3 commits intomain fromrefs/pull/24006/headmerged by juliusmarminge View on GitHub

Showing 2 changed files with 15 additions and 0 deletions.

x
0 of 2 viewed, 2 left
11 additions and 0 deletionspackages/shared/src/fileLinks.test.tsModified
View file
@@ -103,6 +103,17 @@
it.each(["https://example.com/a.ts", "file://%", "/Users/julius/a.ts"])("rejects %s", (href) => {
expect(parseFileUrlHref(href)).toBeNull();
});
+​
+ it("rejects one-letter schemes that Chromium on Windows reads as drive letters", () => {
+ const NativeURL = URL;
+ using _url = vi.spyOn(globalThis, "URL").mockImplementation(function (input: string | URL) {
+ const drive = /^([a-z]):/i.exec(String(input));
+ return drive
+ ? new NativeURL(`file:///${drive[1]!.toUpperCase()}:/${String(input).slice(2)}`)
+ : new NativeURL(input);
+ });
+ expect(parseFileUrlHref("d:x")).toBeNull();
+ });
});
​
describe("splitFilePathPosition", () => {
4 additions and 0 deletionspackages/shared/src/fileLinks.tsModified
View file
@@ -13,6 +13,10 @@
export function parseFileUrlHref(
href: string,
): { readonly path: string; readonly hash: string } | null {
+ // Chromium on Windows parses any one-letter scheme as a drive letter
+ // (`new URL("d:x")` is `file:///D:/x`), so the protocol check alone would turn
+ // such links into file links only on Windows.
+ if (!href.trim().toLowerCase().startsWith("file:")) return null;