mirror of
https://github.com/bitwarden/clients.git
synced 2026-11-16 07:03:29 +00:00
[PM-40268] Unwrap fido2 transports for postMessage (#24397)
Signed-off-by: Ben Brooks <bbrooks@bitwarden.com>
This commit is contained in:
@@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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: {
|
||||
|
||||
Reference in New Issue
Block a user