As an experienced website operations expert, I know that the flexibility of a content management system (CMS) is both an advantage and may also bring potential security challenges.AnQiCMS is widely popular among many enterprises and content operators due to its simplicity, efficiency, and powerful customizability.{% diy %}Tag acquisition of background customized content, which greatly enriches the dynamics of the template.However, how to ensure that these customized contents are safely output in the front-end template and avoid potential risks is a serious problem that every operator must think about.

Know{% diy %}its essence and potential risks

{% diy %}Tags play the role of obtaining backend custom configuration in AnQiCMS.Imagine that you can define some less commonly used parameters in the background "Global Settings" or "Contact Information Settings", such as special announcement text, slogans for a promotional activity, or even dynamic customer service QQ numbers.{% diy with name="参数名" %}extracted and displayed in the front-end template.

This custom capability brings great convenience, as it allows operators to adjust certain dynamic information on the website without modifying the code.However, behind the convenience there are also risks.<script>alert('XSS');</script>), and if these contents are output directly on the front end without any processing, then these malicious scripts will execute in the browser of the users accessing the website, which is known as cross-site scripting (XSS).XSS attacks can lead to session hijacking, data leakage, and even website tampering, causing serious damage to the trust of both users and the website.

The default security mechanism of AnQiCMS: automatic escaping is the first line of defense

It is fortunate that AnQiCMS fully considered content security during the design.Its template engine (similar to Django template engine) adopts a security mechanism called 'autoescape' when handling default output.

This means, when you use double curly braces in the template{{ 变量 }}When outputting content, the template engine will automatically convert special HTML characters in the content. For example:

  • <will be escaped as&lt;
  • >will be escaped as&gt;
  • &will be escaped as&amp;
  • "will be escaped as&quot;
  • 'will be escaped as&#39;

This default behavior greatly enhances security. Even if someone is in the background:{% diy %}Custom content entered here<script>alert('XSS');</script>will also be output on the front end&lt;script&gt;alert('XSS');&lt;/script&gt;The browser will treat it as plain text instead of executable JavaScript code, effectively preventing XSS attacks. Therefore, in most cases, you don't need to worry about it any further.{% diy %}Content default output security issues.

When do we need to pay extra attention?|safeThe double-edged sword of filters

However, automatic escaping is not always the result we expect. Sometimes, we do indeed need{% diy %}The content retrieved is displayed in rich text format containing HTML tags, such as a custom announcement with bold text, links, and even images.In this case, simply outputting the escaped HTML code will not achieve the expected visual effect.

To solve this problem, AnQiCMS template engine provides|safefilters. When you are sure that the content of a variable is pure and harmless HTML, you can use{{ 变量|safe }}The form indicates to the template engine: "This content is safe, please do not escape, and output it as HTML code directly."

This is exactly|safeThe reason why it is called a 'double-edged sword'.It gives you greater control, allowing for custom output of HTML, but it also means you take on the responsibility of content safety review.|safeThe template engine will completely trust this content and will not perform any escaping. If at this time{% diy %}The content includes malicious scripts that will execute smoothly in the user's browser, putting the website at serious risk of XSS attacks.

Ensure{% diy %}Practicalities of content security**

Understood the security mechanism of AnQiCMS and|safeits features, we can then develop a set of strategies to ensure{% diy %}content safety output:

  1. Never use unless necessary|safeFilter.This is the most core principle. If{% diy %}the content obtained is purely text (such as contact information, website name, etc.), then keeping the default automatic escaping behavior is**the choice, no need to add|safeOnly when the content truly needs to be displayed in HTML format (such as containing), should it be considered to use it.<b>/<a>/<img>etc.), it should be considered to use it.

  2. Strictly control the source of background content input.Even if you need to use|safe, make sure that{% diy %}The source of the retrieved custom content is highly可信. This usually means:

    • Restrict modification permissions:Only core administrators (who have a basic understanding of web security) can modify the potentially used|safeLabel custom parameters.
    • Manual review:For custom content that needs to output HTML, there must be a strict manual review process. Before saving content in the background, it should be carefully checked for any suspicious tags or scripts.
    • Minimize HTML:If HTML is indeed necessary, try to allow only the most basic tags (such as<b>,<i>,<a>,<strong>,<em>). Avoid allowing<script>,<iframe>,<style>tags that may pose security risks.
  3. Make good use of other filters for content purification.AnQiCMS provides a variety of filters, even if you decide to use|safeThe value can also be used to refine and control the content more precisely:

    • |striptags:If you only want to display plain text, no matter how much HTML code is entered in the background, you can use{{ diy_content|striptags|safe }}Remove all HTML tags.
    • |removetags:"tag1,tag2":If you want to retain some safe tags while removing other potentially dangerous tags, you can use this filter. For example,{{ diy_content|removetags:"script,iframe,style"|safe }}can be removedscript/iframeandstyleLabel, while retaining other labels that have not been removed.
    • |truncatechars_html:长度:If the custom content may be long and you wish to display only a part of it on the page while maintaining the integrity of the HTML structure, this filter is very useful. For example,{{ diy_content|truncatechars_html:100|safe }}Can extract the first 100 characters while maintaining the HTML structure.
  4. Regular audits and testing.No matter how完善(sophisticated) the security measures, there may be loopholes. As an operator, one should regularly review the HTML source code of the website's front-end page, check{% diy %}Does the output content meet the expectation, without unexpected HTML structure or suspicious scripts.Try entering some test XSS code in the background and check if the front-end output is correctly escaped.

Summary

Anqi CMS's{% diy %}Tags have brought unprecedented flexibility to content operations, but also require us to maintain a high level of vigilance over content safety.It is fortunate that AnQiCMS's default automatic escaping mechanism provides us with a solid first line of defense.|safeBy adhering to the principle and combining strict backstage management processes with flexible filters for content purification, you can enjoy highly customized content while ensuring the safety and stability of the front-end output of the website.


Common Questions (FAQ)

Q1:{% diy %}The content obtained by the tag and the common one{{ 变量 }}What are the differences in security processing for the output content?A1: At the template engine level of AnQiCMS, whether it is `{% diy %`