Skip to content

trestle.core.jinja.sanitize

trestle.core.jinja.sanitize ¤

Trestle core.jinja sanitization of untrusted text.

Functions:¤

neutralize_jinja_delimiters(text) ¤

Neutralize Jinja2 template delimiters to prevent SSTI attacks.

Replaces {{ and }} with [[ and ]], and the statement ({%) and comment ({#) openers with [% and [#, to prevent untrusted OSCAL data from being interpreted as Jinja2 template code when included in markdown files that are later processed by Jinja2 include tags.

Statement tags must be neutralized as well as expressions because a sandboxed environment still permits tags such as include and for, which allow in-tree file disclosure and output amplification.

Parameters:

Name Type Description Default
text str

The text to neutralize

required

Returns:

Type Description
str

Text with Jinja delimiters replaced

Source code in trestle/core/jinja/sanitize.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def neutralize_jinja_delimiters(text: str) -> str:
    """Neutralize Jinja2 template delimiters to prevent SSTI attacks.

    Replaces {{ and }} with [[ and ]], and the statement ({%) and comment ({#)
    openers with [% and [#, to prevent untrusted OSCAL data from being
    interpreted as Jinja2 template code when included in markdown files that
    are later processed by Jinja2 include tags.

    Statement tags must be neutralized as well as expressions because a sandboxed
    environment still permits tags such as include and for, which allow in-tree
    file disclosure and output amplification.

    Args:
        text: The text to neutralize

    Returns:
        Text with Jinja delimiters replaced
    """
    if not text:
        return text
    return text.replace('{{', '[[').replace('}}', ']]').replace('{%', '[%').replace('{#', '[#')

handler: python