Skip to content

Commit

Permalink
Set Content-Security-Policy: default-src 'self' header
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Feb 15, 2017
1 parent 7aa2d9c commit 63425d0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ unreleased
* Fully URL-encode the pathname in the 404 message
* Only include the pathname in the 404 message
* Send complete HTML document
* Set `Content-Security-Policy: default-src 'self'` header
* deps: debug@2.6.1
- Allow colors in workers
- Deprecated `DEBUG_FD` environment variable set to `3` or higher
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ function send (req, res, status, headers, message) {
// response headers
setHeaders(res, headers)

// security header for content sniffing
// security headers
res.setHeader('Content-Security-Policy', "default-src 'self'")
res.setHeader('X-Content-Type-Options', 'nosniff')

// standard headers
Expand Down
18 changes: 16 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,20 @@ describe('finalhandler(req, res)', function () {
.expect(404, '', done)
})

it('should include security header', function (done) {
it('should include X-Content-Type-Options header', function (done) {
request(createServer())
.get('/foo')
.expect('X-Content-Type-Options', 'nosniff')
.expect(404, done)
})

it('should includeContent-Security-Policy header', function (done) {
request(createServer())
.get('/foo')
.expect('Content-Security-Policy', "default-src 'self'")
.expect(404, done)
})

it('should not hang/error if there is a request body', function (done) {
var buf = new Buffer(1024 * 16)
var server = createServer()
Expand All @@ -287,13 +294,20 @@ describe('finalhandler(req, res)', function () {
.expect(404, '', done)
})

it('should include security header', function (done) {
it('should include X-Content-Type-Options header', function (done) {
request(createServer(createError('boom!')))
.get('/foo')
.expect('X-Content-Type-Options', 'nosniff')
.expect(500, done)
})

it('should includeContent-Security-Policy header', function (done) {
request(createServer(createError('boom!')))
.get('/foo')
.expect('Content-Security-Policy', "default-src 'self'")
.expect(500, done)
})

it('should handle non-error-objects', function (done) {
request(createServer('lame string'))
.get('/foo')
Expand Down

0 comments on commit 63425d0

Please sign in to comment.