Skip to content

Commit 9eb16b3

Browse files
committed
HCK-4140: reduce complexity of shell command escaping function
1 parent 0d90a99 commit 9eb16b3

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

forward_engineering/helpers/azureCLIScriptHelpers/buildAzureCLIScript.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { wrapInSingleQuotes, getEscapeFunction } = require('./escapeShellSpecialCharacters');
1+
const { wrapInSingleQuotes, escapeShellCommand } = require('./escapeShellSpecialCharacters');
22
const applyToInstanceHelper = require('../../applyToInstance/applyToInstanceHelper');
33
const { getUniqueKeyPolicyScript } = require('../getUniqueKeyPolicyScript');
44
const { getCliParamsDelimiter } = require('./getCliParamsDelimiter');
@@ -18,7 +18,7 @@ const buildAzureCLIScript =
1818
_ =>
1919
({ modelData, containerData, shellName }) => {
2020
const cliParamsDelimiter = getCliParamsDelimiter(shellName);
21-
const escapeAndWrapInQuotes = string => wrapInSingleQuotes(getEscapeFunction(shellName)(string));
21+
const escapeAndWrapInQuotes = string => wrapInSingleQuotes(escapeShellCommand(shellName, string));
2222

2323
const accountName = escapeAndWrapInQuotes(modelData[0]?.accountName || '');
2424
const dbName = escapeAndWrapInQuotes(containerData[0]?.dbId || '');
Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
1-
const getEscapeFunction = shell => {
1+
/**
2+
* @param {"bash" | "zsh" | "powershell"} shell
3+
* @param {string} command
4+
* @return {string}
5+
*/
6+
const escapeShellCommand = (shell, command) => {
27
switch (shell) {
38
case 'powershell':
4-
return string => {
5-
string = string
6-
.replace(/[\u009B\u001B\u0008\0]/gu, '')
7-
.replace(/\r(?!\n)/gu, '')
8-
.replace(/(['])/gu, '$1$1');
9+
command = command
10+
.replace(/[\u009B\u001B\u0008\0]/gu, '')
11+
.replace(/\r(?!\n)/gu, '')
12+
.replace(/(['])/gu, '$1$1');
913

10-
if (/[\u0085\s]/u.test(string)) {
11-
string = string.replace(/(?<!\\)(\\*)"/gu, '$1$1""').replace(/(?<!\\)(\\+)$/gu, '$1$1');
12-
} else {
13-
string = string.replace(/(?<!\\)(\\*)"/gu, '$1$1\\"');
14-
}
15-
return string;
16-
};
14+
if (/[\u0085\s]/u.test(command)) {
15+
command = command.replace(/(?<!\\)(\\*)"/gu, '$1$1""').replace(/(?<!\\)(\\+)$/gu, '$1$1');
16+
} else {
17+
command = command.replace(/(?<!\\)(\\*)"/gu, '$1$1\\"');
18+
}
19+
return command;
1720
case 'bash':
1821
case 'zsh':
19-
return string => {
20-
return string
21-
.replace(/[\0\u0008\u001B\u009B]/gu, '')
22-
.replace(/\r(?!\n)/gu, '')
23-
.replace(/'/gu, "'\\''");
24-
};
22+
return command
23+
.replace(/[\0\u0008\u001B\u009B]/gu, '')
24+
.replace(/\r(?!\n)/gu, '')
25+
.replace(/'/gu, "'\\''");
26+
2527
default:
26-
return string => string;
28+
return command;
2729
}
2830
};
2931

30-
const wrapInSingleQuotes = string => `'${string}'`;
32+
/**
33+
* @param {string} command
34+
* @return {string}
35+
*/
36+
const wrapInSingleQuotes = command => `'${command}'`;
3137

32-
module.exports = { getEscapeFunction, wrapInSingleQuotes };
38+
module.exports = { escapeShellCommand, wrapInSingleQuotes };

0 commit comments

Comments
 (0)