Skip to content

Commit

Permalink
Fixes #16705
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed Jan 27, 2025
1 parent cd53d32 commit be154b1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
30 changes: 10 additions & 20 deletions src/bun.js/bindings/BunObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -652,29 +652,19 @@ JSC_DEFINE_HOST_FUNCTION(functionFileURLToPath, (JSC::JSGlobalObject * globalObj
#endif

// ban url-encoded slashes. '/' on posix, '/' and '\' on windows.
StringView p = url.path();
if (p.length() > 3) {
for (int i = 0; i < p.length() - 2; i++) {
if (p[i] == '%') {
const char second = p[i + 1];
const uint8_t third = p[i + 2] | 0x20;
const StringView p = url.path();

#if OS(WINDOWS)
if (
(second == '2' && third == 102) || // 2f 2F '/'
(second == '5' && third == 99) // 5c 5C '\'
) {
Bun::ERR::INVALID_FILE_URL_PATH(scope, globalObject, "must not include encoded \\ or / characters"_s);
return {};
}
if (p.contains("%2f"_s) || p.contains("%5c"_s)) {
Bun::ERR::INVALID_FILE_URL_PATH(scope, globalObject, "must not include encoded \\ or / characters"_s);
return {};
}
#else
if (second == '2' && third == 102) {
Bun::ERR::INVALID_FILE_URL_PATH(scope, globalObject, "must not include encoded / characters"_s);
return {};
}
#endif
}
}
if (p.contains("%2f"_s)) {
Bun::ERR::INVALID_FILE_URL_PATH(scope, globalObject, "must not include encoded / characters"_s);
return {};
}
#endif

auto fileSystemPath = url.fileSystemPath();

Expand Down
7 changes: 7 additions & 0 deletions test/js/node/url/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,10 @@ it("URL constructor throws ERR_MISSING_ARGS", () => {
// @ts-expect-error
expect(err?.code).toEqual("ERR_MISSING_ARGS");
});

// https://github.com/oven-sh/bun/issues/16705
it("#16705", () => {
expect(Bun.fileURLToPath("file://C:/firebase-gen-%7B%7B%20firebase.gen%20%7D%7D")).toEqual(
"/C:/firebase-gen-{{ firebase.gen }}",
);
});

0 comments on commit be154b1

Please sign in to comment.