Lower authenticated resource downloads

This commit is contained in:
Meta Vens 2026-08-25 18:00:03 +02:00
commit 672c0c1e9f
4 changed files with 120 additions and 1 deletions

View file

@ -11,6 +11,7 @@ This package owns the shared browser renderer contract:
- `EveBrowserProviderHost` owns advertisement-backed surface selection and uses one provider-neutral embedded-document resolver; provider schema dispatch stays behind the host transport boundary.
- Editable string, textarea, number, select, and choice controls use named Eve state bindings. The renderer preserves edited drafts by provider/surface/binding identity; untouched controls contribute their authored value when an operation captures those bindings atomically under `payload.bindings`.
- `gamecult.eve.command_result.v1` carries the persisted receipt, an optional one-time Eve projection, a binding-clear directive, and at most one plugin-scoped payload. Only the matching plugin adapter may consume that payload. The renderer-owned receipt region survives the authoritative surface refresh that follows the command.
- `resource.download` lowers a provider-issued resource grant to an accessible download link. The reference browser accepts only same-origin HTTP(S) or root-relative URLs and sanitizes the suggested filename; the provider remains responsible for authorization, expiry, single-use semantics, and resource bytes.
Host applications own transport and command delivery. Providers own command acceptance and receipts through their advertisements. For example, Eir resolves surfaces through Odin/CultMesh and posts commands to `/eir/command`; Aetheria resolves surfaces through Electron IPC. Neither host should duplicate DOM lowering or import provider internals to find a command route.

View file

@ -631,6 +631,23 @@ function renderEveComponentProjection(node, options) {
wireCommand(button, node, resolveComponentCommandId(props, node), props, options);
return button;
}
if (kind === "resource.download") {
const link = el("a", "cultui-button eve-resource-download", stringProp(props.label, "Download"));
assignId(link, node);
applyGeneratedLayout(link, layout, style);
const uri = firstString(props.uri, props.href, props.assetUri, "");
const href = resolveSafeDownloadUrl(uri, options);
if (!href) {
link.removeAttribute("href");
link.setAttribute("aria-disabled", "true");
}
else {
link.href = href;
link.download = safeDownloadFilename(stringProp(props.filename, "download"));
}
link.setAttribute("aria-label", stringProp(props.ariaLabel, stringProp(props.label, "Download")));
return link;
}
if (kind === "control.popup") {
const button = el("button", "cultui-button eve-control-popup", stringProp(props.label, "Open"));
assignId(button, node);
@ -2128,6 +2145,30 @@ function resolveAssetUrl(uri, options) {
}
return uri;
}
function resolveSafeDownloadUrl(uri, options) {
const resolved = resolveAssetUrl(uri, options).trim();
if (!resolved || resolved.startsWith("//"))
return "";
if (resolved.startsWith("/"))
return resolved;
try {
const base = new URL(document.baseURI);
const candidate = new URL(resolved, base);
if (!/^https?:$/.test(candidate.protocol) || candidate.origin !== base.origin)
return "";
return candidate.href;
}
catch {
return "";
}
}
function safeDownloadFilename(value) {
const filename = value
.replace(/[\\/:*?"<>|\u0000-\u001f\u007f]/g, "_")
.replace(/^\.+/, "")
.trim();
return filename || "download";
}
function loadFontStylesheet(href) {
const normalized = typeof href === "string" ? href.trim() : "";
if (!normalized)

View file

@ -850,6 +850,27 @@ function renderEveComponentProjection(
return button;
}
if (kind === "resource.download") {
const link = el(
"a",
"cultui-button eve-resource-download",
stringProp(props.label, "Download"),
) as HTMLAnchorElement;
assignId(link, node);
applyGeneratedLayout(link, layout, style);
const uri = firstString(props.uri, props.href, props.assetUri, "");
const href = resolveSafeDownloadUrl(uri, options);
if (!href) {
link.removeAttribute("href");
link.setAttribute("aria-disabled", "true");
} else {
link.href = href;
link.download = safeDownloadFilename(stringProp(props.filename, "download"));
}
link.setAttribute("aria-label", stringProp(props.ariaLabel, stringProp(props.label, "Download")));
return link;
}
if (kind === "control.popup") {
const button = el("button", "cultui-button eve-control-popup", stringProp(props.label, "Open")) as HTMLButtonElement;
assignId(button, node);
@ -2514,6 +2535,28 @@ function resolveAssetUrl(uri: string, options: EveBrowserLoweringOptions): strin
return uri;
}
function resolveSafeDownloadUrl(uri: string, options: EveBrowserLoweringOptions): string {
const resolved = resolveAssetUrl(uri, options).trim();
if (!resolved || resolved.startsWith("//")) return "";
if (resolved.startsWith("/")) return resolved;
try {
const base = new URL(document.baseURI);
const candidate = new URL(resolved, base);
if (!/^https?:$/.test(candidate.protocol) || candidate.origin !== base.origin) return "";
return candidate.href;
} catch {
return "";
}
}
function safeDownloadFilename(value: string): string {
const filename = value
.replace(/[\\/:*?"<>|\u0000-\u001f\u007f]/g, "_")
.replace(/^\.+/, "")
.trim();
return filename || "download";
}
function loadFontStylesheet(href: unknown): void {
const normalized = typeof href === "string" ? href.trim() : "";
if (!normalized) return;

View file

@ -1,7 +1,7 @@
import assert from "node:assert/strict";
import test from "node:test";
import { JSDOM } from "jsdom";
import { EveBrowserDraftStore, EveBrowserProviderHost, renderEveSurface } from "../dist/index.js";
import { EveBrowserDraftStore, EveBrowserProviderHost, renderEveComponent, renderEveSurface } from "../dist/index.js";
function installDom() {
const dom = new JSDOM("<!doctype html><html><head></head><body><div id='a'></div><div id='b'></div></body></html>", {
@ -31,6 +31,40 @@ function installDom() {
};
}
test("resource downloads lower to same-origin one-time links without scriptable URLs", () => {
const restoreDom = installDom();
try {
const link = renderEveComponent({
id: "campaign.export",
kind: "resource.download",
props: {
label: "Download campaign",
uri: "/ghostlight/api/eve/resources/opaque-grant",
filename: "campaign:bad?.cc",
},
});
assert.equal(link.tagName, "A");
assert.equal(link.getAttribute("href"), "/ghostlight/api/eve/resources/opaque-grant");
assert.equal(link.getAttribute("download"), "campaign_bad_.cc");
assert.equal(link.getAttribute("aria-disabled"), null);
const rejected = renderEveComponent({
kind: "resource.download",
props: { label: "Bad", uri: "javascript:alert(1)" },
});
assert.equal(rejected.getAttribute("href"), null);
assert.equal(rejected.getAttribute("aria-disabled"), "true");
const crossOrigin = renderEveComponent({
kind: "resource.download",
props: { label: "Bad", uri: "https://elsewhere.test/private.cc" },
});
assert.equal(crossOrigin.getAttribute("href"), null);
} finally {
restoreDom();
}
});
function testInputAdapter(onRender = () => {}) {
return {
pluginId: "test.input",