fix: Filter out the ZWNBSP character from response body, fixed charset parse logic (#2351)

* fix(#1003): content type for client_credentials & password grant types

* feature(#1003): added client is & secret for password credentials grant type

* fix: filter out non-printable control character and ZWNBSP character

* fix: filter out non-printable control character and ZWNBSP character

* remove ZWNBSP character from response body

---------

Co-authored-by: Anoop M D <anoop.md1421@gmail.com>
This commit is contained in:
lohit 2024-05-22 18:42:25 +05:30 committed by GitHub
parent e56fb74801
commit e0b8de5337
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -260,13 +260,16 @@ const configureRequest = async (
const parseDataFromResponse = (response) => {
const dataBuffer = Buffer.from(response.data);
// Parse the charset from content type: https://stackoverflow.com/a/33192813
const charset = /charset=([^()<>@,;:"/[\]?.=\s]*)/i.exec(response.headers['Content-Type'] || '');
const charsetMatch = /charset=([^()<>@,;:"/[\]?.=\s]*)/i.exec(response.headers['content-type'] || '');
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#using_exec_with_regexp_literals
const charsetValue = charsetMatch?.[1];
// Overwrite the original data for backwards compatibility
let data = dataBuffer.toString(charset || 'utf-8');
let data = dataBuffer.toString(charsetValue || 'utf-8');
// Try to parse response to JSON, this can quietly fail
try {
// Filter out control characters and ZWNBSP character
data = data.replace(/[\x00-\x08\x0E-\x1F\x7F\uFEFF]/g, '');
// Filter out ZWNBSP character
// https://gist.github.com/antic183/619f42b559b78028d1fe9e7ae8a1352d
data = data.replace(/^\uFEFF/, '');
data = JSON.parse(data);
} catch {}