Templates in email and PDF

A message body or a PDF layout is still a template. If the string you render was built from caller text plus engine syntax, the render is code, not correspondence.

Lesson 67 of 69 · 14 min read · practice only with authorization

Diagram

From a message draft to a render on the server

Follow the path. One example at a time.

Step 1 of 4

Draft

A person writes an email, an invoice note, or a PDF cover page.

Illustration: the note is plain text
note = "See you Tuesday."
# The note is text inside a message you designed.
What changes the outcome
Build

A string joins the draft into the template source.

Engine

It compiles the whole string.

Output

The draft can change the render.

The example on the open step is one illustration, or the fix you can paste into an application you own.

What this page is made of

4 pieces. Each one says what fails, who is affected, what you would notice, and what you change.

  1. Email is a render

    What fails
    The fact that the output is a message and not a browser page does not change the compiler. The engine still runs on the server.
    Who is affected
    Recipients, and anyone whose data was available to the render.
    What you would notice
    A mailer call that builds a template from the request body.
    What you change
    Map a message type to a file. Pass the note as text.
  2. PDF engines

    What fails
    HTML-to-PDF and office-to-PDF pipelines are template engines plus a converter. Caller HTML in that pipeline is the same class of bug.
    Who is affected
    The person who receives or stores the PDF, and the server account that ran the converter.
    What you would notice
    A cover page field concatenated into HTML that a headless renderer prints.
    What you change
    Use a fixed HTML file and escaped variables. Do not convert caller HTML.
  3. Preview is production

    What fails
    A preview endpoint renders the same way the send does. A sandbox only on send, or only on preview, leaves the other one exposed.
    Who is affected
    Whoever can open the preview, including staff looking at a customer’s draft.
    What you would notice
    Two code paths, one of which concatenates.
    What you change
    One render function. Both buttons call it.
  4. Formatting without a template language

    What fails
    If the product needs bold in the note, sanitize a tiny allow-list. Do not hand the note to the template engine as source so the author can “use tags.”
    Who is affected
    Readers of the message.
    What you would notice
    A help page that tells authors to write engine syntax.
    What you change
    Document the slots you support. Reject engine syntax by never compiling it.

Correspondence is still a render

Teams put their template discipline on the website and then build emails by concatenating a greeting, a note, and a footer into a string the mail library compiles. The note is the caller’s. The compiler is the server’s. That is the same failure as a page template built from a profile field. The recipient does not have to be a browser for the server to have run the template.

PDF cover sheets and invoice layouts do this too. A headless renderer that turns HTML into a file will parse whatever HTML you hand it. If the HTML was a fixed file plus escaped variables, you get a document. If the HTML was the customer’s note wrapped in tags you glued together, you compiled the note.

Preview and send are one function

A preview that concatenates “just for display” trains the data model to store template source, and it is already a render. Staff opening a preview may have more data on the context than the eventual recipient: internal notes, other invoices, feature flags. Use one function that loads a template file by message type and passes the note as an escaped variable. Both buttons call it.

Choose the file from a map. An unknown type is an error, not a chance to use the type string as a path. That is how a template picker becomes path traversal and template injection at once.

Slots, not a second language

Product will ask for bold, a link, or a logo. Give them slots: a sanitized fragment you produce, or a choice among layouts you shipped. Do not document the template engine’s syntax in the help center. Authors will use what you document, and the engine will honor it.

You would notice string addition around a render call, a message type that selects a file by concatenating a path, and a preview implementation copied from an old example. The people affected are recipients and anyone whose data was on the context. The fix is the map and the variable. Do not attach a sample message that changes the render.

What you change

Check the mailer, the PDF service, and the preview into the same review as page templates. Delete concatenation. Turn escaping on for the format you emit. Keep the template files in the artifact so a change to wording goes through review.

Test that punctuation in the note appears as punctuation in the HTML or PDF text layer, and that an unknown message type does not read a file. Those tests are ordinary unit tests with strings you invented for the assertion.

Check yourself

Recognition and the fix. Nothing here is a procedure.

1 / 3

The invoice mailer concatenates the customer’s note into the HTML template string and compiles it. What failed?

Sibling techniques

Pages at the same depth, plus a lab and a glossary card for this one.

  1. MapServer-side template injection: user text compiled as a templateHow SSTI happens when user input becomes template source instead of template data, how to recognize it with a harmless arithmetic probe in an authorized test, and why passing data to a fixed template is the fix.
  1. 01Sandboxed templates are not a guaranteeA sandbox flag is a claim about an engine.

Practice

  1. LabControl benchA failure is described. Choose the control that changes the outcome.

Glossary

  1. CardServer-side template injectionUser input is evaluated by the server's template engine, which can leak configuration or run template-side code.