In Anqi CMS template development, we often encounter situations where we need to handle special characters or HTML content.The system provides various filters to help us control the display of content more flexibly. Among them,addslashesFilters and|safeFilters are all related to character processing, but their mechanisms of action and application scenarios are quite different.Understanding their specific functions and performance when used together is crucial for ensuring the safety and correct rendering of website content.

Recognize the default security mechanism of Anqi CMS template

Firstly, we need to know that Anqi CMS is developed based on Go language, and its template engine has fully considered security from the design.This means that when you output a variable that may contain HTML tags or JavaScript code directly to a template, the system will automatically escape these special characters by default.For example, a containing<script>The string of the tag, when output without processing, will finally be displayed in the browser&lt;script&gt;This is not executed by the browser. This automatic escaping mechanism is the first line of defense against security vulnerabilities such as cross-site scripting attacks (XSS).

addslashesFilter: Add 'umbrella' for special characters

addslashesAs the name implies, the main function of the filter is to handle predefined characters (single quotes) in strings', double quotes"、backslash\and NULL character) is often used to precede it. This is usually done in specific scenarios, such as when outputting strings as JavaScript variables or regular expressions to ensure that these special characters are recognized as literal values rather than being parsed as code structures.

Imagine that we have a string我喜欢'AnQiCMS'If we pass it throughaddslashesAfter filtering it will output:

{{ "我喜欢'AnQiCMS'"|addslashes }}

At this point, the output content will become我喜欢\'AnQiCMS'However, since the default HTML escaping mechanism of the template is still active, the final effect seen by the browser is actually我喜欢&#39;AnQiCMS&#39;because the newly added backslash is also escaped.&#92;. Here,addslashesIt only modifies the string at the logical level, but the HTML-level security protection still exists.

|safeFilter: Remove the 'seal' of HTML escaping.

withaddslashesdifferent,|safeThe filter's function is to explicitly tell the Anqi CMS template engine: 'This content is safe, please do not escape it as HTML, and output it directly as the original content.'This filter is typically used for outputting content that you are sure has been processed, or is itself HTML code that needs to be parsed normally by the browser, such as articles obtained from rich text editors.

If you have a variable containing HTML tagsunsafe_htmlIts value is<script>alert('xss')</script>and you want it to be rendered as real HTML:

{{ unsafe_html|safe }}

At this point, the browser will directly parse and attempt to execute this JavaScript code. Because of|safeThe filter has the powerful ability to remove the default HTML encoding, so we must be very cautious in its use, ensuring that the content being processed is indeed trustworthy and harmless to avoid introducing security risks.

addslasheswith|safeHow do the effects change when used simultaneously: which one first, and how?

WhenaddslashesFilters and|safeWhen filters are applied to the same variable, they will be executed in order from left to right. This is an operation in sequence, not a priority competition.

In detail, the process is as follows:

  1. addslashesFirst execute:The original string content of the variable will first pass throughaddslashesThe filter processes, where predefined special characters (such as single quotes, double quotes, backslashes) are escaped with a backslash.
  2. |safeThen execute:Then,|safeThe filter acts onhas beenaddslashesThe processed string. Because|safeThe purpose is to disable the subsequent HTML escaping, therefore, this string containing the new backslash will be output directly to the HTML without any HTML escaping.

Let's understand through an example to get a直观 impression:

Suppose we have a string variablemy_string = "这是一个'测试'字符串,包含\\反斜杠和\"双引号\"".

  1. only useaddslashes:

    {{ my_string|addslashes }}
    

    Output effect (as displayed in the browser):这是一个&#39;测试&#39;字符串,包含&#92;反斜杠和&#92;&quot;双引号&#92;&quot;Explanation:addslashesAdded a backslash, then the default template mechanism escapes all HTML special characters (including the backslash itself).

  2. only use|safe:

    {{ my_string|safe }}
    

    Output effect (as displayed in the browser):这是一个'测试'字符串,包含\反斜杠和"双引号"Explanation:|safeDisabled HTML escaping, the original string content is output directly.

  3. At the same timeaddslashesand|safe:

    {{ my_string|addslashes|safe }}
    

    Output effect (as displayed in the browser):这是一个\'测试\'字符串,包含\\反斜杠和\"双引号\"Explanation: First,addslashesProcessing the string as这是一个\'测试\'字符串,包含\\反斜杠和\"双引号\"Then,|safeA string that acts on this processed string, indicating that the template engine should output directly without performing HTML escaping. Therefore, we see the literal backslashes in the string.

Conclusion:addslashesThe filter is responsible for modifying the string content, while|safeThe filter controls whether the template engine performs HTML escaping.There is no priority conflict between them, but they are executed in the order of the pipe (pipe), with the output of the previous filter as the input of the next filter.The final effect depends on the superposition of these two operations.

The choice in practical applications

In most cases, you are unlikely to need to use both these filters to process regular text content.

  • If you needoutput a rich text editor generated HTML contentand make sure that its HTML tags can be parsed by the browser, then just use|safeBut please make sure that these HTML contents are trusted.
  • If you need toembed dynamically generated strings in JavaScript codeAnd you need to ensure that the quotes, backslashes, and other special characters in the string do not break the JS syntaxaddslashesThen consider preprocessing the string and whether it is necessary|safeThis depends on how you finally embed the JS code into HTML (for example, if the JS code itself isscriptpart of the tag, then|safeIt is required; if the JS code is loaded from an external file, then HTML escaping is usually no longer an issue).

Remember, safety is always the first priority.|safeIt is a powerful tool, but it also comes with corresponding risks. Be sure to verify and clean the source of content before using it.


Frequently Asked Questions (FAQ)

  1. What is the default HTML escaping mechanism of the Anqi CMS template? Why is there such a mechanism?The template engine of AnQi CMS defaults to escaping all output variable content. This means that, like</>/&/'/"Special characters are converted to their corresponding HTML entities (for example,<changes to&lt;This mechanism is primarily designed to prevent cross-site scripting attacks (XSS), ensuring that malicious HTML or JavaScript code contained in user or external data is not executed on your website by the browser, thereby improving website security.

  2. When should I use|safeFilter, what are the risks?You usually use in two cases|safeFilter: First, when you know for sure that the output string contains the HTML content you want the browser to parse (such as content obtained from a rich text editor on an article detail page); second, when you have strictly sanitized the string to ensure it does not contain any malicious code.|safeThe risk lies in the fact that it completely disables the HTML escaping protection of the template. If it is|safeContent from untrusted sources, which may contain malicious scripts, will be executed in the user's browser, leading to XSS attacks, severely affecting the security of the website and users.

  3. addslashesCan the filter effectively prevent XSS attacks?No.addslashesThe primary function of the filter is to add a backslash before specific characters (such as single quotes, double quotes, backslashes, etc.), which is more for maintaining the literal meaning of characters in other programming language environments (such as JavaScript strings) rather than for HTML security escaping.It does not convert HTML tags to entities, so it cannot directly prevent XSS attacks.To prevent XSS, you should rely on the default HTML escaping mechanism of the template engine, or perform strict validation and sanitization of the content.