Elffuss BraidHub

utopia-ia/elffuss-code public

Build palette and menu rows as text, not HTML

The palette, the "+" attach menu and the menu bar now build their rows
from text nodes through one helper, with no innerHTML, so a label is
always shown as the text it is. tests/palette_text.mjs checks the
palette, the attach menu and the menu bar.

(cherry picked from commit 194982e35aa576a3fe902b5a5032be72afe130a8)
Changeset67229659129e45135c8d5610b3bcd4e212cccabe3e8efaab421d39d412c2f1b9
AuthorKikoCis <kikocisneros@gmail.com> · 2026-10-11 16:30 +02:00
CommitterKikoCis <kikocisneros@gmail.com> · 2026-10-12 12:45 +02:00
Parentse43b74b24085

Browse the files at this changeset

2 files changed

added tests/palette_text.mjs +78 −0
@@ -0,0 +1,78 @@
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]) => l === 'Command Palette' && h === 'Cmd+Shift+P'), JSON.stringify(view?.slice(0, 4)));
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);
modified web/js/main.js +10 −7
@@ -2405,7 +2405,7 @@
24052405 if (it.sep) { const s = document.createElement('div'); s.className = 'sep'; s.textContent = it.sep; menu.appendChild(s); continue; }
24062406 const row = document.createElement('button');
24072407 row.className = 'menu-item';
2408- row.innerHTML = `<b>${it.label}</b>${it.hint ? `<span>${it.hint}</span>` : ''}`;
2408+ menuRow(row, it);
24092409 row.onclick = () => { menu.hidden = true; it.run(); };
24102410 row.onmouseenter = () => { menuSel = menuFilas().indexOf(row); pintaMenuSel(); };
24112411 menu.appendChild(row);
@@ -2440,6 +2440,13 @@
24402440 else if (e.key === 'ArrowUp') { e.preventDefault(); menuSel = (menuSel - 1 + filas.length) % filas.length; pintaMenuSel(); }
24412441 else if (e.key === 'Enter' || e.key === 'Tab') { e.preventDefault(); filas[Math.max(0, menuSel)].click(); }
24422442 });
2443+// Filas de menú y de paleta: SIEMPRE como texto. Las etiquetas llevan nombres
2444+// de fichero, mensajes de commit o títulos de comandos de extensiones, y un
2445+// nombre como «<img src=x onerror=…>.js» no puede convertirse en HTML.
2446+function menuRow(row, it) {
2447+ row.appendChild(el('b', null, it.label));
2448+ if (it.hint) row.appendChild(el('span', null, it.hint));
2449+}
24432450 document.addEventListener('click', e => {
24442451 if (!e.target.closest('#menu, #btn-slash, #btn-plus')) cierraMenu();
24452452 });
@@ -3088,9 +3095,7 @@
30883095 row.setAttribute('role', 'option');
30893096 row.setAttribute('aria-selected', i === 0 ? 'true' : 'false');
30903097 row.tabIndex = -1;
3091- row.innerHTML = '<span class="pi-name"></span><span class="pi-hint"></span>';
3092- row.querySelector('.pi-name').textContent = it.label;
3093- row.querySelector('.pi-hint').textContent = it.hint || '';
3098+ row.append(el('span', 'pi-name', it.label), el('span', 'pi-hint', it.hint || ''));
30943099 row.onmouseenter = () => { palSel = i; paintPalSel(); };
30953100 row.onclick = () => it.run();
30963101 list.appendChild(row);
@@ -3236,9 +3241,7 @@
32363241 row.type = 'button';
32373242 row.setAttribute('role', 'menuitem');
32383243 row.tabIndex = -1;
3239- row.innerHTML = `<b></b>${it.hint ? '<span></span>' : ''}`;
3240- row.querySelector('b').textContent = it.label;
3241- if (it.hint) row.querySelector('span').textContent = it.hint;
3244+ menuRow(row, it);
32423245 if (it.title) row.title = it.title;
32433246 if (it.disabled) { row.setAttribute('aria-disabled', 'true'); row.classList.add('disabled'); }
32443247 // stopPropagation: el clic no debe seguir hasta los «clic fuera» del