HTML Rendering¶
For rendering HTML documents, gg.jte.ContentType.Html
is highly recommended for security but also for convenience.
Smart Attributes¶
Expressions in HTML attributes are evaluated, so that optimal output is generated. This means attributes with a single output that evaluates to null
or false
are not rendered. For instance:
Will be rendered as:
If an HTML attribute is boolean, jte requires you to provide a boolean expression, and it will omit the attribute if that expression evaluates to false
. For example:
Will render this HTML:
Natural comments¶
jte does not render HTML, CSS and JavaScript comments. You can use the natural comment syntax without worrying about leaking information/data to the outside.
HTML Escaping¶
Output escaping depends on the gg.jte.ContentType
the engine is created with:
- With
ContentType.Plain
, there is no output escaping. - With
ContentType.Html
, the OwaspHtmlTemplateOutput is used for context sensitive output escaping.
In Html
mode, user content ${}
is automatically escaped, depending on what part of the template it is placed into:
- HTML tag bodies
- HTML attributes
- JavaScript attributes, e.g.
onclick
<script>
blocks
HTML tag bodies¶
User output in HTML tag bodies is escaped with gg.jte.html.escape.Escape.htmlContent
.
With userName
being <script>alert('xss');</script>
, the output would be:
HTML attributes¶
User output in HTML attributes is escaped with gg.jte.html.escape.Escape.htmlAttribute
. It ensures that all quotes are escaped, so an attacker cannot escape the attribute.
With userName
being "><script>alert('xss')</script>
, the output would be:
The quote "
is escaped with "
and the attacker cannot escape the attribute.
JavaScript attributes¶
User output in HTML attributes is escaped with gg.jte.html.escape.Escape.javaScriptAttribute
. Those are all HTML attributes starting with on
.
With userName
being '); alert('xss
, the output would be
In case you run a strict content security policy without unsafe-inline
, you could configure jte to run with gg.jte.html.policy.PreventInlineEventHandlers
. Then, using inline event handlers would cause errors at compile time. See this issue for additional context.
Then, you set it with templateEngine.setHtmlPolicy(new MyHtmlPolicy());
.
For more examples, you may want to check out the TemplateEngine_HtmlOutputEscapingTest
.
Unsafe¶
In rare cases, you may want to skip output escaping for a specific element. You can do this by using $unsafe{}
instead of ${}
. For instance, to trust the userName
, you would write:
The syntax $unsafe{}
was picked on purpose. Whenever you use it, you're risking XSS attacks, and you should carefully consider if it really is okay to trust the data you're outputting.
Custom output escaping¶
It is possible to provide your own implementation of gg.jte.html.HtmlTemplateOutput
. Maybe you want to extend the default gg.jte.html.OwaspHtmlTemplateOutput
, or use your implementation.
Before rendering, you'd simply wrap the actual gg.jte.TemplateOutput
you are using:
Raw output¶
Sometimes, it is required to output text as is. Use the @raw
keyword to open a raw section unprocessed by jte.