Skip to content

Commit d0acbf5

Browse files
Merge remote-tracking branch 'origin/main' into fix/issue-32829
2 parents 4b2a6e8 + 09a54f0 commit d0acbf5

25 files changed

Lines changed: 1030 additions & 113 deletions

‎apps/server/src/htmlRender/HtmlRender.test.ts‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { assert, describe, expect, it } from "@effect/vitest";
44
import { EnvironmentId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
55
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
66
import * as HostProcess from "@t3tools/shared/HostProcess";
7+
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
78
import * as Deferred from "effect/Deferred";
89
import * as Effect from "effect/Effect";
910
import * as Exit from "effect/Exit";
@@ -259,6 +260,119 @@ describe("HtmlRender", () => {
259260
}).pipe(Effect.provide(layerTest)),
260261
);
261262

263+
it.effect("publishes a page from an .html file as a snapshot that path calls recover", () =>
264+
Effect.gen(function* () {
265+
const fileSystem = yield* FileSystem.FileSystem;
266+
const path = yield* Path.Path;
267+
const config = yield* ServerConfig.ServerConfig;
268+
const htmlRender = yield* HtmlRender.HtmlRender;
269+
const mcpSessions = yield* McpProviderSessions.McpProviderSessions;
270+
const threadId = ThreadId.make("thread-html-path");
271+
const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-html-page-" });
272+
const file = path.join(directory, "report.html");
273+
yield* fileSystem.writeFileString(file, "<!doctype html><p>First draft</p>");
274+
const page = { path: file, title: "Report", height: 300 };
275+
276+
const first = yield* htmlRender.publish({ threadId, ...page });
277+
yield* fileSystem.writeFileString(file, "<!doctype html><p>Second draft</p>");
278+
const second = yield* htmlRender.publish({ threadId, ...page });
279+
const inline = { html: "<!doctype html><p>First draft</p>", title: "Report", height: 300 };
280+
const inlineReference = yield* htmlRender.publish({ threadId, ...inline });
281+
282+
const stored = yield* fileSystem.readFileString(
283+
resolveAttachmentPathById({
284+
attachmentsDir: config.attachmentsDir,
285+
attachmentId: first.attachmentId,
286+
}) ?? "",
287+
);
288+
expect(stored).toContain("<p>First draft</p>");
289+
expect(yield* mcpSessions.takeHtmlRender(threadId, { ...page, path: `${file}x` })).toBe(
290+
undefined,
291+
);
292+
for (const html of [inline.html, false]) {
293+
expect(yield* mcpSessions.takeHtmlRender(threadId, { ...page, html })).toBe(undefined);
294+
}
295+
expect(yield* mcpSessions.takeHtmlRender(threadId, { ...inline, path: null })).toEqual(
296+
inlineReference,
297+
);
298+
expect(yield* mcpSessions.takeHtmlRender(threadId, { ...page, html: null })).toEqual(first);
299+
expect(yield* mcpSessions.takeHtmlRender(threadId, page)).toEqual(second);
300+
expect(yield* mcpSessions.takeHtmlRender(threadId, page)).toBe(undefined);
301+
}).pipe(Effect.provide(layerTest)),
302+
);
303+
304+
it.effect("refuses anything but a regular, singly linked .html file of at most 25 MiB", () =>
305+
Effect.gen(function* () {
306+
const fileSystem = yield* FileSystem.FileSystem;
307+
const path = yield* Path.Path;
308+
const config = yield* ServerConfig.ServerConfig;
309+
const htmlRender = yield* HtmlRender.HtmlRender;
310+
const threadId = ThreadId.make("thread-html-path-guard");
311+
const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-html-page-" });
312+
const secret = path.join(directory, "id_rsa");
313+
yield* fileSystem.writeFileString(secret, "PRIVATE KEY");
314+
const page = path.join(directory, "page.html");
315+
yield* fileSystem.writeFileString(page, "<!doctype html><p>linked</p>");
316+
const hardLinked = path.join(directory, "linked.html");
317+
yield* fileSystem.link(page, hardLinked);
318+
// `mv .env renamed.html` keeps one link and an .html name; only its bytes give it away.
319+
const renamed = path.join(directory, "renamed.html");
320+
yield* fileSystem.writeFileString(renamed, "API_KEY=abc123\n");
321+
const folder = path.join(directory, "folder.html");
322+
yield* fileSystem.makeDirectory(folder);
323+
const internal = path.join(config.secretsDir, "page.html");
324+
yield* fileSystem.makeDirectory(config.secretsDir, { recursive: true });
325+
yield* fileSystem.writeFileString(internal, "<!doctype html><p>internal</p>");
326+
327+
for (const file of [
328+
"page.html",
329+
"//attacker/share/page.html",
330+
path.join(directory, "missing.html"),
331+
secret,
332+
hardLinked,
333+
renamed,
334+
folder,
335+
internal,
336+
]) {
337+
const error = yield* htmlRender
338+
.publish({ threadId, path: file, title: "X", height: 200 })
339+
.pipe(Effect.flip);
340+
expect(error, file).toBeInstanceOf(HtmlRender.HtmlRenderFileError);
341+
}
342+
const huge = path.join(directory, "huge.html");
343+
yield* fileSystem.writeFileString(huge, "");
344+
yield* fileSystem.truncate(huge, 25 * 1024 * 1024 + 1);
345+
const tooLarge = yield* htmlRender
346+
.publish({ threadId, path: huge, title: "X", height: 200 })
347+
.pipe(Effect.flip);
348+
expect(tooLarge).toBeInstanceOf(HtmlRender.HtmlRenderFileTooLargeError);
349+
}).pipe(Effect.provide(layerTest)),
350+
);
351+
352+
it.effect.skipIf(!symlinksSupported)("refuses an .html symlink to another file", () =>
353+
Effect.gen(function* () {
354+
const fileSystem = yield* FileSystem.FileSystem;
355+
const path = yield* Path.Path;
356+
const htmlRender = yield* HtmlRender.HtmlRender;
357+
const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-html-page-" });
358+
const secret = path.join(directory, "id_rsa");
359+
yield* fileSystem.writeFileString(secret, "PRIVATE KEY");
360+
const symlinked = path.join(directory, "symlinked.html");
361+
yield* fileSystem.symlink(secret, symlinked);
362+
363+
const error = yield* htmlRender
364+
.publish({
365+
threadId: ThreadId.make("thread-html-path-link"),
366+
path: symlinked,
367+
title: "X",
368+
height: 200,
369+
})
370+
.pipe(Effect.flip);
371+
372+
expect(error).toBeInstanceOf(HtmlRender.HtmlRenderFileError);
373+
}).pipe(Effect.provide(layerTest)),
374+
);
375+
262376
it.effect("removes the page when publishing is interrupted", () =>
263377
Effect.gen(function* () {
264378
const threadId = ThreadId.make("thread-html-cancel");

‎apps/server/src/htmlRender/HtmlRender.ts‎

Lines changed: 125 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ThreadId } from "@t3tools/contracts";
22
import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
33
import * as HostProcess from "@t3tools/shared/HostProcess";
4+
import { isNetworkPath } from "@t3tools/shared/path";
45
import {
56
clampHtmlRenderHeight,
67
HTML_RENDER_COLUMN_WIDTH,
@@ -22,14 +23,16 @@ import * as Effect from "effect/Effect";
2223
import * as FileSystem from "effect/FileSystem";
2324
import * as Layer from "effect/Layer";
2425
import * as Option from "effect/Option";
25-
import type * as Path from "effect/Path";
26+
import * as Path from "effect/Path";
2627
import * as Schema from "effect/Schema";
2728
import * as Semaphore from "effect/Semaphore";
2829
import * as Stream from "effect/Stream";
2930
import type * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
3031

32+
import { openMediaFile } from "../assets/MediaFile.ts";
3133
import { resolveAttachmentRelativePath } from "../attachmentPaths.ts";
3234
import { createAttachmentId } from "../attachmentStore.ts";
35+
import { makeIsServerInternalPath } from "../auth/serverInternalPaths.ts";
3336
import { resolveRootCliCommand } from "../cli/invocation.ts";
3437
import * as ServerConfig from "../config.ts";
3538
import * as HeadlessChrome from "./headlessChrome.ts";
@@ -39,8 +42,29 @@ import * as PreviewBrowserHost from "../preview/PreviewBrowserHost.ts";
3942
const MIB = 1024 * 1024;
4043
const MAX_IMAGE_BYTES = 10 * MIB;
4144
const MAX_PAGE_BYTES = 25 * MIB;
45+
const HTML_FILE_EXTENSION = /\.html?$/i;
46+
// Like the image check, judge the bytes: a renamed .env or key does not start like a page.
47+
const HTML_DOCUMENT_START = /^\s*<(?:!doctype\s+html|html)[\s>]/i;
4248
const formatMib = (bytes: number) => `${(bytes / MIB).toFixed(1)} MiB`;
4349

50+
export class HtmlRenderFileError extends Schema.TaggedError<HtmlRenderFileError>()(
51+
"HtmlRenderFileError",
52+
{ path: Schema.String, cause: Schema.optional(Schema.Defect()) },
53+
) {
54+
override get message(): string {
55+
return `${this.path} is not a readable HTML document. Pass an absolute path to a regular .html file that starts with <!doctype html> or <html>, or pass html instead.`;
56+
}
57+
}
58+
59+
export class HtmlRenderFileTooLargeError extends Schema.TaggedError<HtmlRenderFileTooLargeError>()(
60+
"HtmlRenderFileTooLargeError",
61+
{ path: Schema.String, sizeBytes: Schema.Number },
62+
) {
63+
override get message(): string {
64+
return `${this.path} is ${formatMib(this.sizeBytes)}; a page must be at most ${formatMib(MAX_PAGE_BYTES)}.`;
65+
}
66+
}
67+
4468
export class HtmlRenderImagesNotFoundError extends Schema.TaggedError<HtmlRenderImagesNotFoundError>()(
4569
"HtmlRenderImagesNotFoundError",
4670
{ paths: Schema.Array(Schema.String) },
@@ -108,6 +132,8 @@ export type HtmlRenderPrepareError =
108132
| HtmlRenderPageTooLargeError
109133
| HtmlRenderViewportHeightError;
110134

135+
export type HtmlRenderSource = { readonly html: string } | { readonly path: string };
136+
111137
export interface HtmlPreview {
112138
readonly png: string;
113139
readonly width: number;
@@ -121,18 +147,28 @@ export class HtmlRender extends Context.Service<
121147
HtmlRender,
122148
{
123149
readonly prepare: (html: string) => Effect.Effect<string, HtmlRenderPrepareError>;
124-
readonly publish: (input: {
125-
readonly threadId: ThreadId;
126-
readonly html: string;
127-
readonly title: string;
128-
readonly height: number;
129-
}) => Effect.Effect<HtmlRenderReference, HtmlRenderPrepareError | HtmlRenderStoreError>;
130-
readonly preview: (input: {
131-
readonly html: string;
132-
readonly width?: number | undefined;
133-
readonly appearance?: ThemeAppearance | undefined;
134-
}) => Effect.Effect<
150+
readonly publish: (
151+
input: HtmlRenderSource & {
152+
readonly threadId: ThreadId;
153+
readonly title: string;
154+
readonly height: number;
155+
},
156+
) => Effect.Effect<
157+
HtmlRenderReference,
158+
| HtmlRenderFileError
159+
| HtmlRenderFileTooLargeError
160+
| HtmlRenderPrepareError
161+
| HtmlRenderStoreError
162+
>;
163+
readonly preview: (
164+
input: HtmlRenderSource & {
165+
readonly width?: number | undefined;
166+
readonly appearance?: ThemeAppearance | undefined;
167+
},
168+
) => Effect.Effect<
135169
HtmlPreview,
170+
| HtmlRenderFileError
171+
| HtmlRenderFileTooLargeError
136172
| Exclude<HtmlRenderPrepareError, HtmlRenderImagesNotFoundError>
137173
| PreviewBrowser.PreviewBrowserInstallError
138174
| PreviewBrowser.PreviewBrowserInstallingError
@@ -331,7 +367,9 @@ const MEASURE_FRAGMENT = htmlRenderThemeFragment(
331367

332368
const make = Effect.gen(function* () {
333369
const fileSystem = yield* FileSystem.FileSystem;
370+
const path = yield* Path.Path;
334371
const config = yield* ServerConfig.ServerConfig;
372+
const isServerInternalPath = yield* makeIsServerInternalPath;
335373
const previewBrowser = yield* PreviewBrowser.PreviewBrowser;
336374
const mcpSessions = yield* McpProviderSessions.McpProviderSessions;
337375
const services = yield* Effect.context<
@@ -400,6 +438,53 @@ const make = Effect.gen(function* () {
400438
),
401439
);
402440

441+
const readPageFile = Effect.fn("HtmlRender.readPageFile")(function* (filePath: string) {
442+
const unreadable = new HtmlRenderFileError({ path: filePath });
443+
if (!path.isAbsolute(filePath) || isNetworkPath(filePath)) return yield* unreadable;
444+
// Check the resolved file, so a symlink named .html cannot reach another file.
445+
const realPath = yield* fileSystem
446+
.realPath(filePath)
447+
.pipe(Effect.mapError((cause) => new HtmlRenderFileError({ path: filePath, cause })));
448+
if (!HTML_FILE_EXTENSION.test(realPath) || (yield* isServerInternalPath(realPath))) {
449+
return yield* unreadable;
450+
}
451+
// Open without following links and recheck the path, so a link or FIFO swapped in is refused.
452+
const html = yield* Effect.scoped(
453+
Effect.gen(function* () {
454+
const file = yield* openMediaFile(realPath).pipe(
455+
Effect.mapError((cause) => new HtmlRenderFileError({ path: filePath, cause })),
456+
);
457+
// Unlike client reads, the caller may be sandboxed: a second hard link could give any
458+
// file an .html name.
459+
if (file === null || file.info.nlink !== 1n) return yield* unreadable;
460+
if (file.info.size > BigInt(MAX_PAGE_BYTES)) {
461+
return yield* new HtmlRenderFileTooLargeError({
462+
path: filePath,
463+
sizeBytes: Number(file.info.size),
464+
});
465+
}
466+
// One read may return fewer bytes, so fill up to the checked size; growth past it is ignored.
467+
return yield* Effect.tryPromise({
468+
try: async () => {
469+
const buffer = Buffer.alloc(Number(file.info.size));
470+
let filled = 0;
471+
while (filled < buffer.length) {
472+
const { bytesRead } = await file.handle.read(buffer, filled, buffer.length - filled);
473+
if (bytesRead === 0) break;
474+
filled += bytesRead;
475+
}
476+
return buffer.toString("utf8", 0, filled);
477+
},
478+
catch: (cause) => new HtmlRenderFileError({ path: filePath, cause }),
479+
});
480+
}),
481+
);
482+
return HTML_DOCUMENT_START.test(html) ? html : yield* unreadable;
483+
});
484+
485+
const readSource = (input: HtmlRenderSource) =>
486+
"path" in input ? readPageFile(input.path) : Effect.succeed(input.html);
487+
403488
// The bootstrap goes in first so its head scan never runs over inlined image data.
404489
const inline = (html: string) =>
405490
inlineLocalImages(injectHtmlRenderBootstrap(html)).pipe(Effect.provideContext(services));
@@ -413,13 +498,15 @@ const make = Effect.gen(function* () {
413498
return inlined.html;
414499
});
415500

416-
const publish = Effect.fn("HtmlRender.publish")(function* (input: {
417-
readonly threadId: ThreadId;
418-
readonly html: string;
419-
readonly title: string;
420-
readonly height: number;
421-
}) {
422-
const html = yield* prepare(input.html);
501+
const publish = Effect.fn("HtmlRender.publish")(function* (
502+
input: HtmlRenderSource & {
503+
readonly threadId: ThreadId;
504+
readonly title: string;
505+
readonly height: number;
506+
},
507+
) {
508+
const source = yield* readSource(input);
509+
const html = yield* prepare(source);
423510
// The `-html` id suffix makes the asset route serve it as a sandboxed text/html document.
424511
const attachmentId = createAttachmentId(input.threadId, "html");
425512
const filePath =
@@ -447,7 +534,16 @@ const make = Effect.gen(function* () {
447534
...(heights === undefined ? {} : { heights }),
448535
};
449536
return mcpSessions
450-
.recordHtmlRender(input.threadId, input, reference)
537+
.recordHtmlRender(
538+
input.threadId,
539+
{
540+
html: source,
541+
path: "path" in input ? input.path : undefined,
542+
title: input.title,
543+
height: input.height,
544+
},
545+
reference,
546+
)
451547
.pipe(Effect.as(reference));
452548
}),
453549
// An interrupted publish must leave neither an attachment nor a recoverable reference.
@@ -463,18 +559,20 @@ const make = Effect.gen(function* () {
463559
);
464560
});
465561

466-
const preview = Effect.fn("HtmlRender.preview")(function* (input: {
467-
readonly html: string;
468-
readonly width?: number | undefined;
469-
readonly appearance?: ThemeAppearance | undefined;
470-
}) {
562+
const preview = Effect.fn("HtmlRender.preview")(function* (
563+
input: HtmlRenderSource & {
564+
readonly width?: number | undefined;
565+
readonly appearance?: ThemeAppearance | undefined;
566+
},
567+
) {
471568
const width = Math.min(
472569
MAX_PREVIEW_WIDTH,
473570
Math.max(MIN_PREVIEW_WIDTH, Math.round(input.width ?? HTML_RENDER_COLUMN_WIDTH)),
474571
);
475572
const appearance = input.appearance ?? "dark";
476-
yield* rejectViewportHeight(input.html);
477-
const inlined = yield* inline(input.html);
573+
const source = yield* readSource(input);
574+
yield* rejectViewportHeight(source);
575+
const inlined = yield* inline(source);
478576
const executable = yield* previewBrowser.executable;
479577
const theme = htmlRenderTheme(
480578
appearance === "light" ? T3_CODE_LIGHT_THEME_COLORS : T3_CODE_DARK_THEME_COLORS,

0 commit comments

Comments
 (0)