Merge pull request 'esc() misses single quotes, enabling attribute JS breakout in task row onclick (archie)' (#71) from feat/55-esc-misses-single-quotes-enabling-attribute-js-breakout-in-t into main

Reviewed-on: sam/archie-core#71
This commit is contained in:
sam committed 2026-07-25 01:30:09 +00:00
commit d5f3b5fe3d
2 files changed
+65 -1

No files matched your search

+2 -1
View File
@@ -130,6 +130,7 @@
<script>
const $ = s => document.querySelector(s);
const esc = s => String(s ?? "").replace(/[&<>"]/g, c => ({"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;"}[c]));
const escJS = s => String(s ?? "").replace(/['\\]/g, c => '\\' + c);
const fmt = n => (n ?? 0).toLocaleString();
const ms = v => v >= 60000 ? (v/60000).toFixed(1)+"m" : v >= 1000 ? (v/1000).toFixed(1)+"s" : v+"ms";
@@ -157,7 +158,7 @@ async function refresh() {
.join("") || '<div class="card"><div class="n">0</div><div class="l">tasks</div></div>';
$("#tasks tbody").innerHTML = (tasks ?? []).map(t => `
<tr class="task" onclick="showTask(${t.id}, '${esc(t.owner)}/${esc(t.repo)}#${t.issue_number}')">
<tr class="task" onclick="showTask(${t.id}, '${escJS(t.owner)}/${escJS(t.repo)}#${t.issue_number}')">
<td>${esc(t.owner)}/${esc(t.repo)}</td><td>#${t.issue_number}</td>
<td>${esc(t.title)}</td><td>${esc(t.workflow)}</td>
<td><span class="status ${esc(t.status)}">${esc(t.status)}</span></td>
+63
View File
@@ -205,6 +205,69 @@ func TestHandleSSEBacklogAndLive(t *testing.T) {
}
}
func TestIndexEscapesSingleQuotesInOnclick(t *testing.T) {
srv := newTestServer(t)
req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status = %d", w.Code)
}
body := w.Body.String()
// escJS helper must be defined.
if !strings.Contains(body, "escJS") {
t.Fatal("escJS helper not found in HTML")
}
// The onclick template must use escJS (not esc) for owner and
// repo — the values interpolated into the single-quoted JS str.
if !strings.Contains(body, "escJS(t.owner)") {
t.Error("onclick does not use escJS for owner")
}
if !strings.Contains(body, "escJS(t.repo)") {
t.Error("onclick does not use escJS for repo")
}
}
func TestIndexEscDoesNotEscapeSingleQuotes(t *testing.T) {
srv := newTestServer(t)
req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status = %d", w.Code)
}
body := w.Body.String()
// esc() must NOT escape single quotes — it's used for HTML
// content/attribute contexts where apostrophes are legitimate
// (e.g. issue titles like "Don't panic").
idx := strings.Index(body, "const esc =")
if idx == -1 {
t.Fatal("esc definition not found")
}
snippet := body[idx : idx+200]
// The regex should escape & < > " but NOT '
if !strings.Contains(snippet, "[&<>\"") {
t.Error("esc regex character class is missing or malformed")
}
// The character class must NOT include a single quote.
reStart := strings.Index(snippet, "[&<>\"")
reEnd := strings.Index(snippet[reStart:], "]")
if reEnd == -1 {
t.Fatal("could not find end of esc regex character class")
}
charClass := snippet[reStart : reStart+reEnd]
if strings.Contains(charClass, "'") {
t.Error("esc() regex escapes single quotes — this would cause backslash artifacts in HTML text")
}
}
// readLineUntil scans SSE "data:" lines until one contains want.
func readLineUntil(r *bufio.Reader, want string) (string, error) {
deadline := time.Now().Add(3 * time.Second)