| 1 | // Palette and menus render their rows as TEXT: a file named like an HTML tag |
| 2 | // shows up literally and never becomes an element (no image, no handler). |
| 3 | // It held before level 2 too: file names reach the palette (Cmd+P), the "+" |
| 4 | // attach menu and the menu bar. |
| 5 | // |
| 6 | // node tests/palette_text.mjs (starts its own server: tests/lib/x2-server.mjs) |
| 7 | import { chromium } from 'playwright'; |
| 8 | import { startServer } from './lib/x2-server.mjs'; |
| 9 | |
| 10 | let fails = 0; |
| 11 | const ok = (name, c, extra = '') => { console.log((c ? '✅' : '❌') + ' ' + name + (extra ? ' — ' + extra : '')); if (!c) fails++; }; |
| 12 | const sleep = ms => new Promise(r => setTimeout(r, ms)); |
| 13 | async function until(fn, { timeout = 15000, every = 150 } = {}) { |
| 14 | const end = Date.now() + timeout; |
| 15 | let v; |
| 16 | while (Date.now() < end) { try { v = await fn(); if (v) return v; } catch { /* retry */ } await sleep(every); } |
| 17 | return v; |
| 18 | } |
| 19 | |
| 20 | const HOSTILE = '<img src=x onerror="window.__xss=1">.js'; |
| 21 | const srv = await startServer(); |
| 22 | const browser = await chromium.launch(); |
| 23 | const ctx = await browser.newContext(); |
| 24 | await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch { /* no storage */ } }); |
| 25 | const page = await ctx.newPage(); |
| 26 | const errors = []; |
| 27 | page.on('pageerror', e => { if (e.message !== 'Canceled') errors.push(e.message); }); |
| 28 | await page.goto(srv.codeOrigin + '/', { waitUntil: 'domcontentloaded' }); |
| 29 | const created = await page.evaluate(async name => { |
| 30 | const o = await navigator.storage.getDirectory(); |
| 31 | try { |
| 32 | const f = await o.getFileHandle(name, { create: true }); |
| 33 | const s = await f.createWritable(); await s.write('x'); await s.close(); |
| 34 | return true; |
| 35 | } catch (e) { return String(e && e.message || e); } |
| 36 | }, HOSTILE); |
| 37 | ok('the browser accepts the hostile file name in the test project', created === true, String(created)); |
| 38 | |
| 39 | await page.goto(srv.codeOrigin + '/?test-opfs', { waitUntil: 'domcontentloaded' }); |
| 40 | await page.waitForFunction(() => !!window.monaco?.editor?.getEditors?.().length, null, { timeout: 30000 }); |
| 41 | |
| 42 | // palette (files) |
| 43 | await page.evaluate(() => document.dispatchEvent(new KeyboardEvent('keydown', { key: 'p', ctrlKey: true, bubbles: true }))); |
| 44 | const pal = await until(() => page.evaluate(() => { |
| 45 | const list = document.getElementById('pal-list'); |
| 46 | const rows = [...list.querySelectorAll('.pal-item')]; |
| 47 | return rows.length ? { texts: rows.map(r => r.querySelector('.pi-name')?.textContent), imgs: list.querySelectorAll('img').length, spans: rows.every(r => r.children.length === 2) } : null; |
| 48 | })); |
| 49 | ok('palette: the name appears as text', !!pal && pal.texts.includes(HOSTILE), JSON.stringify(pal?.texts)); |
| 50 | ok('palette: no element was created from it', !!pal && pal.imgs === 0 && pal.spans); |
| 51 | await page.keyboard.press('Escape'); |
| 52 | |
| 53 | // "+" attach menu (openMenu) |
| 54 | await page.evaluate(() => document.getElementById('btn-plus').click()); |
| 55 | const plus = await until(() => page.evaluate(() => { |
| 56 | const m = document.getElementById('menu'); |
| 57 | const rows = [...m.querySelectorAll('.menu-item')]; |
| 58 | return rows.length ? { texts: rows.map(r => r.querySelector('b')?.textContent), imgs: m.querySelectorAll('img').length } : null; |
| 59 | })); |
| 60 | ok('attach menu: the name appears as text, with no element', !!plus && plus.texts.includes('@' + HOSTILE) && plus.imgs === 0, JSON.stringify(plus)); |
| 61 | |
| 62 | // menu bar (openMenuAt): same row builder; check one menu still looks the same |
| 63 | await page.evaluate(() => document.querySelector('#menubar button[data-menu="view"]').click()); |
| 64 | const view = await until(() => page.evaluate(() => { |
| 65 | const m = document.getElementById('topmenu'); |
| 66 | const rows = [...m.querySelectorAll('.menu-item')]; |
| 67 | return rows.length ? rows.map(r => [r.querySelector('b')?.textContent, r.querySelector('span')?.textContent ?? null]) : null; |
| 68 | })); |
| 69 | ok('menu bar rows keep their label and hint', !!view && view.some(([l, h]) => /^Command palette$/i.test(l || '') && /^(⌘⇧P|Ctrl\+Shift\+P|Cmd\+Shift\+P)$/.test(h || '')), JSON.stringify(view?.slice(0, 6))); |
| 70 | |
| 71 | await sleep(300); |
| 72 | ok('no handler from a file name ever ran', await page.evaluate(() => window.__xss === undefined)); |
| 73 | ok('no page errors', errors.length === 0, errors.slice(0, 3).join(' | ')); |
| 74 | |
| 75 | await browser.close(); |
| 76 | await srv.close(); |
| 77 | console.log(fails ? `\n❌ ${fails} failed` : '\n✅ all passed'); |
| 78 | process.exit(fails ? 1 : 0); |