Skip to content

Commit faf5fe8

Browse files
authored
Merge pull request #664 from MasterKale/fix/browser-tolerate-old-call-structures
fix/browser-tolerate-old-call-structures
2 parents 3983978 + 62025dc commit faf5fe8

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

packages/browser/src/methods/startAuthentication.test.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
assertRejects,
88
assertStringIncludes,
99
} from '@std/assert';
10-
import { assertSpyCalls, type Spy, spy } from '@std/testing/mock';
10+
import { assertSpyCalls, type Spy, spy, stub } from '@std/testing/mock';
1111
import { afterEach, beforeEach, describe, it } from '@std/testing/bdd';
1212
import { JSDOM } from 'jsdom';
1313
import type {
@@ -442,6 +442,31 @@ describe('Method: startAuthentication()', () => {
442442

443443
assertEquals(response.authenticatorAttachment, 'cross-platform');
444444
});
445+
446+
it('should continue despite use of old call structure', async () => {
447+
const stubConsoleWarn = stub(console, 'warn');
448+
449+
// @ts-ignore: Intentionally call this method in a pre-v11 way
450+
await startAuthentication(goodOpts1);
451+
452+
assertSpyCalls(stubConsoleWarn, 1);
453+
454+
// Assert there's some browser console output
455+
const warning = stubConsoleWarn.calls.at(0)?.args[0] as string;
456+
assertStringIncludes(warning, 'startAuthentication() was not called correctly');
457+
458+
// Check that the method was able to handle the bad call anyway
459+
const args = getSpy.calls.at(0)?.args[0] as CredentialCreationOptions;
460+
const argsPublicKey = args.publicKey!;
461+
462+
// Make sure challenge and user.id are converted to Buffers
463+
assertEquals(
464+
new Uint8Array(argsPublicKey.challenge as ArrayBuffer),
465+
new Uint8Array([213, 62, 174, 30, 184, 184, 56, 4]),
466+
);
467+
468+
stubConsoleWarn.restore();
469+
});
445470
});
446471

447472
describe('WebAuthnError', () => {

packages/browser/src/methods/startAuthentication.ts

+10
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ export async function startAuthentication(
2828
verifyBrowserAutofillInput?: boolean;
2929
},
3030
): Promise<AuthenticationResponseJSON> {
31+
// @ts-ignore: Intentionally check for old call structure to warn about improper API call
32+
if (!options.optionsJSON && options.challenge) {
33+
console.warn(
34+
'startAuthentication() was not called correctly. It will try to continue with the provided options, but this call should be refactored to use the expected call structure instead. See https://simplewebauthn.dev/docs/packages/browser#typeerror-cannot-read-properties-of-undefined-reading-challenge for more information.',
35+
);
36+
37+
// @ts-ignore: Reassign the options, passed in as a positional argument, to the expected variable
38+
options = { optionsJSON: options };
39+
}
40+
3141
const {
3242
optionsJSON,
3343
useBrowserAutofill = false,

packages/browser/src/methods/startRegistration.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,31 @@ describe('Method: startRegistration', () => {
374374
// The most important bit
375375
assertEquals(argsMediation, 'conditional');
376376
});
377+
378+
it('should continue despite use of old call structure', async () => {
379+
const stubConsoleWarn = stub(console, 'warn');
380+
381+
// @ts-ignore: Intentionally call this method in a pre-v11 way
382+
await startRegistration(goodOpts1);
383+
384+
assertSpyCalls(stubConsoleWarn, 1);
385+
386+
// Assert there's some browser console output
387+
const warning = stubConsoleWarn.calls.at(0)?.args[0] as string;
388+
assertStringIncludes(warning, 'startRegistration() was not called correctly');
389+
390+
// Check that the method was able to handle the bad call anyway
391+
const args = createSpy.calls.at(0)?.args[0] as CredentialCreationOptions;
392+
const argsPublicKey = args.publicKey!;
393+
394+
// Make sure challenge and user.id are converted to Buffers
395+
assertEquals(
396+
new Uint8Array(argsPublicKey.challenge as ArrayBuffer),
397+
new Uint8Array([213, 62, 174, 30, 184, 184, 56, 4]),
398+
);
399+
400+
stubConsoleWarn.restore();
401+
});
377402
});
378403

379404
describe('WebAuthnError', () => {

packages/browser/src/methods/startRegistration.ts

+10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ export async function startRegistration(
2626
useAutoRegister?: boolean;
2727
},
2828
): Promise<RegistrationResponseJSON> {
29+
// @ts-ignore: Intentionally check for old call structure to warn about improper API call
30+
if (!options.optionsJSON && options.challenge) {
31+
console.warn(
32+
'startRegistration() was not called correctly. It will try to continue with the provided options, but this call should be refactored to use the expected call structure instead. See https://simplewebauthn.dev/docs/packages/browser#typeerror-cannot-read-properties-of-undefined-reading-challenge for more information.',
33+
);
34+
35+
// @ts-ignore: Reassign the options, passed in as a positional argument, to the expected variable
36+
options = { optionsJSON: options };
37+
}
38+
2939
const { optionsJSON, useAutoRegister = false } = options;
3040

3141
if (!browserSupportsWebAuthn()) {

0 commit comments

Comments
 (0)