Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/bun-types/bun.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,7 @@ declare module "bun" {
* Convert a {@link URL} to a filesystem path.
*
* @param url The URL to convert.
* @param options.windows When `true`, the path is returned in Windows format. Defaults to the current platform.
* @returns A filesystem path.
* @throws If `url` is not a valid URL.
*
Expand All @@ -1883,7 +1884,7 @@ declare module "bun" {
* console.log(path); // "/foo/bar.txt"
* ```
*/
function fileURLToPath(url: URL | string): string;
function fileURLToPath(url: URL | string, options?: { windows?: boolean }): string;

/**
* Fast incremental writer that becomes an {@link ArrayBuffer} on end().
Expand Down
129 changes: 97 additions & 32 deletions src/jsc/bindings/BunObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "helpers.h"
#include "IDLTypes.h"
#include "DOMURL.h"
#include <JavaScriptCore/Error.h>
#include <JavaScriptCore/JSPromise.h>
#include <JavaScriptCore/JSBase.h>
#include <JavaScriptCore/BuiltinNames.h>
Expand Down Expand Up @@ -840,13 +841,76 @@
RELEASE_AND_RETURN(throwScope, JSC::JSValue::encode(jsonValue));
}

// ECMA-262 `Decode` with an empty preserve set, which is what decodeURIComponent does.
// WTF::URL::fileSystemPath() is not usable here: it passes malformed escapes through
// verbatim and returns a null string for escapes that decode to invalid UTF-8.
static bool decodeFileURLPath(JSC::JSGlobalObject* globalObject, JSC::ThrowScope& scope, StringView input, String& result)
{
if (!input.contains('%')) {
result = input.toString();
return true;
}

// The URL parser percent-encodes everything outside of ASCII, so escapes are the
// only source of non-ASCII bytes.
ASSERT(input.containsOnlyASCII());

const unsigned length = input.length();
Vector<Latin1Character, 256> decoded;
decoded.reserveInitialCapacity(length);
for (unsigned i = 0; i < length;) {
char16_t character = input[i];
if (character != '%') {
decoded.append(static_cast<Latin1Character>(character));
++i;
continue;
}

if (i + 2 >= length || !isASCIIHexDigit(input[i + 1]) || !isASCIIHexDigit(input[i + 2])) {
throwException(globalObject, scope, createURIError(globalObject, "URI malformed"_s));
return false;
}

decoded.append(toASCIIHexValue(input[i + 1], input[i + 2]));
i += 3;
}

result = String::fromUTF8(decoded.span());
if (result.isNull()) {
throwException(globalObject, scope, createURIError(globalObject, "URI malformed"_s));
return false;
}
return true;
}

JSC_DEFINE_HOST_FUNCTION(functionFileURLToPath, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
auto& vm = JSC::getVM(globalObject);
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue arg0 = callFrame->argument(0);
WTF::URL url;

#if OS(WINDOWS)
constexpr bool windowsByDefault = true;
constexpr ASCIILiteral platformName = "win32"_s;
#elif OS(DARWIN)
constexpr bool windowsByDefault = false;
constexpr ASCIILiteral platformName = "darwin"_s;
#else
constexpr bool windowsByDefault = false;
constexpr ASCIILiteral platformName = "linux"_s;
#endif

// `options?.windows`, falling back to the host platform when absent.
bool windows = windowsByDefault;
JSValue options = callFrame->argument(1);
if (!options.isUndefinedOrNull()) {
JSValue windowsOption = options.get(globalObject, Identifier::fromString(vm, "windows"_s));
RETURN_IF_EXCEPTION(scope, {});
if (!windowsOption.isUndefinedOrNull())
windows = windowsOption.toBoolean(globalObject);
}

auto path = JSC::JSValue::encode(arg0);
auto* domURL = WebCoreCast<WebCore::JSDOMURL, WebCore::DOMURL>(path);
if (!domURL) {
Expand All @@ -867,50 +931,51 @@
return {};
}

// NOTE: On Windows, WTF::URL::fileSystemPath will handle UNC paths
// (`file:\\server\share\etc` -> `\\server\share\etc`), so hostname check only
// needs to happen on posix systems
#if !OS(WINDOWS)
// file://host/path is illegal if `host` is not `localhost`.
// Should be `file:///` instead
if (url.host().length() > 0 && url.host() != "localhost"_s) [[unlikely]] {
const StringView encodedPath = url.path();
const StringView host = url.host();

#if OS(DARWIN)
Bun::ERR::INVALID_FILE_URL_HOST(scope, globalObject, "darwin"_s);
return {};
#else
Bun::ERR::INVALID_FILE_URL_HOST(scope, globalObject, "linux"_s);
return {};
#endif
}
#endif

// ban url-encoded slashes. '/' on posix, '/' and '\' on windows.
const StringView p = url.path();
if (p.contains('%')) {
#if OS(WINDOWS)
if (p.contains("%2f"_s) || p.contains("%5c"_s) || p.contains("%2F"_s) || p.contains("%5C"_s)) {
Bun::ERR::INVALID_FILE_URL_PATH(scope, globalObject, "must not include encoded \\ or / characters"_s);
if (!windows) {
// file://host/path is illegal if `host` is not `localhost`.
// Should be `file:///` instead
if (host.length() > 0 && host != "localhost"_s) [[unlikely]] {
Bun::ERR::INVALID_FILE_URL_HOST(scope, globalObject, platformName);
return {};
}
#else
if (p.contains("%2f"_s) || p.contains("%2F"_s)) {

// ban url-encoded slashes.
if (encodedPath.contains('%') && (encodedPath.contains("%2f"_s) || encodedPath.contains("%2F"_s))) [[unlikely]] {
Bun::ERR::INVALID_FILE_URL_PATH(scope, globalObject, "must not include encoded / characters"_s);
return {};
}
#endif

String decodedPath;
if (!decodeFileURLPath(globalObject, scope, encodedPath, decodedPath))
return {};
return JSC::JSValue::encode(JSC::jsString(vm, decodedPath));
}

// ban url-encoded slashes, '/' and '\' on windows.
if (encodedPath.contains('%') && (encodedPath.contains("%2f"_s) || encodedPath.contains("%2F"_s) || encodedPath.contains("%5c"_s) || encodedPath.contains("%5C"_s))) [[unlikely]] {
Bun::ERR::INVALID_FILE_URL_PATH(scope, globalObject, "must not include encoded \\ or / characters"_s);
return {};
}

auto fileSystemPath = url.fileSystemPath();
const String separatedPath = makeStringByReplacingAll(encodedPath, '/', '\\');
String decodedPath;
if (!decodeFileURLPath(globalObject, scope, separatedPath, decodedPath))
return {};

#if OS(WINDOWS)
if (!isAbsolutePath(fileSystemPath)) {
Bun::ERR::INVALID_FILE_URL_PATH(scope, globalObject, "must be an absolute path"_s);
// UNC paths look like '\\server\share\etc', but in a URL they look like 'file://server/share/etc'.
if (!host.isEmpty())
return JSC::JSValue::encode(JSC::jsString(vm, makeString("\\\\"_s, host, decodedPath)));

Check failure on line 970 in src/jsc/bindings/BunObject.cpp

View check run for this annotation

Claude / Claude Code Review

UNC hostname not converted from punycode to Unicode

The Windows UNC branch uses `url.host()` directly, but Node's `getPathFromURLWin32` runs the hostname through `domainToUnicode` first — so `fileURLToPath('file://münchen/foo', {windows: true})` returns `\\\\xn--mnchen-3ya\\foo` here vs Node's `\\\\münchen\\foo`. `domainToUnicode` (via `uidna_nameToUnicode`) already exists in `src/jsc/bindings/NodeURL.cpp`; wrapping `host` with it before the `makeString` restores parity.
Comment thread
claude[bot] marked this conversation as resolved.
Resolved

// Otherwise it is a local path, which requires a drive letter: '\C:\etc' -> 'C:\etc'.
if (decodedPath.length() < 3 || !isASCIIAlpha(decodedPath[1]) || decodedPath[2] != ':') [[unlikely]] {
Bun::ERR::INVALID_FILE_URL_PATH(scope, globalObject, "must be absolute"_s);
return {};
}
#endif

return JSC::JSValue::encode(JSC::jsString(vm, fileSystemPath));
return JSC::JSValue::encode(JSC::jsString(vm, decodedPath.substring(1)));
}

/* Source for BunObject.lut.h
Expand Down
16 changes: 15 additions & 1 deletion test/js/bun/util/fileUrl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("pathToFileURL", () => {
});

describe("fileURLToPath", () => {
const absoluteErrorMessage = "File URL path must be an absolute";
const absoluteErrorMessage = "File URL path must be absolute";
it("should convert a file url to a path", () => {
if (isWindows) {
expect(() => fileURLToPath("file:///path/to/file.js")).toThrow(absoluteErrorMessage);
Expand Down Expand Up @@ -61,4 +61,18 @@ describe("fileURLToPath", () => {
expect(fileURLToPath(url)).toBe(import.meta.path);
expect(fileURLToPath(import.meta.url)).toBe(import.meta.path);
});

it("should throw on malformed percent-encoding", () => {
expect(() => fileURLToPath("file:///%c3%28")).toThrow(URIError);
expect(() => fileURLToPath("file:///%")).toThrow(URIError);
expect(() => fileURLToPath(new URL("file:///%c3"))).toThrow(URIError);
});

it("should accept a windows option", () => {
expect(fileURLToPath("file:///C:/foo", { windows: true })).toBe("C:\\foo");
expect(fileURLToPath("file:///foo/bar", { windows: false })).toBe("/foo/bar");
expect(() => fileURLToPath("file:///C:/a%5Cb", { windows: true })).toThrow(
"File URL path must not include encoded \\ or / characters",
);
});
});
91 changes: 91 additions & 0 deletions test/js/node/url/url-fileurltopath.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,95 @@ describe("url.fileURLToPath", () => {
assert.strictEqual(fromURL, path);
}
});

describe("percent-decoding is validated", () => {
// Escape sequences that decodeURIComponent rejects must reject here too, instead of
// passing through verbatim or collapsing into the empty string.
const malformed = [
"file:///%", // truncated escape
"file:///%c", // truncated escape
"file:///%zz", // not hex digits
"file:///%c3", // truncated UTF-8 sequence
"file:///%c3%28", // invalid UTF-8 continuation byte
"file:///%80", // lone UTF-8 continuation byte
"file:///%ff", // never valid in UTF-8
"file:///%C0%80", // overlong encoding of U+0000
"file:///%ED%A0%80", // UTF-8 encoded surrogate
"file:///%F5%80%80%80", // beyond U+10FFFF
];

for (const input of malformed) {
for (const windows of [false, true]) {
test(`${input} (windows: ${windows})`, () => {
assert.throws(() => url.fileURLToPath(input, { windows }), {
name: "URIError",
message: "URI malformed",
});
assert.throws(() => url.fileURLToPath(new URL(input), { windows }), {
name: "URIError",
message: "URI malformed",
});
});
}
}

test("well-formed escapes still decode", () => {
assert.strictEqual(url.fileURLToPath("file:///%41", { windows: false }), "/A");
assert.strictEqual(url.fileURLToPath("file:///caf%C3%A9", { windows: false }), "/café");
assert.strictEqual(url.fileURLToPath("file:///%F0%9F%9A%80", { windows: false }), "/🚀");
// A percent sign that is not an escape sequence has to be encoded, and round-trips.
assert.strictEqual(url.fileURLToPath("file:///100%25", { windows: false }), "/100%");
});
});

describe("windows option", () => {
test("applies win32 path rules regardless of platform", () => {
assert.strictEqual(url.fileURLToPath("file:///C:/foo", { windows: true }), "C:\\foo");
assert.strictEqual(url.fileURLToPath("file:///C:/dir/foo%20bar", { windows: true }), "C:\\dir\\foo bar");
assert.strictEqual(url.fileURLToPath("file://nas/My%20Docs/File.doc", { windows: true }), "\\\\nas\\My Docs\\File.doc");
});

test("rejects encoded separators", () => {
assert.throws(() => url.fileURLToPath("file:///C:/a%5Cb", { windows: true }), {
code: "ERR_INVALID_FILE_URL_PATH",
message: "File URL path must not include encoded \\ or / characters",
});
assert.throws(() => url.fileURLToPath("file:///C:/a%2Fb", { windows: true }), {
code: "ERR_INVALID_FILE_URL_PATH",
message: "File URL path must not include encoded \\ or / characters",
});
// On posix only '/' is rejected, '\' is a legal filename character.
assert.strictEqual(url.fileURLToPath("file:///a%5Cb", { windows: false }), "/a\\b");
assert.throws(() => url.fileURLToPath("file:///a%2Fb", { windows: false }), {
code: "ERR_INVALID_FILE_URL_PATH",
message: "File URL path must not include encoded / characters",
});
});

test("rejects paths without a drive letter", () => {
for (const input of ["file:///foo", "file:///", "file:///1:/foo", "file:///C|foo"]) {
assert.throws(() => url.fileURLToPath(input, { windows: true }), {
code: "ERR_INVALID_FILE_URL_PATH",
message: "File URL path must be absolute",
});
}
});

test("applies posix path rules regardless of platform", () => {
assert.strictEqual(url.fileURLToPath("file:///foo/bar", { windows: false }), "/foo/bar");
assert.throws(() => url.fileURLToPath("file://host/a", { windows: false }), {
code: "ERR_INVALID_FILE_URL_HOST",
});
});

test("absent, null and undefined fall back to the platform", () => {
const expected = url.fileURLToPath("file:///C:/foo", { windows: isWindows });
assert.strictEqual(url.fileURLToPath("file:///C:/foo"), expected);
assert.strictEqual(url.fileURLToPath("file:///C:/foo", {}), expected);
assert.strictEqual(url.fileURLToPath("file:///C:/foo", { windows: undefined }), expected);
assert.strictEqual(url.fileURLToPath("file:///C:/foo", { windows: null }), expected);
assert.strictEqual(url.fileURLToPath("file:///C:/foo", null), expected);
assert.strictEqual(url.fileURLToPath("file:///C:/foo", 5), expected);
});
});
});
Loading