Why should the validation of reading and writing data be separated when a second injection occurs in the exported file?
The reason why second-order injection is easily overlooked is that it does not occur at the moment of 'writing data'. In the official security fix of WordPress 7.1.3, there is such an item: There is a second-order SQL injection (second-Order SQL injection) in the WXR export process. The danger does not lie in how malicious the data is when it comes in, but in the fact that this data is stored and then reused as a trusted value in the next link, which is then pieced into the query. This is the fundamental reason why the verification of reading and writing data needs to be separated.
What is meant by being triggered for the second time
Common injection is blocked at the submission entry: the user input contains the intention to concatenate SQL, and the validation layer rejects it. The second injection changes its approach - the data coming in looks harmless to humans and animals, even having passed a surface check, quietly lying in the database. The real problem occurs in the second instance: a function takes out the value from the database as is, combines it into a new statement or exports it to a file. At this point, it no longer goes through the original entry validation, and the malicious structure is 'activated' in a new position.
The keyword is 'No longer goes through original verification'. The defense line set on the write path assumes that the data is clean by default, so the read path, export path, and then import path often skip checks. The attacker is exploiting this trust gap.
Why is the import phase a high-risk exit?
Export and import are the most typical secondary entry points on this chain. Export organizes data from the library into files, seemingly just moving, but actually it is 're-arranging'——if the assembly is done by directly concatenating trusted values, dirty data will enter the file with its original malicious structure. The next part is even more dangerous: if this file is re-imported into the system or imported into another system, it is treated as 'user-provided batch data' at this point. If the new system does not perform escape-level decoding, injection will occur in the import step.
WXR is the format used by WordPress to export content and data, the fact that it is listed separately in security fixes just shows that data migration interfaces are a common entry point for such problems: its responsibility is to read out the contents of the library and write them out again, naturally stepping on the second use step.
What do the entry check and exit check check?
Split the two checks into different responsibilities in order to cover the complete path of secondary injection.
The check for entering the data is whether「This data is legally valid according to its type」:field length, character set, enumeration range, whether it is unauthorized to write columns that should not be decided by the user. It manages the shape of the incoming data. The check for exiting the data is whether「This data is about to be拼进 into which syntax」:bind parameters or escape according to SQL syntax when拼进 SQL, encode output when拼进 HTML, and reorganize according to the target format when written into an export file instead of transmitting it as is.
One sentence: Check by type for entry, escape by target grammar for output. The same data is not necessarily clean when entering, it does not mean it is clean when reused by other statements, so the check at the exit cannot be omitted.
How to implement on-site batch import and collection?
AnQiCMS batch import supports ZIP compressed packages and Excel tables as carriers. Content collection is to move content from other sites. Both of these paths are typical 'external content entering the library', and they may also become secondary entry points. The processing principle is consistent: treat all incoming content as untrusted input.
There are three points in the implementation. **, The import and collection are checked according to the field type and length when entering the database, and not relaxed because 'it comes from the file'. Second, when the data is read, combined, or exported by subsequent functions, escape at the output level, and do not assume that the things in the library are always clean.Third, the rules for preventing SQL injection should cover sorting and filtering parameters, not just the text content - the list sorting parameters are directly inserted into the query, which is also a second use of the data in the tiger's mouth.AnQiCMS built-in SQL injection and XSS defense receives this kind of untrusted input,配合sensitive word filtering and interference codes, the entry, storage, and exit segments are all passed through once.
Common questions
Ask: The data has been verified when it was stored, why does the export need to be processed again? Answer: The entry verification only guarantees compliance at that moment, when the data is re-read and combined, it no longer goes through it, and it needs to be escaped again according to the target syntax at the exit.
Ask: What should be paid attention to when importing external files in batches? Answer: Treat the content in ZIP or Excel as untrusted input, verify according to the field type, and go through the exit for escaping when reading and using it.
Ask: Will content collection amplify the risk? Answer: Yes, the collected content is large in volume and diverse in source, both entry and exit verifications must be done, and cannot be skipped because of batch processing.
Ask: Is it enough to correct the text injection? Answer: No, sorting, filtering and other parameters are also common junctions that need to be included in the prevention of SQL injection checks.