Skip to content

Commit b1ca09a

Browse files
committed
fix(core): Strictened the sign routine
Closes #14
1 parent dd1bc7e commit b1ca09a

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

lib/u2f-api.ts

+41-13
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ export interface RegisterRequest {
2121
challenge: string;
2222
}
2323

24-
export interface SignRequest
25-
extends RegisterRequest {
24+
export interface SignRequest extends RegisterRequest {
2625
keyHandle: string;
2726
}
2827

@@ -38,6 +37,16 @@ export interface SignResponse {
3837
signatureData: string;
3938
}
4039

40+
export type Transport = 'bt' | 'ble' | 'nfc' | 'usb';
41+
export type Transports = Array< Transport >;
42+
43+
export interface RegisteredKey {
44+
version: string;
45+
keyHandle: string;
46+
transports: Transports;
47+
appId: string;
48+
}
49+
4150
var _backend: Promise< API > = null;
4251
function getBackend( )
4352
{
@@ -147,6 +156,22 @@ export function ensureSupport( )
147156
.then( _ensureSupport );
148157
}
149158

159+
function arrayify< T >(
160+
value:
161+
T | Array< T > | Readonly< T > | ReadonlyArray< T > | undefined | null
162+
)
163+
: Array< T >
164+
{
165+
if ( value != null && Array.isArray( value ) )
166+
return value;
167+
168+
return value == null
169+
? [ ]
170+
: Array.isArray( value )
171+
? [ ...value ]
172+
: [ < T >value ];
173+
}
174+
150175
export function register(
151176
registerRequests: RegisterRequest | ReadonlyArray< RegisterRequest >,
152177
signRequests: SignRequest | ReadonlyArray< SignRequest >,
@@ -163,17 +188,17 @@ export function register(
163188
)
164189
: Promise< RegisterResponse >
165190
{
166-
if ( !Array.isArray( registerRequests ) )
167-
registerRequests = [ registerRequests ] as ReadonlyArray< RegisterRequest >;
191+
const _registerRequests = arrayify( registerRequests );
168192

169193
if ( typeof signRequests === 'number' && typeof timeout === 'undefined' )
170194
{
171195
timeout = signRequests;
172196
signRequests = null;
173197
}
174198

175-
if ( !signRequests )
176-
signRequests = [ ];
199+
const _signRequests = arrayify(
200+
< SignRequest | ReadonlyArray< SignRequest > >signRequests
201+
);
177202

178203
return getBackend( )
179204
.then( function( backend )
@@ -195,10 +220,10 @@ export function register(
195220
}
196221
}
197222

198-
const appId = registerRequests[ 0 ].appId;
223+
const appId = _registerRequests[ 0 ].appId;
199224

200225
u2f.register(
201-
appId, registerRequests, signRequests, callback, timeout );
226+
appId, _registerRequests, _signRequests, callback, timeout );
202227
} );
203228
} );
204229
}
@@ -209,8 +234,7 @@ export function sign(
209234
)
210235
: Promise< SignResponse >
211236
{
212-
if ( !Array.isArray( signRequests ) )
213-
signRequests = [ signRequests ] as ReadonlyArray< SignRequest >;
237+
const _signRequests = arrayify( signRequests );
214238

215239
return getBackend( )
216240
.then( function( backend )
@@ -232,10 +256,14 @@ export function sign(
232256
}
233257
}
234258

235-
const appId = signRequests[ 0 ].appId;
236-
const challenge = signRequests[ 0 ].challenge;
259+
const appId = _signRequests[ 0 ].appId;
260+
const challenge = _signRequests[ 0 ].challenge;
261+
const registeredKeys = _signRequests
262+
.map( ( { version, keyHandle, appId } ) =>
263+
( { version, keyHandle, appId } as RegisteredKey )
264+
);
237265

238-
u2f.sign( appId, challenge, signRequests, callback, timeout );
266+
u2f.sign( appId, challenge, registeredKeys, callback, timeout );
239267
} );
240268
} );
241269
}

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@
3838
"@types/mocha": "5.x",
3939
"already": "1.x",
4040
"chai": "4.x",
41+
"commitizen": "3.x",
4142
"cz-conventional-changelog": "2.x",
4243
"jsdom": "11.x",
4344
"mocha": "5.x",
4445
"pre-commit": "1.x",
4546
"rimraf": "2.x",
4647
"semantic-release": "15.x",
48+
"source-map-support": "0.x",
4749
"travis-deploy-once": "5.x",
4850
"typescript": "3.x"
4951
},

test/u2f-api/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
declare var global: any;
44
declare var require: any;
55

6+
import 'source-map-support/register'
7+
68
import 'mocha';
79
import { expect } from 'chai';
810
import { Finally, Try, delay } from 'already';
@@ -161,7 +163,7 @@ function u2fMock( props: MockProps = { } )
161163
sign(
162164
appId,
163165
challenge,
164-
signRequests: Array< FakeRequest >,
166+
registeredKeys: Array< u2fApi.RegisteredKey >,
165167
cbNative,
166168
timeout
167169
)
@@ -171,10 +173,8 @@ function u2fMock( props: MockProps = { } )
171173
if ( props.appId && props.appId !== appId )
172174
return { errorCode: ErrorCodesEnum.BAD_REQUEST };
173175

174-
const found = signRequests.some( req =>
176+
const found = registeredKeys.some( req =>
175177
store.some( storeReq =>
176-
storeReq.request === req.request
177-
&&
178178
storeReq.appId === req.appId
179179
)
180180
);

0 commit comments

Comments
 (0)