Skip to content

Commit

Permalink
fixup! url: handle "unsafe" characters properly in pathToFileURL
Browse files Browse the repository at this point in the history
replaceAll
  • Loading branch information
aduh95 committed Aug 27, 2024
1 parent a978fd5 commit 49959c3
Showing 1 changed file with 56 additions and 38 deletions.
94 changes: 56 additions & 38 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const {
StringPrototypeCodePointAt,
StringPrototypeIncludes,
StringPrototypeIndexOf,
StringPrototypeReplaceAll,
StringPrototypeSlice,
StringPrototypeStartsWith,
StringPrototypeToWellFormed,
Expand Down Expand Up @@ -1502,48 +1503,65 @@ function fileURLToPath(path, options = kEmptyObject) {

// RFC1738 defines the following chars as "unsafe" for URLs
// @see https://www.ietf.org/rfc/rfc1738.txt 2.2. URL Character Encoding Issues
const percentRegEx = /%/g;
const backslashRegEx = /\\/g;
const newlineRegEx = /\n/g;
const carriageReturnRegEx = /\r/g;
const tabRegEx = /\t/g;
const quoteRegEx = /"/g;
const hashRegex = /#/g;
const spaceRegEx = / /g;
const lowerThanRegEx = /</g;
const greaterThanRegEx = />/g;
const questionRegex = /\?/g;
const openSquareBracketRegEx = /\[/g;
const closeSquareBracketRegEx = /]/g;
const openCurlyBracketRegEx = /{/g;
const closeCurlyBracketRegEx = /}/g;
const caretRegEx = /\^/g;
const backtickRegEx = /`/g;
const verticalBarRegEx = /\|/g;

function encodePathChars(filepath, options = kEmptyObject) {
filepath = RegExpPrototypeSymbolReplace(percentRegEx, filepath, '%25');

filepath = RegExpPrototypeSymbolReplace(tabRegEx, filepath, '%09');
filepath = RegExpPrototypeSymbolReplace(newlineRegEx, filepath, '%0A');
filepath = RegExpPrototypeSymbolReplace(carriageReturnRegEx, filepath, '%0D');
filepath = RegExpPrototypeSymbolReplace(spaceRegEx, filepath, '%20');
filepath = RegExpPrototypeSymbolReplace(quoteRegEx, filepath, '%22');
filepath = RegExpPrototypeSymbolReplace(hashRegex, filepath, '%23');
filepath = RegExpPrototypeSymbolReplace(lowerThanRegEx, filepath, '%3C');
filepath = RegExpPrototypeSymbolReplace(greaterThanRegEx, filepath, '%3E');
filepath = RegExpPrototypeSymbolReplace(questionRegex, filepath, '%3F');
filepath = RegExpPrototypeSymbolReplace(openSquareBracketRegEx, filepath, '%5B');
if (StringPrototypeIncludes(filepath, '%')) {
filepath = StringPrototypeReplaceAll(filepath, '%', '%25');
}

if (StringPrototypeIncludes(filepath, '\t')) {
filepath = StringPrototypeReplaceAll(filepath, '\t', '%09');
}
if (StringPrototypeIncludes(filepath, '\n')) {
filepath = StringPrototypeReplaceAll(filepath, '\n', '%0A');
}
if (StringPrototypeIncludes(filepath, '\r')) {
filepath = StringPrototypeReplaceAll(filepath, '\r', '%0D');
}
if (StringPrototypeIncludes(filepath, ' ')) {
filepath = StringPrototypeReplaceAll(filepath, ' ', '%20');
}
if (StringPrototypeIncludes(filepath, '"')) {
filepath = StringPrototypeReplaceAll(filepath, '"', '%22');
}
if (StringPrototypeIncludes(filepath, '#')) {
filepath = StringPrototypeReplaceAll(filepath, '#', '%23');
}
if (StringPrototypeIncludes(filepath, '<')) {
filepath = StringPrototypeReplaceAll(filepath, '<', '%3C');
}
if (StringPrototypeIncludes(filepath, '>')) {
filepath = StringPrototypeReplaceAll(filepath, '>', '%3E');
}
if (StringPrototypeIncludes(filepath, '?')) {
filepath = StringPrototypeReplaceAll(filepath, '?', '%3F');
}
if (StringPrototypeIncludes(filepath, '[')) {
filepath = StringPrototypeReplaceAll(filepath, '[', '%5B');
}
// Back-slashes must be special-cased on Windows, where they are treated as path separator.
if (!options.windows) {
filepath = RegExpPrototypeSymbolReplace(backslashRegEx, filepath, '%5C');
}
filepath = RegExpPrototypeSymbolReplace(closeSquareBracketRegEx, filepath, '%5D');
filepath = RegExpPrototypeSymbolReplace(caretRegEx, filepath, '%5E');
filepath = RegExpPrototypeSymbolReplace(backtickRegEx, filepath, '%60');
filepath = RegExpPrototypeSymbolReplace(openCurlyBracketRegEx, filepath, '%7B');
filepath = RegExpPrototypeSymbolReplace(verticalBarRegEx, filepath, '%7C');
filepath = RegExpPrototypeSymbolReplace(closeCurlyBracketRegEx, filepath, '%7D');
if (!options.windows && StringPrototypeIncludes(filepath, '\\')) {
filepath = StringPrototypeReplaceAll(filepath, '\\', '%5C');
}
if (StringPrototypeIncludes(filepath, ']')) {
filepath = StringPrototypeReplaceAll(filepath, ']', '%5D');
}
if (StringPrototypeIncludes(filepath, '^')) {
filepath = StringPrototypeReplaceAll(filepath, '^', '%5E');
}
if (StringPrototypeIncludes(filepath, '`')) {
filepath = StringPrototypeReplaceAll(filepath, '`', '%60');
}
if (StringPrototypeIncludes(filepath, '{')) {
filepath = StringPrototypeReplaceAll(filepath, '{', '%7B');
}
if (StringPrototypeIncludes(filepath, '|')) {
filepath = StringPrototypeReplaceAll(filepath, '|', '%7C');
}
if (StringPrototypeIncludes(filepath, '}')) {
filepath = StringPrototypeReplaceAll(filepath, '}', '%7D');
}

return filepath;
}
Expand Down

0 comments on commit 49959c3

Please sign in to comment.