diff --git a/apps/server/src/assets/AssetAccess.ts b/apps/server/src/assets/AssetAccess.ts
index d921045eda..3b0aaa8223 100644
--- a/apps/server/src/assets/AssetAccess.ts
+++ b/apps/server/src/assets/AssetAccess.ts
@@ -410,22 +410,20 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
: /^mate-asset:([a-f0-9-]{36})$/.exec(input.resource.path)?.[1];
if (occurrenceId) {
const config = yield* ServerConfig.ServerConfig;
+ const store = contentAssetsAt(config.stateDir);
+ // A page a call published is kept apart from the pictures, under the same reference.
const occurrence = yield* Effect.tryPromise({
- try: () => contentAssetsAt(config.stateDir).occurrence(occurrenceId),
+ try: () => store.occurrence(occurrenceId).catch(() => store.pageOccurrence(occurrenceId)),
catch: (cause) => new AssetWorkspaceAssetInspectionError({ resource: input.resource, cause }),
});
- if (occurrence.original.status !== "ready")
+ const original = occurrence.original;
+ if (original.status !== "ready")
return yield* new AssetWorkspaceAssetNotFoundError({ resource: input.resource });
return {
- relativeUrl: `/api/assets/objects/${occurrence.original.digest}/original`,
+ relativeUrl: `/api/assets/objects/${original.digest}/original`,
expiresAt: 0,
- ...(occurrence.original.width && occurrence.original.height
- ? {
- imageDimensions: {
- width: occurrence.original.width,
- height: occurrence.original.height,
- },
- }
+ ...("width" in original && original.width && original.height
+ ? { imageDimensions: { width: original.width, height: original.height } }
: {}),
};
}
diff --git a/apps/server/src/assets/ContentAssetHttp.test.ts b/apps/server/src/assets/ContentAssetHttp.test.ts
index a643640ddd..c55a033ae3 100644
--- a/apps/server/src/assets/ContentAssetHttp.test.ts
+++ b/apps/server/src/assets/ContentAssetHttp.test.ts
@@ -138,3 +138,34 @@ it.effect.each([
expect(resolutions).toBe(1);
}),
);
+
+it.effect("serves a published page as a document that runs nothing at its own address", () =>
+ Effect.gen(function* () {
+ const root = yield* Effect.promise(() =>
+ NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "mate-http-page-")),
+ );
+ roots.push(root);
+ const store = new ContentAssets(root);
+ // A page that opens with an SVG still is a page, never a picture.
+ const html = '';
+ const page = yield* Effect.promise(() =>
+ store.ingestPage(["call"], Buffer.from(html), {
+ threadId: ThreadId.make("thread"),
+ ownerId: "call",
+ name: "page-1.html",
+ }),
+ );
+ const object = yield* Effect.promise(() => store.object(page.original.digest));
+ const response = yield* protectedContentAsset(Effect.succeed(null), Effect.succeed(object), {
+ method: "GET",
+ headers: {},
+ });
+ expect(response.status).toBe(200);
+ expect(response.headers["content-type"]).toBe("text/html");
+ expect(response.headers["content-security-policy"]).toBe("default-src 'none'; sandbox");
+ const bytes = yield* Effect.promise(async () =>
+ Buffer.from(await HttpServerResponse.toWeb(response).arrayBuffer()).toString(),
+ );
+ expect(bytes).toBe(html);
+ }),
+);
diff --git a/apps/server/src/assets/ContentAssetHttp.ts b/apps/server/src/assets/ContentAssetHttp.ts
index 67395262ff..ac07104811 100644
--- a/apps/server/src/assets/ContentAssetHttp.ts
+++ b/apps/server/src/assets/ContentAssetHttp.ts
@@ -2,6 +2,7 @@ import type { AssetRepresentation } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import { HttpServerResponse } from "effect/http";
+import { PAGE_MIME_TYPE } from "./ContentAssets.ts";
import { openMediaFile, streamMediaFile } from "./MediaFile.ts";
export const contentAssetFailure = (code: string, status: number) =>
@@ -32,6 +33,10 @@ export const contentAssetResponse = Effect.fn("contentAssetResponse")(function*
...(object.mimeType === "image/svg+xml"
? { "Content-Security-Policy": "default-src 'none'; style-src 'unsafe-inline'; sandbox" }
: {}),
+ // A page is read by its conversation's frame, never opened here: at this address it runs nothing.
+ ...(object.mimeType === PAGE_MIME_TYPE
+ ? { "Content-Security-Policy": "default-src 'none'; sandbox" }
+ : {}),
};
if (
request.headers["if-none-match"]
diff --git a/apps/server/src/assets/ContentAssets.test.ts b/apps/server/src/assets/ContentAssets.test.ts
index c4f4fc6045..73d1814c24 100644
--- a/apps/server/src/assets/ContentAssets.test.ts
+++ b/apps/server/src/assets/ContentAssets.test.ts
@@ -6,7 +6,7 @@ import * as NodePath from "node:path";
import { ThreadId } from "@t3tools/contracts";
import sharp from "sharp";
-import { afterEach, expect, it } from "vite-plus/test";
+import { afterEach, describe, expect, it } from "vite-plus/test";
import { ContentAssets } from "./ContentAssets.ts";
@@ -240,3 +240,55 @@ it("a pressure retry of the preview recipe never publishes a reference to reclai
await NodeFSP.rm(directory, { recursive: true, force: true });
}
});
+
+describe("a page an agent published, in the asset store", () => {
+ const pageOwner = { threadId: ThreadId.make("thread"), ownerId: "call", name: "page-1.html" };
+
+ it("is kept apart from the pictures, so a build that reads only pictures never meets it", async () => {
+ const { root, store } = await fixture();
+ const picture = await store.ingestBytes(await image(), owner);
+ const page = await store.ingestPage(["call"], Buffer.from("
Plan
"), pageOwner);
+ const pictures = await NodeFSP.readdir(NodePath.join(root, "home", "occurrences"));
+ expect(pictures).toEqual([`${picture.id}.json`]);
+ expect(await store.pageOccurrence(page.id)).toEqual(page);
+ expect(await store.pageOwners(page.original.digest)).toEqual([page]);
+ expect(await store.owners(page.original.digest)).toEqual([]);
+ });
+
+ it("is kept once for the same call, however often it is told", async () => {
+ const { store } = await fixture();
+ const first = await store.ingestPage(["call"], Buffer.from("Plan
"), pageOwner);
+ const again = await store.ingestPage(["call"], Buffer.from("Plan
"), pageOwner);
+ expect(again.id).toBe(first.id);
+ });
+
+ it("is never the same object as the same bytes kept as a picture", async () => {
+ const { store } = await fixture();
+ const svg = Buffer.from('');
+ const picture = await store.ingestBytes(svg, { ...owner, name: "dot.svg" });
+ const page = await store.ingestPage(["call"], svg, pageOwner);
+ if (picture.original.status !== "ready") throw new Error("picture refused");
+ expect(page.original.digest).not.toBe(picture.original.digest);
+ expect((await store.object(picture.original.digest)).mimeType).toBe("image/svg+xml");
+ expect((await store.object(page.original.digest)).mimeType).toBe("text/html");
+ });
+});
+
+it("an occurrence this build cannot read never hides every other picture's readers", async () => {
+ const { root, store } = await fixture();
+ const picture = await store.ingestBytes(await image(), owner);
+ if (picture.original.status !== "ready") throw new Error("picture refused");
+ await NodeFSP.writeFile(
+ NodePath.join(root, "home", "occurrences", "00000000-0000-4000-8000-000000000000.json"),
+ JSON.stringify({
+ ...picture,
+ id: "x",
+ original: { ...picture.original, mimeType: "text/x-newer" },
+ }),
+ );
+ expect(
+ (await new ContentAssets(NodePath.join(root, "home")).owners(picture.original.digest)).map(
+ (each) => each.id,
+ ),
+ ).toEqual([picture.id]);
+});
diff --git a/apps/server/src/assets/ContentAssets.ts b/apps/server/src/assets/ContentAssets.ts
index 76731a2be2..bbaedccc2d 100644
--- a/apps/server/src/assets/ContentAssets.ts
+++ b/apps/server/src/assets/ContentAssets.ts
@@ -4,9 +4,10 @@ import * as NodeFS from "node:fs";
import * as NodeFSP from "node:fs/promises";
import * as NodePath from "node:path";
-import type { AssetRepresentation, ImageOccurrence } from "@t3tools/contracts";
+import type { AssetRepresentation, ImageOccurrence, PageOccurrence } from "@t3tools/contracts";
import {
ImageOccurrence as OccurrenceSchema,
+ PageOccurrence as PageOccurrenceSchema,
AssetRepresentation as RepresentationSchema,
} from "@t3tools/contracts";
import * as Schema from "effect/Schema";
@@ -14,8 +15,12 @@ import { mediaMimeTypeFromExtension } from "@t3tools/shared/filePreview";
import sharp, { type Metadata } from "sharp";
const decodeOccurrence = Schema.decodeUnknownSync(OccurrenceSchema);
+const decodePageOccurrence = Schema.decodeUnknownSync(PageOccurrenceSchema);
const decodeRepresentation = Schema.decodeUnknownSync(RepresentationSchema);
const digestOf = (bytes: Uint8Array) => NodeCrypto.createHash("sha256").update(bytes).digest("hex");
+/** A page's object identity: its bytes as a page, never the same object as the same bytes as a picture. */
+const pageDigestOf = (bytes: Uint8Array) =>
+ NodeCrypto.createHash("sha256").update("mate-page\0").update(bytes).digest("hex");
const hasCode = (error: unknown, code: string) =>
typeof error === "object" && error !== null && "code" in error && error.code === code;
export class ContentAssetError extends Error {
@@ -56,6 +61,8 @@ export function isStorageFull(error: unknown): boolean {
return false;
}
type Owner = Omit & { readonly mimeType?: string };
+/** The one kind of original besides a picture: a page an agent published. */
+export const PAGE_MIME_TYPE = "text/html";
type StoredObject = AssetRepresentation & { readonly path: string };
const stores = new Map();
export const contentAssetsAt = (stateDir: string) => {
@@ -72,6 +79,7 @@ export const contentAssetsAt = (stateDir: string) => {
export class ContentAssets {
private mutation: Promise = Promise.resolve();
private ownership: Promise