Writing the type header of the upload directory correctly is not enough; why do we need to add a separate response header to prohibit content sniffing?
The type header of the upload directory is correct, but why do we need to add the separate response header for禁止内容嗅探? Because 'what the server said' and 'how the browser finally understands' are two different things.MDN defines X-Content-Type-Options clearly: this response header indicates that the type declared in Content-Type should be adhered to and not changed, as it is deliberately configured to avoid type sniffing;When the requested destination is script or style and the type does not match, the browser will block the response. In other words, sniffing occurs on the browser side, and only the type is written on the server side, which cannot guarantee that the browser will not guess.
Where does sniffing occur
When the browser lacks a trusted type or believes the type does not match the content, it will try to infer the content type from the bytes themselves, which is MIME sniffing. Its starting point is error tolerance: a large number of responses on the early Internet indeed declared the wrong type.
The issue is in the upload directory. The content of the uploaded file is not trustworthy, and both the filename and the extension can be arbitrarily constructed. A file named with an image extension but containing script bytes may be executed as another identity if accessed directly. At this point, the server 'declaring the type' does not constitute protection because what is needed is that 'the browser does not change it'.
Two effects of disabling the sniffing header
According to MDN's description, there are two effects after adding this header.
The first layer is the locking declaration. The browser processes according to the Content-Type given and no longer changes it on its own. This layer takes effect on all resources.
The second layer is interception. When the destination is style and the type is not a text style type, or the destination is script and the type is not a script type, the browser directly blocks the response. This layer only takes effect on the two types of destinations, script and style, and is a strong constraint.
It is important to understand what it does not do: it does not prevent files from being downloaded, it does not replace the type whitelist check, and it does not affect behavior outside of rendering logic. It is a signal that the server has explicitly stated, 'browser, don't be clever', and therefore must be used with the correct type declaration—if the declaration itself is incorrect, the locking declaration will also lock the page into an error state.
Why is the upload directory a focus
The upload directory has three overlapping features: the content comes from untrusted input, the path is usually directly addressable, and it is often accessed by browsers rather than business code. All three together mean that it is the most concentrated location for sniffing risks.
The handling needs to be done in two layers, one cannot be missed.
The program layer is responsible for the entry: it only accepts whitelist types, judges according to the real content rather than the extension name, and clearly declares the generated access address and type.
The deployment layer is responsible for providing a safety net: it adds an additional nosniff response header to the upload directory and limits the script execution capabilities under the directory. The reason for adding another layer at the deployment level is that the program layer's determination is only effective when accessed, so once a path is bypassed or historical files already exist on the disk, the rules of the deployment layer still take effect with each access.
PbootCMS repair records explain the same thing: its V3.2.22 update records specify the addition of MIME and nosniff rules for the upload directory, and distinguish between Apache automatic deployment and Nginx requiring manual introduction of the corresponding configuration file. That is, this type of reinforcement is treated as a deployment layer rule, and the introduction methods of different web servers are different, and the default configuration will not do it for you.
On-site landing checklist
| Inspection items | Which layer | Verification method |
|---|---|---|
| Upload type whitelist | Program | Whether rejected when trying to upload non-whitelisted content |
| Determine type by content | Program | Is the storage type correct after forging the extension |
| Upload directory disables sniffer probe | Deployment | Directly access an uploaded file to see the response header |
| Upload directory prohibits script execution | Deployment | Check if the script file in the directory is executed when accessed |
| Output-side XSS protection | Program | Rich text and user submitted rendering results |
The fifth item is easy to confuse with the first four, but they are not on the same level. The XSS and SQL injection protection, sensitive word filtering, and anti-collection interference code built into the site act on the content and output links; the interference code is to disturb the display layer of the collector, which is irrelevant to the security semantics of the response header. The former governs whether executable expressions can be contained in the content, and the latter governs whether the browser will change the file type, and both are indispensable.
Three-step self-check
The first step is to directly request a historical file in the upload directory and check whether the prohibition statement is included in the response. If there is no such statement, it means that the deployment layer rules do not cover the path.
The second step, check the introduction method on different Web servers. The same rule has different writing and effective positions under Apache and Nginx, it needs to be confirmed whether the configuration file is truly loaded, not just by looking at the existence of the file.
The third step, check the statement itself. Before adding a locking statement to static resources, make sure the type is correct; locking the wrong type is worse than allowing sniffing.
Common questions
Is it enough to add it only once in Nginx?It depends on the chain. When there are multiple proxy layers, the statement may be lost in the forwarding, and it should be checked at the final response instead of just at the source configuration.
Will adding a header affect the normal display of the image?Usually not. It requires the browser to comply with the declared type, provided that the declaration is correct; when resources cannot be displayed, check the type declaration first rather than removing the header.
Should historically uploaded files be processed again?It is recommended to at least check the rule coverage of the directory layer, as it is impossible to rewrite the content of existing files one by one, and the deployment layer rules are prepared for this kind of risk.
Can content review replace type checking?No. Content review is for text compliance, and type checking is for file identity, and the two are different.
How can you confirm that an interception really occurred?Check the resource loading error in the browser console, there will be a clear record when the type does not match and is blocked, which is faster than guessing.