Files
core/src/views.ts
T
chrnx 53660adcce fix(views): confine static serving to the root's real path
The static resolver's traversal guard compared paths lexically, and the
read that followed it resolved symlinks, so a link inside the root such
as `public/leak -> /etc/hosts` served a file outside it.

Containment now compares real paths. The root's real path is resolved
on the first request and cached (a failed lookup is not, so a root
created after boot still starts serving); the requested file and a
directory's `index.html` are each realpath'd and must lie at or beneath
it, with the `root + sep` rule keeping a sibling such as `root-evil`
out. The lexical guard stays in front, since it rejects `..` without a
syscall.

An escape or a dangling link is a plain miss: 404 through `onError`, or
405/204 where the path has routes. Not 403, so a probe cannot tell a
file outside the root from one that does not exist. Re-pointing a root
symlink at runtime needs a restart. The realpath-to-read race is left
open and marked; closing it needs per-component O_NOFOLLOW, which is
not portable across Node, Deno and Bun.

Closes #106

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-10-09 13:59:03 -05:00

190 lines
8.2 KiB
TypeScript

// src/views.ts — HTML views: built-in templating, content-type map, and boot-time builders.
// fs/path are lazy-required inside functions so the barrel stays edge-loadable (workerd has no filesystem).
import type { TransformerFn } from './metadata';
import type { HtmlMeta } from './metadata';
import { nodeRequire } from './node-require';
/** Escape the five HTML-significant characters so interpolated data can't inject markup. */
export function escapeHtml(s: string): string {
const map: Record<string, string> = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' };
return s.replace(/[&<>"']/g, (c) => map[c]);
}
function stringify(v: unknown): string {
return v === null || v === undefined ? '' : String(v);
}
/**
* Minimal built-in template engine: `{{ key }}` interpolates HTML-escaped, `{{{ key }}}` raw,
* `{{ a.b }}` walks nested keys, a missing key renders empty. No loops/conditionals/partials.
*/
export function render(source: string, data: unknown): string {
const lookup = (key: string): unknown =>
key.split('.').reduce<any>((obj, k) => (obj === null || obj === undefined ? undefined : obj[k]), data);
return source
.replace(/\{\{\{\s*([\w.]+)\s*\}\}\}/g, (_, key) => stringify(lookup(key)))
.replace(/\{\{\s*([\w.]+)\s*\}\}/g, (_, key) => escapeHtml(stringify(lookup(key))));
}
const CONTENT_TYPES: Record<string, string> = {
'.html': 'text/html; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.js': 'text/javascript; charset=utf-8',
'.mjs': 'text/javascript; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.svg': 'image/svg+xml',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.webp': 'image/webp',
'.ico': 'image/x-icon',
'.woff2': 'font/woff2',
'.txt': 'text/plain; charset=utf-8',
};
/** Content-type for a file extension (case-insensitive, leading dot); unknown → `application/octet-stream`. */
export function contentTypeFor(ext: string): string {
return CONTENT_TYPES[ext.toLowerCase()] ?? 'application/octet-stream';
}
const HTML_HEADERS = { 'content-type': 'text/html; charset=utf-8' } as const;
/** Boot-time context for building `@Html` transformers: the views base dir and an optional BYO engine. */
export interface ViewsContext {
views?: string;
viewEngine?: (source: string, data: unknown) => string;
}
/** Reads a view file at boot (lazy fs). Relative paths resolve against `views` (default cwd); absolute as-is.
* node:fs/node:path are loaded lazily so importing the barrel stays edge/workerd-safe
* (workerd's nodejs_compat provides no filesystem). This path only runs for path/template `@Html` modes.
* Unguarded on purpose: `@Html('file')` cannot degrade into anything useful, so a runtime with no
* filesystem should hear about it here rather than at the first request. */
function readViewFile(views: string | undefined, path: string): string {
const nodePath = nodeRequire<typeof import('node:path')>('node:path');
const fs = nodeRequire<typeof import('node:fs')>('node:fs');
const base = views ?? process.cwd();
const full = nodePath.isAbsolute(path) ? path : nodePath.resolve(base, path);
try {
return fs.readFileSync(full, 'utf8');
} catch (err) {
throw new Error(
`@Html('${path}') could not read '${full}' — on a runtime without a filesystem (edge), return a string from @Html instead. (${(err as Error).message})`,
);
}
}
/** A static-file hit: the raw bytes and the content-type to send. */
export interface StaticHit {
body: Buffer;
contentType: string;
}
/** Resolves a URL path to a static file under a fixed root, or `undefined` for a miss. */
export type StaticResolver = (urlPath: string) => Promise<StaticHit | undefined>;
/** The real path of `path` when it is `root` or lies beneath it, else `undefined`. Rejects on ENOENT.
* The separator suffix keeps a sibling such as `root-evil` out of `root`. */
async function confine(fs: typeof import('node:fs'), sep: string, root: string, path: string) {
const real = await fs.promises.realpath(path);
return real === root || real.startsWith(root + sep) ? real : undefined;
}
/** Reads `full` (or its `index.html` when a directory) if its real path stays inside the real `root`. */
async function readConfined(
fs: typeof import('node:fs'),
nodePath: typeof import('node:path'),
root: string,
full: string,
): Promise<StaticHit | undefined> {
let real = await confine(fs, nodePath.sep, root, full);
if (!real) return undefined;
const isDir = (await fs.promises.stat(real)).isDirectory();
// `index.html` is a second resolution and can itself be a link, so it is confined on its own.
if (isDir) real = await confine(fs, nodePath.sep, root, nodePath.join(real, 'index.html'));
if (!real) return undefined;
// ponytail: a link swapped between `realpath` and `readFile` can still escape. Exploiting it needs
// write access inside the served root, which already owns the app; closing it needs `O_NOFOLLOW`
// on every path component, which Node, Deno and Bun do not offer portably.
const body = await fs.promises.readFile(real);
// The content-type follows the requested name, not the link target (`app.js -> app.3f2a.js`).
const target = isDir ? nodePath.join(full, 'index.html') : full;
return { body, contentType: contentTypeFor(nodePath.extname(target)) };
}
/**
* Builds a static-file resolver rooted at `root` (`true` → `./public`). Requires a filesystem: throws at
* boot on a runtime without one (edge). Path-traversal-safe — a resolved path outside the root is a miss,
* and so is a symlink whose real target lies outside the root's real path (#106). Escapes are misses
* (404), not 403, so a probe cannot tell "exists outside the root" from "does not exist".
*/
export function buildStaticResolver(root: string | true): StaticResolver {
let fs: typeof import('node:fs');
let nodePath: typeof import('node:path');
try {
fs = nodeRequire('node:fs');
nodePath = nodeRequire('node:path');
} catch {
throw new Error(
'`static` needs a filesystem and is unavailable on this runtime (edge). Remove it or serve assets from a CDN.',
);
}
const abs = nodePath.resolve(process.cwd(), root === true ? 'public' : root);
// The root's real path, resolved once on the first request rather than at build time, so a missing
// `public/` at boot is still not fatal — it just makes every request a miss, as before. Containment
// compares real paths, so the root must be real too: a symlinked `public/` would otherwise fail every
// check. Cached for the life of the app: re-pointing a root symlink at runtime needs a restart. A failed
// lookup is not cached, so a root created after boot starts serving, as it did before.
let realRoot: Promise<string | undefined> | undefined;
const resolveRoot = () =>
(realRoot ??= fs.promises.realpath(abs).catch(() => {
realRoot = undefined;
return undefined;
}));
return async (urlPath) => {
let rel: string;
try {
rel = decodeURIComponent(urlPath.split('?')[0]);
} catch {
return undefined;
}
if (rel === '' || rel.endsWith('/')) rel += 'index.html';
const full = nodePath.resolve(abs, '.' + (rel.startsWith('/') ? rel : '/' + rel));
if (full !== abs && !full.startsWith(abs + nodePath.sep)) return undefined; // traversal guard
const rootReal = await resolveRoot();
if (!rootReal) return undefined;
try {
return await readConfined(fs, nodePath, rootReal, full);
} catch {
return undefined; // includes ENOENT from `realpath` on a dangling link: a miss, never a rejection
}
};
}
/** Turns `@Html` metadata into a response transformer at boot. String mode is fs-free; path/template modes read+cache the file. */
export function buildHtmlTransformer(meta: HtmlMeta, ctx: ViewsContext): TransformerFn {
if (!meta.path) {
return (value) => ({ status: 200, headers: { ...HTML_HEADERS }, body: String(value) });
}
const source = readViewFile(ctx.views, meta.path);
if (meta.template) {
const engine = ctx.viewEngine ?? render;
return (value) => ({ status: 200, headers: { ...HTML_HEADERS }, body: engine(source, value) });
}
return () => ({ status: 200, headers: { ...HTML_HEADERS }, body: source });
}