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?

📅 2026-10-11 👁️ 8

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.

Related articles

To enable HTTP/3 on a web server, you need to adjust the port, certificate, and compilation options.

The Nginx official module document states that ngx_http_v3_module has provided experimental HTTP/3 support since version 1.25.0 and is not compiled by default. It requires --with-http_v3_module to enable, the example listening port is recommended to be consistent with HTTPS, and 0-RTT requires OpenSSL 3.5.1 or higher. This article explains the enablement sequence in four steps: compiling, listening, certificate, and library version.

2026-10-11

When the open source agreement is written as GPL, what should be noted when modifying and delivering to the customer.

Based on the official WordPress authorization page, the requirements of GPLv2 or later versions cover derived works, plugins and themes are considered derived works by the official, and there is a legal gray area. This article explains the three things to be delivered to the outside world, the boundary between multi-site and secondary development, as well as the authorization verification sequence at the selection stage.

2026-10-11

The pictures on the list page are replaced with a new generation of compression format, will the volume and crawling change together?

The compression readout given in the official document is: The new lossy format is about 50% smaller than JPEG on average, another common format is about 25%—35% smaller on average, and the lossless scenario is about 26% smaller; at the same time, it is explicitly recommended to retain the fallback format. This article explains that the volume will decrease after changing the format, but what is read depends on the image list and alternative text, and the order of implementation on the site is given.

2026-10-11

Changing the permanent redirect from 301 to 308, will the method of form submission be changed?

308 The client is explicitly required not to modify the method and request body in the redirect request; 301 also requires the same in the specification, but old clients may mistakenly use GET. This article explains what can be gained by changing numbers, what cannot be gained, and what should be confirmed first before changing the target form before the redirection.

2026-10-11

After logging in, the page is stored in a shared cache, and how should the private and non-cacheable headers in the cache header be written.

The MDN's Cache-Control document states that 'no-store' indicates that no type of cache should store the response, 'private' means that it can only be stored in private caches, and it also hints that shared caches will reuse a response for multiple users, and personalized content should not be given to it. This article explains the boundaries of both and why member-tiered pages are more likely to have issues.

2026-10-11

Why would a translation be sent to someone in another language if the site negotiated the language in the request header and cached it?

The MDN Vary document specifies that this response header describes the method of request information that affects the response content outside of the URL, including Vary to ensure that the response is cached based on the listed header fields separately. The most common use is to construct a cache key during content negotiation. This article explains the cause of the translation string given to other visitors, how to declare the response, and the differences in the two methods of language arrangement.

2026-10-11

Why returning a 503 when the site is upgraded and maintenance page is displayed is more reliable than returning a blank page

The MDN 503 document states that this status code indicates that the server is not ready to handle the request, common reasons include maintenance or overload, the response should only be used for temporary conditions and should ideally include the Retry-After header with the expected recovery time; the document also hints that 503 indicates a temporary issue and should not be cached. This article explains how to configure the status code, retry time, and caching during the maintenance window.

2026-10-11

The page actually exists but always returns 404, what impact will it have on indexing?

The content is still in the library, but the front-end has deemed the page non-existent. The signal received by the search engine is that the resource has disappeared, and the inclusion and traffic will follow suit. This article explains why a unified error code is dangerous, the several cases of status codes being semantically分流, and the troubleshooting sequence of document status, pseudo-static rules, redirection, and site map configuration.

2026-10-11