Skip to main content

umbral_core/
templates.rs

1//! Server-side HTML rendering via minijinja.
2//!
3//! Templates live under one or more directories on disk. At boot,
4//! `App::build()` assembles an ordered search list:
5//!
6//! 1. The project-level directory configured via
7//!    `AppBuilder::templates_dir` (default `./templates`).
8//! 2. Each registered plugin's `Plugin::templates_dirs()` contributions,
9//!    in topological dependency order.
10//!
11//! The first directory that contains a given template name wins. This
12//! makes cross-plugin `{% extends "base.html" %}` work automatically —
13//! the extends lookup searches every directory the same way a direct
14//! render call does. Plugin A can extend `base.html` from plugin B as
15//! long as B's directory appears in the search list.
16//!
17//! When two directories both provide a template with the same name, the
18//! first-match-wins policy applies and a `tracing::warn!` is emitted at
19//! boot so the collision is visible in the log. First-match-wins across
20//! all template directories. Silently-overridden templates are a
21//! well-known footgun, so the warning is non-optional.
22//!
23//! Rendering goes through one ambient accessor, [`render`], which reads
24//! the engine the App builder published into an `OnceLock` during build.
25//!
26//! ```ignore
27//! let html = umbral::templates::render("articles_list.html", &context!(articles))?;
28//! ```
29//!
30//! ## Autoescape
31//!
32//! Any template whose name ends in `.html` or `.htm` renders with
33//! autoescape on. Text templates (`.txt`) render verbatim. The autoescape
34//! callback extension whitelist MUST stay in sync with the loader's
35//! `load_directory` filter (currently `html | htm | txt`).
36//!
37//! ## v1 scope
38//!
39//! - One project-level templates directory (default `./templates/`,
40//!   relative to the binary's cwd) plus per-plugin directories.
41//! - Jinja2-compatible syntax via minijinja: `{% extends %}`, `{% block %}`,
42//!   `{% if %}`, `{% for %}`, `{{ value }}`, the standard filter set.
43//! - Autoescape for any template whose name ends in `.html` or `.htm`.
44//! - Init is best-effort: if no directory exists the engine boots empty.
45//!   Calls to [`render`] then return `TemplateError::Missing`.
46//!
47//! ## Deferred
48//!
49//! - Custom filters and tests registered through `Plugin::on_ready`.
50//! - Hot reload in development via `minijinja-autoreload`.
51
52use std::collections::HashSet;
53use std::future::Future;
54use std::path::{Path, PathBuf};
55use std::pin::Pin;
56use std::sync::Arc;
57use std::sync::OnceLock;
58
59use minijinja::{AutoEscape, Environment};
60use syntect::highlighting::ThemeSet;
61use syntect::html::{ClassStyle, ClassedHTMLGenerator, css_for_theme_with_class_style};
62use syntect::parsing::SyntaxSet;
63use syntect::util::LinesWithEndings;
64
65tokio::task_local! {
66    /// Per-request ambient user value, set by a session-aware layer
67    /// (typically `umbral_sessions::UserContextLayer<U>`) and read by
68    /// [`render`] to expose the current `user` in
69    /// templates. `None` means an anonymous request.
70    ///
71    /// Outside the layer's scope, `try_with` returns `Err(AccessError)`
72    /// and `render` skips the merge — explicit ctx behaviour is
73    /// preserved when no layer is installed.
74    pub static CURRENT_USER: Option<minijinja::Value>;
75
76    /// Per-request CSRF token, set by `umbral-security`'s middleware and
77    /// read by [`render`] to inject `csrf_token` / `csrf_input` into
78    /// every template, for the `{% csrf_token %}` ergonomic. Outside
79    /// the middleware's scope nothing is injected (a template that
80    /// references `{{ csrf_token }}` then renders it empty under the
81    /// engine's lenient-undefined behaviour).
82    pub static CURRENT_CSRF: Option<String>;
83
84    /// Lazy counterpart to `CURRENT_USER`: a resolver that produces the
85    /// user value on first access, memoized. Set by an auth middleware that
86    /// wants per-request laziness (resolve only if a template reads `user`).
87    pub static CURRENT_USER_LAZY: LazyUser;
88}
89
90type UserFut = Pin<Box<dyn Future<Output = minijinja::Value> + Send>>;
91type UserResolver = Arc<dyn Fn() -> UserFut + Send + Sync>;
92
93/// A lazily-resolved, per-request template `user`. The `resolver` runs at
94/// most once (guarded by the `OnceCell`); resolution happens synchronously
95/// from inside minijinja's sync render via `block_in_place`.
96///
97/// The lazy value is injected into the template context as a minijinja `Object`
98/// proxy ([`LazyUserProxy`]). Minijinja calls `get_value` on the proxy only
99/// when the template actually accesses an attribute on `user`, so requests that
100/// never render `user` skip resolution entirely.
101#[derive(Clone)]
102pub struct LazyUser {
103    cell: Arc<tokio::sync::OnceCell<minijinja::Value>>,
104    resolver: UserResolver,
105}
106
107impl LazyUser {
108    pub fn new<F, Fut>(resolver: F) -> Self
109    where
110        F: Fn() -> Fut + Send + Sync + 'static,
111        Fut: Future<Output = minijinja::Value> + Send + 'static,
112    {
113        Self {
114            cell: Arc::new(tokio::sync::OnceCell::new()),
115            resolver: Arc::new(move || Box::pin(resolver())),
116        }
117    }
118
119    /// Resolve (memoized) from a synchronous context. Requires a multi-thread
120    /// tokio runtime; on a current-thread runtime or outside any runtime it
121    /// logs and returns the anonymous value so callers fall back cleanly.
122    fn resolve_blocking(&self) -> minijinja::Value {
123        use tokio::runtime::{Handle, RuntimeFlavor};
124        let Ok(handle) = Handle::try_current() else {
125            return anonymous_user_value();
126        };
127        if handle.runtime_flavor() == RuntimeFlavor::CurrentThread {
128            tracing::warn!(
129                "umbral::templates: lazy `user` needs a multi-thread runtime; rendering anonymous"
130            );
131            return anonymous_user_value();
132        }
133        let cell = self.cell.clone();
134        let resolver = self.resolver.clone();
135        tokio::task::block_in_place(move || {
136            handle.block_on(async move { cell.get_or_init(|| resolver()).await.clone() })
137        })
138    }
139
140    /// Wrap this `LazyUser` in a minijinja `Value` proxy that resolves on
141    /// first attribute access from inside the synchronous render loop.
142    fn into_proxy_value(self) -> minijinja::Value {
143        minijinja::Value::from_object(LazyUserProxy(self))
144    }
145}
146
147/// A minijinja Object proxy that defers resolution of the user until the
148/// template actually accesses an attribute (e.g. `{{ user.is_staff }}`).
149/// Minijinja calls `get_value` for attribute access — we resolve there, not
150/// at context-merge time.
151struct LazyUserProxy(LazyUser);
152
153impl std::fmt::Debug for LazyUserProxy {
154    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
155        f.write_str("LazyUserProxy")
156    }
157}
158
159impl std::fmt::Display for LazyUserProxy {
160    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
161        // `{{ user }}` — resolve (memoized) and delegate to the resolved
162        // value's own Display so bare rendering is faithful. Uses the same
163        // `resolve_blocking` path as `get_value` to ensure at-most-once
164        // resolution and the same current-thread / no-runtime fallback.
165        let resolved = self.0.resolve_blocking();
166        std::fmt::Display::fmt(&resolved, f)
167    }
168}
169
170impl minijinja::value::Object for LazyUserProxy {
171    fn get_value(self: &Arc<Self>, key: &minijinja::Value) -> Option<minijinja::Value> {
172        let resolved = self.0.resolve_blocking();
173        resolved.get_item(key).ok()
174    }
175
176    fn is_true(self: &Arc<Self>) -> bool {
177        // `{% if user %}` — resolve (memoized) and delegate to the resolved
178        // value's truthiness so the proxy faithfully represents whether the
179        // resolved value is truthy. Uses the same `resolve_blocking` path as
180        // `get_value` so resolution is still at most once per request.
181        self.0.resolve_blocking().is_true()
182    }
183}
184
185/// Scope a lazy `user` resolver for the duration of `fut`.
186pub async fn with_current_user_lazy<F: Future>(lazy: LazyUser, fut: F) -> F::Output {
187    CURRENT_USER_LAZY.scope(lazy, fut).await
188}
189
190/// Run `fut` with the ambient template user value scoped to `user`
191/// for its duration. Intended for the session-aware layer in
192/// `umbral-sessions`; downstream handler code reads the value
193/// transparently through [`render`].
194pub async fn with_current_user<F: std::future::Future>(
195    user: Option<minijinja::Value>,
196    fut: F,
197) -> F::Output {
198    CURRENT_USER.scope(user, fut).await
199}
200
201/// Run `fut` with the ambient CSRF token scoped for its duration.
202/// Intended for the CSRF middleware in `umbral-security`; downstream
203/// handler code reads the value transparently through [`render`]
204/// (as `{{ csrf_token }}` / `{{ csrf_input }}`) or [`current_csrf`].
205pub async fn with_current_csrf<F: std::future::Future>(token: Option<String>, fut: F) -> F::Output {
206    CURRENT_CSRF.scope(token, fut).await
207}
208
209/// Read the ambient CSRF token, if a middleware has scoped one for
210/// this request. Non-template consumers (e.g. the admin's login form
211/// builder) use this to embed the same token the middleware minted,
212/// instead of minting their own.
213pub fn current_csrf() -> Option<String> {
214    CURRENT_CSRF.try_with(|t| t.clone()).ok().flatten()
215}
216
217/// Watched template directories captured at `init` time. Stored
218/// separately so the dev-mode render path can rebuild the environment
219/// from the same sources without re-publishing the OnceLock.
220static WATCHED_DIRS: OnceLock<Vec<PathBuf>> = OnceLock::new();
221use serde::Serialize;
222
223static ENGINE: OnceLock<Environment<'static>> = OnceLock::new();
224
225/// A plugin-contributed mutation of the template [`Environment`]: adds
226/// custom filters, functions, or globals at engine-build time
227/// (feature #67 - custom template tags/filters). Returned by
228/// `Plugin::template_registrars` and stored process-wide so the dev-mode
229/// hot-reload rebuild re-applies it.
230///
231/// It is `Fn` (not `FnOnce`) on purpose: in dev mode the engine is
232/// rebuilt on every template edit, so each registrar runs once per build.
233/// Make it owned and `'static` (no borrows of the plugin) so it survives
234/// in the [`REGISTRARS`] handle past `App::build`.
235pub type TemplateRegistrar = Box<dyn Fn(&mut Environment<'static>) + Send + Sync>;
236
237/// Plugin-contributed [`TemplateRegistrar`]s captured at `init_with` time.
238/// Stored separately from [`ENGINE`] so the dev-mode rebuild path (which
239/// goes through [`build_env`]) re-applies them without the App builder.
240static REGISTRARS: OnceLock<Vec<TemplateRegistrar>> = OnceLock::new();
241
242/// Register the built-in default 404/500 templates into an environment.
243///
244/// Called from `init` before any disk directories are scanned. The names
245/// use the `__umbral__/` prefix so they can never collide with a user's
246/// `templates/` directory (slashes aren't meaningful to the engine's name
247/// lookup — `__umbral__/default_404.html` is just a unique string key).
248///
249/// Because the user's disk directories are added after this call and
250/// first-match-wins is enforced by the `seen` set, a user who places a
251/// file named `__umbral__/default_404.html` in their own templates dir will
252/// silently replace the built-in — which is the intended escape hatch.
253/// (Callers who want a cleaner opt-out should use
254/// `App::builder().disable_default_error_pages()` instead.)
255/// gaps2 #21 — register the `img` MiniJinja filter that turns a URL
256/// into a fully-formed, performance-correct `<img>` tag.
257///
258/// Filter signature:
259///   `{{ url | img(alt="…", width=N, height=N, class="…") }}`
260///
261/// Output shape:
262///   `<img src="<url>" alt="<alt>" loading="lazy" decoding="async"
263///        width="<w>" height="<h>" class="<class>">`
264///
265/// Why this set of attributes:
266/// - `loading="lazy"` — the gap's primary ask. Browsers defer
267///   off-viewport image fetches until they're about to be needed,
268///   shrinking LCP + initial bandwidth.
269/// - `decoding="async"` — lets the browser decode the image off
270///   the main thread; prevents render-blocking decode work on
271///   slower devices.
272/// - explicit `width`/`height` (when provided) reserves layout
273///   space immediately so lazy-loading doesn't cause CLS
274///   (cumulative layout shift). Omitted if either is missing.
275/// - empty `alt=""` default is screen-reader-friendly for
276///   decorative images. Callers SHOULD pass a real `alt` for
277///   meaningful content images.
278///
279/// What's NOT included on day one (deferred to a later slice):
280/// - `srcset` for responsive resolutions — needs the on-the-fly
281///   resize handler (gap 21 Option C) before the filter knows
282///   real asset dimensions.
283/// - `<picture>` with `webp`/`avif` sources — same blocker; the
284///   transcode endpoint has to exist first.
285///
286/// Output is wrapped in `minijinja::value::Value::from_safe_string`
287/// so MiniJinja's autoescape doesn't double-escape the `<` / `>`
288/// characters — the attribute values themselves still go through
289/// `html_escape` so a hostile alt-text can't break out of the
290/// attribute quote.
291/// True when `url` is safe to place in an `<img src>`: a relative URL
292/// (no scheme) or an `http`/`https` absolute URL. Any other scheme
293/// (`javascript:`, `data:`, `vbscript:`, …) is rejected. Fails closed:
294/// a malformed scheme (embedded control chars, spaces) is also rejected.
295fn url_scheme_is_safe(url: &str) -> bool {
296    let trimmed = url.trim();
297    // A URL scheme is the run before the first ':' — but only if no
298    // '/', '?', '#' appears first (those mean a relative path/query).
299    let mut scheme_end = None;
300    for (i, c) in trimmed.char_indices() {
301        match c {
302            ':' => {
303                scheme_end = Some(i);
304                break;
305            }
306            '/' | '?' | '#' => break,
307            _ => {}
308        }
309    }
310    let Some(end) = scheme_end else {
311        return true; // no scheme → relative URL → safe
312    };
313    let scheme = &trimmed[..end];
314    // A real scheme is alpha then [a-z0-9+.-]*. Anything else is suspicious.
315    let mut chars = scheme.chars();
316    let well_formed = matches!(chars.next(), Some(c) if c.is_ascii_alphabetic())
317        && chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '.' | '-'));
318    if !well_formed {
319        return false;
320    }
321    let lower = scheme.to_ascii_lowercase();
322    lower == "http" || lower == "https"
323}
324
325fn register_img_filter(env: &mut Environment<'static>) {
326    env.add_filter(
327        "img",
328        |url: String,
329         kwargs: minijinja::value::Kwargs|
330         -> Result<minijinja::Value, minijinja::Error> {
331            let alt: String = kwargs.get::<Option<String>>("alt")?.unwrap_or_default();
332            let width: Option<i64> = kwargs.get("width")?;
333            let height: Option<i64> = kwargs.get("height")?;
334            let class: Option<String> = kwargs.get("class")?;
335            // Accept the extras even when the call doesn't pass them
336            // — kwargs.get returns Ok(None) for absent keys but
337            // .assert_all_used() at the end will catch a typo'd
338            // `alt_text` so the user gets a clear error instead of
339            // silent drop. Matches the rest of the framework's
340            // strict-input posture.
341            kwargs.assert_all_used()?;
342
343            // Defense-in-depth: never emit a `javascript:` / `data:` /
344            // other non-http(s) scheme into `src`. Not a live XSS (browsers
345            // don't run JS from `<img src>` and the value is HTML-escaped),
346            // but a hostile stored URL has no business here. A disallowed
347            // scheme neutralises to an empty src (broken image) rather than
348            // erroring the whole page on user data.
349            let url = if url_scheme_is_safe(&url) {
350                url
351            } else {
352                String::new()
353            };
354
355            let mut out = String::with_capacity(url.len() + 128);
356            out.push_str("<img src=\"");
357            html_escape_into(&mut out, &url);
358            out.push_str("\" alt=\"");
359            html_escape_into(&mut out, &alt);
360            out.push_str("\" loading=\"lazy\" decoding=\"async\"");
361            if let Some(w) = width {
362                out.push_str(" width=\"");
363                out.push_str(&w.to_string());
364                out.push('"');
365            }
366            if let Some(h) = height {
367                out.push_str(" height=\"");
368                out.push_str(&h.to_string());
369                out.push('"');
370            }
371            if let Some(c) = class {
372                if !c.is_empty() {
373                    out.push_str(" class=\"");
374                    html_escape_into(&mut out, &c);
375                    out.push('"');
376                }
377            }
378            out.push('>');
379            Ok(minijinja::Value::from_safe_string(out))
380        },
381    );
382}
383
384/// features.md #4 — register the `markdown` filter that turns a
385/// CommonMark + GFM string into sanitized HTML.
386///
387/// Filter signature: `{{ body | markdown }}`.
388///
389/// Pipeline:
390/// 1. `pulldown-cmark` parses the input with GFM extensions on
391///    (tables, strikethrough, task lists, footnotes) and renders to
392///    HTML.
393/// 2. `ammonia` sanitizes that HTML — strips `<script>`, inline event
394///    handlers (`onerror=`, `onclick=`), `javascript:` URLs, and any
395///    tag/attribute outside its safe allowlist. This is the security
396///    boundary: user-supplied markdown (plugin bodies, usage docs,
397///    reviews) is rendered, never trusted.
398/// 3. The result is wrapped in `Value::from_safe_string` so MiniJinja's
399///    autoescape emits the generated tags as markup instead of
400///    re-escaping them into `&lt;...&gt;`.
401///
402/// Why sanitize after rendering rather than trusting the parser: raw
403/// HTML embedded in a markdown source (`<script>...`) passes straight
404/// through pulldown-cmark by design. ammonia is the layer that makes
405/// "render whatever the user typed" safe.
406///
407/// Deferred (separate slices): syntax highlighting on fenced code
408/// blocks (ammonia strips the `language-*` class today) and a
409/// configurable allowlist for embeds — see the gap entries.
410/// Register the global `static()` template function so templates can
411/// write `{{ static("admin/admin.css") }}` and get back a URL prefixed
412/// with the configured `static_url`.
413///
414/// `static_url` is captured into the closure when the environment is
415/// built (rather than read per-call) — the value is fixed for the
416/// process at `App::build()` time, and minijinja functions can't reach
417/// the ambient `Settings` directly. The dev-mode render path rebuilds
418/// the env per render via [`build_env`], so a `static_url` change would
419/// be picked up there too; in practice it never changes at runtime.
420///
421/// Resolution joins `static_url` and the argument with exactly one
422/// slash: a leading slash on the argument (`static("/admin/x")`) is
423/// trimmed so the result never double-slashes. With the default
424/// `static_url = "/static/"`, `static("admin/admin.css")` yields
425/// `"/static/admin/admin.css"`; with a CDN origin
426/// `static_url = "https://cdn.example.com/s/"` it yields
427/// `"https://cdn.example.com/s/admin/admin.css"`.
428fn register_static_function(env: &mut Environment<'static>, static_url: String) {
429    env.add_function("static", move |path: String| -> String {
430        // Route through the manifest-aware resolver so a `--hashed`
431        // collect makes `{{ static("css/app.css") }}` emit the
432        // content-hashed URL. The captured `static_url` is the fixed
433        // prefix; the manifest lookup is the only per-call ambient read.
434        if let Some(hashed) = crate::static_files::manifest_lookup(&path) {
435            return join_static_url(&static_url, hashed);
436        }
437        join_static_url(&static_url, &path)
438    });
439}
440
441/// Join a `static_url` prefix and an asset path with exactly one slash.
442///
443/// `static_url` is normalised to end in a slash by [`crate::settings`];
444/// the asset path may or may not lead with one, so its leading slash is
445/// trimmed before the join. With `static_url = "/static/"`,
446/// `join_static_url(.., "admin/admin.css")` yields
447/// `"/static/admin/admin.css"`.
448fn join_static_url(static_url: &str, path: &str) -> String {
449    format!("{}{}", static_url, path.trim_start_matches('/'))
450}
451
452/// Resolve an asset path against the ambient `static_url`, mirroring the
453/// `static()` template global outside a minijinja render.
454///
455/// Plugins that build their own minijinja [`Environment`] (the admin
456/// engine, for one) call this to register an equivalent `static()`
457/// function so their templates can write `{{ static("admin/admin.css") }}`
458/// and resolve through the same unified static pipeline URL as the core
459/// engine. Reads `static_url` from ambient [`crate::settings`], defaulting
460/// to `/static/` when settings aren't initialised yet (bare unit tests).
461pub fn resolve_static_url(path: &str) -> String {
462    let static_url = crate::settings::get_opt()
463        .map(|s| s.static_url.clone())
464        .unwrap_or_else(|| "/static/".to_string());
465
466    // Manifest cache-busting (hashed static-file storage): when
467    // `collectstatic --hashed` has run, a `staticfiles.json` maps the
468    // logical path the template wrote (`css/app.css`) to its
469    // content-hashed name (`css/app.<hash>.css`). Resolving to the hashed
470    // URL lets the asset carry far-future cache headers — the hash in the
471    // name changes whenever the bytes do, so a stale cache can never mask
472    // a new build. When no manifest is loaded (no `--hashed` run), the
473    // lookup misses and we serve the plain path exactly as before.
474    if let Some(hashed) = crate::static_files::manifest_lookup(path) {
475        return join_static_url(&static_url, hashed);
476    }
477
478    join_static_url(&static_url, path)
479}
480
481/// Register the global `media_url()` template function so a template can
482/// write `{{ media_url(plugin.logo) }}` and get back the public URL for a
483/// stored file/image KEY, resolved through the ambient
484/// [`crate::storage::Storage`] backend.
485///
486/// Mirrors the `static()` global ([`register_static_function`]) but for
487/// user-uploaded media instead of developer-shipped assets:
488/// `ImageField` / `FileField` serialize as the bare storage key, so
489/// `{{ media_url(plugin.logo) }}` (where `plugin.logo` is the key string)
490/// resolves to the storage backend's public URL.
491///
492/// - An empty key yields the empty string (the surrounding `{% if %}`
493///   guard skips the markup).
494/// - With no `Storage` backend registered, the raw key falls through
495///   unchanged.
496/// - A `None`/optional field serializes to null, which the template's
497///   `{% if %}` guard handles before the helper is ever called.
498fn register_media_url_function(env: &mut Environment<'static>) {
499    env.add_function("media_url", |key: String| -> String {
500        if key.is_empty() {
501            return String::new();
502        }
503        crate::storage::storage_opt()
504            .map(|s| s.url(&key))
505            .unwrap_or(key)
506    });
507}
508
509/// Register the `{{ querystring_with(current_query, key, value) }}` global
510/// (gaps/features #65 — template pagination). Rebuilds a querystring
511/// replacing one key while preserving every other parameter, the fiddly bit
512/// behind a pagination nav that has to carry `?sort=name` across every
513/// `?page=N` link. Backed by [`crate::pagination::querystring_with`] so the
514/// encode/replace logic stays in one place and is unit-tested there. The
515/// returned string has no leading `?`; the template prepends one.
516fn register_querystring_with_function(env: &mut Environment<'static>) {
517    env.add_function(
518        "querystring_with",
519        // `value` is a `minijinja::Value`, not a `String`: the nav passes
520        // `page.next_page_number` / `item.n`, which are integers, and
521        // minijinja does NOT auto-coerce an int arg into a `String`
522        // parameter — it'd raise a type error at render. Accepting `Value`
523        // and stringifying covers ints, strings, and bools uniformly.
524        |current_query: String, key: String, value: minijinja::Value| -> String {
525            crate::pagination::querystring_with(&current_query, &key, &value.to_string())
526        },
527    );
528}
529
530fn register_markdown_filter(env: &mut Environment<'static>) {
531    env.add_filter("markdown", |input: String| -> minijinja::Value {
532        minijinja::Value::from_safe_string(render_markdown(&input))
533    });
534}
535
536/// Register the `{{ highlight_styles() }}` global: emits the generated
537/// `base16-ocean.dark` token stylesheet wrapped in a `<style>` block, for a
538/// base template to drop into `<head>` once. The CSS is a safe string
539/// (generated by syntect from a fixed theme, no user input), so it is
540/// marked safe to skip minijinja autoescape.
541fn register_highlight_styles_function(env: &mut Environment<'static>) {
542    env.add_function("highlight_styles", || -> minijinja::Value {
543        minijinja::Value::from_safe_string(format!("<style>{}</style>", highlight_css()))
544    });
545}
546
547/// features.md #67 — `{{ now() }}` / `{{ now("%Y-%m-%d") }}`. Renders the
548/// current UTC time, optionally via a chrono `strftime` format string.
549/// With no argument it emits RFC 3339 (e.g. `2026-06-13T10:30:00+00:00`).
550/// The reference built-in tag for the custom-tag surface.
551fn register_now_function(env: &mut Environment<'static>) {
552    env.add_function("now", |fmt: Option<String>| -> String {
553        let now = chrono::Utc::now();
554        match fmt {
555            Some(f) if !f.is_empty() => now.format(&f).to_string(),
556            _ => now.to_rfc3339(),
557        }
558    });
559}
560
561/// features.md #67 — `{{ price | currency }}` / `{{ price | currency("EUR") }}`.
562/// Formats a number as money: two decimals, thousands grouping, and a
563/// leading symbol for the common ISO codes (USD/EUR/GBP/JPY); an unknown
564/// code falls back to `1,234.56 CODE`. The reference built-in filter.
565fn register_currency_filter(env: &mut Environment<'static>) {
566    env.add_filter("currency", |amount: f64, code: Option<String>| -> String {
567        let code = code.unwrap_or_else(|| "USD".to_string());
568        let symbol = match code.as_str() {
569            "USD" | "AUD" | "CAD" | "NZD" => "$",
570            "EUR" => "€",
571            "GBP" => "£",
572            "JPY" | "CNY" => "¥",
573            "KES" => "KSh ",
574            _ => "",
575        };
576        // Sign goes outside the symbol: -$12.40, not $-12.40.
577        let sign = if amount < 0.0 { "-" } else { "" };
578        let body = group_thousands(amount.abs());
579        if symbol.is_empty() {
580            format!("{sign}{body} {code}")
581        } else {
582            format!("{sign}{symbol}{body}")
583        }
584    });
585}
586
587/// Format a float with two decimals and comma thousands separators on the
588/// integer part: `1234567.5 -> "1,234,567.50"`, `-12.4 -> "-12.40"`.
589fn group_thousands(amount: f64) -> String {
590    let negative = amount.is_sign_negative() && amount != 0.0;
591    let formatted = format!("{:.2}", amount.abs());
592    let (int_part, frac_part) = formatted.split_once('.').unwrap_or((&formatted, "00"));
593
594    let mut grouped = String::new();
595    let digits: Vec<char> = int_part.chars().collect();
596    for (i, ch) in digits.iter().enumerate() {
597        if i > 0 && (digits.len() - i) % 3 == 0 {
598            grouped.push(',');
599        }
600        grouped.push(*ch);
601    }
602    format!("{}{grouped}.{frac_part}", if negative { "-" } else { "" })
603}
604
605/// features.md #4 — register the `sanitize` filter: clean a string of
606/// HTML (e.g. the output of the admin's RTE widget, which stores
607/// HTML rather than markdown) down to ammonia's safe allowlist and
608/// hand it to the template as a safe string.
609///
610/// `{{ body | sanitize }}` is the display companion to the `rte`
611/// widget the way `{{ body | markdown }}` is to the `markdown` widget:
612/// the stored value is HTML, so it's sanitized — never trusted — before
613/// it reaches the page. A value tampered with via the REST write path
614/// (which doesn't go through the editor) is made safe here.
615fn register_sanitize_filter(env: &mut Environment<'static>) {
616    env.add_filter("sanitize", |input: String| -> minijinja::Value {
617        minijinja::Value::from_safe_string(sanitize_html(&input))
618    });
619}
620
621/// Clean `input` HTML down to ammonia's safe allowlist (strips
622/// `<script>`, event handlers, `javascript:` URLs, etc.). The
623/// non-markdown sibling of [`render_markdown`] — use it on stored HTML
624/// (the RTE widget's output).
625pub fn sanitize_html(input: &str) -> String {
626    ammonia::clean(input)
627}
628
629/// The class prefix syntect token spans carry (`hl-keyword`, `hl-string`,
630/// `hl-source`, …). Shared by the highlighter and the generated
631/// stylesheet so the two never drift.
632const HL_PREFIX: &str = "hl-";
633
634fn hl_class_style() -> ClassStyle {
635    ClassStyle::SpacedPrefixed { prefix: HL_PREFIX }
636}
637
638/// The bundled syntect syntax set, loaded once. The load parses a binary
639/// dump and is expensive, so it is cached for the life of the process.
640fn syntax_set() -> &'static SyntaxSet {
641    static SYNTAX_SET: OnceLock<SyntaxSet> = OnceLock::new();
642    SYNTAX_SET.get_or_init(SyntaxSet::load_defaults_newlines)
643}
644
645/// The `base16-ocean.dark` token stylesheet, generated once from syntect's
646/// bundled theme with the `hl-` class prefix. This is the single source of
647/// truth for token colors — the markdown highlighter emits matching
648/// classes. Returns `""` only if syntect cannot generate the CSS (it
649/// always can for a bundled theme), so callers never need to handle an
650/// error.
651pub fn highlight_css() -> &'static str {
652    static HIGHLIGHT_CSS: OnceLock<String> = OnceLock::new();
653    HIGHLIGHT_CSS
654        .get_or_init(|| {
655            let themes = ThemeSet::load_defaults();
656            match themes.themes.get("base16-ocean.dark") {
657                Some(theme) => {
658                    css_for_theme_with_class_style(theme, hl_class_style()).unwrap_or_default()
659                }
660                None => String::new(),
661            }
662        })
663        .as_str()
664}
665
666/// Return `true` iff every character in a fence info token is safe to
667/// embed verbatim in a `class="language-…"` HTML attribute value.
668///
669/// A legitimate language token is just a word: `rust`, `c++`, `c#`,
670/// `shell`, `text/plain`, etc. It never needs `<`, `>`, `"`, `'`, `=`,
671/// backticks, or whitespace. Rejecting those characters closes the
672/// class-injection vector that would otherwise let a hostile fence like
673/// `` ```<script>alert(1)</script> `` survive ammonia's pass (ammonia
674/// allows `class` on `<code>` but does not filter the attribute VALUE).
675fn fence_lang_is_safe(lang: &str) -> bool {
676    !lang.is_empty()
677        && lang.len() <= 64
678        && lang.chars().all(|c| {
679            c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '_' | '.' | '#' | '/' | '@')
680        })
681}
682
683/// Render one fenced code block to safe HTML. `lang` is the fence info
684/// token (`Some("rust")`) or `None` for an unlabelled / indented block.
685/// With a known language the body is syntect-highlighted into `hl-` token
686/// spans; otherwise — or on any highlighter error — it falls back to a
687/// plain escaped block that still carries `class="language-…"` so the
688/// `md-enhance.js` label keeps working. Never panics, never drops the
689/// user's code.
690fn highlight_code_block(lang: Option<&str>, src: &str) -> String {
691    // Validate the lang token before touching it. A hostile fence info
692    // string (e.g. `<script>alert(1)</script>`) must never land in the
693    // `class="language-…"` attribute value even after HTML-escaping,
694    // because ammonia re-parses the tree and may not re-escape `<`/`>`
695    // that appear inside attribute values of allowed elements. Treating
696    // an unsafe token as `None` produces a plain unlabelled code block
697    // (still safe and still readable) rather than a class-injection path.
698    let lang = lang.filter(|l| fence_lang_is_safe(l));
699
700    let ss = syntax_set();
701    let syntax = lang.and_then(|l| {
702        ss.find_syntax_by_token(l)
703            .or_else(|| ss.find_syntax_by_extension(l))
704    });
705    if let Some(syntax) = syntax {
706        let mut generator =
707            ClassedHTMLGenerator::new_with_class_style(syntax, ss, hl_class_style());
708        let mut ok = true;
709        for line in LinesWithEndings::from(src) {
710            if generator
711                .parse_html_for_line_which_includes_newline(line)
712                .is_err()
713            {
714                ok = false;
715                break;
716            }
717        }
718        if ok {
719            // `finalize()` returns safe `<span class="hl-…">` markup —
720            // pass it through unescaped.
721            return wrap_code_block(lang, &generator.finalize());
722        }
723    }
724    // Fallback: escape the raw text so it is inert, then wrap.
725    let mut escaped = String::with_capacity(src.len());
726    html_escape_into(&mut escaped, src);
727    wrap_code_block(lang, &escaped)
728}
729
730/// Wrap inner code HTML (token spans, or escaped plain text) in
731/// `<pre><code class="language-…">` so the md-enhance frame + language
732/// label attach. The language token is HTML-escaped before it lands in the
733/// class value (it comes straight from the fence info string).
734fn wrap_code_block(lang: Option<&str>, inner: &str) -> String {
735    let mut out = String::with_capacity(inner.len() + 48);
736    out.push_str("<pre><code");
737    if let Some(l) = lang {
738        out.push_str(" class=\"language-");
739        html_escape_into(&mut out, l);
740        out.push('"');
741    }
742    out.push('>');
743    out.push_str(inner);
744    out.push_str("</code></pre>");
745    out
746}
747
748/// Render CommonMark + GFM `input` to sanitized HTML. Pulled out of the
749/// filter closure so it's unit-testable and reusable by any future
750/// Rust-side caller (e.g. a REST endpoint that returns pre-rendered
751/// HTML).
752pub fn render_markdown(input: &str) -> String {
753    use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag, TagEnd, html};
754
755    let mut options = Options::empty();
756    options.insert(Options::ENABLE_TABLES);
757    options.insert(Options::ENABLE_STRIKETHROUGH);
758    options.insert(Options::ENABLE_TASKLISTS);
759    options.insert(Options::ENABLE_FOOTNOTES);
760
761    let parser = Parser::new_ext(input, options);
762
763    // Rewrite the event stream: replace each code block with a single
764    // pre-highlighted Html event. The fence info token selects the syntect
765    // syntax; everything else passes through unchanged.
766    let mut events: Vec<Event> = Vec::new();
767    let mut in_code = false;
768    let mut code_lang: Option<String> = None;
769    let mut code_buf = String::new();
770    for event in parser {
771        match event {
772            Event::Start(Tag::CodeBlock(kind)) => {
773                in_code = true;
774                code_buf.clear();
775                code_lang = match kind {
776                    CodeBlockKind::Fenced(info) => {
777                        info.split_whitespace().next().map(str::to_string)
778                    }
779                    CodeBlockKind::Indented => None,
780                };
781            }
782            Event::End(TagEnd::CodeBlock) => {
783                in_code = false;
784                let highlighted = highlight_code_block(code_lang.as_deref(), &code_buf);
785                events.push(Event::Html(highlighted.into()));
786            }
787            Event::Text(text) if in_code => code_buf.push_str(&text),
788            other => events.push(other),
789        }
790    }
791
792    let mut rendered = String::new();
793    html::push_html(&mut rendered, events.into_iter());
794
795    // Sanitize. `pre`/`code`/`span` are already default-allowed tags, so we
796    // widen the allowlist by exactly one inert attribute — `class` on those
797    // three — letting syntect's `hl-` token spans and the `language-*` label
798    // survive. style / on* handlers / javascript: URLs stay stripped: this is
799    // the whole "safely" surface. Built per call: ammonia::Builder isn't Sync
800    // (boxed attribute_filter), so it can't be a shared static without a Mutex
801    // that would serialize rendering; this costs the same as ammonia::clean.
802    let mut cleaner = ammonia::Builder::default();
803    cleaner.add_tag_attributes("pre", &["class"]);
804    cleaner.add_tag_attributes("code", &["class"]);
805    cleaner.add_tag_attributes("span", &["class"]);
806    cleaner.clean(&rendered).to_string()
807}
808
809/// Tiny HTML attribute-value escape — covers the four characters
810/// that can break out of a double-quoted attribute context.
811/// Centralised here because the framework doesn't otherwise need
812/// to ship an html_escape crate dep just for the img filter.
813fn html_escape_into(out: &mut String, s: &str) {
814    for ch in s.chars() {
815        match ch {
816            '&' => out.push_str("&amp;"),
817            '<' => out.push_str("&lt;"),
818            '>' => out.push_str("&gt;"),
819            '"' => out.push_str("&quot;"),
820            '\'' => out.push_str("&#39;"),
821            c => out.push(c),
822        }
823    }
824}
825
826fn register_default_templates(
827    env: &mut Environment<'static>,
828    seen: &mut std::collections::HashSet<String>,
829) {
830    let entries = [
831        (
832            crate::errors::DEFAULT_404_TEMPLATE_NAME,
833            crate::errors::DEFAULT_404_HTML,
834        ),
835        (
836            crate::errors::DEFAULT_500_TEMPLATE_NAME,
837            crate::errors::DEFAULT_500_HTML,
838        ),
839    ];
840    for (name, source) in entries {
841        if seen.contains(name) {
842            continue; // already provided by user — skip
843        }
844        // These are compile-time constants so they're `&'static str`; we can
845        // add them without cloning via `add_template` (non-owned variant).
846        if env.add_template(name, source).is_ok() {
847            seen.insert(name.to_string());
848        }
849    }
850}
851
852/// Publish the template engine into the process-wide ambient handle.
853///
854/// `dirs` is the ordered list of directories to search — the first
855/// entry is searched first (highest priority). Typically this is:
856/// `[app_templates_dir, plugin_a_dir, plugin_b_dir, ...]`.
857///
858/// For each directory in order, every `.html` / `.htm` / `.txt` file is
859/// registered under its path-relative-to-that-dir name. If a name was
860/// already registered by an earlier directory, the later file is skipped
861/// and a `tracing::warn!` is emitted so the collision is visible.
862///
863/// If none of the directories exist, init succeeds with an empty engine.
864/// This is the right default for binaries that don't render HTML.
865///
866/// Returns the list of template names that collided (appeared in more
867/// than one directory). The caller (`App::build`) logs these via tracing.
868/// Tests can inspect the returned list to assert collision detection
869/// without needing a tracing subscriber.
870pub fn init(dirs: &[PathBuf]) -> Result<Vec<String>, TemplateError> {
871    let (env, collisions) = build_env(dirs)?;
872
873    for name in &collisions {
874        tracing::warn!(
875            template = %name,
876            "umbral templates: template `{name}` is provided by multiple directories; \
877             the first-registered copy wins"
878        );
879    }
880
881    // Stash the dirs so the dev-mode render path can rebuild the env
882    // on demand without re-running the (more expensive) init flow.
883    let _ = WATCHED_DIRS.set(dirs.to_vec());
884
885    ENGINE
886        .set(env)
887        .map_err(|_| TemplateError::AlreadyInitialised)?;
888    Ok(collisions)
889}
890
891/// Like [`init`], but also installs plugin-contributed
892/// [`TemplateRegistrar`]s (feature #67). The registrars are stashed in
893/// the process-wide [`REGISTRARS`] handle *before* the engine is built so
894/// [`build_env`] applies them — both here and on every dev-mode rebuild.
895///
896/// Called by `App::build` with the flattened registrars from every
897/// plugin's `template_registrars()`, in topological order. The plain
898/// [`init`] stays the no-plugin entry point used by template unit tests.
899pub fn init_with(
900    dirs: &[PathBuf],
901    registrars: Vec<TemplateRegistrar>,
902) -> Result<Vec<String>, TemplateError> {
903    // Set even when empty so a second (errant) init can't smuggle in a
904    // different registrar set behind the already-published engine.
905    let _ = REGISTRARS.set(registrars);
906    init(dirs)
907}
908
909/// Build a fresh `Environment` from the given dirs. Shared by the
910/// init path and the dev-mode hot-reload path; both produce
911/// bit-identical engines from the same input.
912fn build_env(dirs: &[PathBuf]) -> Result<(Environment<'static>, Vec<String>), TemplateError> {
913    let mut env = Environment::new();
914    // Autoescape extensions MUST stay in sync with the loader
915    // whitelist in `load_directory` (currently `html | htm | txt`).
916    // If you add `.svg` or `.xml` to the loader, add them HERE too
917    // — `.svg` carries inline-script XSS risk and `.xml` is generally
918    // parsed by something downstream that wants attribute escaping.
919    // `.txt` stays `None` because plaintext rendering shouldn't HTML-
920    // escape (would replace `<` with `&lt;` in plain email bodies).
921    env.set_auto_escape_callback(|name| {
922        if name.ends_with(".html") || name.ends_with(".htm") {
923            AutoEscape::Html
924        } else {
925            AutoEscape::None
926        }
927    });
928
929    // gaps2 #21 — register the `img` filter for ergonomic, perf-
930    // forward image markup. `{{ url | img(alt="...", width=400,
931    // height=300) }}` expands to a fully-formed `<img>` with the
932    // hat-trick that catches LCP regressions out of the box:
933    // `loading="lazy"`, `decoding="async"`, explicit `width`/
934    // `height` to reserve layout space (no CLS), and an `alt`
935    // attribute that's empty rather than omitted (screen-reader-
936    // friendly default for purely decorative images). Optional
937    // `class="..."` flows through for Tailwind / scoped styling.
938    register_img_filter(&mut env);
939
940    // `{{ highlight_styles() }}` — the syntect token stylesheet for
941    // server-highlighted code, emitted once into <head> by a base template.
942    register_highlight_styles_function(&mut env);
943
944    // Unified static pipeline — `{{ static("admin/admin.css") }}`
945    // expands to `<static_url>admin/admin.css`. The `static_url` is read
946    // from ambient settings (defaulting to `/static/` when settings
947    // aren't initialised yet, e.g. in a bare template unit test) and
948    // captured into the function closure. See `register_static_function`.
949    let static_url = crate::settings::get_opt()
950        .map(|s| s.static_url.clone())
951        .unwrap_or_else(|| "/static/".to_string());
952    register_static_function(&mut env, static_url);
953
954    // `{{ media_url(plugin.logo) }}` resolves a stored file/image KEY
955    // through the ambient Storage backend's `url()`, the media-side
956    // companion to `static()`. ImageField/FileField serialize as the
957    // bare key; this turns it into the public URL. See
958    // `register_media_url_function`.
959    register_media_url_function(&mut env);
960
961    // features.md #4 — `{{ body | markdown }}` renders user-supplied
962    // CommonMark/GFM to sanitized HTML. The reusable "safely show a
963    // body/usage field" surface shared by the admin and end-user
964    // templates; pairs with `#[umbral(widget = "markdown")]` on the
965    // model field that captures the source.
966    register_markdown_filter(&mut env);
967
968    // features.md #4 — `{{ html | sanitize }}` cleans stored HTML (the
969    // `rte` admin widget's output) to a safe allowlist. The HTML-side
970    // companion to the markdown filter.
971    register_sanitize_filter(&mut env);
972
973    // gaps2 #19 follow-up — render `None` / `Undefined` as the
974    // empty string instead of the literal "none" / "undefined" tokens
975    // MiniJinja defaults to. Bug screenshot 2026-06-10 01-08-30: an
976    // `Option<String>` model field with `value=None` rendered into
977    // `<input value="{{ form.phone }}">` produced `value="none"` on a
978    // fresh form, which the user then has to manually clear before
979    // typing. Every form with optional fields hit this footgun.
980    //
981    // Defining a custom formatter is the framework-level fix — every
982    // template (admin, shop, plugins) inherits the new behaviour
983    // automatically. Non-null/non-undefined values pass through the
984    // default formatter unchanged so HTML escaping, number / bool /
985    // string rendering, and safe-string handling stay identical.
986    env.set_formatter(|out, state, value| {
987        if value.is_none() || value.is_undefined() {
988            return Ok(());
989        }
990        minijinja::escape_formatter(out, state, value)
991    });
992
993    // features.md #67 — built-in example tags/filters. These ship as the
994    // reference implementations for the custom-tag surface: `now()` for a
995    // server-rendered timestamp, `currency` for money formatting. Plugins
996    // add their own via `Plugin::template_registrars` (applied below).
997    register_now_function(&mut env);
998    register_currency_filter(&mut env);
999
1000    // features #65 — `{{ querystring_with(base_query, "page", item.n) }}`
1001    // rebuilds the current querystring replacing one key, so the bundled
1002    // `_pagination.html` nav carries `?sort=...` filters across every
1003    // `?page=N` link. See `register_querystring_with_function`.
1004    register_querystring_with_function(&mut env);
1005
1006    // features.md #67 — plugin-contributed filters/functions. Applied
1007    // AFTER the built-ins so a plugin can deliberately override one by
1008    // re-registering the same name (minijinja's add_* overwrites). Runs
1009    // on every rebuild (dev hot-reload) because `Fn`, not `FnOnce`.
1010    if let Some(registrars) = REGISTRARS.get() {
1011        for registrar in registrars {
1012            registrar(&mut env);
1013        }
1014    }
1015
1016    let mut seen: HashSet<String> = HashSet::new();
1017    let mut collisions: Vec<String> = Vec::new();
1018
1019    // Register the built-in default error templates before scanning disk
1020    // directories. Because disk directories are first-match-wins and are
1021    // scanned after this call, a user template with the same name (unlikely,
1022    // since the `__umbral__/` prefix is reserved) would silently replace the
1023    // built-in. Callers who want a clean opt-out should use
1024    // `App::builder().disable_default_error_pages()`.
1025    register_default_templates(&mut env, &mut seen);
1026
1027    for dir in dirs {
1028        if dir.exists() {
1029            load_directory(&mut env, dir, dir, &mut seen, &mut collisions)?;
1030        }
1031    }
1032
1033    Ok((env, collisions))
1034}
1035
1036/// Render a template by name with a serde-serializable context value.
1037///
1038/// The name is the path relative to its templates directory, with
1039/// forward slashes regardless of host OS. `articles_list.html`,
1040/// `admin/base.html`, etc.
1041///
1042/// Returns `TemplateError::NotInitialised` if `App::build()` hasn't
1043/// run yet, `TemplateError::Missing` if the name doesn't match a
1044/// loaded template, and `TemplateError::Render` for any minijinja-
1045/// reported issue (syntax error, missing variable when strict undefined
1046/// is on, etc.).
1047pub fn render<C: Serialize>(name: &str, ctx: &C) -> Result<String, TemplateError> {
1048    // Dev-mode hot reload: when settings.environment == Dev, rebuild
1049    // the environment from disk on every render so template edits are
1050    // picked up without a server restart. This makes the dev loop —
1051    // edit `home.html`, hit reload, see the change — work without
1052    // `cargo run`-ing again. Production stays on the cached engine
1053    // for the fast path.
1054    //
1055    // Cost: one disk walk + minijinja parse per render in dev. For a
1056    // typical handler doing one render per request at ~10 RPS during
1057    // development, that's negligible. We chose this over per-file
1058    // stat checks because the per-render rebuild is dependency-free
1059    // and the staleness window is zero (a save followed instantly
1060    // by a reload always sees the new content).
1061    if dev_mode_active() {
1062        if let Some(dirs) = WATCHED_DIRS.get() {
1063            // Rebuild fresh; ignore collisions log here (init already
1064            // logged them once; we don't spam every render).
1065            match build_env(dirs) {
1066                Ok((env, _collisions)) => return render_with(&env, name, ctx),
1067                Err(e) => return Err(e),
1068            }
1069        }
1070    }
1071
1072    let env = ENGINE.get().ok_or(TemplateError::NotInitialised)?;
1073    render_with(env, name, ctx)
1074}
1075
1076/// Render an inline template source through the ambient-context path.
1077/// Test/bench helper only.
1078#[doc(hidden)]
1079pub fn render_str<C: Serialize>(src: &str, ctx: &C) -> Result<String, TemplateError> {
1080    let mut env = minijinja::Environment::new();
1081    env.add_template("__inline", src)
1082        .map_err(TemplateError::Render)?;
1083    render_with(&env, "__inline", ctx)
1084}
1085
1086/// True when the ambient settings say we're in Dev. Returns false if
1087/// settings haven't been initialised (production-style binaries that
1088/// never went through `App::build()`).
1089fn dev_mode_active() -> bool {
1090    crate::settings::get_opt()
1091        .map(|s| matches!(s.environment, crate::settings::Environment::Dev))
1092        .unwrap_or(false)
1093}
1094
1095/// Render a named template against the given env. Extracted so dev-mode
1096/// (fresh env per render) and prod (cached env) share one error mapping.
1097fn render_with<C: Serialize>(
1098    env: &Environment<'_>,
1099    name: &str,
1100    ctx: &C,
1101) -> Result<String, TemplateError> {
1102    let tmpl = env.get_template(name).map_err(|e| match e.kind() {
1103        minijinja::ErrorKind::TemplateNotFound => TemplateError::Missing(name.to_string()),
1104        _ => TemplateError::Render(e),
1105    })?;
1106    let merged = merge_ambient_context(ctx);
1107    tmpl.render(&merged).map_err(TemplateError::Render)
1108}
1109
1110/// Merge the ambient task-locals into a serializable template context:
1111/// `user` (from `CURRENT_USER`) and the CSRF pair `csrf_token` /
1112/// `csrf_input` (from `CURRENT_CSRF`). The handler's own keys always
1113/// win — the ambient injection is the default, not an override.
1114///
1115/// `user` is injected unconditionally (anonymous fallback below);
1116/// the CSRF pair only when a middleware actually scoped a token —
1117/// there is no meaningful fallback token, and rendering an empty
1118/// hidden input would make a form post a guaranteed-403 silently.
1119///
1120/// Most code should use [`render`], which calls this automatically.
1121/// Plugins that own a private MiniJinja environment can call this before
1122/// `Template::render` to get the same `{{ user }}`, `{{ csrf_token }}`,
1123/// and `{{ csrf_input }}` semantics as the framework renderer.
1124pub fn merge_ambient_context<C: Serialize>(ctx: &C) -> minijinja::Value {
1125    let ctx_value = minijinja::Value::from_serialize(ctx);
1126    merge_ambient_value(ctx_value)
1127}
1128
1129/// Same as [`merge_ambient_context`], but accepts an already-built
1130/// MiniJinja [`Value`](minijinja::Value). This is useful for private
1131/// plugin renderers that build context with `minijinja::context!`.
1132pub fn merge_ambient_value(ctx_value: minijinja::Value) -> minijinja::Value {
1133    let has = |key: &str| {
1134        ctx_value
1135            .get_attr(key)
1136            .map(|v| !v.is_undefined())
1137            .unwrap_or(false)
1138    };
1139
1140    let need_user = !has("user");
1141    let csrf = current_csrf();
1142    let need_csrf = csrf.is_some() && !(has("csrf_token") && has("csrf_input"));
1143
1144    if !need_user && !need_csrf {
1145        return ctx_value;
1146    }
1147
1148    // Build a fresh object that contains every original key plus the
1149    // ambient ones. minijinja's `Value::from_iter` over (key, value)
1150    // pairs produces a Map value; we walk the original keys and add
1151    // ours last.
1152    let mut pairs: Vec<(String, minijinja::Value)> = Vec::new();
1153    if let Ok(keys) = ctx_value.try_iter() {
1154        for key in keys {
1155            let key_str = key.to_string();
1156            if let Ok(v) = ctx_value.get_item(&key) {
1157                pairs.push((key_str, v));
1158            }
1159        }
1160    }
1161
1162    if need_user {
1163        // Resolve which `user` value should land in the rendered ctx:
1164        //   1. Task-local set by a middleware (AuthPlugin's
1165        //      `user_context_layer`) — the live request shape.
1166        //   2. Anonymous fallback `{ is_authenticated: false }` for
1167        //      callers WITHOUT a layer mounted AND for renders that
1168        //      happen outside the middleware's scope (notably the
1169        //      `render_500_middleware` recovery path — the
1170        //      user-context task-local has already dropped by the time
1171        //      the error layer renders, but the 500 template still
1172        //      needs `user.is_authenticated` to evaluate cleanly).
1173        //
1174        // The fallback is the same shape `serialize_anonymous` would
1175        // produce, kept in core so umbral-auth isn't a dependency of
1176        // the templates module.
1177        // Prefer the lazy channel (proxy defers resolution until attribute access),
1178        // then the eager task-local, then the anonymous fallback.
1179        let user_value = if let Ok(lazy) = CURRENT_USER_LAZY.try_with(|lazy| lazy.clone()) {
1180            lazy.into_proxy_value()
1181        } else if let Some(v) = CURRENT_USER.try_with(|u| u.clone()).ok().flatten() {
1182            v
1183        } else {
1184            anonymous_user_value()
1185        };
1186        pairs.push(("user".to_string(), user_value));
1187    }
1188
1189    if let Some(token) = csrf {
1190        if !has("csrf_token") {
1191            pairs.push((
1192                "csrf_token".to_string(),
1193                minijinja::Value::from(token.clone()),
1194            ));
1195        }
1196        if !has("csrf_input") {
1197            // Today's tokens are hex (signed mode adds `.` + hex sig),
1198            // so the escape is belt-and-braces against a future
1199            // token-shape change — not a live attack surface.
1200            let escaped = token
1201                .replace('&', "&amp;")
1202                .replace('"', "&quot;")
1203                .replace('<', "&lt;")
1204                .replace('>', "&gt;");
1205            pairs.push((
1206                "csrf_input".to_string(),
1207                minijinja::Value::from_safe_string(format!(
1208                    r#"<input type="hidden" name="csrf_token" value="{escaped}">"#
1209                )),
1210            ));
1211        }
1212    }
1213
1214    minijinja::Value::from_iter(pairs)
1215}
1216
1217/// Anonymous-user sentinel — the value `user` resolves to in
1218/// templates rendered outside an authenticated context (no auth
1219/// middleware, anonymous request, or the 500-rendering path
1220/// where the middleware's task-local has already dropped).
1221/// Carries only `{ is_authenticated: false }` — enough for
1222/// `{% if user.is_authenticated %}` / `{% if user.is_staff %}`
1223/// to evaluate to false without `umbral templates: undefined
1224/// value` errors that would otherwise mask the original failure.
1225fn anonymous_user_value() -> minijinja::Value {
1226    let mut map = serde_json::Map::new();
1227    map.insert(
1228        "is_authenticated".to_string(),
1229        serde_json::Value::Bool(false),
1230    );
1231    // is_staff / is_superuser default to false too so a template
1232    // gating on either doesn't accidentally render the privileged
1233    // branch when `user` is the anonymous fallback.
1234    map.insert("is_staff".to_string(), serde_json::Value::Bool(false));
1235    map.insert("is_superuser".to_string(), serde_json::Value::Bool(false));
1236    minijinja::Value::from_serialize(serde_json::Value::Object(map))
1237}
1238
1239/// Walk a directory recursively and register every `.html` / `.htm` /
1240/// `.txt` file as a template under its path-relative-to-root name.
1241/// Subdirectories are reachable via forward-slash names: `admin/base.html`.
1242///
1243/// `seen` tracks which names have already been registered across all
1244/// directories. When a name collision is detected (a later directory
1245/// ships a template with the same relative name as an earlier one),
1246/// the duplicate is skipped and the name is appended to `collisions`.
1247/// First-match-wins.
1248fn load_directory(
1249    env: &mut Environment<'static>,
1250    root: &Path,
1251    dir: &Path,
1252    seen: &mut HashSet<String>,
1253    collisions: &mut Vec<String>,
1254) -> Result<(), TemplateError> {
1255    for entry in std::fs::read_dir(dir)? {
1256        let entry = entry?;
1257        let path = entry.path();
1258        if path.is_dir() {
1259            load_directory(env, root, &path, seen, collisions)?;
1260            continue;
1261        }
1262        let Some(ext) = path.extension().and_then(|e| e.to_str()) else {
1263            continue;
1264        };
1265        if !matches!(ext, "html" | "htm" | "txt") {
1266            continue;
1267        }
1268        let rel: PathBuf = path
1269            .strip_prefix(root)
1270            .expect("walked path is rooted at the templates dir")
1271            .to_path_buf();
1272        // minijinja template names are forward-slashed regardless of OS;
1273        // the path display would emit `\` on Windows, so build the name
1274        // explicitly.
1275        let name: String = rel
1276            .components()
1277            .map(|c| c.as_os_str().to_string_lossy().to_string())
1278            .collect::<Vec<_>>()
1279            .join("/");
1280
1281        if seen.contains(&name) {
1282            // Collision: a higher-priority directory already registered
1283            // this name. Record it and skip; init will log after all
1284            // dirs are processed.
1285            if !collisions.contains(&name) {
1286                collisions.push(name.clone());
1287            }
1288            continue;
1289        }
1290
1291        let source = std::fs::read_to_string(&path)?;
1292        env.add_template_owned(name.clone(), source)
1293            .map_err(TemplateError::Render)?;
1294        seen.insert(name);
1295    }
1296    Ok(())
1297}
1298
1299/// Errors the template engine can produce. Narrow at v1: load-time IO,
1300/// engine-not-ready, missing template, render-time minijinja error.
1301#[derive(Debug)]
1302pub enum TemplateError {
1303    /// `App::build()` hasn't run yet, so the ambient engine isn't set.
1304    NotInitialised,
1305    /// `init` was called twice — a programming error in the framework
1306    /// itself, not the user. Surfaced as a `BuildError` if it ever fires.
1307    AlreadyInitialised,
1308    /// IO error reading a template file at boot.
1309    Io(std::io::Error),
1310    /// The requested template name isn't loaded.
1311    Missing(String),
1312    /// Any other minijinja error (syntax, render-time, etc.). The
1313    /// inner `minijinja::Error` carries the diagnostic (line / col /
1314    /// undefined name) so the caller can pass it through `Display`.
1315    Render(minijinja::Error),
1316}
1317
1318impl std::fmt::Display for TemplateError {
1319    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1320        match self {
1321            TemplateError::NotInitialised => write!(
1322                f,
1323                "umbral templates: engine not initialised — call App::build() first"
1324            ),
1325            TemplateError::AlreadyInitialised => {
1326                write!(f, "umbral templates: init called more than once")
1327            }
1328            TemplateError::Io(e) => write!(f, "umbral templates: io: {e}"),
1329            TemplateError::Missing(name) => write!(
1330                f,
1331                "umbral templates: no template named `{name}`; check the templates directory"
1332            ),
1333            TemplateError::Render(e) => write!(f, "umbral templates: {e}"),
1334        }
1335    }
1336}
1337
1338impl std::error::Error for TemplateError {}
1339
1340impl From<std::io::Error> for TemplateError {
1341    fn from(e: std::io::Error) -> Self {
1342        Self::Io(e)
1343    }
1344}
1345
1346#[cfg(test)]
1347mod tests {
1348    use super::*;
1349    use serde_json::json;
1350
1351    #[test]
1352    fn img_url_scheme_safety() {
1353        // Relative + http(s) are allowed.
1354        assert!(url_scheme_is_safe("/media/cat.png"));
1355        assert!(url_scheme_is_safe("cat.png"));
1356        assert!(url_scheme_is_safe("../up/cat.png"));
1357        assert!(url_scheme_is_safe("http://example.com/cat.png"));
1358        assert!(url_scheme_is_safe("https://example.com/cat.png"));
1359        assert!(url_scheme_is_safe("HTTPS://EXAMPLE.com/cat.png"));
1360        assert!(url_scheme_is_safe("?query=only"));
1361        assert!(url_scheme_is_safe("#fragment"));
1362        // Dangerous / non-http schemes are rejected.
1363        assert!(!url_scheme_is_safe("javascript:alert(1)"));
1364        assert!(!url_scheme_is_safe("  javascript:alert(1)"));
1365        assert!(!url_scheme_is_safe("JaVaScRiPt:alert(1)"));
1366        assert!(!url_scheme_is_safe(
1367            "data:text/html,<script>alert(1)</script>"
1368        ));
1369        assert!(!url_scheme_is_safe("vbscript:msgbox(1)"));
1370        assert!(!url_scheme_is_safe("mailto:a@b.com"));
1371        // Malformed scheme (embedded control char) fails closed.
1372        assert!(!url_scheme_is_safe("java\u{0}script:alert(1)"));
1373    }
1374
1375    #[test]
1376    fn img_filter_neutralises_javascript_url() {
1377        let mut env = minijinja::Environment::new();
1378        register_img_filter(&mut env);
1379        env.add_template("t", "{{ url | img }}").unwrap();
1380        let tmpl = env.get_template("t").unwrap();
1381        let out = tmpl
1382            .render(minijinja::context! { url => "javascript:alert(1)" })
1383            .unwrap();
1384        assert!(
1385            !out.contains("javascript:"),
1386            "javascript: URL must be neutralised; got {out}"
1387        );
1388        assert!(out.contains("src=\"\""), "expected empty src; got {out}");
1389    }
1390
1391    #[test]
1392    fn nested_template_names_are_relative_to_templates_root() {
1393        let tmp = tempfile::tempdir().expect("create temp dir");
1394        let templates = tmp.path().join("templates");
1395        std::fs::create_dir_all(templates.join("base")).expect("create base template dir");
1396        std::fs::create_dir_all(templates.join("content")).expect("create content template dir");
1397
1398        std::fs::write(
1399            templates.join("base").join("site.html"),
1400            "<main>{% block content %}{% endblock %}</main>",
1401        )
1402        .expect("write nested base template");
1403        std::fs::write(
1404            templates.join("content").join("contact.html"),
1405            r#"{% extends "base/site.html" %}{% block content %}<h1>{{ title }}</h1><p>Contact from nested content.</p>{% endblock %}"#,
1406        )
1407        .expect("write nested content template");
1408
1409        let (env, collisions) = build_env(&[templates]).expect("build template env");
1410        assert!(collisions.is_empty());
1411
1412        let rendered = render_with(
1413            &env,
1414            "content/contact.html",
1415            &json!({ "title": "Nested contact" }),
1416        )
1417        .expect("render nested template by relative name");
1418
1419        assert!(rendered.contains("<main>"));
1420        assert!(rendered.contains("<h1>Nested contact</h1>"));
1421        assert!(rendered.contains("Contact from nested content."));
1422    }
1423
1424    /// Render `{{ static(arg) }}` against an env whose `static()` was
1425    /// registered with the given `static_url`. Exercises the helper
1426    /// directly without needing the ambient `Settings` OnceLock (which
1427    /// can't be set under cargo's parallel test runner).
1428    fn render_static(static_url: &str, arg: &str) -> String {
1429        let mut env = Environment::new();
1430        register_static_function(&mut env, static_url.to_string());
1431        env.add_template("t.txt", "{{ static(arg) }}")
1432            .expect("add template");
1433        let tmpl = env.get_template("t.txt").expect("get template");
1434        tmpl.render(json!({ "arg": arg })).expect("render")
1435    }
1436
1437    #[test]
1438    fn static_helper_prepends_root_relative_url() {
1439        assert_eq!(
1440            render_static("/static/", "admin/admin.css"),
1441            "/static/admin/admin.css"
1442        );
1443    }
1444
1445    #[test]
1446    fn static_helper_prepends_cdn_origin() {
1447        assert_eq!(
1448            render_static("https://cdn.example.com/s/", "admin/admin.css"),
1449            "https://cdn.example.com/s/admin/admin.css"
1450        );
1451    }
1452
1453    #[test]
1454    fn static_helper_does_not_double_slash_on_leading_slash_arg() {
1455        assert_eq!(render_static("/static/", "/admin/x"), "/static/admin/x");
1456    }
1457
1458    #[test]
1459    fn highlight_css_contains_hl_rules() {
1460        let css = highlight_css();
1461        assert!(!css.is_empty(), "generated theme CSS should not be empty");
1462        assert!(
1463            css.contains(".hl-"),
1464            "theme CSS must target hl- classes: {css}"
1465        );
1466    }
1467
1468    #[test]
1469    fn fenced_rust_block_gets_syntect_token_spans() {
1470        let html = render_markdown("```rust\nfn main() {}\n```\n");
1471        assert!(
1472            html.contains("language-rust"),
1473            "keeps the language class for the md-enhance label: {html}"
1474        );
1475        assert!(
1476            html.contains("class=\"hl-"),
1477            "emits syntect hl- token spans: {html}"
1478        );
1479    }
1480
1481    #[test]
1482    fn script_in_code_fence_is_escaped_not_executed() {
1483        let html = render_markdown("```\n<script>alert(1)</script>\n```\n");
1484        assert!(!html.contains("<script>"), "no live script tag: {html}");
1485        assert!(
1486            html.contains("&lt;script&gt;"),
1487            "rendered as inert text: {html}"
1488        );
1489    }
1490
1491    #[test]
1492    fn prose_script_is_still_stripped() {
1493        let html = render_markdown("hello <script>alert(1)</script> world");
1494        assert!(!html.contains("<script>"), "prose script stripped: {html}");
1495    }
1496
1497    #[test]
1498    fn markdown_allows_class_but_not_style() {
1499        let html = render_markdown("<span class=\"x\" style=\"color:red\">hi</span>");
1500        assert!(html.contains("class=\"x\""), "class survives: {html}");
1501        assert!(!html.contains("style="), "style stripped: {html}");
1502    }
1503
1504    #[test]
1505    fn unknown_and_plain_fences_do_not_panic() {
1506        let unknown = render_markdown("```notalanguage\nx := 1\n```\n");
1507        let plain = render_markdown("```\nplain text\n```\n");
1508        assert!(
1509            unknown.contains("<pre><code"),
1510            "unknown lang block: {unknown}"
1511        );
1512        assert!(plain.contains("<pre><code"), "plain block: {plain}");
1513        assert!(
1514            unknown.contains("language-notalanguage"),
1515            "unknown lang still labelled: {unknown}"
1516        );
1517    }
1518
1519    /// Security: a hostile fence info token (e.g. `<script>alert(1)</script>`)
1520    /// must NOT appear as a live tag in the output. `wrap_code_block` HTML-escapes
1521    /// the lang token before inserting it into the class attribute value, and
1522    /// ammonia's builder only permits `class` on `<code>` — it does not allow
1523    /// arbitrary attributes or values. So a `<script>` info string is inert.
1524    ///
1525    /// Also asserts that the SAFE path — a plain `language-rust` class on
1526    /// the `<code>` element — still survives after the widened allowlist so
1527    /// the syntect token spans have a hook. This is the regression pin for
1528    /// gaps2 #36 sub-part (a).
1529    #[test]
1530    fn hostile_fence_info_string_is_escaped_and_language_class_survives() {
1531        // Hostile: info token that looks like a script injection.
1532        let hostile = render_markdown("```<script>alert(1)</script>\ncode\n```\n");
1533        assert!(
1534            !hostile.contains("<script>"),
1535            "live <script> from fence info must be stripped: {hostile}"
1536        );
1537        // The escaped form will appear inside a class value; ammonia lets
1538        // class through but the content is HTML-escaped so it is inert.
1539        assert!(
1540            hostile.contains("<pre><code"),
1541            "code block structure must survive: {hostile}"
1542        );
1543
1544        // Hostile: info token with a class-injection attempt.
1545        let class_inject = render_markdown("```evil\" onmouseover=\"alert(1)\ncode\n```\n");
1546        assert!(
1547            !class_inject.contains("onmouseover"),
1548            "event handler injected via fence info must not survive: {class_inject}"
1549        );
1550
1551        // Safe: the normal case — language-rust class must survive so
1552        // syntect hl- spans (server-side) and the md-enhance label both work.
1553        let safe = render_markdown("```rust\nfn ok() {}\n```\n");
1554        assert!(
1555            safe.contains("language-rust"),
1556            "language-rust class must survive sanitization (gaps2 #36a): {safe}"
1557        );
1558        assert!(
1559            safe.contains("class=\"hl-"),
1560            "syntect hl- token spans must survive sanitization: {safe}"
1561        );
1562    }
1563
1564    #[test]
1565    fn highlight_styles_global_emits_a_style_block() {
1566        let mut env = Environment::new();
1567        register_highlight_styles_function(&mut env);
1568        env.add_template("t", "{{ highlight_styles() }}")
1569            .expect("add template");
1570        let out = env
1571            .get_template("t")
1572            .expect("get template")
1573            .render(())
1574            .expect("render");
1575        assert!(out.starts_with("<style>"), "wraps in a style block: {out}");
1576        assert!(out.contains(".hl-"), "carries the token CSS: {out}");
1577        assert!(
1578            out.trim_end().ends_with("</style>"),
1579            "closes the style block: {out}"
1580        );
1581    }
1582}