From a9863717b1b82b3077edd0db85454ba801eac9bd Mon Sep 17 00:00:00 2001 From: Maximilian Krambach Date: Thu, 30 Aug 2018 12:04:50 +0200 Subject: [PATCH] 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 | 67 ++++++++++++++++++++---------- lang/js/src/permittedOperations.js | 59 ++++++++++++++------------ 2 files changed, 77 insertions(+), 49 deletions(-) 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)){ + } + default: { + let answerType = null; + if (poa.payload && poa.payload.hasOwnProperty(key)){ + answerType = 'p'; + } else if (poa.info && poa.info.hasOwnProperty(key)){ + answerType = 'i'; + } + if (answerType !== 'p' && answerType !== 'i'){ return gpgme_error('CONN_UNEXPECTED_ANSWER'); } - if ( typeof (_decodedResponse[key]) !== poa.data[key] ){ - 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'){ - _response[key] = _decodedResponse[key]; - _response.format = 'base64'; - } else { - _response[key] = Utf8ArrayToStr( - atobArray(_decodedResponse[key])); - _response.format = 'string'; + + if (answerType === 'i') { + if ( typeof (_decodedResponse[key]) !== poa.info[key] ){ + return gpgme_error('CONN_UNEXPECTED_ANSWER'); } - } else { _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]; + } else { + // fallthrough, should not be reached + // (payload is always string) + return gpgme_error('CONN_UNEXPECTED_ANSWER'); + } } break; - } + } } } return _response; } diff --git a/lang/js/src/permittedOperations.js b/lang/js/src/permittedOperations.js index efb34f9b..c3c72ca1 100644 --- a/lang/js/src/permittedOperations.js +++ b/lang/js/src/permittedOperations.js @@ -42,8 +42,12 @@ * @property {Object} answer The definition on what to expect as answer, if the * answer is not an error * @property {Array} answer.type the type(s) as reported by gpgme-json. - * @property {Object} answer.data key-value combinations of expected properties - * of an answer and their type ('boolean', 'string', object) + * @property {Object} answer.payload key-value combinations of expected + * properties of an answer and their type ('boolean', 'string', object), which + * may need further decoding from base64 + * @property {Object} answer.info key-value combinations of expected + * properties of an answer and their type ('boolean', 'string', object), which + * are meant to be data directly sent by gpgme (i.e. user ids) @const */ export const permittedOperations = { @@ -104,8 +108,10 @@ export const permittedOperations = { }, answer: { type: ['ciphertext'], - data: { - 'data': 'string', + payload: { + 'data': 'string' + }, + info: { 'base64':'boolean' } } @@ -129,8 +135,10 @@ export const permittedOperations = { }, answer: { type: ['plaintext'], - data: { + payload: { 'data': 'string', + }, + info: { 'base64': 'boolean', 'mime': 'boolean', 'info': 'object', @@ -171,11 +179,12 @@ export const permittedOperations = { }, answer: { type: ['signature', 'ciphertext'], - data: { + payload: { 'data': 'string', + }, + info: { 'base64':'boolean' } - } }, @@ -223,9 +232,9 @@ export const permittedOperations = { }, answer: { type: ['keys'], - data: { + info: { + 'keys': 'object', 'base64': 'boolean', - 'keys': 'object' } } }, @@ -263,8 +272,10 @@ export const permittedOperations = { }, answer: { type: ['keys'], - data: { + payload: { 'data': 'string', + }, + info: { 'base64': 'boolean', 'sec-fprs': 'object' } @@ -288,7 +299,7 @@ export const permittedOperations = { }, answer: { type: [], - data: { + info: { 'result': 'object' } } @@ -308,7 +319,7 @@ export const permittedOperations = { }, }, answer: { - data: { + info: { 'success': 'boolean' } } @@ -319,7 +330,7 @@ export const permittedOperations = { optional: {}, answer: { type: [''], - data: { + info: { 'gpgme': 'string', 'info': 'object' } @@ -346,7 +357,7 @@ export const permittedOperations = { }, answer: { type: [''], - data: { 'fingerprint': 'string' } + info: { 'fingerprint': 'string' } } }, @@ -370,10 +381,12 @@ export const permittedOperations = { }, answer: { type: ['plaintext'], - data:{ - data: 'string', - base64:'boolean', - info: 'object' + payload:{ + 'data': 'string' + }, + info: { + 'base64':'boolean', + 'info': 'object' // info.file_name: Optional string of the plaintext file name. // info.is_mime: Boolean if the messages claims it is MIME. // info.signatures: Array of signatures @@ -395,15 +408,9 @@ export const permittedOperations = { optional: {}, answer: { type: [], - data: { - option: 'object' + info: { + 'option': 'object' } } } - - /** - * TBD handling of secrets - * TBD key modification? - */ - };