In website operation, ensuring the security of content is a crucial link, especially when your website allows users to submit content or display data from different sources.Cross-site scripting (XSS) attacks are one of the common threats, which may lead to data leakage of website users, session hijacking, or even website tampering.For those of us who use AnQiCMS to manage content, understanding how to safely display strings that may contain HTML tags is the foundation of preventing such attacks.
AnQiCMS template engine defaults to taking a very important security measure when processing variable output: automatically escaping HTML special characters. This means that when you use in the template,{{变量}}The form to display content when变量contains</>/&/"/'such as HTML special characters are automatically converted to</>/&/"/'HTML entities. This default behavior is a key defense against cross-site scripting (XSS) attacks, as it effectively prevents malicious HTML code or JavaScript scripts from being parsed and executed by the browser.
However, in certain specific operational scenarios, you may indeed want to display content on the page that includes real HTML tags.For example, when you retrieve the article content from the backend rich text editor, or display some strictly reviewed and confirmed harmless HTML fragments.In this scenario, AnQiCMS provides asafeThe filter explicitly tells the template engine that this content is safe and does not require HTML escaping. Its usage is very intuitive: add it after the variable that needs to be unescaped.|safeFor example, when you need to display the content of an articlearchiveContentyou may use{{ archiveContent|safe }}. Once usedsafea filter, which means you have explicitly undertaken the responsibility to ensure the safety of the content. You need to ensurearchiveContentAll HTML code is validated and harmless, does not contain any malicious scripts, otherwise it may leave an opportunity for XSS attacks.
In addition to using for individual variables:safeFilter, AnQiCMS templates also provide{% autoescape on/off %}tags to control the automatic escaping behavior within a code block. Usually, after using{% autoescape off %}all{{变量}}The output will no longer be HTML-escaped until{% endautoescape %}Or the entire template ends. Therefore, within such a block, you must be particularly vigilant about the content of each variable to ensure it is safe.On the other hand, if you want to force an escape of a variable within a block that has automatic escaping turned off, you can use|escapefilter. For example, if you are in{% autoescape off %}block{{ malicious_string|escape }}thenmalicious_stringthe HTML special characters within will still be escaped.
When handling rich text or Markdown content, AnQiCMS also provides the corresponding filters.For example, when the Markdown editor is enabled on the backend, the system will automatically convert Markdown formatted content to HTML.At this time, if the content contains potential malicious HTML, the output after conversion also needs to be properly handled.The AnQiCMS usually considers this internally, but it can still be combined in the final output of the template|safeUse, on the premise that you have a full understanding of the source and security of the content.
In order to comprehensively enhance the security of the website, in addition to the application of technical filters, the content operation strategy should also be followed up.For comments, messages, and other content submitted by users, it is necessary to conduct strict backend validation and content review.You can set up sensitive word filtering or limit the whitelist of HTML tags available in the rich text editor.In addition, regularly updating the AnQiCMS system to keep its core security mechanisms up to date is also essential.
By understanding the default security mechanism of AnQiCMS template engine and using it appropriately.safe/autoescapeThis function allows us to provide rich content display while effectively preventing XSS injection attacks, offering users a secure and reliable browsing environment.
Frequently Asked Questions (FAQ)
What is an XSS attack, why is it necessary to defend against it in the AnQiCMS template?XSS (Cross-Site Scripting, cross-site scripting) attack is a common network security vulnerability.An attacker injects malicious scripts (usually JavaScript) into a website. When other users visit pages containing these scripts, the malicious code executes in the user's browser, thereby stealing user information, hijacking sessions, modifying web content, and so on.AnQiCMS as a content management system, users (or administrators) may enter code snippets containing HTML or scripts. If not defended, this content displayed directly on the page may lead to XSS attacks.The default HTML encoding mechanism of AnQiCMS is exactly to prevent this unauthorized script execution.
|safeCan filters be used arbitrarily in any situation?Absolutely not.|safeThe filter tells AnQiCMS template engine, "I know this content is safe, please parse it as HTML directly without escaping." Therefore, only when youvery certainshould be used when the content of a variable is pure and harmless HTML|safe. For example, these HTML come from your own strictly reviewed rich text editor, and the editor itself has strong XSS protection capabilities.If used to display content from unknown or unreliable sources (such as user submitted comments, messages), use|safewill almost certainly introduce an XSS vulnerability.If I am not sure whether the user's input content is safe, how should I handle it?The **way to do when you are unsure whether the content entered by the user is safe, or the content may contain HTML but you do not want these HTML to be parsed**Do notUse
|safeThe filter. Keep the AnQiCMS template engine in its default HTML escaping behavior. This way, any HTML tags entered by the user will be displayed as plain text, for example<script>Will become<script>Thus effectively avoiding the execution of malicious scripts. For scenarios where partial formatted text needs to be displayed, consider using a Markdown editor, and ensure that the Markdown parser on the backend has XSS protection capabilities, or use a whitelist mechanism to allow only specific safe HTML tags to pass.