How to safely display HTML content generated by the rich text editor in AnQiCMS templates?

Calendar 👁️ 69

In website operation, we often need to handle content from rich text editors, which usually contains richly formatted HTML code.However, when displaying these HTML contents on a website template, security is the primary concern.If not handled properly, malicious code (such as XSS attacks) may be injected, thereby harming the website and users.

AnQi CMS is a highly efficient, customizable, and secure enterprise-level content management system that fully considers content security issues from the very beginning.The system is committed to making all websites safe in the world, built-in anti-crawling interference codes, content security management, and sensitive word filtering functions, ensuring the safety and compliance of content from multiple levels.In terms of template rendering, Anqi CMS also provides a flexible and powerful mechanism to help users safely display rich text content.

Understanding rich text content and potential risks

The rich text editor allows content creators to easily add titles, lists, images, links, and even custom styles, which are ultimately stored in the form of HTML tags. For example, a simple bold text will be saved as<strong>您的文本</strong>Although this brings great convenience to the richness of the content, it also opens the door to potential security risks.

The most common risk is cross-site scripting (XSS) attacks. Malicious users may insert into rich text content.<script>Label, execute illegal JavaScript code, steal user data, tamper with page content, and even hijack user sessions.Moreover, non-standard HTML may also lead to chaotic page layout and style errors, affecting user experience.Therefore, how to display this content while preserving its richness and ensuring safety is a challenge that every website operator must face.

How AnQiCMS handles HTML content: default behavior and manual control

The Anqi CMS is developed based on Go language and Django template engine syntax, and its template system takes strict security measures by default when processing variable output.Unless explicitly indicated, all HTML tags and JavaScript code output from the backend will be automatically escaped.This means, similar<script>tags will become&lt;script&gt;Therefore, it is displayed in plain text, loses its execution capability, and greatly reduces the risk of XSS attacks.This 'default security' design is the foundation for AnQiCMS to ensure content security.

However, the original intention of using a rich text editor is to display formatted content, if all HTML is escaped, then the richness of the content will be lost.Therefore, AnQiCMS provides several ways to allow us to selectively display HTML based on the trust level and needs of the content.

Practical methods to ensure safe display of HTML

When you need to display the HTML content generated by the rich text editor in AnQiCMS templates, you can choose from the following strategies based on the actual situation:

1. UsesafeFilter (when you are sure the content is safe and you need to render HTML)

safeThe filter is the most direct and commonly used method in AnQiCMS templates, used to inform the template engine that the content it processes is "safe", does not require HTML escaping, and can be rendered directly as HTML code.

For example, on the document detail page, we usually call the document'sContentfield to display the article content. IfContentThe field contains HTML, and we want these HTML to be parsed normally by the browser, so we need to usesafeFilter:

{# 显示文档内容,并确保HTML被正常渲染 #}
<div>
    {%- archiveDetail articleContent with name="Content" %}
    {{ articleContent|safe }}
</div>

HerearchiveDetailThe tag is used to get the details of the document,articleContentis a temporary variable we define to storeContentthe content of the field. Then, through{{ articleContent|safe }}We explicitly tell the template engine,articleContentThe HTML in the variable is trustworthy, do not escape it and render it directly.

Important reminder: safeThe use of filters means you fully trust the source of the content. Once usedsafe,AnQiCMS will not perform any HTML escaping on the content.If the content contains malicious scripts, they may be executed.Therefore, ensure that only rich text content from trusted sources or strictly reviewed is usedsafefilter.

Second, use Markdown rendering (when editing Markdown content)

AnQi CMS supports Markdown editor, which provides another secure and efficient way for content creation and display.When the Markdown editor is enabled in the background and the content is stored in Markdown format, AnQiCMS can automatically convert Markdown to HTML during template rendering.

First, you need to enable the Markdown editor in the "Global Settings" -> "Content Settings" of the AnQiCMS backend.

Then, when calling fields containing Markdown content in the template, you can addrender=trueParameters to indicate the system to perform Markdown to HTML conversion. For example:

{# 假设Content字段存储的是Markdown内容,通过render=true进行转换,并用safe渲染 #}
<div>
    {%- archiveDetail articleContent with name="Content" render=true %}
    {{ articleContent|safe }}
</div>

The advantage of Markdown is that its syntax is relatively simple and not easily injected with complex malicious HTML.Convert it to HTML using the system's built-in converter, which to some extent provides a controlled HTML generation process, reducing the risk of directly handling users' arbitrary HTML.

3. Fine control: Remove or strip unnecessary HTML tags.

If some rich text content source is not fully trusted, or if you only want to display plain text and do not want any HTML tags to appear, AnQiCMS provides `striptags

Related articles

How to directly output HTML code without being automatically escaped in AnQiCMS templates?

In AnQiCMS templates, we often need to flexibly control the way content is displayed.You may have encountered such a situation: you carefully formatted some content containing HTML tags in the rich text editor on the back end, but when the output was displayed on the front page, the tags were displayed as plain text, rather than rendered according to the HTML structure.This is actually a security mechanism that is in operation by default in AnQiCMS and many modern content management systems.

2025-11-08

How to implement the like function for comments in AnQi CMS?

In website operation, enhancing user interaction is a key factor in maintaining the vitality of the website and increasing user stickiness.The comment section is an important battlefield for users to express their opinions and interact with each other. Adding a like feature would undoubtedly significantly enhance the interactivity and community atmosphere of the content.The Anqi CMS is an efficient, customizable, and feature-rich enterprise-level content management system that provides flexible mechanisms, allowing users to easily add the popular interactive feature of liking comments in the comment section.This article will give a detailed explanation of how to implement the like function for comments in Anqi CMS.

2025-11-08

How to display the document comment list and implement pagination?

When using Anqi CMS to manage website content, the comment function on the document detail page is an important part of interacting with readers.Effectively display these comments and ensure that the page maintains good loading speed and user experience as the number of comments increases, it is necessary to properly handle the display and pagination of the comment list.The Anqi CMS provides very intuitive and powerful template tags, helping us easily achieve this goal.

2025-11-08

How to display the comment form and submit comments in Anqi CMS?

It is crucial to establish an effective communication channel with visitors in website operation, and the message function is one of the key tools to achieve this goal.It can not only help websites collect user feedback and answer questions, but also promote user participation, enhance the interactivity and stickiness of the website.For users using AnQiCMS, the built-in message function provides a convenient and flexible solution, allowing you to easily display and submit message forms without writing complex code.### Overview of AnQi CMS Message Function The AnQi CMS message function is designed to be very user-friendly

2025-11-08

What potential security risks should be considered when outputting HTML using the `safe` filter?

In AnQiCMS template development, the `safe` filter is a very useful tool that allows us to directly output some content containing HTML tags to the page, rather than escaping the HTML tags as well.This is crucial for displaying details of articles generated by rich text editors, custom HTML modules, and other scenarios, as it ensures the correct rendering of the style and structure of the content.However, like all powerful tools, the use of the `safe` filter also comes with some potential security risks that should not be overlooked.

2025-11-08

Why does my AnQiCMS template still escape HTML code even though I used `safe`?

When using AnQiCMS for website content creation and template customization

2025-11-08

In AnQiCMS, what is the difference in usage scenarios between the `safe` filter and the `autoescape off` tag?

In AnQiCMS template development, we often encounter the need to display content containing HTML tags.For the security of the website, the AnQiCMS template engine defaults to automatically escaping all output variables.This means that if you directly output a text containing HTML tags such as `<strong>` or `<em>`, these tags will not be parsed as styles by the browser, but will be displayed as raw text.

2025-11-08

How to truncate the first N characters of an AnQiCMS article title and add an ellipsis at the end?

In website operation, we often encounter such needs: in order to make the page layout beautiful, present information succinctly, or improve search engine friendliness, it is necessary to truncate the article title and add an ellipsis at the end when the title is too long.AnQiCMS (AnQiCMS) flexible template system and rich built-in filters make this operation very simple and efficient.

2025-11-08