Answer the independent review of slice 1
Human
Prompt
Task 11: get an independent review (Fable), fix what it finds.
From this commit's own message. The prompt ledger has no entries for it — which is what a squashed, rebased or amended commit looks like, and what every commit looks like in a repo without a ledger.
Summary
Send waits for its uploads and sends nothing if one of them didn't take; one submit at a time, so a second press can't release the first send's files; a send with no answer (network, 502, page going) leaves its files alone (the beacon and Remove skip them), which also fixes a beacon racing a send Chrome rejects as the page goes; Remove after the bytes have gone lets the upload land and deletes it, since Chrome may keep an aborted request open; a removed chip whose send was refused deletes its upload; pageshow settles a waiting send; paste counts a file: URL or the exact name as the file's own; the beacon includes stale ids. Server: the body buffer is sized from Content-Length, the gone-client delete has its own context, files carry CORP same-origin. Tests: a refused upload during Send, a double Enter, a pending state, the in-process no-deadline refusal, the discard's cap and a sent file; the existing tests click button.send now that the paperclip comes first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
9 files changed,
+197
−57
internal/browsertest/ask_test.go+5 −5internal/browsertest/attach_test.go+61 −9internal/browsertest/layout_test.go+1 −1internal/browsertest/light_test.go+1 −1internal/browsertest/mobile_test.go+1 −1internal/look/static/ask-attach.js+56 −17internal/look/static/ask.js+18 −13internal/web/ask_files.go+13 −2internal/webtest/ask_files_test.go+41 −8
CI passed — run details
CI log
=== build === CGO_ENABLED=0 go build ./... === vet === go vet ./... === fmt-check === === staticcheck === go run honnef.co/go/tools/cmd/staticcheck@v0.7.0 ./... === govulncheck === go run golang.org/x/vuln/cmd/govulncheck@v1.8.0 ./... No vulnerabilities found. === gitleaks === [90m3:44PM[0m [32mINF[0m [1mUnknown SCM platform. Use --platform to include links in findings.[0m [36mhost=[0mamadan.net [90m3:44PM[0m [32mINF[0m [1m1 commits scanned.[0m [90m3:44PM[0m [32mINF[0m [1mscanned ~2703260 bytes (2.70 MB) in 342ms[0m [90m3:44PM[0m [32mINF[0m [1mno leaks found[0m === budget === go run amadan.net/rastrillo/rastrillo/cmd/rastrillo budget size budget size: 22 directories within budget === script-test === scripts/prepare-apps_test.sh ok releases lookup failure stops before ship ok app owned elsewhere stops the script ok apps listing failure stops before create ok ambiguous release dir is refused ok explicit version resolves the ambiguity ok releases match is exact, not substring ok shipped version is only promoted, existing app not created ok fresh account creates, ships, promotes to edge, enables ok failing promote stops the script ok ship is retried while uploads are rate limited all passed hack/fonts_test.sh fonts_test: ok === test === go run amadan.net/rastrillo/rastrillo/cmd/rastrillo budget test -count=1 ./... receipt: test step 10.637s against a 60s target (the gate's other steps are not in this figure) receipt: 16 packages ran, 0 cached, 6 without tests, 0 failed receipt: slow package 9.586s amadan.net/oficina/base/internal/webtest receipt: slow package 629ms amadan.net/oficina/base/internal/equipo receipt: slow package 576ms amadan.net/oficina/base/internal/operations receipt: slow package 572ms amadan.net/oficina/base/internal/issuer receipt: slow package 172ms amadan.net/oficina/base/internal/consumer receipt: slow test 1.52s amadan.net/oficina/base/internal/webtest TestHomeDoesNotWaitOnAStalledSnapshot receipt: slow test 1.09s amadan.net/oficina/base/internal/webtest TestAskZoneIsToldOnce receipt: slow test 670ms amadan.net/oficina/base/internal/webtest TestAskUploadStalledBodyFreesItsSlot receipt: slow test 520ms amadan.net/oficina/base/internal/webtest TestAskUploadBusy receipt: slow test 520ms amadan.net/oficina/base/internal/webtest TestAskUploadTooBig === native === strings: copy review's strings.json is not here (git-ignored); its comparison skipped strings: 120 in the catalog, 117 used worktree-lib_test: ok
diff --git a/internal/browsertest/ask_test.go b/internal/browsertest/ask_test.go| index a2d081e..1feda3c 100644 |
| --- a/internal/browsertest/ask_test.go |
| +++ b/internal/browsertest/ask_test.go |
| @@ -142,7 +142,7 @@ func askClosed(t *testing.T, b *baseApp, want bool) { |
| func TestAskPanelSendsOnce(t *testing.T) { |
| rig, f := askBase(t) |
| openPanel(t, rig) |
| - send := `#ask-panel form.composer button` |
| + send := `#ask-panel form.composer button.send` |
| drive(t, rig, chromedp.SendKeys(chromedp.CSS(`#ask-text`), "Build me a quick website"), |
| chromedp.Evaluate[chromedp.Void](`(() => { const b = document.querySelector("`+send+`"); b.click(); b.click(); })()`), |
| chromedp.Poll[chromedp.Void](`document.querySelectorAll("#ask-panel .msg.person").length === 1`, chromedp.WithPollingTimeout(5*time.Second))) |
| @@ -185,10 +185,10 @@ func TestAskPanelNoRunner(t *testing.T) { |
| const text = "Build me a quick website" |
| f.RunnerTooOld() |
| drive(t, rig, chromedp.SendKeys(chromedp.CSS(`#ask-text`), text), |
| - chromedp.Click(chromedp.CSS(`#ask-panel form.composer button`)), |
| + chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.send`)), |
| chromedp.WaitVisible(chromedp.CSS(old)), chromedp.WaitNotVisible(chromedp.CSS(note))) |
| f.NoRunner() |
| - drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel form.composer button`)), |
| + drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.send`)), |
| chromedp.WaitVisible(chromedp.CSS(note)), chromedp.WaitNotVisible(chromedp.CSS(old))) |
| if n := len(f.Calls("/api/v1/assistant/messages")); n != 2 { |
| t.Fatalf("sends relayed: %d, want 2", n) |
| @@ -238,7 +238,7 @@ func TestAskPanelStatesShowApprovedWords(t *testing.T) { |
| openOnly(rig, "norunner") |
| f.RunnerTooOld() |
| drive(t, rig, chromedp.SendKeys(chromedp.CSS(`#ask-text`), "Build me a quick website"), |
| - chromedp.Click(chromedp.CSS(`#ask-panel form.composer button`)), |
| + chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.send`)), |
| chromedp.WaitVisible(chromedp.CSS(`#ask-panel .ask-runner[data-when="oldrunner"]`))) |
| if got, want := visible(t, rig), "oldrunner=Ask Base needs an update first. Update it, then try again."; got != want { |
| t.Errorf("visible notes %q, want %q", got, want) |
| @@ -294,7 +294,7 @@ func TestAskDockedOpenByDefault(t *testing.T) { |
| t.Errorf(`"/" in the field typed %q`, got) |
| } |
| drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.querySelector("#ask-text").value = "What can I do"`), |
| - chromedp.Click(chromedp.CSS(`#ask-panel form.composer button`)), |
| + chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.send`)), |
| chromedp.Poll[chromedp.Void](`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.textContent.includes("What would you like to do?") && m.dataset.id)`, |
| chromedp.WithPollingTimeout(10*time.Second))) |
| rig.Screen("#ask-panel", "the docked chat with one question answered") |
diff --git a/internal/browsertest/attach_test.go b/internal/browsertest/attach_test.go| index 47dcd8d..c3b378f 100644 |
| --- a/internal/browsertest/attach_test.go |
| +++ b/internal/browsertest/attach_test.go |
| @@ -400,8 +400,8 @@ func TestAskAttachDrop(t *testing.T) { |
| } |
| } |
| -// A chip shows its upload's progress; Remove during the upload aborts it |
| -// (and Base deletes what Equipo took), Remove after deletes it; a removed |
| +// A chip shows its upload's progress; Remove once its bytes have gone |
| +// deletes the upload as it lands, Remove after deletes it; a removed |
| // file is never sent. Send waits for uploads, then sends once, in chip |
| // order. |
| func TestAskAttachProgressAndRemove(t *testing.T) { |
| @@ -418,12 +418,36 @@ func TestAskAttachProgressAndRemove(t *testing.T) { |
| drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel .chip-remove`))) |
| waitChips(t, rig, "gone", func(c []chip) bool { return len(c) == 0 }) |
| release() |
| - waitFor(t, "Base deleted the aborted upload", func() bool { return len(f.Uploads()) == 0 && len(f.Calls("/api/v1/attachments")) == 1 }) |
| + deletes := func() int { |
| + n := 0 |
| + for _, c := range f.Calls("") { |
| + if c.Method == http.MethodDelete && strings.HasPrefix(c.Path, "/api/v1/attachments/") { |
| + n++ |
| + } |
| + } |
| + return n |
| + } |
| + for i := 0; deletes() != 1 || len(f.Uploads()) != 0; i++ { |
| + if i > 200 { |
| + var calls []string |
| + for _, c := range f.Calls("") { |
| + calls = append(calls, c.Method+" "+c.Path) |
| + } |
| + t.Fatalf("the removed upload wasn't deleted: held %v; calls %v", f.Uploads(), calls) |
| + } |
| + time.Sleep(25 * time.Millisecond) |
| + } |
| pick(t, rig, paths["n2.txt"]) |
| waitChips(t, rig, "done", allDone(1)) |
| held := f.Uploads()[0] |
| drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel .chip-remove`))) |
| - waitFor(t, "Remove deleted the upload", func() bool { return len(f.Uploads()) == 0 }) |
| + for i := 0; len(f.Uploads()) != 0; i++ { |
| + if i > 200 { |
| + _, name, _, _, _ := f.Upload(f.Uploads()[0]) |
| + t.Fatalf("Remove left %v (%s); %d deletes; chips %+v", f.Uploads(), name, deletes(), chipsOf(t, rig)) |
| + } |
| + time.Sleep(25 * time.Millisecond) |
| + } |
| if calls := f.Calls("/api/v1/attachments/" + held); len(calls) != 1 || calls[0].Method != http.MethodDelete { |
| t.Fatalf("delete: %+v", calls) |
| } |
| @@ -478,6 +502,20 @@ func TestAskAttachRefusals(t *testing.T) { |
| paths := attachFiles(t) |
| rig, f := attachReady(t) |
| rig.Allow(http.MethodPost, "/ask/attachments", http.StatusUnsupportedMediaType) |
| + // A file refused while Send waits for it: nothing goes, and the words |
| + // stay. |
| + entered, release := f.HoldNextUpload() |
| + pick(t, rig, paths["data.json"]) |
| + <-entered |
| + drive(t, rig, chromedp.SendKeys(chromedp.CSS(`#ask-panel textarea`), "What's in this?"), chromedp.Click(chromedp.CSS(`#ask-panel button.send`))) |
| + release() |
| + waitChips(t, rig, "refused", func(c []chip) bool { return len(c) == 1 && c[0].State == "refused" }) |
| + time.Sleep(300 * time.Millisecond) |
| + if n := len(f.Calls("/api/v1/assistant/messages")); n != 0 || harness.Evaluate[string](rig, `document.querySelector("#ask-panel textarea").value`) != "What's in this?" { |
| + t.Fatalf("sent without its file: %d", n) |
| + } |
| + harness.Evaluate[bool](rig, `(() => { document.querySelector("#ask-panel textarea").value = ""; return true; })()`) |
| + clearChips(t, rig) |
| pick(t, rig, paths["big.txt"], paths["photo.heic"], paths["data.json"]) |
| c := waitChips(t, rig, "settled", func(c []chip) bool { |
| return len(c) == 3 && c[0].State == "refused" && c[1].State == "refused" && c[2].State == "refused" |
| @@ -486,8 +524,8 @@ func TestAskAttachRefusals(t *testing.T) { |
| c[2].Meta != "Ask takes images (PNG, JPEG, WebP, GIF) and text, Markdown, CSV, JSON and PDF files." { |
| t.Fatalf("refusals: %+v", c) |
| } |
| - if n := len(f.Calls("/api/v1/attachments")); n != 1 { |
| - t.Fatalf("%d uploads reached Equipo, want 1 (the JSON)", n) |
| + if n := len(f.Calls("/api/v1/attachments")); n != 2 { |
| + t.Fatalf("%d uploads reached Equipo, want 2 (the JSON, twice)", n) |
| } |
| waitFor(t, "a refusal announced", func() bool { |
| return harness.Evaluate[string](rig, `document.querySelector("#ask-panel form.composer [aria-live]").textContent`) == |
| @@ -577,15 +615,29 @@ func TestAskAttachBeacon(t *testing.T) { |
| waitChips(t, rig, "done", allDone(1)) |
| id := f.Uploads()[0] |
| entered, release := f.HoldNextSend() |
| + // Enter twice: the second press neither sends again nor lets go of the |
| + // first send's files. |
| drive(t, rig, chromedp.SendKeys(chromedp.CSS(`#ask-panel textarea`), "Keep this"), chromedp.Click(chromedp.CSS(`#ask-panel button.send`))) |
| <-entered |
| + harness.Evaluate[bool](rig, `(() => { document.querySelector("#ask-panel form.composer").requestSubmit(); return true; })()`) |
| + time.Sleep(200 * time.Millisecond) |
| drive(t, rig, chromedp.Navigate(rig.Origin+"/settings"), chromedp.WaitVisible(chromedp.CSS(`form[action="/ask/disconnect"]`))) |
| time.Sleep(300 * time.Millisecond) |
| release() |
| - waitFor(t, "the message landed with its file", func() bool { |
| + for i := 0; ; i++ { |
| _, _, _, msg, ok := f.Upload(id) |
| - return ok && msg != "" |
| - }) |
| + if ok && msg != "" { |
| + break |
| + } |
| + if i > 200 { |
| + var calls []string |
| + for _, c := range f.Calls("") { |
| + calls = append(calls, c.Method+" "+c.Path+" "+c.Body) |
| + } |
| + t.Fatalf("the message never landed with its file %s (held %v): %v", id, ok, calls[len(calls)-min(len(calls), 8):]) |
| + } |
| + time.Sleep(25 * time.Millisecond) |
| + } |
| for _, c := range f.Calls("/api/v1/attachments/" + id) { |
| if c.Method == http.MethodDelete { |
| t.Fatal("the beacon deleted a file of a send on its way") |
diff --git a/internal/browsertest/layout_test.go b/internal/browsertest/layout_test.go| index 04105d6..a0fd0e4 100644 |
| --- a/internal/browsertest/layout_test.go |
| +++ b/internal/browsertest/layout_test.go |
| @@ -242,7 +242,7 @@ func TestAskDockLayout(t *testing.T) { |
| openPanel(t, rig) |
| if harness.Evaluate[int](rig, `document.querySelectorAll("#ask-panel .msg.coordinator").length`) == 0 { |
| drive(t, rig, chromedp.SendKeys(chromedp.CSS(`#ask-text`), "What can I do"), |
| - chromedp.Click(chromedp.CSS(`#ask-panel form.composer button`)), |
| + chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.send`)), |
| chromedp.Poll[chromedp.Void](`!document.querySelector("#ask-panel .thinking, #ask-panel .draft, #ask-panel .think-line") && [...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.dataset.id)`, |
| chromedp.WithPollingTimeout(10*time.Second))) |
| } |
diff --git a/internal/browsertest/light_test.go b/internal/browsertest/light_test.go| index 65c5def..492076d 100644 |
| --- a/internal/browsertest/light_test.go |
| +++ b/internal/browsertest/light_test.go |
| @@ -282,7 +282,7 @@ func TestLightMode(t *testing.T) { |
| // The docked chat mid-conversation: both bubbles, the header and status. |
| drive(t, rig, chromedp.Click(chromedp.CSS(`.base-btn`)), chromedp.Poll[chromedp.Void](settledJS, chromedp.WithPollingTimeout(3*time.Second)), |
| chromedp.SendKeys(chromedp.CSS(`#ask-text`), "What can I do"), |
| - chromedp.Click(chromedp.CSS(`#ask-panel form.composer button`)), |
| + chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.send`)), |
| chromedp.Poll[chromedp.Void](`!document.querySelector("#ask-panel .thinking, #ask-panel .draft, #ask-panel .think-line") && [...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.dataset.id)`, |
| chromedp.WithPollingTimeout(10*time.Second)), |
| chromedp.Poll[chromedp.Void](`getComputedStyle(document.querySelector("#ask-panel")).transform === "none"`, chromedp.WithPollingTimeout(3*time.Second))) |
diff --git a/internal/browsertest/mobile_test.go b/internal/browsertest/mobile_test.go| index 41442bf..fff541c 100644 |
| --- a/internal/browsertest/mobile_test.go |
| +++ b/internal/browsertest/mobile_test.go |
| @@ -139,7 +139,7 @@ func TestMobile(t *testing.T) { |
| // Mid-conversation: the sheet fills the screen, the header is at the |
| // top, the composer on screen at the bottom, targets 44px. |
| drive(t, rig, chromedp.SendKeys(chromedp.CSS(`#ask-text`), "What can I do"), |
| - chromedp.Click(chromedp.CSS(`#ask-panel form.composer button`)), |
| + chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.send`)), |
| chromedp.Poll[chromedp.Void](`!document.querySelector("#ask-panel .thinking, #ask-panel .draft, #ask-panel .think-line") && [...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.dataset.id)`, |
| chromedp.WithPollingTimeout(10*time.Second)), |
| chromedp.Poll[chromedp.Void](`(() => { const r = document.querySelector("#ask-panel").getBoundingClientRect(); return r.left === 0 && r.top === 0 })()`, |
diff --git a/internal/look/static/ask-attach.js b/internal/look/static/ask-attach.js| index 2267f15..a0a0d3d 100644 |
| --- a/internal/look/static/ask-attach.js |
| +++ b/internal/look/static/ask-attach.js |
| @@ -117,8 +117,11 @@ const refusals = { |
| // attach, only while on; |
| // count() chips that would go with a send; |
| // ready() a promise of their ids, in chip order, once every upload |
| -// has settled; |
| -// sending(ids, on) a send with ids is on its way (the beacon leaves them); |
| +// has settled; null when one that was uploading didn't |
| +// take (its chip says why, and nothing should go); |
| +// sending(ids, on, outcome) a send with ids is on its way (the beacon |
| +// leaves them), or has ended: outcome "sent", "refused" |
| +// (Base answered no) or "unknown" (no answer came); |
| // sent(ids) they went: their chips go; |
| // refused(why, ids) the send was refused for its files: "unavailable" or |
| // "stale", said on their chips; |
| @@ -133,6 +136,11 @@ export function attach(root, hooks) { |
| let enabled = false; |
| const items = []; // every chip, in order |
| const inflight = new Set(); // ids of a send on its way |
| + const removedInflight = new Set(); // of those, ids whose chip was removed meanwhile |
| + // ids of a send whose outcome never came (the network failed, or the |
| + // page is going): it may have landed, so nothing here deletes them; a |
| + // send that is never made leaves them to Equipo's 24-hour drop. |
| + const unsure = new Set(); |
| let running = 0; |
| // The paperclip, left of Send; the picker it opens. |
| @@ -287,7 +295,12 @@ export function attach(root, hooks) { |
| xhr.setRequestHeader("Accept", "application/json"); |
| xhr.setRequestHeader("Content-Type", it.type || "application/octet-stream"); |
| xhr.setRequestHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodeURIComponent(it.name).replace(/['()*]/g, (c) => "%" + c.charCodeAt(0).toString(16).toUpperCase())); |
| - xhr.upload.onprogress = (e) => { if (e.lengthComputable) progress(it, (e.loaded / e.total) * 100); }; |
| + xhr.upload.onprogress = (e) => { |
| + if (!e.lengthComputable) return; |
| + it.sending = e.loaded < e.total; |
| + progress(it, (e.loaded / e.total) * 100); |
| + }; |
| + xhr.upload.onload = () => { it.sending = false; }; |
| const finish = () => { |
| running--; |
| it.xhr = null; |
| @@ -295,9 +308,14 @@ export function attach(root, hooks) { |
| }; |
| xhr.onload = () => { |
| finish(); |
| - if (it.removed) return; |
| let out = {}; |
| try { out = JSON.parse(xhr.responseText); } catch {} |
| + if (it.removed) { |
| + // Removed (or the page came back from the cache) as it landed: the |
| + // upload goes too. |
| + if (xhr.status === 201 && out.id) fetch(href(out.id), { method: "DELETE", headers: { Accept: "application/json" } }).catch(() => {}); |
| + return; |
| + } |
| if (xhr.status === 201 && out.id) { |
| it.id = out.id; |
| it.kind = out.kind; |
| @@ -334,18 +352,22 @@ export function attach(root, hooks) { |
| xhr.send(it.file); |
| } |
| - // drop takes a chip away: Remove aborts its upload, or deletes the |
| - // upload it made (Equipo's own 404 counts as done). focus moves to the |
| - // next chip, or the field. |
| + // drop takes a chip away: Remove aborts its upload while its bytes are |
| + // seen still going; otherwise the upload is left to land and is deleted |
| + // then (onload): a browser that has sent them all may keep the request |
| + // open, and Base would never hear it was given up. An upload already made is |
| + // deleted (Equipo's own 404 counts as done). Focus moves to the next |
| + // chip, or the field. |
| function drop(it, focus) { |
| const at = items.indexOf(it); |
| if (at < 0) return; |
| items.splice(at, 1); |
| it.removed = true; |
| - if (it.xhr) it.xhr.abort(); |
| - else if (it.id && !inflight.has(it.id)) { |
| - fetch(href(it.id), { method: "DELETE", headers: { Accept: "application/json" } }).catch(() => {}); |
| - } |
| + if (it.xhr && it.sending) it.xhr.abort(); |
| + else if (it.xhr) { |
| + // onload deletes it. |
| + } else if (it.id && inflight.has(it.id)) removedInflight.add(it.id); |
| + else if (it.id && !unsure.has(it.id)) fetch(href(it.id), { method: "DELETE", headers: { Accept: "application/json" } }).catch(() => {}); |
| it.li.remove(); |
| chips.hidden = items.length === 0; |
| if (focus) { |
| @@ -367,13 +389,25 @@ export function attach(root, hooks) { |
| waiters = []; |
| } |
| function ready() { |
| + const pending = items.filter((it) => it.state === "queued" || it.state === "uploading"); |
| return new Promise((resolve) => { |
| - waiters.push(resolve); |
| + waiters.push((ids) => resolve(pending.some((it) => !it.removed && it.state !== "done") ? null : ids)); |
| settle(); |
| }); |
| } |
| - function sending(ids, on) { |
| - for (const id of ids) on ? inflight.add(id) : inflight.delete(id); |
| + function sending(ids, on, outcome) { |
| + for (const id of ids) { |
| + if (on) { |
| + inflight.add(id); |
| + continue; |
| + } |
| + inflight.delete(id); |
| + if (outcome === "unknown") unsure.add(id); |
| + else unsure.delete(id); |
| + // A chip removed while its send was on its way: if Base said the |
| + // send didn't take the file, the upload goes now. |
| + if (removedInflight.delete(id) && outcome === "refused") fetch(href(id), { method: "DELETE", headers: { Accept: "application/json" } }).catch(() => {}); |
| + } |
| } |
| function sent(ids) { |
| for (const it of [...items]) { |
| @@ -408,10 +442,13 @@ export function attach(root, hooks) { |
| const html = d.getData("text/html"); |
| if (html && new DOMParser().parseFromString(html, "text/html").body.textContent.trim()) return false; |
| const names = new Set(files.map((f) => f.name.trim())); |
| + // A line is a file's own when it is the name, or a file: URL to it. |
| const own = (line) => { |
| + if (names.has(line)) return true; |
| + if (!/^file:\/\//i.test(line)) return false; |
| let last = line.split("/").pop(); |
| try { last = decodeURIComponent(last); } catch {} |
| - return names.has(line) || names.has(last); |
| + return names.has(last); |
| }; |
| const plain = d.getData("text/plain"); |
| if (plain && plain.split(/\r?\n/).map((l) => l.trim()).some((l) => l && !own(l))) return false; |
| @@ -450,7 +487,7 @@ export function attach(root, hooks) { |
| // apart from a send's on its way (Fable B7). Kept for Back, it comes back |
| // without its chips, whose uploads are gone; the words stay. |
| addEventListener("pagehide", () => { |
| - const ids = items.filter((it) => it.id && !inflight.has(it.id) && it.state !== "stale").map((it) => it.id); |
| + const ids = items.filter((it) => it.id && !inflight.has(it.id) && !unsure.has(it.id)).map((it) => it.id); |
| if (!ids.length) return; |
| navigator.sendBeacon("/ask/attachments/discard", new URLSearchParams(ids.map((id) => ["id", id]))); |
| }); |
| @@ -464,6 +501,7 @@ export function attach(root, hooks) { |
| items.length = 0; |
| chips.hidden = true; |
| hooks.changed(); |
| + settle(); |
| }); |
| // The viewer: a thumbnail in the thread opens its image full size, in a |
| @@ -479,7 +517,7 @@ export function attach(root, hooks) { |
| let back = null; |
| thread.addEventListener("click", (e) => { |
| const thumb = e.target.closest("a.thumb[data-attachment]"); |
| - if (!thumb || e.metaKey || e.ctrlKey || e.shiftKey || e.button !== 0) return; |
| + if (!thumb || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || e.button !== 0) return; |
| e.preventDefault(); |
| back = thumb; |
| const img = thumb.querySelector("img"); |
| @@ -500,6 +538,7 @@ export function attach(root, hooks) { |
| viewer.addEventListener("close", () => { |
| full.removeAttribute("src"); |
| if (back && back.isConnected) back.focus({ preventScroll: true }); |
| + else field.focus({ preventScroll: true }); |
| back = null; |
| }); |
diff --git a/internal/look/static/ask.js b/internal/look/static/ask.js| index 2daecf5..73879b8 100644 |
| --- a/internal/look/static/ask.js |
| +++ b/internal/look/static/ask.js |
| @@ -1692,27 +1692,32 @@ function start(root) { |
| } |
| // Send waits for the chips' uploads, then sends once with their ids in |
| - // chip order. Files refused at send keep Ask ready: the chips say why |
| + // chip order; if one of them didn't take, nothing goes (its chip says |
| + // why). Files refused at send keep Ask ready: the chips say why |
| // (unavailable: the paperclip goes; stale: attach it again), and the |
| - // words stay in the field. |
| - let settling = false; |
| + // words stay in the field. One submit at a time, from the wait to the |
| + // answer. |
| + let submitting = false; |
| composer.addEventListener("submit", async (e) => { |
| e.preventDefault(); |
| const withFiles = !!(files && files.count()); |
| - if ((!field.value.trim() && !withFiles) || settling) return; |
| + if ((!field.value.trim() && !withFiles) || submitting) return; |
| + submitting = true; |
| let ids = []; |
| - if (withFiles) { |
| - settling = true; |
| - try { |
| + let sent = null; |
| + try { |
| + if (withFiles) { |
| ids = await files.ready(); |
| - } finally { |
| - settling = false; |
| + if (ids === null || (!field.value.trim() && !ids.length)) return; |
| + files.sending(ids, true); |
| } |
| - if (!field.value.trim() && !ids.length) return; |
| - files.sending(ids, true); |
| + sent = await post(composer, ids.length ? { attachments: ids } : null); |
| + } finally { |
| + // No answer at all (lastStatus 0), or Equipo not answering Base |
| + // (502): the send may have landed. |
| + if (ids && ids.length) files.sending(ids, false, sent ? "sent" : lastStatus && lastStatus !== 502 ? "refused" : "unknown"); |
| + submitting = false; |
| } |
| - const sent = await post(composer, ids.length ? { attachments: ids } : null); |
| - if (ids.length) files.sending(ids, false); |
| if (!sent) { |
| if (ids.length && lastFiles) { |
| files.refused(lastFiles, ids); |
diff --git a/internal/web/ask_files.go b/internal/web/ask_files.go| index 80885c0..0b38514 100644 |
| --- a/internal/web/ask_files.go |
| +++ b/internal/web/ask_files.go |
| @@ -1,6 +1,7 @@ |
| package web |
| import ( |
| + "bytes" |
| "context" |
| "errors" |
| "io" |
| @@ -159,7 +160,14 @@ func (a *app) askUpload(w http.ResponseWriter, r *http.Request) { |
| if UploadReading != nil { |
| UploadReading(int(n)) |
| } |
| - body, err := io.ReadAll(io.LimitReader(r.Body, equipo.MaxUpload+1)) |
| + // Read into a buffer of the declared size, so a slot holds about 10 MiB |
| + // at most, not the doubling ReadAll does as it grows. |
| + var buf bytes.Buffer |
| + if r.ContentLength > 0 { |
| + buf.Grow(int(r.ContentLength) + 1) |
| + } |
| + _, err := buf.ReadFrom(io.LimitReader(r.Body, equipo.MaxUpload+1)) |
| + body := buf.Bytes() |
| if err != nil { |
| http.Error(w, "bad request", http.StatusBadRequest) |
| return |
| @@ -179,7 +187,9 @@ func (a *app) askUpload(w http.ResponseWriter, r *http.Request) { |
| defer cancel() |
| at, err := a.ask.client.Upload(ctx, c.token, name, r.Header.Get("Content-Type"), body) |
| if err == nil && r.Context().Err() != nil { |
| - if err := a.ask.client.DeleteAttachment(ctx, c.token, at.ID); err != nil { |
| + gone, cancel := context.WithTimeout(context.WithoutCancel(r.Context()), 10*time.Second) |
| + defer cancel() |
| + if err := a.ask.client.DeleteAttachment(gone, c.token, at.ID); err != nil { |
| a.logger.Warn("ask: delete an upload whose page went", "err", err) |
| } |
| return |
| @@ -340,6 +350,7 @@ func (a *app) askAttachment(preview bool) http.HandlerFunc { |
| h.Set("Content-Disposition", cd) |
| h.Set("X-Content-Type-Options", "nosniff") |
| h.Set("Content-Security-Policy", fileCSP) |
| + h.Set("Cross-Origin-Resource-Policy", "same-origin") |
| if preview { |
| h.Set("Cache-Control", "private, max-age=3600") |
| h.Add("Vary", "Cookie") |
diff --git a/internal/webtest/ask_files_test.go b/internal/webtest/ask_files_test.go| index fbacc9d..da0c743 100644 |
| --- a/internal/webtest/ask_files_test.go |
| +++ b/internal/webtest/ask_files_test.go |
| @@ -182,6 +182,29 @@ func TestAskUploadNeedsAskReady(t *testing.T) { |
| if s, _ := a.askState(); s.State != "reconnect" { |
| t.Fatalf("after a refused bearer: %+v", s) |
| } |
| + // A code waiting for approval is not ready either. |
| + b, g := newAskApp(t) |
| + b.post("/ask/connect", url.Values{"next": {"ask"}}) |
| + r = b.upload("n.txt", "text/plain", []byte("note")) |
| + if _, state, _ := codeOf(r); r.Code != 409 || state != "pending" || len(g.Calls("/api/v1/attachments")) != 0 { |
| + t.Fatalf("pending: %d %s", r.Code, r.Body) |
| + } |
| +} |
| + |
| +// Where no read deadline can be set (here, Base served in-process with |
| +// no connection under it), an upload is refused before its body is read: |
| +// never read without one. |
| +func TestAskUploadNeedsAReadDeadline(t *testing.T) { |
| + web, app, f := newNativeAskApp(t) |
| + web.approveEquipo(f) |
| + req, _ := http.NewRequest(http.MethodPost, app.base+"/ask/attachments", strings.NewReader("# Notes")) |
| + req.Header.Set("Content-Type", "text/markdown") |
| + req.Header.Set("Origin", homeOrigin) |
| + req.Header.Set("Accept", "application/json") |
| + r := app.do(req) |
| + if code, _, _ := codeOf(r); r.Code != 503 || code != "busy" || len(f.Calls("/api/v1/attachments")) != 0 { |
| + t.Fatalf("in-process upload: %d %s", r.Code, r.Body) |
| + } |
| } |
| // cookieHeader is the session cookies a raw request must carry. |
| @@ -230,9 +253,9 @@ func TestAskUploadStalledBodyFreesItsSlot(t *testing.T) { |
| u.Host, a.origin, a.cookieHeader(), strings.Repeat("a", 500)) |
| start := time.Now() |
| _ = conn.SetReadDeadline(time.Now().Add(5 * time.Second)) |
| - _, err = http.ReadResponse(bufio.NewReader(conn), nil) |
| - if took := time.Since(start); took > 3*time.Second { |
| - t.Fatalf("the stalled upload held on for %s (%v)", took, err) |
| + res, err := http.ReadResponse(bufio.NewReader(conn), nil) |
| + if took := time.Since(start); took > 3*time.Second || err == nil && res.StatusCode != 400 { |
| + t.Fatalf("the stalled upload held on for %s (%v %v)", took, res, err) |
| } |
| f.SlowUploads(120 * time.Millisecond) |
| for i, code := range a.uploadAll(4) { |
| @@ -539,19 +562,29 @@ func TestAskDiscard(t *testing.T) { |
| if r := a.do(req); r.Code != 403 { |
| t.Fatalf("cross-site: %d", r.Code) |
| } |
| - beacon := url.Values{"id": {one, "../../etc", two}} |
| + f.FilesReady(true) |
| + f.Hold = true |
| + sent := uploaded(t, a.upload("s.txt", "text/plain", []byte("s"))) |
| + if r := a.postJSON("/ask/messages", url.Values{"attachments": {sent}}); r.Code != 200 { |
| + t.Fatalf("send: %d", r.Code) |
| + } |
| + // Five at most; a malformed one skipped; one already sent (Equipo's |
| + // 409) doesn't stop the rest. |
| + beacon := url.Values{"id": {sent, one, "../../etc", two, "at_abcdefgh99", keep}} |
| req, _ = http.NewRequest(http.MethodPost, a.base+"/ask/attachments/discard", strings.NewReader(beacon.Encode())) |
| req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| req.Header.Set("Sec-Fetch-Site", "same-origin") |
| if r := a.do(req); r.Code != 204 { |
| t.Fatalf("beacon: %d %s", r.Code, r.Body) |
| } |
| - left := f.Uploads() |
| - if len(left) != 1 || left[0] != keep { |
| + left := map[string]bool{} |
| + for _, id := range f.Uploads() { |
| + left[id] = true |
| + } |
| + if len(left) != 2 || !left[keep] || !left[sent] { |
| t.Fatalf("left: %v", left) |
| } |
| - f.FailReads(0) |
| - if r := a.newBrowser().post("/ask/attachments/discard", beacon); r.Code != 204 && r.Code != 403 && r.Code != 303 { |
| + if r := a.newBrowser().post("/ask/attachments/discard", url.Values{"id": {keep}}); r.Code == 204 || len(f.Uploads()) != 2 { |
| t.Fatalf("signed out: %d", r.Code) |
| } |
| } |