Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
fix(webauthn): serialize virtual credentials in toJSON() (#43132)
Pavel Feldmana2ce3781eb7c
diff --git a/packages/injected/src/webAuthn.ts b/packages/injected/src/webAuthn.tsindex deb940ba32..3e5fcdd4ce 100644--- a/packages/injected/src/webAuthn.ts+++ b/packages/injected/src/webAuthn.ts@@ -41,6 +41,8 @@ export type CreateResponse = {id: string,clientDataJSON: string,attestationObject: string,+ authenticatorData: string,+ publicKey: string,} | { ok: false, name: string, message: string };export type GetResponse = {@@ -98,17 +100,13 @@ export function inject(globalThis: GlobalThis) {Object.defineProperty(target, k, { value: props[k], enumerable: true, configurable: true });}- function makeAttestationResponse(clientDataJSON: ArrayBuffer, attestationObject: ArrayBuffer) {+ function makeAttestationResponse(clientDataJSON: ArrayBuffer, attestationObject: ArrayBuffer, authenticatorData: ArrayBuffer, publicKey: ArrayBuffer) {const proto = AuthAttestationResponseCtor?.prototype || Object.prototype;const r = Object.create(proto);defineReadonly(r, { clientDataJSON, attestationObject });r.getTransports = () => ['internal'];- r.getAuthenticatorData = () => {- // Extract authData from attestationObject (CBOR: { fmt, attStmt, authData }).- // For simplicity, return the whole attestationObject — callers rarely use this in tests.- return attestationObject;- };- r.getPublicKey = () => null;+ r.getAuthenticatorData = () => authenticatorData;+ r.getPublicKey = () => publicKey;r.getPublicKeyAlgorithm = () => -7;return r;}@@ -120,7 +118,7 @@ export function inject(globalThis: GlobalThis) {return r;}- function makePublicKeyCredential(id: string, response: any) {+ function makePublicKeyCredential(id: string, response: any, responseToJSON: () => Record<string, any>) {const proto = PublicKeyCredentialCtor?.prototype || Object.prototype;const cred = Object.create(proto);defineReadonly(cred, {@@ -131,7 +129,15 @@ export function inject(globalThis: GlobalThis) {response,});cred.getClientExtensionResults = () => ({});- cred.toJSON = () => ({ id, rawId: id, type: 'public-key', response: {} });+ // https://w3c.github.io/webauthn/#dom-publickeycredential-tojson+ cred.toJSON = () => ({+ id,+ rawId: id,+ response: responseToJSON(),+ authenticatorAttachment: 'platform',+ clientExtensionResults: {},+ type: 'public-key',+ });return cred;}@@ -176,8 +182,20 @@ export function inject(globalThis: GlobalThis) {const result: CreateResponse = await binding(req);if (!result.ok)failure(result.name, result.message);- const resp = makeAttestationResponse(fromBase64Url(result.clientDataJSON), fromBase64Url(result.attestationObject));- return makePublicKeyCredential(result.id, resp);+ const resp = makeAttestationResponse(+ fromBase64Url(result.clientDataJSON),+ fromBase64Url(result.attestationObject),+ fromBase64Url(result.authenticatorData),+ fromBase64Url(result.publicKey),+ );+ return makePublicKeyCredential(result.id, resp, () => ({+ clientDataJSON: result.clientDataJSON,+ authenticatorData: result.authenticatorData,+ transports: resp.getTransports(),+ publicKey: result.publicKey,+ publicKeyAlgorithm: resp.getPublicKeyAlgorithm(),+ attestationObject: result.attestationObject,+ }));};globalThis.navigator.credentials.get = async function(options?: CredentialRequestOptions): Promise<Credential | null> {@@ -201,7 +219,12 @@ export function inject(globalThis: GlobalThis) {fromBase64Url(result.signature),result.userHandle ? fromBase64Url(result.userHandle) : null,);- return makePublicKeyCredential(result.id, resp);+ return makePublicKeyCredential(result.id, resp, () => ({+ clientDataJSON: result.clientDataJSON,+ authenticatorData: result.authenticatorData,+ signature: result.signature,+ ...(result.userHandle ? { userHandle: result.userHandle } : {}),+ }));};if (PublicKeyCredentialCtor) {diff --git a/packages/playwright-core/src/server/credentials.ts b/packages/playwright-core/src/server/credentials.tsindex 26ff3e521f..b129fbdf5f 100644--- a/packages/playwright-core/src/server/credentials.ts+++ b/packages/playwright-core/src/server/credentials.ts@@ -178,6 +178,8 @@ export class Credentials {id: credentialIdB64,clientDataJSON: bufToB64Url(clientDataJSON),attestationObject: bufToB64Url(attestationObject),+ authenticatorData: bufToB64Url(authData),+ publicKey,};}diff --git a/tests/library/browsercontext-webauthn.spec.ts b/tests/library/browsercontext-webauthn.spec.tsindex b43325e94b..af47308341 100644--- a/tests/library/browsercontext-webauthn.spec.ts+++ b/tests/library/browsercontext-webauthn.spec.ts@@ -273,3 +273,81 @@ it('should preserve signCount via the storageState option', async ({ contextFactawait legacyPage.goto(server.EMPTY_PAGE);expect(await assertAndGetSignCount(legacyPage, server.HOSTNAME)).toBe(1);});++it('should serialize credentials with toJSON', {+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43093' },+}, async ({ contextFactory, server }) => {+ const context = await contextFactory();+ await context.credentials.install();+ const page = await context.newPage();+ await page.goto(server.EMPTY_PAGE);++ const { registration, authentication, expected } = await page.evaluate(async ({ rpId }) => {+ const toB64Url = (buf: ArrayBuffer) => btoa(String.fromCharCode(...new Uint8Array(buf))).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');+ const created = await navigator.credentials.create({+ publicKey: {+ challenge: crypto.getRandomValues(new Uint8Array(32)),+ rp: { id: rpId, name: 'Test RP' },+ user: { id: new Uint8Array([1, 2, 3, 4]), name: 'u', displayName: 'User' },+ pubKeyCredParams: [{ type: 'public-key', alg: -7 }],+ authenticatorSelection: { residentKey: 'required' },+ },+ }) as PublicKeyCredential;+ const attestation = created.response as AuthenticatorAttestationResponse;+ const got = await navigator.credentials.get({+ publicKey: { challenge: crypto.getRandomValues(new Uint8Array(32)), rpId },+ }) as PublicKeyCredential;+ const assertion = got.response as AuthenticatorAssertionResponse;+ return {+ registration: JSON.parse(JSON.stringify(created)),+ authentication: JSON.parse(JSON.stringify(got)),+ expected: {+ id: created.id,+ attestation: {+ clientDataJSON: toB64Url(attestation.clientDataJSON),+ authenticatorData: toB64Url(attestation.getAuthenticatorData()),+ publicKey: toB64Url(attestation.getPublicKey()!),+ attestationObject: toB64Url(attestation.attestationObject),+ },+ assertion: {+ clientDataJSON: toB64Url(assertion.clientDataJSON),+ authenticatorData: toB64Url(assertion.authenticatorData),+ signature: toB64Url(assertion.signature),+ userHandle: toB64Url(assertion.userHandle!),+ },+ },+ };+ }, { rpId: server.HOSTNAME });++ expect(registration).toEqual({+ id: expected.id,+ rawId: expected.id,+ response: {+ ...expected.attestation,+ transports: ['internal'],+ publicKeyAlgorithm: -7,+ },+ authenticatorAttachment: 'platform',+ clientExtensionResults: {},+ type: 'public-key',+ });+ expect(authentication).toEqual({+ id: expected.id,+ rawId: expected.id,+ response: expected.assertion,+ authenticatorAttachment: 'platform',+ clientExtensionResults: {},+ type: 'public-key',+ });++ const [credential] = await context.credentials.get();+ expect(registration.response.publicKey).toBe(credential.publicKey);+ expect(authentication.response.userHandle).toBe(credential.userHandle);+ expect(JSON.parse(Buffer.from(registration.response.clientDataJSON, 'base64url').toString()).type).toBe('webauthn.create');+ expect(JSON.parse(Buffer.from(authentication.response.clientDataJSON, 'base64url').toString()).type).toBe('webauthn.get');++ // Registration authenticator data is embedded in the attestation object and carries attested credential data (AT flag).+ const authData = Buffer.from(registration.response.authenticatorData, 'base64url');+ expect(Buffer.from(registration.response.attestationObject, 'base64url').includes(authData)).toBe(true);+ expect(authData[32] & 0x40).toBe(0x40);+});