What are the potential uses of the `safe` filter in AnQiCMS besides displaying HTML?

Calendar 👁️ 66

In Anqi CMS template engine, the default automatic escaping mechanism is an important security feature, which can escape special characters in HTML tags and JS scripts (such as</>/&Convert to the corresponding HTML entity to effectively prevent cross-site scripting (XSS) attacks. However, in certain specific content output scenarios, we indeed need to allow the browser to parse and render the HTML or similar HTML code in the content as it is.safeThe filter is crucial.

We all know,safeThe most common use of the filter is to display HTML content generated by rich text editors (such as the Anqi CMS background article content editor).When you write articles, product descriptions, or single-page content in the background, use formats such as bold, italic, images, links, tables, etc., which are usually stored in the database in the form of HTML tags.If the variable is output directly without addingsafeThe filter, the user will see the original HTML code, not the beautifully formatted content. For example, on the article detail page, the document content{{ archive.Content|safe }}Must be added.|safeIn order to correctly display richly styled article content.

However,safeThe potential uses of the filter are not limited to this, it also plays a key role in handling the following situations:

1. Render HTML content converted from Markdown

With the popularity of Markdown editors, Anqi CMS also supports converting Markdown formatted content to HTML for display. When your document or category content has enabled the Markdown editor and contains Markdown syntax, it is displayed througharchiveDetailorcategoryDetailUse the tag to get contentrender=trueThe parameter indicates that the template engine should convert it to HTML. However, the converted HTML string still needssafeA filter that informs the template engine to skip escaping. This ensures that Markdown content is correctly rendered by the browser as formatted text, images, and even complex elements such as mathematical formulas and flowcharts mentioned in the document. For example,{{ content|render|safe }}This combination can achieve smooth display of Markdown content.

2. Dynamic embedded scripts and structured data

safeThe filter also plays an important role in processing some code that is not directly displayed to the user but is crucial for page functionality or search engine optimization.

  • JSON-LD structured data:JSON-LD is a format used to provide structured information to search engines, it is usually embedded in<script type="application/ld+json">Label. If these dynamically generated JSON strings contain quotes or other special characters, if notsafeFilter, may cause JSON structure to be escaped and damaged, affecting the correct parsing of search engines. Although Anqi CMS provides specialjsonLdLabel to simplify this kind of operation, but its internal principle is to rely on ensuring the 'safe' output of content.
  • Website statistics and tracking script:When you need to dynamically insert JavaScript code snippets generated from external services (such as Baidu Statistics, Google Analytics) or custom logic into templates, for example, the statistical code mentioned in the document{{- pluginJsCode|safe }},safeThe filter is the key to ensure that this code can be correctly identified and executed by the browser. It avoids<script>Tags and their internal code are escaped as plain text, thereby ensuring the normal operation of the script.
  • Inline SVG or CSS:In some advanced template customization, if you need to dynamically generate SVG graphics or inline CSS styles, and this content is dynamic and known to be safe,safeFilters can also help them be parsed and displayed correctly by browsers, rather than being mistaken for plain text.

3. Integrate the output of third-party modules or plugins.

If you have integrated some third-party modules or plugins in AnQi CMS, the output content may already include pre-formatted HTML fragments. In this case, if the output of the module is known to be safe and needs to maintain its original HTML structure, then usesafeThe filter is a necessary step to integrate it correctly into the page. This avoids double escaping, which can cause content to display abnormally.

UsesafeSecurity tips for the filter:

ThoughsafeThe filter provides great flexibility, but its use mustextremely cautious. As the name implies, it is to declare that 'this content is safe and should not be escaped'.This means, if you use user input content (such as comments, messages, etc.) without strict filtering and verification directlysafeThe filter output, so the potential malicious script code (XSS) may be executed, posing a serious security risk to the website. Therefore, when usingsafeMake sure the content source is completely trustworthy or has passed strict content filtering and disinfection processing on the server side before the filter.

In short, the Anqi CMS'ssafeThe filter is not just a tool for displaying rich text, it is also a key to processing all content that needs to be parsed exactly as the browser in template development. From the rich text generated from the backend, the HTML converted from Markdown, to the dynamically embedded scripts and structured data,safeThe filter ensures correct rendering of content while also requiring developers to be highly vigilant about the security of the content source in order to fully utilize its value.


Frequently Asked Questions (FAQ):

1. UsesafeDoes the filter mean that my website is not safe?Not.safeThe filter itself is a tool, its security depends on how it is used. It allows you to declare that certain content is safe and will not be escaped. If you directly use unprocessed user input withsafeFiltering together, it will indeed introduce XSS risk. However, if the content comes from a source you completely trust (such as content entered by an administrator in the background editor, or code that has been strictly filtered on the server side), then usingsafeIt is necessary and safe in these scenarios.

2. When should it be usedsafeWhen not to use a filter

  • Should use:When you need to display the HTML content generated by the backend rich text editor, the HTML converted from Markdown, dynamically inserted JSON-LD data, and known to be safe and need to be executed directly by the browser JS scripts or CSS styles.
  • Should not use:Any content directly from user input (such as comments, message form submissions) that has not been strictly filtered and sanitized on the server side.For this content, the default automatic escaping mechanism should be enabled to prevent potential XSS attacks.

3. How can I correctly display my article content that contains both Markdown syntax and HTML tags, and the Markdown editor is enabled on the backend?In this case, you should use first:render=trueThe parameter converts Markdown content to HTML and then appliessafethe filter. The correct order is:{{ archiveContent|render|safe }}.renderResponsible for parsing Markdown into HTML, andsafethen ensure that the generated HTML code is not escaped again by the template engine, so that it can be displayed correctly in the browser.

Related articles

How to prevent AnQiCMS template from automatically escaping HTML tags and output the original content directly?

When using AnQiCMS to build a website and design a template, you may encounter a common problem: when outputting some content in the template, the tags that were originally expected to be displayed as HTML are automatically converted to plain text, for example, `<p>This is a paragraph</p>` becomes `&lt;p&gt;This is a paragraph&lt;/p&gt;`.This loses the original style and structure of the content.Understanding this problem and knowing how to handle it is very important for template developers.Why does the AnQiCMS template automatically escape HTML tags?

2025-11-08

How to call and safely display the `Content` field that contains HTML on the AnQiCMS document detail page?

On a website built with AnQiCMS, the core of the document detail page is often the main content of the article, namely the `Content` field.This field carries a wealth of information, ranging from simple text to complex text and image layouts, multimedia embedding, and even custom code segments.Therefore, how to correctly and safely display these contents containing HTML format in the template is a key skill that every AnQiCMS user needs to master.AnQiCMS when designing template rendering, fully considers the security of the content.

2025-11-08

Can I control whether Markdown content in AnQiCMS templates is automatically converted to HTML?

In website content management, we often need to balance the writing efficiency of content and the final presentation effect.Markdown with its concise syntax greatly enhances the speed of content creation.But the question that follows is: do we always want the content to be automatically converted to HTML when it enters the template?Or in some specific scenarios, we want to maintain the original Markdown format, or manually control the conversion process?

2025-11-08

How does AnQiCMS render Markdown formatted article content to HTML?

AnQiCMS boasts its efficient content management capabilities, is favored by users, especially in handling text content, providing flexible and diverse options.For users accustomed to using Markdown format, AnQiCMS also provides comprehensive support, able to seamlessly render Markdown formatted article content into user-friendly HTML pages.To understand how AnQiCMS achieves this transformation, we can explore from three aspects: content creation, system configuration, and template rendering.

2025-11-08

How does AnQiCMS template escape HTML to prevent XSS attacks when displaying user submitted content?

In today's network environment, website security is of great concern to operators, among which cross-site scripting attacks (XSS) are one of the common security threats.XSS attacks inject malicious scripts into web pages, steal user data, alter page content, and even control user sessions.AnQiCMS as a content management system that focuses on security, built a series of powerful HTML escaping mechanisms to effectively prevent such attacks when processing user submitted content and displaying it in templates.

2025-11-08

What are the differences and usage scenarios between the `escape` filter and the `e` filter in AnQiCMS?

In the development of Anqi CMS templates, we often encounter the need to handle the security of content display, especially when the content may contain user input or be obtained from external sources.It is particularly important to escape special characters at this time to prevent potential cross-site scripting attacks (XSS).AnQi CMS provides the `escape` and `e` filters to help us deal with such issues, they have the same function, and `e` is just an abbreviation alias of `escape`.

2025-11-08

How to handle JavaScript code output containing special characters (such as `&lt;script&gt;`) in AnQiCMS?

In website operation, we sometimes need to output custom JavaScript code on the page, which may be to implement specific interactive functions, integrate third-party service scripts (such as statistical codes, advertising codes), or add some dynamic effects to the page.However, when these JavaScript codes themselves contain some special characters, especially HTML tags (such as `<script>`), if not handled correctly, it may cause the page to display abnormally, disable functions, or even bring serious security vulnerabilities.

2025-11-08

How to control the automatic escaping of HTML content with the `autoescape` tag in AnQiCMS templates?

In AnQiCMS template development, for the safety of the website, the system defaults to automatically escaping all HTML content output to the page. This means that when you directly output a variable containing special HTML characters in the template, for example, `<script>alert('XSS')</script>`, AnQiCMS will convert it to `&lt;script&gt;alert(&#39;XSS&#39;)&lt;/script&gt;`

2025-11-08