Injections can also be hidden in exported files, where can the prevention of second-order SQL injection be found?
Second-order injection should be prevented at the consumption step, not the submission step. Its characteristic is delayed execution: when malicious content is first written, it is just ordinary data, passes the validation, enters the database, and appears harmless; it is only when it is taken out in the next process and executed as part of a statement that the injection actually occurs. The reason why exporting files is worth discussing separately is because it turns 'next consumption' into the most common action on the site.
The recent WordPress 7.1.3 security announcement listed this fix: There is a second-order SQL injection in the WXR export of WordPress. Among the batch of fixes, there is also an item that allows unauthorized reading of comments on private and unpublished articles, and when taken together, they cover both paths of 'data being taken out' and 'data being read out.'
Difference between first-order and second-order.
First-order injection is immediate execution upon request: parameters directly enter the query construction, with inputs and consequences in the same request. Detection is relatively easy, and protection is mature - parameterized queries separate data from statement structure, and inputs are always treated as values regardless of how they are constructed.
Second-order injection is divided into two stages: the first time it is data when written, and the second time it becomes structure when read. The problem lies in the different assumptions made in these two stages. The first time it is verified as 'this is the user's input content', and the second time it is concatenated as 'this is the trusted data in the library'. 'Trusted' is the entire basis for second-order injection.
Therefore, "Filtered before entry" cannot be used as a reason for exemption. The filtering rules are aimed at the dangerous forms in the submission scenario, while the second consumption often occurs in a completely different context: exporting to a file, importing into another library, rebuilding the search index, generating statistical reports. The ways of splicing in these processes are different, and the first filtering cannot foresee all downstream uses.
Why has exporting become a delivery channel
The exported file has three characteristics that exactly match the conditions for the second-order injection.
It is persistent. The file is used repeatedly once generated, and content that does not show any problems today may be executed in another environment six months later. It is structured. WXR and such export formats come with titles, content, categories, custom fields, and various slots, each of which may be spliced into different sentences on the import side. It crosses boundaries. Export and import often occur on different sites, different versions, and different database types, and the validation on the import side is often more lenient than that on the writing side, for the reason that 'This is the file I exported myself'.
The batch import is the most typical entry point. When importing documents in bulk using ZIP compression packages or Excel spreadsheets, the system needs to read and write fields one by one and store them in the database. It is possible that the same file may contain both normal content and fields that may cause problems in other contexts. Therefore, the import entry needs to be checked separately, and 'coming from in-site export' cannot be taken as a reliable basis. When importing external images, it is also necessary to block internal network addresses and large files to avoid the import process being mistaken for an entry point.
Where should the protection be placed?
The protection position is arranged in three steps: verify when writing, keep the original when storing, and do not combine statements when consuming.
Write this step according to the field type to tighten constraints, use whitelist for enumerative values, and use format verification for length and character set limitations, rather than relying on blacklist to pick dangerous words. The key point of this step is not to make a second interpretation: keep the data in its original value, and do not store assembled statement fragments in the database. The consumption of this step is decisive - wherever values in the library are used as part of a statement structure, they should be changed to parameterized methods, including report queries, index input, and batch updates, which are easily overlooked paths.
Within the security design of AnQiCMS, built-in JWT authentication and sensitive word filtering, and anti-interference code for collection prevention are part of the same chain, the official defense targets include SQL injection and XSS categories. In terms of data flow, there are also two complementary capabilities available: document status is divided into formal, draft, pending release, and recycling bin, deleting a formal document is moved to the recycling bin instead of physical deletion; backup and recovery support data and static files to be processed together. These two capabilities determine the radius of impact of a misoperation - as long as the original data is still in a recoverable state, the issues on the import side are not likely to become permanent losses.
The checklist for backup and recovery process
The recovery process is the most easily overlooked scenario in second-order injection: the recovery itself is to 'consume historical data again'. The items are listed in order: before recovery, make a backup of the current state to avoid being unable to retreat after overwriting; confirm that the recovery source comes from a controlled location, not from an external file specified by the address parameters; after recovery, check that the database version and structure are consistent, and then check each content field for any abnormal prefixes or extra escape characters; finally, check whether the import entry validation is effective for new data.
There is one thing often ignored: the habit of using exported files as logs. Some websites regularly export content for statistics or archiving, and these files are often left in directories that can be directly downloaded, both as a data leak surface and as a potential for unreviewed execution when imported next time. The archives should either be placed in a location that cannot be read externally or a structural check should be performed before import. The same reason also applies to the content in the recycle bin, as long-term retained deleted data will also enter the consumption path when restored and imported.
Common questions
Question: Can parameterized queries prevent second-order injection?
Answer: It can prevent the execution link. The parameterization solves the problem of data being treated as a statement structure, which is a key step in the second-order injection.
Question: Should the export file be escaped?
Answer: The export should keep the data unchanged, placing the protection on the consumption method on the import side. Rewriting the content during export will cause data distortion.
Question: How to judge whether there is such a hidden danger in your site?
Answer: Check all the positions where the fields in the database are spliced into the statement, focusing on the three paths of reports, index import, and batch update, rather than just looking at the form submission entry.