In AnQiCMS (AnQiCMS) template development, we often encounter the need to display content containing HTML tags.For the security of the website, AnQiCMS's template engine defaults to automatically escaping all output variables.<strong>or<em>Text within HTML tags, which will not be parsed as styles by the browser but will be displayed as raw text.This mechanism is mainly to prevent cross-site scripting attacks (XSS), to avoid malicious code from being injected into the page.
However, in certain specific scenarios, we indeed need to allow the browser to correctly parse and render these HTML contents, such as the article body output by rich text editors, third-party content containing specific embedded code, and so on. At this point, Anqi CMS provides two main ways to manage or disable the automatic escaping of content:safeFilters andautoescape offLabel. Although they can all achieve the effect of 'not escaping', there are obvious differences in their usage scenarios and scope.
safeFilter: Precisely control single output
safeA filter, as the name implies, is used to declare that the content of a variable is "safe" and does not require HTML escaping.When we apply this filter to a variable, we are actually telling the AnQiCMS template engine: 'I am sure that the content in this variable is verified, trustworthy HTML code, please render it as HTML directly.'
Usage scenario:The most common application scenario is to obtain and display the article text from a rich text editor (such as the content editor of AnQiCMS backend).This content usually includes user-defined HTML formats, such as paragraphs, bold, images, links, and so on.safeFilter, the original HTML code will be displayed on the page instead of the formatted layout that the user expects.
For example, if the variable of your article content isarchiveContentAnd it includes HTML tags, you can output it safely in the template like this:
{# 假设 archiveContent 包含了 HTML 内容,如 <p>这是<b>加粗</b>的文字</p> #}
<div>
{{ archiveContent|safe }}
</div>
Features and precautions:
safeThe granularity of the filter's effect is very fine, it only targets the applied toa single variableThis means that even if other content on the page still follows the automatic escaping rules, this one issafeThe marked variable will also be processed specially.This precise control method allows developers to maintain security protection for most content while only opening HTML rendering permissions for a few confirmed risk-free specific contents.
However, this \safeThe content marked actually contains malicious script, it will not be escaped, which may lead to XSS vulnerabilities. Therefore,Only when you are completely sure that the content of a variable is pure and harmless HTML should you use itsafefilter.
autoescape offTag: Regional management of escape behavior
withsafeFilters operate on individual variables differently,autoescape offThe tag provides a broader, regional control range.It is a block-level tag used to define a starting and ending point, within which all output variables are by default not subject to HTML escaping.
Usage scenario:
autoescape offThe tag is applicable to a larger template area that contains multiple variables or static HTML fragments, and you confirm that all the content within this area should be raw HTML and does not require escaping.For example, you might have a custom page layout, where most of the content is manually written or obtained from an internal system, and you want them to be rendered in their original HTML form.
A simple usage example:
{% autoescape off %}
<p>以下内容将不会被自动转义:</p>
<div>
{{ variable_one }} {# 如果 variable_one 包含HTML,将直接渲染 #}
<span>{{ variable_two }}</span> {# variable_two 同样不会被转义 #}
</div>
<script>
// 这里的JS代码,如果通过变量输出,也不会被转义
var data = "{{ trusted_js_data }}"; // 注意:这种用法在处理JS字符串时需要特别谨慎
</script>
{% endautoescape %}
Features and precautions:
autoescape offThe scope of the tag is the entireCode block. Once inside this block, unless explicitly re-enable automatic escaping (by{% autoescape on %}), otherwise all variable outputs will skip HTML escaping.
Because its scope is wider,autoescape offBe more careful when using the label.In a large code block, disabling automatic escaping significantly increases the risk of XSS attacks because any unreviewed variable output may become an entry point for attacks.
- The entire template or local template file is designed to render known-safe HTML fragments and contains a large number of variable outputs.
- Temporarily disable escaping to check the original output during development or debugging.
- Process a large amount of clear HTML content from the inside that is safe and pre-processed to reduce redundancy in template code.
Core differences and selection suggestions
In summary,safeFilters andautoescape offThe core difference of tags lies in theirScope of action:
safeThe filter isLocal and preciseOnly affects a single variable.autoescape offis a tagRegionalAffects the entire code block it wraps.
Select a recommendation:In most cases,We recommend using it preferentiallysafeFilter.Because it can limit the range of content that needs to be escaped to the minimum, thus reducing potential security risks to the maximum extent possible.autoescape off.
No matter which way you choose, safety is always the top priority.At any time, when content comes from user input or untrusted external data, it should be strictly validated and sanitized on the backend to ensure that it is safe before entering the template rendering process, in order to prevent the injection of malicious scripts.
Frequently Asked Questions (FAQ)
Q1: If I do not usesafeorautoescape offAnQiCMS will handle my content in what way?A1: AnQiCMS's template engine is enabled by default to automatically escape HTML. This means that all variables contain HTML tags such as<,>,&,",'Will be converted to the corresponding HTML entities (such as<,>,&), displayed as plain text to effectively prevent XSS attacks.
Q2: In most cases, I should choose to usesafeOrautoescape offWhat?A2: It is recommended to use firstsafeFilter. Because it only affects the single variable applied, the control range is smaller, and the risk is easier to control.autoescape offThe tag has a wide range of effects, and any unsafe content within its wrapping area may lead to vulnerabilities, so it should be used with caution.
Q3: Can I{% autoescape off %}Within the block, is it still possible to force escaping for certain content?A3: Of course, evenautoescape offWithin the block, you can also explicitly useescapeFilter (or its aliase)to force HTML escaping of the content of a specific variable. For example:{{ variable_with_html | escape }}. Even in most of the content that is not escaped, you can provide additional security protection for some particularly sensitive or uncertain content. Conversely, in a block, {% autoescape on %}block, safeThe filter is still valid, it will force the variable not to be escaped.