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 involve injecting malicious scripts into web pages, stealing user data, tampering with page content, and even controlling user sessions.AnQiCMS as a content management system that focuses on security, built-in a series of powerful HTML escaping mechanisms to effectively prevent such attacks when processing user submitted content and displaying it in templates.

The security cornerstone of AnQiCMS template: automatic HTML escaping

AnQiCMS's template engine uses a syntax similar to Django templates, one of its core security designs is the default enabled automatic HTML escaping feature. This means that when you use double curly braces in the template{{变量}}When outputting user-submitted or stored content in a database, the system automatically escapes HTML special characters. For example:

  • &Will be escaped to&
  • <Will be escaped to&lt;
  • >Will be escaped to&gt;
  • "Will be escaped to&quot;
  • 'Will be escaped to&#39;

This mechanism ensures that even if users try to input<script>alert('XSS');</script>Such malicious code will also be displayed on the page&lt;script&gt;alert(&#39;XSS&#39;);&lt;/script&gt;Therefore, it is displayed as plain text instead of being parsed as executable script by the browser, effectively preventing XSS attacks.This is the first and most important line of defense against XSS attacks in the content output layer of AnQiCMS.

When is it necessary to trust content? Understand|safeFilter

Although the automatic HTML escaping mechanism provides strong protection, in certain specific scenarios, we may need to display unescaped HTML content.For example, when the website backend provides a rich text editor (WYSIWYG editor), allowing content editors to insert custom HTML tags to achieve richer formatting effects, these contents cannot be simply escaped when displayed.

In this case, the AnQiCMS template provides|safeFilter. When you are sure that the content of a variable is safe and harmless HTML, you can use{{变量|safe}}to output it. For example, when displaying the main content of an article, you might see something similar{{archiveContent|safe}}This usage.

Use|safeThe filter means that you explicitly declare to the template engine: 'I trust this content is safe HTML, please do not escape it.' Therefore, when using|safeBefore, be sure to ensure that the source of the content is trustworthy, and that it has been strictly verified and filtered to avoid potential security vulnerabilities. If there are any doubts about the security of the content, it should be avoided.|safefilter.

Fine control:autoescapeTag

In addition to the globally default automatic escaping and|safefilter, AnQiCMS template also provides{% autoescape on/off %}tags to fine-tune the automatic escaping behavior within module blocks.

  • {% autoescape off %}All output within this tag block{{变量}}will be automatically escaped by default unless explicitly|escapeor|eescaped by the filter.
  • {% autoescape on %}All output within this tag block{{变量}}The output will be automatically escaped by default, even if the automatic escaping is turned off in the external environment.

This tag is usually used to override the default automatic escaping settings in a local area.For example, you may temporarily disable automatic escaping in a complex template fragment to handle specific HTML structures, and then re-enable it after processing.In practice, to maintain code clarity and security, in most cases, it is recommended to keep the template's default automatic escaping behavior and only use it when it is necessary to display the original HTML.|safefilter.

Active escaping:escapeandeFilter

In{% autoescape off %}The block, if you still need to escape a specific variable in HTML, you can use|escapeor its shorthand form|ea filter. For example:{{ 变量|escape }}This is equivalent to manually applying HTML escaping rules, even if the current template environment defaults to not escaping, it can ensure the safe display of content.

Moreover, for user submitted content that needs to be inserted into JavaScript code, AnQiCMS also provides|escapejsFilter. This filter will convert special characters in the content to JavaScript-safe encoding, for example, converting a newline character to\nto\u000APrevent users from launching XSS attacks by injecting malicious JavaScript code.

In summary, AnQiCMS enables the automatic HTML escaping mechanism by default, as well as providing|safe/autoescape/escape/escapejsA variety of control methods provide website operators with comprehensive and flexible XSS protection strategies.Understanding and reasonably applying these functions is the key to building a safe and stable AnQiCMS website.However, the ultimate content security does not rely solely on the system itself, but also on the careful attitude and security awareness of operation personnel during the content publishing process.


Frequently Asked Questions (FAQ)

What is XSS attack? Why does my website need to prevent it?

XSS (Cross-Site Scripting, a common web vulnerability, refers to the injection of malicious client-side scripts (usually JavaScript) into your website, which will execute in the browser of other users when they visit the page.)This could lead to sensitive information being stolen (such as cookies, session tokens), user interface tampering, phishing attacks, or even being redirected to malicious websites.To protect user data security, maintain the reputation of the website, and ensure the normal operation of the website, all websites must strictly prevent XSS attacks.

2. When should it be used|safeWhat risks are associated with using a filter to display content?

You should consider using it only in the following two main cases|safeFilter:

  • Rich text editor content:When your backend allows content editors to use a rich text editor (such as a WYSIWYG editor) to create content containing HTML tags (such as bold, italic, links, images, etc.), and you want these HTML tags to be parsed and displayed normally.
  • Trusted static HTML:When you are sure that the content to be displayed is hard-coded by the website developer, or comes from a static HTML fragment from an absolutely trustworthy and strictly vetted third-party source. Use|safeThe filter hashigh riskOnce used, you have given up the automatic safe escaping of the content by the template engine.If content marked as "safe" actually contains malicious scripts, these scripts will execute in the user's browser, leading to XSS attacks.Therefore, in using|safeBefore that, make sure that all user input has been strictly validated and filtered on the server side, and that the source of the content is absolutely trustworthy.

3. If accidentally closedautoescapeWhat security risks will my website have?

If you accidentally or improperly use the template in{% autoescape off %}Tags, then all defaults within that tag block{{变量}}will no longer be HTML escaped. This means that any user input containing HTML special characters, if directly passed through{{变量}}The output will be parsed by the browser as the original HTML or JavaScript code.This will greatly increase the risk of your website being attacked by XSS, attackers can easily inject malicious scripts, thereby endangering user safety and website integrity.Therefore, unless you are clear about what you are doing and have taken other stringent protective measures, it is strongly recommended to maintainautoescapeThe feature is enabled.