0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

fix: Allow isolated "%"s when parsing file URLs (#7108)

This commit is contained in:
Nayeem Rahman 2020-08-21 14:37:06 +01:00 committed by GitHub
parent fd83df7cdb
commit cf603be24c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 4 deletions

View file

@ -63,11 +63,13 @@
});
}
// Keep in sync with `fromFileUrl()` in `std/path/win32.ts`.
function pathFromURLWin32(url) {
let path = decodeURIComponent(
url.pathname
.replace(/^\/*([A-Za-z]:)(\/|$)/, "$1/")
.replace(/\//g, "\\"),
.replace(/\//g, "\\")
.replace(/%(?![0-9A-Fa-f]{2})/g, "%25"),
);
if (url.hostname != "") {
// Note: The `URL` implementation guarantees that the drive letter and
@ -78,12 +80,15 @@
return path;
}
// Keep in sync with `fromFileUrl()` in `std/path/posix.ts`.
function pathFromURLPosix(url) {
if (url.hostname !== "") {
throw new TypeError(`Host must be empty.`);
}
return decodeURIComponent(url.pathname);
return decodeURIComponent(
url.pathname.replace(/%(?![0-9A-Fa-f]{2})/g, "%25"),
);
}
function pathFromURL(pathOrUrl) {

View file

@ -7,6 +7,7 @@ Deno.test("[path] fromFileUrl", function () {
assertEquals(posix.fromFileUrl("file:///"), "/");
assertEquals(posix.fromFileUrl("file:///home/foo"), "/home/foo");
assertEquals(posix.fromFileUrl("file:///home/foo%20bar"), "/home/foo bar");
assertEquals(posix.fromFileUrl("file:///%"), "/%");
assertEquals(posix.fromFileUrl("file://localhost/foo"), "/foo");
assertEquals(posix.fromFileUrl("file:///C:"), "/C:");
assertEquals(posix.fromFileUrl("file:///C:/"), "/C:/");
@ -29,6 +30,7 @@ Deno.test("[path] fromFileUrl (win32)", function () {
assertEquals(win32.fromFileUrl("file:///"), "\\");
assertEquals(win32.fromFileUrl("file:///home/foo"), "\\home\\foo");
assertEquals(win32.fromFileUrl("file:///home/foo%20bar"), "\\home\\foo bar");
assertEquals(win32.fromFileUrl("file:///%"), "\\%");
assertEquals(win32.fromFileUrl("file://localhost/foo"), "\\\\localhost\\foo");
assertEquals(win32.fromFileUrl("file:///C:"), "C:\\");
assertEquals(win32.fromFileUrl("file:///C:/"), "C:\\");

View file

@ -436,5 +436,7 @@ export function fromFileUrl(url: string | URL): string {
if (url.protocol != "file:") {
throw new TypeError("Must be a file URL.");
}
return decodeURIComponent(url.pathname);
return decodeURIComponent(
url.pathname.replace(/%(?![0-9A-Fa-f]{2})/g, "%25"),
);
}

View file

@ -919,7 +919,8 @@ export function fromFileUrl(url: string | URL): string {
let path = decodeURIComponent(
url.pathname
.replace(/^\/*([A-Za-z]:)(\/|$)/, "$1/")
.replace(/\//g, "\\"),
.replace(/\//g, "\\")
.replace(/%(?![0-9A-Fa-f]{2})/g, "%25"),
);
if (url.hostname != "") {
// Note: The `URL` implementation guarantees that the drive letter and