return null on error with signed objects

This commit is contained in:
Timo Hocker 2020-03-10 13:37:12 +01:00
parent 3715ec5183
commit 38542bb422
2 changed files with 15 additions and 1 deletions

View File

@ -104,6 +104,8 @@ function sign_object (obj, key, key_info = null) {
* @returns {any} returns object if successful, else null * @returns {any} returns object if successful, else null
*/ */
function verify_signature (str, key, timeout = 0) { function verify_signature (str, key, timeout = 0) {
if (typeof str !== 'string')
return null;
const dec = decodeURIComponent (str) const dec = decodeURIComponent (str)
.split ('.'); .split ('.');
const json = JSON.parse (encoding.to_utf8 (dec[0], 'base64')); const json = JSON.parse (encoding.to_utf8 (dec[0], 'base64'));
@ -124,6 +126,8 @@ function verify_signature (str, key, timeout = 0) {
* @returns {any} data * @returns {any} data
*/ */
function get_signature_info (str) { function get_signature_info (str) {
if (typeof str !== 'string')
return null;
const dec = decodeURIComponent (str) const dec = decodeURIComponent (str)
.split ('.'); .split ('.');
const json = JSON.parse (encoding.to_utf8 (dec[0], 'base64')); const json = JSON.parse (encoding.to_utf8 (dec[0], 'base64'));
@ -137,7 +141,10 @@ function get_signature_info (str) {
* @returns {any} object * @returns {any} object
*/ */
function decode_signed (str) { function decode_signed (str) {
return get_signature_info (str).obj; const info = get_signature_info (str);
if (info)
return info.obj;
return null;
} }
/** /**

View File

@ -107,6 +107,13 @@ test ('verify_signature', (t) => {
t.deepEqual (obj, dec); t.deepEqual (obj, dec);
}); });
test ('return null on invalid input', (t) => {
const ver = crypto.verify_signature (null, 'foo');
t.is (ver, null);
const dec = crypto.decode_signed (null, 'foo');
t.is (dec, null);
});
test ('do not fail verification if timeout unspecified', async (t) => { test ('do not fail verification if timeout unspecified', async (t) => {
const obj = { foo: 'bar' }; const obj = { foo: 'bar' };
const str = crypto.sign_object (obj, 'baz'); const str = crypto.sign_object (obj, 'baz');