Console: the cycle page no longer builds a handler from the address bar

viewCycleDetail took the id from the hash and, for an id it did not know,
drew a made-up open cycle with a Close button whose onclick carried that
id as '${esc(id)}'. esc() is no protection inside a quoted JS string in an
attribute: the HTML parser turns ' back into ' before the handler is
compiled. A link to #/cycles/<text> therefore ran that text as script on
one click, with the reader's session, on the platform's one origin.

- An unknown cycle is "Review cycle not found." (after one re-read of the
  list, so a cycle created since this tab loaded still opens), like a firm
  or a matter.
- The page uses the server's c.id from then on, also for the snapshots call.
- jsArg() (esc(JSON.stringify(v))) for every inline handler argument: the
  cycle's Close button and the two Edit buttons that used the same shape.
- test_frontend_xss_tripwires pins the helper, the absence of '${esc(' in a
  handler and of the fallback cycle. CLAUDE.md states the rule.

Checked without a browser: the console script parses, and the new asserts
hold against the file. pytest not run here (no dredd-core install).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Piotr Chojnacki
2026-11-14 22:24:34 +01:00
co-authored by Claude Fable 5.2
parent c6d04b1ee5
commit 3943e8a716
3 changed files with 33 additions and 6 deletions
+5
View File
@@ -311,6 +311,11 @@ Backend:
Frontend (`frontend/index.html`) — keep all of these when editing:
- **`esc()`** HTML-escapes every dynamic value interpolated into markup. Use it everywhere.
- **`jsArg()`** for a value passed as an argument of an inline handler (`onclick="fn(${jsArg(v)})"`),
never `'${esc(v)}'`: the HTML parser turns `&#39;` back into `'` before the handler is compiled,
so `esc()` inside a quoted JS string protects nothing. And a page is built from the record the
server returned, never from the id in the address bar: an unknown id is "not found".
`test_frontend_xss_tripwires` pins both.
- The bearer **token is kept in memory only** (`state.token`) — never localStorage/sessionStorage.
- **`sanitizeBase()`** restricts the API base (read from `localStorage.cs_base2`) to a same-origin
path, the same origin, or localhost — so a poisoned base can't exfiltrate the token. One leading
+9
View File
@@ -167,6 +167,15 @@ def test_frontend_xss_tripwires():
# dataTable() must escape cell values by default (raw:true is the explicit opt-out,
# used only by columns whose f() embeds its own esc()).
assert "c.raw?c.f(r):esc(c.f(r)??'')" in html
# An argument of an inline handler goes through jsArg(), never '${esc(...)}': the HTML parser
# turns &#39; back into ' before the handler is compiled, so esc() inside a quoted JS string
# protects nothing (the cycle page's Close button ran whatever the address bar carried).
assert "const jsArg" in html, "jsArg() helper must exist"
assert "('${esc(" not in html, "an inline handler argument must use jsArg()"
# ...and that page is built from the cycle the server returned, never from the address: an
# unknown id is "not found", not a made-up open cycle with a button.
assert "{name:'Cycle',status:'open'}" not in html
assert "closeCycle(${jsArg(c.id)})" in html
def test_response_item_numeric_value_is_bounded():
+19 -6
View File
@@ -385,6 +385,11 @@ table.dt .sub{display:block;color:var(--text-muted);font-size:12.5px;font-weight
const $ = (id) => document.getElementById(id);
const esc = (v) => String(v ?? '').replace(/[&<>"']/g, (ch) =>
({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[ch]));
// A value as a JS argument inside an inline handler (onclick="fn(…)"). esc() alone is NOT enough
// there: the HTML parser turns &#39; back into ' before the handler is compiled, so a value that
// carries a quote ends the string and the rest of it runs as script. JSON.stringify makes the value
// one JS string literal; esc() then only has to protect the attribute around it.
const jsArg = (v) => esc(JSON.stringify(String(v ?? '')));
// The API base (carrying the Bearer token) is persisted in localStorage, so a poisoned
// cs_base2 could point requests — and the token — at an attacker origin. Accept only a
// same-origin path, the same origin, or a local dev backend; anything else falls back.
@@ -764,7 +769,7 @@ async function viewFirmDetail(C,id){
C.innerHTML=`<div class="page">
<div class="kicker">Firm</div>
<div class="page-head"><div><h1>${esc(f.name)}</h1>${f.legal_name?`<div class="sub">${esc(f.legal_name)}</div>`:''}</div>
<div class="page-actions">${badge(f.status)} ${btn('Edit',{cls:'ghost',onclick:`editFirmById('${esc(f.id)}')`})}</div></div>
<div class="page-actions">${badge(f.status)} ${btn('Edit',{cls:'ghost',onclick:`editFirmById(${jsArg(f.id)})`})}</div></div>
<div class="fieldcards">
${card('Website',site?`<a href="${esc(site)}" target="_blank" rel="noopener">${esc(f.website)}</a>`:esc(f.website||'—'))}
${card('Email',esc(f.email||'—'))}
@@ -791,7 +796,7 @@ async function viewMatterDetail(C,id){
C.innerHTML=`<div class="page">
<div class="kicker">Matter</div>
<div class="page-head"><div><h1>${esc(m.reference)}</h1><div class="sub">${esc(m.title)}</div></div>
<div class="page-actions">${badge(m.status)} ${btn('Edit',{cls:'ghost',onclick:`editMatterById('${esc(m.id)}')`})}</div></div>
<div class="page-actions">${badge(m.status)} ${btn('Edit',{cls:'ghost',onclick:`editMatterById(${jsArg(m.id)})`})}</div></div>
<div class="fieldcards">
${card('Firm',`<a href="#/firms/${esc(m.firm_id)}">${esc(firmName(m.firm_id))}</a>`)}
${card('Type',esc(m.matter_type||'—'))}
@@ -873,15 +878,23 @@ function viewDictionaries(C){
// ── cycle detail + snapshots ──
async function viewCycleDetail(C,id){
const ep=renderEpoch;
const c=(cache.cycles||[]).find(x=>x.id===id)||{name:'Cycle',status:'open'};
// `id` is the address bar's, i.e. whatever a link put there. Nothing below is built from it: the
// cycle is looked up (once more from the server if this tab's list is stale), an unknown one is
// "not found" like a firm or a matter, and the page uses the server's own c.id from then on. The
// old fallback drew a made-up open cycle with a Close button carrying the raw id.
let c=(cache.cycles||[]).find(x=>x.id===id);
if(!c){ try{ cache.cycles=await api('GET','/review-cycles'); }catch{ /* keep the list we have */ }
if(ep!==renderEpoch) return;
c=(cache.cycles||[]).find(x=>x.id===id); }
if(!c){ C.innerHTML='<div class="page"><div class="empty">Review cycle not found.</div></div>'; return; }
setCrumb(['Dashboard','Review cycles',c.name||'Cycle']);
C.innerHTML=`<div class="page">
<div class="kicker">Review cycle</div>
<div class="page-head"><div><h1>${esc(c.name||'Cycle')}</h1><div class="sub">${esc(c.start_date||'')} → ${esc(c.end_date||'')}</div>
<div class="sub" style="margin-top:2px"><span>Firms with fewer than</span> <b>${esc(c.min_evaluations||0)}</b> <span>submitted evaluations stay provisional.</span></div></div>
<div class="page-actions">${badge(c.status)} ${c.status==='open'?btn('Close cycle',{cls:'primary',onclick:`closeCycle('${esc(id)}')`}):''}</div></div>
<div class="page-actions">${badge(c.status)} ${c.status==='open'?btn('Close cycle',{cls:'primary',onclick:`closeCycle(${jsArg(c.id)})`}):''}</div></div>
<div class="card"><div class="card-head"><h2>Firm rankings</h2></div><div id="snaps" class="card-body"><div class="empty">Loading snapshots…</div></div></div></div>`;
try{ const snaps=await api('GET',`/review-cycles/${id}/snapshots`); if(ep!==renderEpoch) return; renderSnaps(snaps); }
try{ const snaps=await api('GET',`/review-cycles/${encodeURIComponent(c.id)}/snapshots`); if(ep!==renderEpoch) return; renderSnaps(snaps); }
catch{ if(ep!==renderEpoch) return; $('snaps').innerHTML='<div class="empty">No snapshots — close the cycle to generate rankings.</div>'; }
}
function renderSnaps(snaps){
@@ -1227,7 +1240,7 @@ const I18N = { pl: {
// questionnaire builder (counsel review 2026-07-02)
'Edit questionnaire':'Edytuj kwestionariusz','Save changes':'Zapisz zmiany','Questions & weights':'Pytania i wagi','Tick the questions to include and give each a weight — weights must sum to exactly 1.0.':'Zaznacz pytania do uwzględnienia i nadaj każdemu wagę — suma wag musi wynosić dokładnie 1,0.','Weights sum:':'Suma wag:','Weight':'Waga','No questions yet — create questions first.':'Brak pytań — najpierw utwórz pytania.','Question weights must sum to 1.0':'Wagi pytań muszą sumować się do 1,0','Questionnaire saved':'Kwestionariusz zapisany','Questionnaire not found.':'Nie znaleziono kwestionariusza.',
// evaluations list + detail views (counsel review 2026-07-02)
'Evaluations':'Oceny','Loading evaluations…':'Wczytywanie ocen…','No evaluations yet.':'Brak ocen.','Failed to load evaluations':'Nie udało się wczytać ocen','Evaluation history':'Historia ocen','Evaluations for this matter':'Oceny tej sprawy','Average score':'Średni wynik','submitted evaluations':'wysłanych ocen','Avg score':'Średni wynik','Billing':'Rozliczenie','Firm not found.':'Nie znaleziono kancelarii.','Matter not found.':'Nie znaleziono sprawy.','No history yet.':'Brak historii.','fixed fee':'ryczałt','firm':'kancelaria','matter':'sprawa','question':'pytanie','response':'odpowiedź',
'Evaluations':'Oceny','Loading evaluations…':'Wczytywanie ocen…','No evaluations yet.':'Brak ocen.','Failed to load evaluations':'Nie udało się wczytać ocen','Evaluation history':'Historia ocen','Evaluations for this matter':'Oceny tej sprawy','Average score':'Średni wynik','submitted evaluations':'wysłanych ocen','Avg score':'Średni wynik','Billing':'Rozliczenie','Firm not found.':'Nie znaleziono kancelarii.','Matter not found.':'Nie znaleziono sprawy.','Review cycle not found.':'Nie znaleziono cyklu przeglądu.','No history yet.':'Brak historii.','fixed fee':'ryczałt','firm':'kancelaria','matter':'sprawa','question':'pytanie','response':'odpowiedź',
// review-cycle wording (counsel review 2026-07-02: a bare "min evals 3" meant nothing)
'Firms with fewer than':'Kancelarie z mniej niż','submitted evaluations stay provisional.':'wysłanymi ocenami mają status wstępny.','How many submitted evaluations a firm needs to be ranked in this cycle.':'Ile wysłanych ocen potrzebuje kancelaria, aby znaleźć się w rankingu tego cyklu.',
// seeded data labels (EN in the DB, localized for display like any other exact-match string)