Skip to content

Commit 9ecc9a6

Browse files
committed
Set X-Content-Type-Options: nosniff header
1 parent 2b7755a commit 9ecc9a6

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

HISTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
unreleased
22
==========
33

4+
* Set `X-Content-Type-Options: nosniff` header
45
56
67
- Allow colors in workers

index.js

+6
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,14 @@ function removeHidden(files) {
495495
*/
496496

497497
function send (res, type, body) {
498+
// security header for content sniffing
499+
res.setHeader('X-Content-Type-Options', 'nosniff')
500+
501+
// standard headers
498502
res.setHeader('Content-Type', type + '; charset=utf-8')
499503
res.setHeader('Content-Length', Buffer.byteLength(body, 'utf8'))
504+
505+
// body
500506
res.end(body, 'utf8')
501507
}
502508

test/test.js

+39
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ describe('serveIndex(root)', function () {
2626
.expect(200, done)
2727
})
2828

29+
it('should include security header', function (done) {
30+
var server = createServer()
31+
32+
request(server)
33+
.get('/')
34+
.expect('X-Content-Type-Options', 'nosniff')
35+
.expect(200, done)
36+
})
37+
2938
it('should serve a directory index', function (done) {
3039
var server = createServer()
3140

@@ -117,6 +126,16 @@ describe('serveIndex(root)', function () {
117126
.expect(/\.txt/)
118127
.expect(200, done)
119128
});
129+
130+
it('should include security header', function (done) {
131+
var server = createServer()
132+
133+
request(server)
134+
.get('/')
135+
.set('Accept', 'application/json')
136+
.expect('X-Content-Type-Options', 'nosniff')
137+
.expect(200, done)
138+
})
120139
});
121140

122141
describe('when Accept: text/html is given', function () {
@@ -136,6 +155,16 @@ describe('serveIndex(root)', function () {
136155
.end(done);
137156
});
138157

158+
it('should include security header', function (done) {
159+
var server = createServer()
160+
161+
request(server)
162+
.get('/')
163+
.set('Accept', 'text/html')
164+
.expect('X-Content-Type-Options', 'nosniff')
165+
.expect(200, done)
166+
})
167+
139168
it('should property escape file names', function (done) {
140169
var server = createServer()
141170

@@ -194,6 +223,16 @@ describe('serveIndex(root)', function () {
194223
.expect(/\.txt/)
195224
.end(done);
196225
});
226+
227+
it('should include security header', function (done) {
228+
var server = createServer()
229+
230+
request(server)
231+
.get('/')
232+
.set('Accept', 'text/plain')
233+
.expect('X-Content-Type-Options', 'nosniff')
234+
.expect(200, done)
235+
})
197236
});
198237

199238
describe('when Accept: application/x-bogus is given', function () {

0 commit comments

Comments
 (0)