Repository navigation
fix(dotprompt-rs): add per-render nonce to template markers - #645
Merged
Merged
Conversation
The role, history, section and media helpers emit in-band markers that are split back out of the rendered string, which also contains user-supplied values verbatim. An end user controlling any interpolated value could forge system/model turns, inject media parts with arbitrary URLs, move history, or inject pending section parts. Every marker now carries a random 128-bit nonce generated per render, and the parser only honors markers with that nonce, classifying pieces by position rather than prefix. Anything else that looks like a marker stays literal text.
This was referenced Nov 16, 2026
Collaborator
Author
|
/gemini review |
The media/section marker regex used `.*?`, which stops at newlines. A user-controlled media URL or section name containing a newline made the trusted marker fail to match, dropping the media part and leaking the raw marker, nonce included, as text. The body now matches across newlines (`(?s:...)`), still lazily up to the first `>>>`. Media bodies are split on single spaces instead of arbitrary whitespace so such a URL survives intact. The marker machinery (`markers` module, `helpers::register_marker_helpers`, `parse::to_messages`) was public, inviting callers to mint nonces and build or parse trusted markers themselves. It is now crate-private and only reachable through `Dotprompt::render`. Doctests that relied on public access were dropped; equivalent unit tests already cover them.
apascal07
reviewed
Nov 16, 2026
The per-render nonce-bound marker helpers are registered as local helpers, which shadow registry helpers. A user helper named role, history, section or media (via DotpromptOptions.helpers or define_helper) was therefore accepted and then silently ignored, whereas it used to replace the builtin (and still does in Dart). Dotprompt now tracks user-registered helper names and skips binding the local marker helper for those names, so the override runs. Overrides take full responsibility for their output, which is not nonce-bound: markers they emit are kept as plain text.
apascal07
approved these changes
Nov 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's new
<<<dotprompt:{nonce}:role:system>>>.<<<dotprompt:...>>>markers written in template source, passed in input, or returned by custom helpers are no longer interpreted. No in-repo templates rely on this.Rust specifics
RenderContext(a root-context key leaked through{{json this}}and brokespec/helpers/json.yaml).getrandom 0.3(already inCargo.locktransitively).parse::to_messagesis now crate-private (it takes the internal nonce type); themarkersmodule andhelpers::register_marker_helpersare crate-private;role/history/section/mediahelper names are effectively reserved during render.gemini-protogemini-flash-latestin README/doc examples..clone()indotprompt.rsthat current stable clippy rejects.MODULE.bazel.lock: only the crate_universe entries needed forgetrandom(unrelated pre-existing Go SDK /rules_dartlock drift left alone).Why
The
role,history,sectionandmediahelpers emit in-band markers (<<<dotprompt:role:system>>>) into the rendered string, which is then split back into messages. The rendered string also contains user-supplied values verbatim, so any end user who controls an interpolated value can forge markers:This forges system/model turns, injects media parts with arbitrary URLs (server-side fetch / SSRF when a model plugin downloads media, e.g. Genkit's Vertex AI plugin), moves history insertion, and injects pending section parts. It works through
{{x}},{{{x}}},{{json x}},#each, and partials. Escaping input can't reliably fix it (triple-stash andSafeStringhelpers skip escaping, and markers can be assembled from adjacent values), so markers are now authenticated instead.Not in this PR
spec/conformance test for injection (follow-up after all runtimes land).{{media url=...}}against user URLs with spaces or>>>(pre-existing parsing quirk, can't forge markers).downloadRequestMediaSSRF hardening (separate repo).Sibling PRs (one per runtime, independent, no stacking): JS #640, Go #641, Python #642, Dart #643, Java #644, Rust #645.