From a9863717b1b82b3077edd0db85454ba801eac9bd Mon Sep 17 00:00:00 2001 From: Maximilian Krambach Date: Thu, 30 Aug 2018 12:04:50 +0200 Subject: js: separate gpgme answer by type of data -- * src/Connection.js; src/permittedOperations.js: To avoid further encoding problems, data sent by gpgme is now sorted as either 'payload' or 'info'. Payload data may come in any encoding, and here the 'expected' and 'format' options are used, 'info' data may contain text created by gnupg which may need re-encoding, but this should not be affected by 'expected' and 'format' --- lang/js/src/Connection.js | 63 +++++++++++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 21 deletions(-) (limited to 'lang/js/src/Connection.js') diff --git a/lang/js/src/Connection.js b/lang/js/src/Connection.js index c4921d53..a421985a 100644 --- a/lang/js/src/Connection.js +++ b/lang/js/src/Connection.js @@ -243,7 +243,7 @@ class Answer{ for (let i= 0; i < messageKeys.length; i++){ let key = messageKeys[i]; switch (key) { - case 'type': + case 'type': { if (_decodedResponse.type === 'error'){ return (gpgme_error('GNUPG_ERROR', decode(_decodedResponse.msg))); @@ -251,39 +251,60 @@ class Answer{ return gpgme_error('CONN_UNEXPECTED_ANSWER'); } break; - case 'base64': + } + case 'base64': { break; - case 'msg': + } + case 'msg': { if (_decodedResponse.type === 'error'){ return (gpgme_error('GNUPG_ERROR', _decodedResponse.msg)); } break; - default: - if (!poa.data.hasOwnProperty(key)){ - return gpgme_error('CONN_UNEXPECTED_ANSWER'); + } + default: { + let answerType = null; + if (poa.payload && poa.payload.hasOwnProperty(key)){ + answerType = 'p'; + } else if (poa.info && poa.info.hasOwnProperty(key)){ + answerType = 'i'; } - if ( typeof (_decodedResponse[key]) !== poa.data[key] ){ + if (answerType !== 'p' && answerType !== 'i'){ return gpgme_error('CONN_UNEXPECTED_ANSWER'); } - if (_decodedResponse.base64 === true - && poa.data[key] === 'string' - ) { - if (this.expected === 'uint8'){ - _response[key] = atobArray(_decodedResponse[key]); - _response.format = 'uint8'; - } else if (this.expected === 'base64'){ + + if (answerType === 'i') { + if ( typeof (_decodedResponse[key]) !== poa.info[key] ){ + return gpgme_error('CONN_UNEXPECTED_ANSWER'); + } + _response[key] = decode(_decodedResponse[key]); + + } else if (answerType === 'p') { + if (_decodedResponse.base64 === true + && poa.payload[key] === 'string' + ) { + if (this.expected === 'uint8'){ + _response[key] = atobArray(_decodedResponse[key]); + _response.format = 'uint8'; + + } else if (this.expected === 'base64'){ + _response[key] = _decodedResponse[key]; + _response.format = 'base64'; + + } else { // no 'expected' + _response[key] = Utf8ArrayToStr( + atobArray(_decodedResponse[key])); + _response.format = 'string'; + } + } else if (poa.payload[key] === 'string') { _response[key] = _decodedResponse[key]; - _response.format = 'base64'; } else { - _response[key] = Utf8ArrayToStr( - atobArray(_decodedResponse[key])); - _response.format = 'string'; + // fallthrough, should not be reached + // (payload is always string) + return gpgme_error('CONN_UNEXPECTED_ANSWER'); } - } else { - _response[key] = decode(_decodedResponse[key]); } break; - } + } } } return _response; } -- cgit v1.2.3