1
1
# Use ` X-Content-Type-Options ` header (` x-content-type-options ` )
2
2
3
- ` x-content-type-options ` requires that all scripts and
4
- stylesheets are served with the ` X-Content-Type-Options: nosniff `
3
+ ` x-content-type-options ` requires that all resources are
4
+ served with the ` X-Content-Type-Options: nosniff `
5
5
HTTP response header.
6
6
7
7
## Why is this important?
@@ -29,19 +29,19 @@ header is sent for the script and the browser detects that it’s a script
29
29
and it wasn’t served with one of the [ JavaScript media types] [ javascript
30
30
media types] , the script will be blocked.
31
31
32
- Note: [ Modern browsers only respect the header for scripts and
33
- stylesheets] [ fetch spec blocking ] and sending the header for other
34
- resources (such as images) when they are served with the wrong media
35
- type may [ create problems in older browsers ] [ fetch spec issue ] .
32
+ While [ modern browsers respect the header mainly for scripts and
33
+ stylesheets] [ fetch spec blocking ] , [ Chromium uses this response header on
34
+ other resources] [ chromium ssca ] for
35
+ [ Cross-Origin Read Blocking ] [ chromium corb ] .
36
36
37
37
## What does the hint check?
38
38
39
- The hint checks if all scripts and stylesheets are served with the
39
+ The hint checks if all resources are served with the
40
40
` X-Content-Type-Options ` HTTP headers with the value of ` nosniff ` .
41
41
42
42
### Examples that ** trigger** the hint
43
43
44
- Resource that is not script or stylesheet is served with the
44
+ Resource is not served with the
45
45
` X-Content-Type-Options ` HTTP header.
46
46
47
47
``` text
@@ -50,7 +50,6 @@ HTTP/... 200 OK
50
50
...
51
51
52
52
Content-Type: image/png
53
- X-Content-Type-Options: nosniff
54
53
```
55
54
56
55
Script is served with the ` X-Content-Type-Options ` HTTP header
@@ -77,6 +76,69 @@ Content-Type: text/javascript; charset=utf-8
77
76
X-Content-Type-Options: nosniff
78
77
```
79
78
79
+ ## How to configure the server to pass this hint
80
+
81
+ <details ><summary >How to configure Apache</summary >
82
+
83
+ Apache can be configured to add headers using the [ ` Header `
84
+ directive] [ header directive ] .
85
+
86
+ ``` apache
87
+ <IfModule mod_headers.c>
88
+ Header always set X-Content-Type-Options nosniff
89
+ </IfModule>
90
+ ```
91
+
92
+ Note that:
93
+
94
+ * The above snippet works with Apache ` v2.2.0+ ` , but you need to have
95
+ [ ` mod_headers ` ] [ mod_headers ] [ enabled] [ how to enable apache modules ]
96
+ for it to take effect.
97
+
98
+ * If you have access to the [ main Apache configuration file] [ main
99
+ apache conf file] (usually called ` httpd.conf ` ), you should add
100
+ the logic in, for example, a [ ` <Directory> ` ] [ apache directory ]
101
+ section in that file. This is usually the recommended way as
102
+ [ using ` .htaccess ` files slows down] [ htaccess is slow ] Apache!
103
+
104
+ If you don't have access to the main configuration file (quite
105
+ common with hosting services), add the snippets in a ` .htaccess `
106
+ file in the root of the web site/app.
107
+
108
+ For the complete set of configurations, not just for this rule, see
109
+ the [ Apache server configuration related documentation] [ apache config ] .
110
+
111
+ </details >
112
+
113
+ <details >
114
+
115
+ <summary >How to configure IIS</summary >
116
+
117
+ You can add this header unconditionally to all responses.
118
+
119
+ ``` xml
120
+ <configuration >
121
+ <system .webServer>
122
+ <httpProtocol >
123
+ <customHeaders >
124
+ <add name =" X-Content-Type-Options" value =" nosniff" />
125
+ </customHeaders >
126
+ </httpProtocol >
127
+ </system .webServer>
128
+ </configuration >
129
+ ```
130
+
131
+ Note that:
132
+
133
+ * The above snippet works with IIS 7+.
134
+ * You should use the above snippet in the ` web.config ` of your
135
+ application.
136
+
137
+ For the complete set of configurations, not just for this rule,
138
+ see the [ IIS server configuration related documentation] [ iis config ] .
139
+
140
+ </details >
141
+
80
142
## How to use this hint?
81
143
82
144
To use it you will have to install it via ` npm ` :
@@ -115,22 +177,24 @@ And then activate it via the [`.hintrc`][hintrc] configuration file:
115
177
116
178
<!-- Link labels: -->
117
179
180
+ [ chromium corb ] : https://chromium.googlesource.com/chromium/src/+/master/services/network/cross_origin_read_blocking_explainer.md
181
+ [ chromium ssca ] : https://www.chromium.org/Home/chromium-security/ssca
118
182
[ fetch spec blocking ] : https://fetch.spec.whatwg.org/#should-response-to-request-be-blocked-due-to-nosniff%3F
119
183
[ fetch spec issue ] : https://github.com/whatwg/fetch/issues/395
184
+ [ hintrc ] : https://webhint.io/docs/user-guide/configuring-webhint/summary/
120
185
[ javascript media types ] : https://html.spec.whatwg.org/multipage/scripting.html#javascript-mime-type
121
186
[ mime sniffing spec ] : https://mimesniff.spec.whatwg.org/
122
- [ hintrc ] : https://webhint.io/docs/user-guide/configuring-webhint/summary/
123
187
124
188
<!-- Apache links -->
125
189
190
+ [ apache config ] : https://webhint.io/docs/user-guide/server-configurations/apache/
126
191
[ apache directory ] : https://httpd.apache.org/docs/current/mod/core.html#directory
127
192
[ header directive ] : https://httpd.apache.org/docs/current/mod/mod_headers.html#header
128
193
[ how to enable apache modules ] : https://github.com/h5bp/server-configs-apache/tree/7eb30da6a06ec4fc24daf33c75b7bd86f9ad1f68#enable-apache-httpd-modules
129
194
[ htaccess is slow ] : https://httpd.apache.org/docs/current/howto/htaccess.html#when
130
195
[ main apache conf file ] : https://httpd.apache.org/docs/current/configuring.html#main
131
196
[ mod_headers ] : https://httpd.apache.org/docs/current/mod/mod_headers.html
132
- [ mod_mime ] : https://httpd.apache.org/docs/current/mod/mod_mime.html
133
197
134
198
<!-- IIS links -->
135
199
136
- [ url rewrite ] : https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-the-url-rewrite-module
200
+ [ iis config ] : https://webhint.io/docs/user-guide/server-configurations/iis/
0 commit comments