11import type { ThreadId } from "@t3tools/contracts" ;
22import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions" ;
33import * as HostProcess from "@t3tools/shared/HostProcess" ;
4+ import { isNetworkPath } from "@t3tools/shared/path" ;
45import {
56 clampHtmlRenderHeight ,
67 HTML_RENDER_COLUMN_WIDTH ,
@@ -22,14 +23,16 @@ import * as Effect from "effect/Effect";
2223import * as FileSystem from "effect/FileSystem" ;
2324import * as Layer from "effect/Layer" ;
2425import * as Option from "effect/Option" ;
25- import type * as Path from "effect/Path" ;
26+ import * as Path from "effect/Path" ;
2627import * as Schema from "effect/Schema" ;
2728import * as Semaphore from "effect/Semaphore" ;
2829import * as Stream from "effect/Stream" ;
2930import type * as ChildProcessSpawner from "effect/process/ChildProcessSpawner" ;
3031
32+ import { openMediaFile } from "../assets/MediaFile.ts" ;
3133import { resolveAttachmentRelativePath } from "../attachmentPaths.ts" ;
3234import { createAttachmentId } from "../attachmentStore.ts" ;
35+ import { makeIsServerInternalPath } from "../auth/serverInternalPaths.ts" ;
3336import { resolveRootCliCommand } from "../cli/invocation.ts" ;
3437import * as ServerConfig from "../config.ts" ;
3538import * as HeadlessChrome from "./headlessChrome.ts" ;
@@ -39,8 +42,29 @@ import * as PreviewBrowserHost from "../preview/PreviewBrowserHost.ts";
3942const MIB = 1024 * 1024 ;
4043const MAX_IMAGE_BYTES = 10 * MIB ;
4144const MAX_PAGE_BYTES = 25 * MIB ;
45+ const HTML_FILE_EXTENSION = / \. h t m l ? $ / 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 * < (?: ! d o c t y p e \s + h t m l | h t m l ) [ \s > ] / i;
4248const 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+
4468export 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+
111137export 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
332368const 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