Skip to content

Fixes issue with semicolon in form data (CURL) #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions codegens/curl/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ self = module.exports = {
if (!(data.disabled)) {
if (data.type === 'file') {
snippet += indent + `${form('-F', format)}`;
snippet += ` '${sanitize(data.key, trim)}=@${sanitize(data.src, trim)}'`;
snippet += ` '${sanitize(data.key, trim)}=@"${sanitize(data.src, trim, true, true)}"'`;
}
else {
snippet += indent + `${form('-F', format)}`;
snippet += ` '${sanitize(data.key, trim)}=${sanitize(data.value, trim)}'`;
snippet += ` '${sanitize(data.key, trim)}="${sanitize(data.value, trim, true, true)}"'`;
}
}
});
Expand All @@ -159,6 +159,7 @@ self = module.exports = {
}
}
}

callback(null, snippet);
},
getOptions: function () {
Expand Down
16 changes: 14 additions & 2 deletions codegens/curl/lib/util.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
module.exports = {
/**
* sanitizes input string by handling escape characters eg: converts '''' to '\'\''
* sanitizes input string by handling escape characters eg: converts '''' to '\'\'', (" to \" and \ to \\ )
* and trim input if required
*
* @param {String} inputString
* @param {Boolean} [trim] - indicates whether to trim string or not
* @param {Boolean} [doubleQuotes] - indicates whether to escape double quotes(") and backslash(\\)
* @param {Boolean} [backSlash] - indicates whether to escape backslash(\\)
* @returns {String}
*/
sanitize: function (inputString, trim) {
sanitize: function (inputString, trim, doubleQuotes, backSlash) {
if (typeof inputString !== 'string') {
return '';
}

if (backSlash) {
inputString = inputString.replace(/\\/g, '\\\\');
}

if (doubleQuotes) {
inputString = inputString.replace(/"/g, '\\"');
}

// for curl escaping of single quotes inside single quotes involves changing of ' to '\''
inputString = inputString.replace(/'/g, "'\\''"); // eslint-disable-line quotes
return trim ? inputString.trim() : inputString;
},

form: function (option, format) {
if (format) {
switch (option) {
Expand Down
6 changes: 3 additions & 3 deletions codegens/curl/test/unit/convert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ describe('curl convert function', function () {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.include('no file=@/path/to/file');
expect(snippet).to.include('no src=@/path/to/file');
expect(snippet).to.include('invalid src=@/path/to/file');
expect(snippet).to.include('no file=@"/path/to/file"');
expect(snippet).to.include('no src=@"/path/to/file"');
expect(snippet).to.include('invalid src=@"/path/to/file"');
});
});

Expand Down
13 changes: 13 additions & 0 deletions npm/ci-requirements.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,18 @@ popd &>/dev/null;
echo "Installing dependencies required for tests in codegens/csharp-restsharp"
sudo apt-get install -y mono-complete

echo "Installing curl v7.68"
sudo apt-get install -y libssl-dev autoconf libtool make
wget https://curl.haxx.se/download/curl-7.68.0.zip
unzip curl-7.68.0.zip
pushd ./curl-7.68.0 &>/dev/null
./buildconf
./configure --with-ssl
make
sudo make install
sudo cp /usr/local/bin/curl /usr/bin/curl
sudo ldconfig
popd &>/dev/null

echo "Installing dependencies required for tests in codegens/shell-httpie"
sudo apt-get install httpie