agent: share_file tool + containment-checked download endpoint

The agent can hand the user a same-origin download link for a file on its
filesystem (outbound complement to uploads). share_file(path, name?) returns a
Markdown link the GUI renders as a download chip.

Safety is containment, not trust: a shared path must canonicalize inside the
session personal folder or the shared uploads/ dir (.. and symlink escapes
refused), regular files only, 64 MiB cap. GET /api/sessions/{id}/download
re-validates independently (author != verifier), forces attachment +
application/octet-stream + nosniff, bare 400 out-of-bounds, 404 unknown session.

TOOL_USE seed migration appends the share_file manual without clobbering
user-edited rows. README documents the tool surface, endpoint and boundary;
Status refreshed to the verified 191 passed / 3 ignored.
This commit is contained in:
adm committed 2026-11-29 01:23:18 +01:00
1 parent 28568f0e89
commit 2f5b263d4e
11 files changed
+668 -15

No files matched your search

+10
View File
@@ -169,6 +169,16 @@ body.resizing { cursor: col-resize; user-select: none; }
.content.rendered li > ul, .content.rendered li > ol { margin: 0.2em 0; }
.content.rendered a { color: var(--accent); text-decoration: none; }
.content.rendered a:hover { text-decoration: underline; }
/* Agent-shared download links read as a distinct chip; the server already forces
Content-Disposition: attachment, so clicking downloads rather than navigates. */
.content.rendered a.download-link {
display: inline-flex; align-items: center; gap: 6px;
padding: 3px 10px; margin: 2px 0;
background: var(--panel-2); border: 1px solid var(--border); border-radius: 999px;
color: var(--accent); font-size: 0.85rem; text-decoration: none;
}
.content.rendered a.download-link::before { content: "\2B07"; font-size: 0.9em; }
.content.rendered a.download-link:hover { border-color: var(--accent); text-decoration: none; }
.content.rendered blockquote {
margin: 0 0 0.6em; padding: 0.2em 0 0.2em 0.9em;
border-left: 3px solid var(--border); color: var(--muted);
+15
View File
@@ -60,6 +60,19 @@ function el(tag, cls, text) {
return n;
}
// Mark agent-shared download links (relative /api/sessions/…/download) so they
// download in place via the same-origin `download` attribute and read as a chip.
// The server already forces Content-Disposition: attachment and a safe type.
function enhanceDownloadLinks(node) {
for (const a of node.querySelectorAll("a[href]")) {
const href = a.getAttribute("href") || "";
if (href.startsWith("/api/sessions/") && href.includes("/download")) {
a.classList.add("download-link");
a.setAttribute("download", "");
}
}
}
function messageNode(role, content, reasoning, usage, ts, html) {
const wrap = el("div", `msg ${role}`);
const roleNode = el("div", "role", role === "user" ? "Tu" : "Assistente");
@@ -77,6 +90,7 @@ function messageNode(role, content, reasoning, usage, ts, html) {
// Server-sanitized Markdown/LaTeX (assistant only): safe to inject.
contentNode.classList.add("rendered");
contentNode.innerHTML = html;
enhanceDownloadLinks(contentNode);
} else {
contentNode.textContent = content || "";
}
@@ -497,6 +511,7 @@ async function agentTurn(content, t, attachments) {
if (ev.content_html != null) {
L.contentNode.classList.add("rendered");
L.contentNode.innerHTML = ev.content_html;
enhanceDownloadLinks(L.contentNode);
}
L.det.open = false;
if (ev.response.usage) L.bubble.appendChild(el("div", "usage", usageText(ev.response.usage)));