Skip to content

Commit

Permalink
Merge pull request #45 from electerious/master
Browse files Browse the repository at this point in the history
Fix "Missing single quote escaping"
  • Loading branch information
leoek authored Nov 8, 2022
2 parents a7e34b2 + 4884579 commit 43698c4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
20 changes: 12 additions & 8 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,29 @@ export const generateHeader = (options = {}) => {
}

/**
*
*
* @export
* @param {Object} body
* @returns {string}
*/
export function escapeBody(body) {
if (typeof body !== 'string') return body
return body.replace(/'/g, `'\\''`)
}

/**
* @export
* @param {Object} body
* @returns {string}
*/
export function generateBody(body) {
if (!body) return '';
if (typeof body === "object"){
return ` --data-binary '${JSON.stringify(body)}'`;
return ` --data-binary '${escapeBody(JSON.stringify(body))}'`;
}
return ` --data-binary '${body}'`;
return ` --data-binary '${escapeBody(body)}'`;
}

/**
*
*
* @export
* @param {boolean} isEncode
* @return {string}
Expand All @@ -106,8 +112,6 @@ export function generateCompress(isEncode) {
}

/**
*
*
* @export
* @param {string|object} requestInfo
* @param {object={}} requestInit
Expand Down
5 changes: 3 additions & 2 deletions test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,11 @@ describe('Generate body param', () => {
const options = {
test: 'test:',
testNumber: 12345,
testDate: new Date(1609251707077)
testDate: new Date(1609251707077),
testQuotes: `'test'`
};
expect(generateBody(options)).toEqual(
' --data-binary \'{"test":"test:","testNumber":12345,"testDate":"2020-12-29T14:21:47.077Z"}\''
` --data-binary '{"test":"test:","testNumber":12345,"testDate":"2020-12-29T14:21:47.077Z","testQuotes":"'\\''test'\\''"}'`
);
});
});
Expand Down

0 comments on commit 43698c4

Please sign in to comment.