Skip to content

Commit

Permalink
fix(auth): proper values returned on auth_decode_ipc_msg errors
Browse files Browse the repository at this point in the history
  - Test case for authenticator error -208 IncompatibleMockStatus
  - Test case for authenticator error -103 when decoding share MData
    request for non-existent MData
  - Test case for authenticator error -1 EncodeDecodeError when decoding a
    64-bit request
  • Loading branch information
hunterlester authored and joshuef committed Feb 26, 2019
1 parent 9b36ec9 commit ce9c2fa
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 36 deletions.
36 changes: 15 additions & 21 deletions app/extensions/safe/ffi/authenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,29 +670,23 @@ class Authenticator extends SafeLib
reject
);

const decodeReqErrorCb = this._pushCb(
ffi.Callback(
types.Void,
[ types.voidPointer, types.FfiResultPointer, types.CString ],
( userData, resultPtr ) =>
const decodeReqErrorCb = this._pushCb( ffi.Callback( types.Void,
[types.voidPointer, types.FfiResultPointer, types.CString], ( userData, resultPtr ) =>
{
const result = resultPtr.deref();
const error =
{
const result = resultPtr.deref();
if (
!(
this[_reqErrListener]
&& this[_reqErrListener].len() !== 0
)
)
{
return;
}

logger.info( 'Error in auth callback.', result );
this[_reqErrListener].broadcast( JSON.stringify( result ) );
reject( result );
error_code : result.error_code,
description : result.description
};
if ( !( this[_reqErrListener] && this[_reqErrListener].len() !== 0 ) )
{
return;
}
)
);

this[_reqErrListener].broadcast( JSON.stringify( error ) );
return reject( error );
} ) );
try
{
this.safeLib.auth_decode_ipc_msg(
Expand Down
11 changes: 8 additions & 3 deletions app/extensions/safe/ffi/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,12 @@ class ReqQueue
.catch( err =>
{
// FIXME: if error occurs for unregistered client process next
self.req.error = err.message;
self.req.error = err;
if ( allAuthCallBacks[self.req.id] )
{
allAuthCallBacks[self.req.id].reject( err );
delete allAuthCallBacks[self.req.id];
}

// TODO/BOOKMARK: leaving off here. share MData req URI is causing error when used to call auth_decode_ipc_msg in authenticator.js

Expand All @@ -245,7 +250,7 @@ class ReqQueue
// TODO: Setup proper rejection from when unauthed.
if ( bgStore )
{
bgStore.dispatch( receivedAuthResponse( err.message ) );
bgStore.dispatch( receivedAuthResponse( ( err.error_code ? `${ err.error_code }: ${ err.description }` : `${ err.message }` ) ) );
}

if ( ipcEvent )
Expand Down Expand Up @@ -294,7 +299,7 @@ const enqueueRequest = ( req, type ) =>

const isUnRegistered = req.isUnRegistered;
const request = new Request( {
id : req.id,
id : req.id || Math.floor( Math.random() * ( 2 ** 32 ) ),
uri : req.uri ? req.uri : req,
type : type || CONSTANTS.CLIENT_TYPES.DESKTOP,
isUnRegistered
Expand Down
57 changes: 54 additions & 3 deletions app/extensions/safe/test/auth/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@ const encodedAuthUri = 'safe-auth:bAAAAAAFBMHKYWAAAAAABWAAAAAAAAAAANZSXILTNMFUWI
+ 'AL5YHKYTMNFRQCAAAAAAAAAAAAAAAAAAB';
/* const encodedUnRegisterAuthUri = 'safe-auth:bAAAAAADKLNT46AQAAAABWAAAAAAAAAAANZSXILT' +
'NMFUWI43BMZSS45DFON2C453FMJQXA4BONFSAC'; */
const encodedContUri = 'safe-auth:bAAAAAAHQQQ2XQAIAAAABWAAAAAAAAAAANZSXILTNMFUWI43BM'
+ 'ZSS45DFON2C453FMJQXA4BONFSAACYAAAAAAAAAABLWKYSBOBYCAVDFON2A2AAAAAAAAAAAJVQWSZCTMFTG'
+ 'KICMORSC4AIAAAAAAAAAAADQAAAAAAAAAAC7OB2WE3DJMMAQAAAAAAAAAAABAAAAAAI';
const encodedContUri = 'safe-auth:bAAAAAAHQQQ2XQAIAAAABWAAAAAAAAAAANZSXILTNMFUWI43BM' +
'ZSS45DFON2C453FMJQXA4BONFSAACYAAAAAAAAAABLWKYSBOBYCAVDFON2A2AAAAAAAAAAAJVQWSZCTMFTG' +
'KICMORSC4AIAAAAAAAAAAADQAAAAAAAAAAC7OB2WE3DJMMAQAAAAAAAAAAABAAAAAAI';

const encodedNonExistentShareMDataReq = 'safe-auth:bAAAAAAA7BILOYAYAAAACOAAAAAAAAAAANZSXILTNMFUWI43BMZSS4YLQNFPXA3DBPFTXE33VNZSC453FMJRWY2LFNZ2C4OIAC4AAAAAAAAAAAU2BIZCSA53FMIQECUCJEBYGYYLZM5ZG65LOMQGQAAAAAAAAAACNMFUWIU3BMZSSATDUMQXACAAAAAAAAAAATE5AAAAAAAAAAXYWSFBJN6MFV5OY6TCN3SYOIESF3L7TDFYE2IEQC6WHASMCZEUPAEAQAAAAAE';

const authReqWithoutMockBit = 'safe-auth:bAAAAAAEKDQ7DAAAAAAACOAAAAAAAAAAANZSXILTNMFUWI43BMZSS4YLQNFPXA3DBPFTXE33VNZSC453FMJRWY2LFNZ2C4OIAC4AAAAAAAAAAAU2BIZCSA53FMIQECUCJEBYGYYLZM5ZG65LOMQGQAAAAAAAAAACNMFUWIU3BMZSSATDUMQXACAQAAAAAAAAAAADQAAAAAAAAAAC7OB2WE3DJMMCAAAAAAAAAAAAAAAAAAAIAAAAAEAAAAABQAAAABQAAAAAAAAAAAX3QOVRGY2LDJZQW2ZLTAQAAAAAAAAAAAAAAAAAACAAAAABAAAAAAMAAAAAA';

const sixtyFourBitReq = 'safe-auth:AAAAAIdLdL0AAAAAJwAAAAAAAABuZXQubWFpZHNhZmUuYXBpX3BsYXlncm91bmQud2ViY2xpZW50LjkAFwAAAAAAAABTQUZFIHdlYiBBUEkgcGxheWdyb3VuZA0AAAAAAAAATWFpZFNhZmUgTHRkLgECAAAAAAAAAAcAAAAAAAAAX3B1YmxpYwQAAAAAAAAAAAAAAAEAAAACAAAAAwAAAAwAAAAAAAAAX3B1YmxpY05hbWVzBAAAAAAAAAAAAAAAAQAAAAIAAAADAAAA';

const decodedReqForRandomClient = uri => helper.createRandomAccount()
.then( () => client.decodeRequest( uri ) );

const decodedReqForRandomClient = uri =>
helper.createRandomAccount().then( () => client.decodeRequest( uri ) );
Expand Down Expand Up @@ -437,6 +446,48 @@ describe( 'Authenticator functions', () =>
}
} );

it( 'throws error when encoded auth request generated in live environment, however decoding for mock routing', async () =>
{
try
{
await decodedReqForRandomClient( authReqWithoutMockBit );
}
catch ( e )
{
expect( e.error_code ).toBe( -208 );
expect( e.description ).toBe( 'IPC error: IncompatibleMockStatus' );
await helper.clearAccount();
}
} );

it( 'throws error when decoding share MData request for non-exsistent MData', async () =>
{
try
{
await decodedReqForRandomClient( encodedNonExistentShareMDataReq );
}
catch ( e )
{
expect( e.error_code ).toBe( -103 );
expect( e.description ).toBe( 'Core error: Routing client error -> Requested data not found' );
await helper.clearAccount();
}
} );

it( 'throws error when decoding 64-bit auth request', async () =>
{
try
{
await decodedReqForRandomClient( sixtyFourBitReq );
}
catch ( e )
{
expect( e.error_code ).toBe( -1 );
expect( e.description ).toBe( 'IPC error: EncodeDecodeError' );
await helper.clearAccount();
}
} );

it( 'throws an error for invalid URI', async () =>
{
await helper.createRandomAccount().catch( console.log );
Expand Down
16 changes: 7 additions & 9 deletions app/extensions/safe/webviewPreload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,7 @@ export const setupSafeAPIs = ( passedStore, win = window ) =>
return app;
};

win.safe.fromAuthUri = async (
appInfo,
authUri,
netStateCallback,
options
) =>
win.safe.fromAuthUri = async ( appInfo, authURI, netStateCallback, options ) =>
{
// TODO: Throw warnings for these options.
const optionsToUse = {
Expand Down Expand Up @@ -159,9 +154,12 @@ export const setupSafeAPIs = ( passedStore, win = window ) =>
win.safe.authorise = async authObj =>
{
if ( !authObj || typeof authObj !== 'object' ) throw new Error( 'Auth object is required' );
return createRemoteCall( 'authenticateFromUriObject', passedStore )(
authObj
);
let authReqObj = authObj;
if ( !authReqObj.id )
{
authReqObj = { ...authObj, id: Math.floor( Math.random() * ( 2 ** 32 ) ) };
}
return createRemoteCall( 'authenticateFromUriObject', passedStore )( authReqObj );
};
};

Expand Down

0 comments on commit ce9c2fa

Please sign in to comment.