From 186a8fe40db67f540f5799b9af6920e727d7f06e Mon Sep 17 00:00:00 2001 From: Ben Brooks <56796209+bensbits91@users.noreply.github.com> Date: Thu, 12 Nov 2026 09:45:26 -0800 Subject: [PATCH] [PM-40268] Unwrap fido2 transports for postMessage (#24397) Signed-off-by: Ben Brooks --- .../fido2/utils/webauthn-utils.spec.ts | 54 +++++++++++++++---- .../autofill/fido2/utils/webauthn-utils.ts | 2 +- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/apps/browser/src/autofill/fido2/utils/webauthn-utils.spec.ts b/apps/browser/src/autofill/fido2/utils/webauthn-utils.spec.ts index 4985afddd8f..36a3bb5e640 100644 --- a/apps/browser/src/autofill/fido2/utils/webauthn-utils.spec.ts +++ b/apps/browser/src/autofill/fido2/utils/webauthn-utils.spec.ts @@ -1,27 +1,63 @@ +import { serialize } from "v8"; + import { WebauthnUtils } from "./webauthn-utils"; +// jsdom does not expose structuredClone. Node's v8.serialize uses the same +// structured-clone algorithm, so it throws DataCloneError on the same inputs. +const assertStructuredCloneable = (value: unknown) => serialize(value); + +const challenge = new Uint8Array([1, 2, 3, 4]).buffer; +const credentialId = new Uint8Array([9, 9, 9]).buffer; +const userId = new Uint8Array([5, 5, 5]).buffer; + +// Reactive frameworks wrap caller inputs in a Proxy, which window.postMessage cannot clone. describe("WebauthnUtils", () => { describe("mapCredentialRequestOptions", () => { - it("copies credential transports into a plain array", () => { - const transports = new Proxy(["usb"] as AuthenticatorTransport[], {}); + it("produces a structured-cloneable result when transports is a Proxy", () => { + const transportsProxy = new Proxy(["usb", "nfc"], {}) as AuthenticatorTransport[]; + const options: CredentialRequestOptions = { publicKey: { - challenge: new Uint8Array([1, 2, 3]), + challenge, allowCredentials: [ { - id: new Uint8Array([4, 5, 6]), + id: credentialId, type: "public-key", - transports, + transports: transportsProxy, }, ], }, }; - const result = WebauthnUtils.mapCredentialRequestOptions(options, true); - const [allowedCredential] = result.allowedCredentials; + const mapped = WebauthnUtils.mapCredentialRequestOptions(options, false); - expect(allowedCredential?.transports).toEqual(["usb"]); - expect(allowedCredential?.transports).not.toBe(transports); + expect(() => assertStructuredCloneable(mapped)).not.toThrow(); + }); + }); + + describe("mapCredentialCreationOptions", () => { + it("produces a structured-cloneable result when excludeCredentials transports is a Proxy", () => { + const transportsProxy = new Proxy(["usb"], {}) as AuthenticatorTransport[]; + + const options: CredentialCreationOptions = { + publicKey: { + challenge, + rp: { id: "example.com", name: "Example" }, + user: { id: userId, name: "jdoe", displayName: "J. Doe" }, + pubKeyCredParams: [{ type: "public-key", alg: -7 }], + excludeCredentials: [ + { + id: credentialId, + type: "public-key", + transports: transportsProxy, + }, + ], + }, + }; + + const mapped = WebauthnUtils.mapCredentialCreationOptions(options, false); + + expect(() => assertStructuredCloneable(mapped)).not.toThrow(); }); }); }); diff --git a/apps/browser/src/autofill/fido2/utils/webauthn-utils.ts b/apps/browser/src/autofill/fido2/utils/webauthn-utils.ts index 98a380ce564..868db68ef48 100644 --- a/apps/browser/src/autofill/fido2/utils/webauthn-utils.ts +++ b/apps/browser/src/autofill/fido2/utils/webauthn-utils.ts @@ -32,7 +32,7 @@ export class WebauthnUtils { ), excludeCredentials: keyOptions.excludeCredentials?.map((credential) => ({ id: Fido2Utils.arrayToString(Fido2Utils.bufferSourceToUint8Array(credential.id)), - transports: credential.transports, + transports: Array.from(credential.transports ?? []), type: credential.type, })), extensions: {