Using the Safe Attribute
Estimated reading time: 3 minutesEvery child expression in Kita Html renders without escaping by default. To escape user
input, apply the safe attribute to the nearest native element wrapping the expression.
Native elements
Add safe to the element that directly contains the dynamic content. The runtime passes
all children through HTML entity escaping before concatenation.
Without safe, malicious input executes. Consider a user profile where the description
field contains
</div><script>fetch('/steal', {method: 'POST', body: document.cookie})</script>.
Rendering this without escaping closes the container early, injects a script tag, and runs
arbitrary code. The safe attribute converts the angle brackets to < and >,
rendering the payload as harmless text.
Place safe on the innermost element that holds the untrusted value. Do not add it to a
parent wrapper, as that would escape the HTML of child components too.
Component children
The safe attribute only applies to native elements. If you pass an unsafe value as a
child to a component, you must either escape it manually or wrap it in a Fragment with the
safe attribute to tell the plugin it's already escaped. This prevents components from
accidentally rendering unescaped HTML when they receive dynamic content as children.
Template literal helper
The e tagged template escapes interpolated values while preserving literal HTML around
them.
Suppression conventions
When the XSS detection plugin flags a value that you know is safe, you can suppress the
warning without adding safe to the element, which would escape all children and
potentially break components.
Suppressing warnings is dangerous because it allows unescaped HTML to slip through. Only use these techniques when you have a guarantee of safety.
Prefix the variable name with safe. The plugin treats any identifier starting with
safe as pre-escaped.
Following the same logic, prefixing a variable with unsafe explicitly marks it as
unescaped and triggers a warning if used in JSX.
Cast the expression to 'safe' inline. This tells the plugin to skip the check for that
specific usage.
Call Html.escapeHtml() directly. The plugin recognizes the return value as escaped.