Bug 1986142 - [devtools] Properly escape all new lines characters (make sure no carraige returns) a=RyanVM

While reverting all the work related to cross platform escaping of curl (See  Bug 1976589), i tried to cleanup the
escaping of new line characters, but introduced carriage returns (\r).

This patch reverts back to the original fix from Bug 1968414

Original Revision: https://phabricator.services.mozilla.com/D265397

Differential Revision: https://phabricator.services.mozilla.com/D267045
This commit is contained in:
Hubert Boma Manilla
2025-10-01 18:43:31 +00:00
committed by rvandermeulen@mozilla.com
parent 4d9d5d34cb
commit c8e6b3801c
3 changed files with 17 additions and 20 deletions

View File

@@ -208,19 +208,19 @@ function testRemoveBinaryDataFromMultipartText(data) {
const EXPECTED_WIN_RESULT = [
'^"',
boundary,
'"^\u000d\u000A\u000d\u000A"',
'^\u000A\u000A',
'Content-Disposition: form-data; name=^\\^"param1^\\^"',
'"^\u000d\u000A\u000d\u000A""^\u000d\u000A\u000d\u000A"',
'^\u000A\u000A^\u000A\u000A',
"value1",
'"^\u000d\u000A\u000d\u000A"',
'^\u000A\u000A',
boundary,
'"^\u000d\u000A\u000d\u000A"',
'^\u000A\u000A',
'Content-Disposition: form-data; name=^\\^"file^\\^"; filename=^\\^"filename.png^\\^"',
'"^\u000d\u000A\u000d\u000A"',
'^\u000A\u000A',
"Content-Type: image/png",
'"^\u000d\u000A\u000d\u000A""^\u000d\u000A\u000d\u000A"',
'^\u000A\u000A^\u000A\u000A',
boundary + "--",
'"^\u000d\u000A\u000d\u000A"',
'^\u000A\u000A',
'^"',
].join("");
@@ -336,7 +336,7 @@ function testEscapeStringWin() {
const newLines = "line1\r\nline2\r\rline3\n\nline4";
is(
CurlUtils.escapeStringWin(newLines),
'^\"line1\"^\r\n\r\n\"line2\"^\r\n\r\n\"\"^\r\n\r\n\"line3\"^\r\n\r\n\"\"^\r\n\r\n\"line4^\"',
'^\"line1^\n\nline2^\n\n^\n\nline3^\n\n^\n\nline4^\"',
"Newlines should be escaped."
);
@@ -357,7 +357,7 @@ function testEscapeStringWin() {
const evilCommand = `query=evil\r\rcmd" /c timeout /t 3 & calc.exe\r\r`;
is(
CurlUtils.escapeStringWin(evilCommand),
'^\"query=evil\"^\r\n\r\n\"\"^\r\n\r\n\"cmd^\\^\" /c timeout /t 3 ^& calc.exe\"^\r\n\r\n\"\"^\r\n\r\n\"^\"',
'^\"query=evil^\n\n^\n\ncmd^\\^\" /c timeout /t 3 ^& calc.exe^\n\n^\n\n^\"',
"The evil command is escaped properly"
);
}

View File

@@ -474,15 +474,10 @@ const CurlUtils = {
// by the previous replace.
.replace(/%(?=[a-zA-Z0-9_])/g, "%^")
// We replace \r and \r\n with \n, this allows to consistently escape all new
// lines in the next replace
.replace(/\r\n?/g, "\n")
// Lastly we replace new lines with ^ and TWO new lines because the first
// new line is there to enact the escape command the second is the character
// to escape (in this case new line).
// The extra " enables escaping new lines with ^ within quotes in cmd.exe.
.replace(/\n/g, '"^\r\n\r\n"') +
// Lastly we replace new lines with ^ and TWO new lines because the first
// new line is there to enact the escape command the second is the character
// to escape (in this case new line).
.replace(/\r?\n|\r/g, '^\n\n') +
encapsChars
);
},

View File

@@ -356,8 +356,10 @@ function quote(str) {
function escapeNewline(txt) {
if (isWin()) {
// Add `"` to close quote, then escape newline outside of quote, then start new quote
return txt.replace(/[\r\n]{1,2}/g, '"^$&$&"');
// For windows we replace new lines with ^ and TWO new lines because the first
// new line is there to enact the escape command the second is the character
// to escape (in this case new line).
return txt.replace(/\r?\n|\r/g, "^\n\n");
}
return txt.replace(/\r/g, "\\r").replace(/\n/g, "\\n");
}