extension.spec.ts (20570B)
1 import { expect, test, type Page } from '@playwright/test'; 2 import { build } from 'vite'; 3 import { fileURLToPath } from 'node:url'; 4 import { createHash } from 'node:crypto'; 5 import { createStaticHarness } from '../integration/harness/static.ts'; 6 import type * as Identity from '../../src/lib/runtime/identity-session.ts'; 7 declare global { 8 interface Window { 9 hcp051: typeof Identity; 10 hcp051Session: Identity.IdentitySession; 11 hcp051Counts: { 12 keys: number; 13 signs: number; 14 encrypts: number; 15 decrypts: number; 16 }; 17 hcp051Key: string; 18 hcp051Release: () => void; 19 hcp051Other: Identity.IdentitySession; 20 hcp051Task: Promise<Identity.IdentitySnapshot>; 21 } 22 } 23 const owner = 24 '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'; 25 const peer = 'c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5'; 26 let server: Awaited<ReturnType<typeof createStaticHarness>>, bundle: string; 27 test.beforeAll(async () => { 28 server = await createStaticHarness(); 29 const source = fileURLToPath( 30 new URL('../../src/lib/runtime/identity-session.ts', import.meta.url) 31 ); 32 const result = await build({ 33 configFile: false, 34 logLevel: 'silent', 35 build: { 36 write: false, 37 minify: false, 38 lib: { entry: source, name: 'hcp051', formats: ['iife'] } 39 } 40 }); 41 const output = Array.isArray(result) ? result[0] : result; 42 if (!('output' in output)) throw new Error('missing bundle'); 43 const chunks = output.output.filter((item) => item.type === 'chunk'); 44 expect(chunks).toHaveLength(1); 45 const moduleIds = Object.keys(chunks[0].modules); 46 expect( 47 moduleIds.filter((id) => 48 /(?:^|\/)node_modules\/(?:@capacitor\/core|nostr-signer-capacitor-plugin)(?:\/|$)|\/(?:private-key-signer|password-signer|nostr-connect-signer)\.[cm]?[jt]s(?:$|\?)/.test( 49 id 50 ) 51 ) 52 ).toEqual([]); 53 expect(moduleIds.some((id) => id.includes('extension-signer'))).toBe(true); 54 bundle = chunks[0].code; 55 console.log( 56 JSON.stringify({ 57 moduleIds, 58 fixture: 'HCP051_ACTUAL_EXTENSION_SIGNER_CONTROLLED_PROVIDER', 59 bundleSha256: createHash('sha256').update(bundle).digest('hex'), 60 claim: 61 'real Chromium and installed SDK; controlled provider is not named real extension qualification' 62 }) 63 ); 64 }); 65 test.afterAll(async () => server.close()); 66 async function load( 67 page: Page, 68 mode: 69 | 'signing' 70 | 'messaging' 71 | 'missing' 72 | 'denied' 73 | 'invalid' 74 | 'pending' 75 | 'connected' = 'signing' 76 ) { 77 await page.goto(server.url + '/search'); 78 await page.evaluate( 79 ({ owner, mode }) => { 80 window.hcp051Counts = { keys: 0, signs: 0, encrypts: 0, decrypts: 0 }; 81 window.hcp051Key = owner; 82 const provider: { 83 getPublicKey: () => Promise<string>; 84 signEvent?: () => Promise<never>; 85 nip44?: { 86 encrypt: (key: string, body: string) => Promise<string>; 87 decrypt: (key: string, body: string) => Promise<string>; 88 }; 89 } = { 90 async getPublicKey() { 91 window.hcp051Counts.keys++; 92 if (mode === 'denied') throw new Error('controlled provider refusal'); 93 if (mode === 'invalid') return 'f'.repeat(64); 94 if (mode === 'pending') 95 await new Promise<void>((resolve) => { 96 window.hcp051Release = resolve; 97 }); 98 return window.hcp051Key; 99 } 100 }; 101 if (mode !== 'connected') 102 provider.signEvent = () => { 103 window.hcp051Counts.signs++; 104 return Promise.reject(new Error('Connect must never sign')); 105 }; 106 if (mode === 'messaging') 107 provider.nip44 = { 108 encrypt(key, body) { 109 window.hcp051Counts.encrypts++; 110 if (key !== window.hcp051Key) 111 return Promise.reject(new Error('wrong self key')); 112 return Promise.resolve('controlled:' + body); 113 }, 114 decrypt(key, body) { 115 window.hcp051Counts.decrypts++; 116 if (key !== window.hcp051Key) 117 return Promise.reject(new Error('wrong self key')); 118 return Promise.resolve(body.replace(/^controlled:/, '')); 119 } 120 }; 121 Object.defineProperty(window, 'nostr', { 122 configurable: true, 123 value: mode === 'missing' ? undefined : provider 124 }); 125 }, 126 { owner, mode } 127 ); 128 await page.addScriptTag({ content: bundle }); 129 await page.evaluate(() => { 130 window.hcp051Session = window.hcp051.createIdentitySession(); 131 }); 132 } 133 async function connect(page: Page) { 134 return page.evaluate(() => 135 window.hcp051.connectIdentity(window.hcp051Session) 136 ); 137 } 138 test('load and construction are inert; explicit Connect obtains key once without signing or probing', async ({ 139 page 140 }) => { 141 await load(page); 142 expect(await page.evaluate(() => window.hcp051Counts)).toEqual({ 143 keys: 0, 144 signs: 0, 145 encrypts: 0, 146 decrypts: 0 147 }); 148 expect(await connect(page)).toMatchObject({ 149 state: 'signing_only', 150 publicKey: owner 151 }); 152 expect(await page.evaluate(() => window.hcp051Counts)).toEqual({ 153 keys: 1, 154 signs: 0, 155 encrypts: 0, 156 decrypts: 0 157 }); 158 }); 159 for (const [mode, reason] of [ 160 ['missing', 'missing'], 161 ['denied', 'refused'], 162 ['invalid', 'invalid_key'] 163 ] as const) 164 test(`${mode} provider leaves guest with safe finite diagnostics`, async ({ 165 page 166 }) => { 167 await load(page, mode); 168 expect(await connect(page)).toEqual({ state: 'guest', reason }); 169 expect( 170 await page.evaluate( 171 () => window.hcp051Counts.signs + window.hcp051Counts.encrypts 172 ) 173 ).toBe(0); 174 }); 175 test('key-only connected state does not promise signing or messaging', async ({ 176 page 177 }) => { 178 await load(page, 'connected'); 179 expect(await connect(page)).toMatchObject({ 180 state: 'connected', 181 publicKey: owner 182 }); 183 }); 184 test('fresh SDK instance detects changed provider key despite the original signer cache', async ({ 185 page 186 }) => { 187 await load(page); 188 await connect(page); 189 await page.evaluate((peer) => { 190 window.hcp051Key = peer; 191 }, peer); 192 expect( 193 await page.evaluate(() => 194 window.hcp051.recheckIdentityOwner(window.hcp051Session) 195 ) 196 ).toEqual({ state: 'guest', reason: 'changed_key' }); 197 expect(await page.evaluate(() => window.hcp051Counts.keys)).toBe(2); 198 }); 199 test('messaging method presence does not probe; explicit reviewed self-copy is required', async ({ 200 page 201 }) => { 202 await load(page, 'messaging'); 203 expect(await connect(page)).toMatchObject({ state: 'signing_only' }); 204 expect( 205 await page.evaluate(() => 206 window.hcp051.probeIdentityMessaging(window.hcp051Session, 'unreviewed') 207 ) 208 ).toMatchObject({ state: 'signing_only' }); 209 expect(await page.evaluate(() => window.hcp051Counts.encrypts)).toBe(0); 210 expect( 211 await page.evaluate(() => 212 window.hcp051.probeIdentityMessaging( 213 window.hcp051Session, 214 'reviewed_self_copy' 215 ) 216 ) 217 ).toMatchObject({ state: 'messaging_capable', publicKey: owner }); 218 expect(await page.evaluate(() => window.hcp051Counts)).toEqual({ 219 keys: 3, 220 signs: 0, 221 encrypts: 1, 222 decrypts: 1 223 }); 224 }); 225 test('late pending connection cannot restore a disconnected owner and original slot is retained', async ({ 226 page 227 }) => { 228 await load(page, 'pending'); 229 const waiting = connect(page); 230 await expect 231 .poll(() => page.evaluate(() => window.hcp051Counts.keys)) 232 .toBe(1); 233 await page.evaluate(() => 234 window.hcp051.disconnectIdentity(window.hcp051Session) 235 ); 236 expect(await connect(page)).toEqual({ 237 state: 'guest', 238 reason: 'disconnected', 239 admission: 'busy' 240 }); 241 expect(await page.evaluate(() => window.hcp051Counts.keys)).toBe(1); 242 await page.evaluate(() => window.hcp051Release()); 243 expect(await waiting).toEqual({ state: 'guest', reason: 'disconnected' }); 244 }); 245 test('missing NIP44 and refusal keep signing-only; no legacy encryption or automatic action', async ({ 246 page 247 }) => { 248 await load(page); 249 await connect(page); 250 expect( 251 await page.evaluate(() => 252 window.hcp051.probeIdentityMessaging( 253 window.hcp051Session, 254 'reviewed_self_copy' 255 ) 256 ) 257 ).toMatchObject({ state: 'signing_only' }); 258 expect(await page.evaluate(() => window.hcp051Counts.encrypts)).toBe(0); 259 }); 260 261 test('reviewed encryption refusal retains public signing candidate with safe messaging refusal state', async ({ 262 page 263 }) => { 264 await load(page, 'messaging'); 265 await connect(page); 266 await page.evaluate(() => { 267 const provider = ( 268 window as unknown as { 269 nostr: { nip44: { encrypt: () => Promise<never> } }; 270 } 271 ).nostr; 272 provider.nip44.encrypt = () => { 273 window.hcp051Counts.encrypts++; 274 return Promise.reject( 275 new Error('controlled private refusal text must not be exposed') 276 ); 277 }; 278 }); 279 const state = await page.evaluate(() => 280 window.hcp051.probeIdentityMessaging( 281 window.hcp051Session, 282 'reviewed_self_copy' 283 ) 284 ); 285 expect(state).toMatchObject({ 286 state: 'signing_only', 287 publicKey: owner, 288 messaging: 'refused' 289 }); 290 expect(JSON.stringify(state)).not.toContain('controlled private'); 291 expect(await page.evaluate(() => window.hcp051Counts.decrypts)).toBe(0); 292 }); 293 test('disconnect during pending encryption keeps slot and prevents later decrypt or capability resurrection', async ({ 294 page 295 }) => { 296 await load(page, 'messaging'); 297 await connect(page); 298 await page.evaluate(() => { 299 const provider = ( 300 window as unknown as { 301 nostr: { 302 nip44: { encrypt: (key: string, body: string) => Promise<string> }; 303 }; 304 } 305 ).nostr; 306 provider.nip44.encrypt = (_key, body) => { 307 window.hcp051Counts.encrypts++; 308 return new Promise((resolve) => { 309 window.hcp051Release = () => resolve('controlled:' + body); 310 }); 311 }; 312 }); 313 const pending = page.evaluate(() => 314 window.hcp051.probeIdentityMessaging( 315 window.hcp051Session, 316 'reviewed_self_copy' 317 ) 318 ); 319 await expect 320 .poll(() => page.evaluate(() => window.hcp051Counts.encrypts)) 321 .toBe(1); 322 await page.evaluate(() => 323 window.hcp051.disconnectIdentity(window.hcp051Session) 324 ); 325 expect(await connect(page)).toEqual({ 326 state: 'guest', 327 reason: 'disconnected', 328 admission: 'busy' 329 }); 330 await page.evaluate(() => window.hcp051Release()); 331 expect(await pending).toEqual({ state: 'guest', reason: 'disconnected' }); 332 expect(await page.evaluate(() => window.hcp051Counts.decrypts)).toBe(0); 333 }); 334 test('repeat Connect detects a changed owner instead of silently adopting it', async ({ 335 page 336 }) => { 337 await load(page); 338 await connect(page); 339 await page.evaluate((peer) => { 340 window.hcp051Key = peer; 341 }, peer); 342 expect(await connect(page)).toEqual({ 343 state: 'guest', 344 reason: 'changed_key' 345 }); 346 }); 347 348 for (const property of ['nostr', 'getPublicKey', 'signEvent'] as const) 349 test(`reentrant ${property} inspection disconnects before any new key request`, async ({ 350 page 351 }) => { 352 await load(page); 353 await page.evaluate((property) => { 354 const provider = window.nostr as { 355 getPublicKey: () => Promise<string>; 356 signEvent: () => Promise<never>; 357 }; 358 const value = property === 'nostr' ? provider : provider[property]; 359 Object.defineProperty( 360 property === 'nostr' ? window : provider, 361 property, 362 { 363 configurable: true, 364 get() { 365 window.hcp051.disconnectIdentity(window.hcp051Session); 366 return value; 367 } 368 } 369 ); 370 }, property); 371 expect(await connect(page)).toEqual({ 372 state: 'guest', 373 reason: 'disconnected' 374 }); 375 expect(await page.evaluate(() => window.hcp051Counts.keys)).toBe(0); 376 }); 377 378 for (const property of ['nip44', 'encrypt', 'decrypt'] as const) 379 test(`reentrant ${property} inspection disconnects before any new encryption`, async ({ 380 page 381 }) => { 382 await load(page, 'messaging'); 383 await connect(page); 384 await page.evaluate((property) => { 385 const provider = window.nostr as { 386 nip44: { 387 encrypt: (key: string, body: string) => Promise<string>; 388 decrypt: (key: string, body: string) => Promise<string>; 389 }; 390 }; 391 const cipher = provider.nip44; 392 const value = property === 'nip44' ? cipher : cipher[property]; 393 Object.defineProperty( 394 property === 'nip44' ? provider : cipher, 395 property, 396 { 397 configurable: true, 398 get() { 399 window.hcp051.disconnectIdentity(window.hcp051Session); 400 return value; 401 } 402 } 403 ); 404 }, property); 405 expect( 406 await page.evaluate(() => 407 window.hcp051.probeIdentityMessaging( 408 window.hcp051Session, 409 'reviewed_self_copy' 410 ) 411 ) 412 ).toEqual({ state: 'guest', reason: 'disconnected' }); 413 expect(await page.evaluate(() => window.hcp051Counts.encrypts)).toBe(0); 414 expect(await page.evaluate(() => window.hcp051Counts.decrypts)).toBe(0); 415 }); 416 417 test('Connect refreshes signing shape after awaited provider key resolution', async ({ 418 page 419 }) => { 420 await load(page, 'pending'); 421 const waiting = connect(page); 422 await expect 423 .poll(() => page.evaluate(() => window.hcp051Counts.keys)) 424 .toBe(1); 425 await page.evaluate(() => { 426 const provider = window.nostr as { signEvent?: unknown }; 427 delete provider.signEvent; 428 window.hcp051Release(); 429 }); 430 expect(await waiting).toMatchObject({ state: 'connected', publicKey: owner }); 431 }); 432 433 test('reentrant recheck shape inspection cannot restore a disconnected capability', async ({ 434 page 435 }) => { 436 await load(page); 437 await connect(page); 438 await page.evaluate(() => { 439 const provider = window.nostr as { signEvent: () => Promise<never> }; 440 const value = provider.signEvent; 441 Object.defineProperty(provider, 'signEvent', { 442 get() { 443 window.hcp051.disconnectIdentity(window.hcp051Session); 444 return value; 445 } 446 }); 447 }); 448 expect( 449 await page.evaluate(() => 450 window.hcp051.recheckIdentityOwner(window.hcp051Session) 451 ) 452 ).toEqual({ state: 'guest', reason: 'disconnected' }); 453 }); 454 455 for (const property of ['nip44', 'encrypt', 'decrypt'] as const) 456 test(`same-key recheck downgrades removed ${property} without automatic reprobe`, async ({ 457 page 458 }) => { 459 await load(page, 'messaging'); 460 await connect(page); 461 expect( 462 await page.evaluate(() => 463 window.hcp051.probeIdentityMessaging( 464 window.hcp051Session, 465 'reviewed_self_copy' 466 ) 467 ) 468 ).toMatchObject({ state: 'messaging_capable' }); 469 await page.evaluate((property) => { 470 const provider = window.nostr as { 471 nip44?: { encrypt?: unknown; decrypt?: unknown }; 472 }; 473 if (property === 'nip44') delete provider.nip44; 474 else if (provider.nip44) delete provider.nip44[property]; 475 }, property); 476 expect( 477 await page.evaluate(() => 478 window.hcp051.recheckIdentityOwner(window.hcp051Session) 479 ) 480 ).toMatchObject({ 481 state: 'signing_only', 482 publicKey: owner, 483 messaging: 'unsupported' 484 }); 485 expect(await page.evaluate(() => window.hcp051Counts.encrypts)).toBe(1); 486 expect(await page.evaluate(() => window.hcp051Counts.decrypts)).toBe(1); 487 }); 488 489 test('disconnect inside an already admitted SDK key call rejects its result without another effect', async ({ 490 page 491 }) => { 492 await load(page); 493 await page.evaluate(() => { 494 const provider = window.nostr as { getPublicKey: () => Promise<string> }; 495 provider.getPublicKey = () => { 496 window.hcp051Counts.keys++; 497 window.hcp051.disconnectIdentity(window.hcp051Session); 498 return Promise.resolve(window.hcp051Key); 499 }; 500 }); 501 expect(await connect(page)).toEqual({ 502 state: 'guest', 503 reason: 'disconnected' 504 }); 505 expect(await page.evaluate(() => window.hcp051Counts)).toEqual({ 506 keys: 1, 507 signs: 0, 508 encrypts: 0, 509 decrypts: 0 510 }); 511 }); 512 513 for (const property of ['nip44', 'signEvent'] as const) 514 test(`final probe key response removes ${property} before capability publication`, async ({ 515 page 516 }) => { 517 await load(page, 'messaging'); 518 await connect(page); 519 await page.evaluate((property) => { 520 const provider = window.nostr as { 521 getPublicKey: () => Promise<string>; 522 nip44?: unknown; 523 signEvent?: unknown; 524 }; 525 provider.getPublicKey = () => { 526 window.hcp051Counts.keys++; 527 if (window.hcp051Counts.keys === 3) delete provider[property]; 528 return Promise.resolve(window.hcp051Key); 529 }; 530 }, property); 531 const result = await page.evaluate(() => 532 window.hcp051.probeIdentityMessaging( 533 window.hcp051Session, 534 'reviewed_self_copy' 535 ) 536 ); 537 expect(result).toMatchObject( 538 property === 'nip44' 539 ? { state: 'signing_only', messaging: 'unsupported' } 540 : { state: 'connected' } 541 ); 542 expect(await page.evaluate(() => window.hcp051Counts)).toEqual({ 543 keys: 3, 544 signs: 0, 545 encrypts: 1, 546 decrypts: 1 547 }); 548 }); 549 550 // HCP052: disconnected and busy callers cannot release or misreport the slot. 551 test('cross-session busy Connect retains slot and exposes explicit retry after settlement', async ({ 552 page 553 }) => { 554 await load(page, 'pending'); 555 await page.evaluate(() => { 556 window.hcp051Task = window.hcp051.connectIdentity(window.hcp051Session); 557 }); 558 await expect 559 .poll(() => page.evaluate(() => window.hcp051Counts.keys)) 560 .toBe(1); 561 const busy = await page.evaluate(async () => { 562 window.hcp051.disconnectIdentity(window.hcp051Session); 563 window.hcp051Other = window.hcp051.createIdentitySession(); 564 return await window.hcp051.connectIdentity(window.hcp051Other); 565 }); 566 expect(busy).toEqual({ 567 state: 'guest', 568 reason: 'disconnected', 569 admission: 'busy' 570 }); 571 expect( 572 await page.evaluate(() => 573 window.hcp051.identitySessionSnapshot(window.hcp051Other) 574 ) 575 ).toEqual({ state: 'guest', reason: 'disconnected' }); 576 expect(await page.evaluate(() => window.hcp051Counts.keys)).toBe(1); 577 await page.evaluate(() => window.hcp051Release()); 578 expect(await page.evaluate(() => window.hcp051Task)).toEqual({ 579 state: 'guest', 580 reason: 'disconnected' 581 }); 582 expect(await page.evaluate(() => window.hcp051Counts.keys)).toBe(1); 583 await page.evaluate(() => { 584 const provider = window.nostr as { getPublicKey: () => Promise<string> }; 585 provider.getPublicKey = () => { 586 window.hcp051Counts.keys++; 587 return Promise.resolve(window.hcp051Key); 588 }; 589 }); 590 expect( 591 await page.evaluate(() => window.hcp051.connectIdentity(window.hcp051Other)) 592 ).toMatchObject({ state: 'signing_only', publicKey: owner }); 593 expect(await page.evaluate(() => window.hcp051Counts.keys)).toBe(2); 594 }); 595 test('busy owner recheck and probe preserve capabilities without claiming a fresh owner check', async ({ 596 page 597 }) => { 598 await load(page, 'messaging'); 599 await connect(page); 600 await page.evaluate(async () => { 601 window.hcp051Other = window.hcp051.createIdentitySession(); 602 await window.hcp051.connectIdentity(window.hcp051Other); 603 const provider = window.nostr as { getPublicKey: () => Promise<string> }; 604 provider.getPublicKey = async () => { 605 window.hcp051Counts.keys++; 606 await new Promise<void>((resolve) => { 607 window.hcp051Release = resolve; 608 }); 609 return window.hcp051Key; 610 }; 611 window.hcp051Task = window.hcp051.recheckIdentityOwner( 612 window.hcp051Session 613 ); 614 }); 615 await expect 616 .poll(() => page.evaluate(() => window.hcp051Counts.keys)) 617 .toBe(3); 618 expect( 619 await page.evaluate(() => 620 window.hcp051.recheckIdentityOwner(window.hcp051Other) 621 ) 622 ).toMatchObject({ 623 state: 'signing_only', 624 publicKey: owner, 625 admission: 'busy' 626 }); 627 expect( 628 await page.evaluate(() => 629 window.hcp051.probeIdentityMessaging( 630 window.hcp051Other, 631 'reviewed_self_copy' 632 ) 633 ) 634 ).toMatchObject({ 635 state: 'signing_only', 636 publicKey: owner, 637 admission: 'busy' 638 }); 639 expect(await page.evaluate(() => window.hcp051Counts)).toEqual({ 640 keys: 3, 641 signs: 0, 642 encrypts: 0, 643 decrypts: 0 644 }); 645 expect( 646 await page.evaluate(() => 647 window.hcp051.identitySessionSnapshot(window.hcp051Other) 648 ) 649 ).toEqual({ 650 state: 'signing_only', 651 publicKey: owner, 652 messaging: 'not_probed' 653 }); 654 await page.evaluate(() => window.hcp051Release()); 655 await page.evaluate(() => window.hcp051Task); 656 });