The template tag outputs the opening tag of the script, how can the content be executed as code?
The site content is executed as code in just one step: The template engine interprets a segment of opening tags in the received text as code. PbootCMS has fixed such an issue in its update records - a risk related to the output script tag of the site, while also enhancing the filtering of tag page parameters. The two fixes correspond to two levels of closure: the output position needs to be filtered, and the parameters entering the tag also need to be verified. Any content allowed to flow into the template or script engine system from the backend editor faces the same boundary.
The step where data becomes code
The template file already contains code: tags, conditions, and loops are instructions for the engine. Danger occurs when 'data output' is treated as 'code input'——the engine directly pastes user可控 text into the source code to be interpreted. Once this text contains starting tags recognized by the engine, the content enters the instruction stream. From this moment on, it is no longer displayed data but part of the execution logic; the same string is just text at the plain text output and behavior at the interpreter's entry. Therefore, the answer to 'how content can be executed' is: it is placed in a position where it can be interpreted.
Why do we need to filter the tag position
The output position of the site tag is very special: it often appears at the outermost layer of template assembly. Some implementations even compile the tag parameters directly into the template source code. Three entrances continuously supply content: background editor input, form and comment submission, and externally collected articles. If any content is marked with a start tag and the exit is not filtered, the problem will explode during rendering. Therefore, both 'enhancing the tag page parameter filtering' and 'the output position no longer generates open tags' must be done at the same time in the repair plan: narrow the shape of controllable input on the parameter side, and ensure that even if it leaks in, it will not be interpreted on the output side.
Escape should be separated by output context
The same text goes in different directions, the dangerous characters are different, and escaping cannot be replaced by a universal cleaning set:
| Output context | Form of dangerous characters | The correct method |
|---|---|---|
| HTML body | Tag start symbol and entity | Escape according to HTML entity |
| Property value | Quotation mark separators and angle brackets | Special escape for attribute context and add quotes |
| Script string | Quotation marks, backslashes, and end markers | Escape according to script syntax or go through the data interface |
| URL parameters | Reserved characters and encoding injection | Percent encoding and verify protocol header |
The principle is that the consumer's interpretation of the syntax determines the escaping method; the practice of 'first deleting all special characters globally and then everywhere' will delete legitimate content and also miss cultural attacks.
The earlier the filtering, the more convenient it is
When layering the mouth, the priority is from front to back: during the entry and import stage, sensitive word filtering and keyword replacement are performed to block obvious anomalies before they are stored; the content security settings of AnQiCMS are responsible for this section.Contextual escaping in the rendering phase is the right solution to prevent cross-site scripting; the interface and authentication layer contain the attack surface of injection attacks, and the protection against SQL injection and XSS attacks built into AnQiCMS belongs to this layer.The earlier it is, the more you can keep the problem in the form of data, not letting it get close to the interpreter; the later it is, the more you have to rely on the self-awareness at every exit. AnQiCMS's visual template editing allows direct modification of template code, and the person modifying the template also needs to adhere to this boundary: when entering content into the template, keep it in the position of 'it is data'.
Common questions
I am the only one who can log in to the background, do I still need to worry about template injection?Yes.可控内容 not only comes from the login user: collected articles, user-uploaded filenames, comments may all be sources, and the permission model cannot stop the 'markers in the data'.
Why not remove the angle brackets globally for convenience?It may harm valid text and cannot prevent injection at non-HTML interpretation locations. Escaping according to the output context will not please both ends.
Is the automatic escaping built into the template engine sufficient?The default scenario is sufficient, but automatic escaping usually only processes the HTML body. Explicitly check the attributes, scripts, and URL locations, and the 'non-HTML context' in the mixed template is exactly the point to bypass.