Why should the script execution permission be removed from the upload directory, and at which layer is it blocked
Because the upload directory meets two conditions at the same time: the file content is written from the outside, and the path can be accessed from the public network. The significance of removing the script execution permission is that even if there are script files placed in this directory, the server will only return them as static content and will not run them as programs. This type of restriction should be configured at the web server layer, which is two independent defenses, not a substitute for the file type check within the program.
Why is the upload directory a high-risk position
The attachment upload is the process of falling external bytes onto the server file system, the path is usually under the root directory of the site and can be directly requested. Once the content here can be interpreted and executed, the script written in has an entry point, which is the attack surface of this type of directory. On the program side, there are input defenses: mechanisms for defending against SQL injection and XSS within the site, as well as the handling of file types and extensions during upload, all belong to the first line of defense. The value of the second line of defense lies in not depending on the first line to not fail - validations can be bypassed, and the fact that the directory does not execute scripts has nothing to do with the file content, as long as the configuration takes effect, it will always be in place.
How to configure the matching order of blocks by path
The web server determines the order of path matching configuration blocks to decide 'who to block first'. For example, according to the nginx documentation: First, check the location defined by the prefix string, and the longest match is selected and remembered;Then check the regular expressions in the order they appear in the configuration file. The search for regular expressions stops at the first match. There is also a common pitfall: if the selected longest prefix location is marked with the ^~ modifier, the regular expression will no longer be checked against other match types.This means that the restrictions set for the upload directory may be preempted by a preceding regular expression block, or vice versa, being skipped by ^~ - the restrictions do not take effect, but the configuration is valid, and this type of issue is only exposed in the request results.
How to remove execution permissions?
The idea is to provide a separate stable match for the upload directory, and within that range, disable script interpretation: do not pass the request to the script processor, but let the script class files return as source code or static content. There are two key points in the writing. First, the path should be written as the actual disk directory, and the attachment site layered by date should cover all subdirectories; second, this match should be able to win stably, using the longest prefix or ^~ to explicitly define the priority, avoiding the order dependence on regular expressions. After the configuration is changed, first request a non-existent path to confirm that the return is as expected, then use a harmless script file to verify that it is not executed, and then delete the test file after verification.
Check the backup after modification
Handle the upload directory often accompanied by cleaning actions: find suspicious files, remove them from the site directory. The risk of this step is to delete normal attachments along with them, as attachments are part of the site data, and after accidental deletion, large areas of the front-end will appear missing images.Before taking action, first confirm that the backup is available - the backup and recovery within the site cover the site database and static files, which is the prerequisite for rolling back the cleaning operation; secondly, filter by time window instead of by file extension for batch deletion, move suspicious files to an isolated directory outside the site instead of deleting them directly, and observe for a period of time before deciding on disposal.The deployment form must also be verified: Changes to files within the image under container deployment are not persistent, the cleaning results will disappear in the new container, and the handling should fall on the storage volume or site directory.
Common questions
Question: The program has already verified the file type, do we still need to configure this layer?Answer: Yes. Verification is the first line of defense and may be bypassed; the directory does not execute scripts related to content, it is the bottom layer, and the two have different points of action.
Question: Will disabling execution affect normal image access?Answer: No. The restriction is aimed at the interpretation and execution of script files, and the reading path and response method of static resources remain unchanged. Confirm each item after modification.
Question: When cleaning the upload directory and finding a large number of unfamiliar files, what should be checked first?Answer: First, check the timestamps of these files and the access logs of the source interface to confirm whether they are normal attachments generated by collection and import or abnormal writes. Do not delete in bulk before the judgment is completed, but isolate and keep a backup first.