[PM-40268] Unwrap fido2 transports for postMessage (#24397)

Signed-off-by: Ben Brooks <bbrooks@bitwarden.com>
This commit is contained in:
Ben Brooks
2026-11-12 09:45:26 -08:00
committed by GitHub
parent 1f8ab9c5a1
commit 186a8fe40d
2 changed files with 46 additions and 10 deletions

View File

@@ -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();
});
});
});

View File

@@ -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: {