Skip to content

Commit 04474f5

Browse files
committed
Set default CSP header in redirect & error responses
1 parent 4698f17 commit 04474f5

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ unreleased
22
==========
33

44
* Send complete HTML document in redirect & error responses
5+
* Set default CSP header in redirect & error responses
56

67
0.14.2 / 2017-01-23
78
===================

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ SendStream.prototype.error = function error (status, error) {
293293
res.statusCode = status
294294
res.setHeader('Content-Type', 'text/html; charset=UTF-8')
295295
res.setHeader('Content-Length', Buffer.byteLength(doc))
296+
res.setHeader('Content-Security-Policy', "default-src 'self'")
296297
res.setHeader('X-Content-Type-Options', 'nosniff')
297298
res.end(doc)
298299
}
@@ -455,6 +456,7 @@ SendStream.prototype.redirect = function redirect (path) {
455456
res.statusCode = 301
456457
res.setHeader('Content-Type', 'text/html; charset=UTF-8')
457458
res.setHeader('Content-Length', Buffer.byteLength(doc))
459+
res.setHeader('Content-Security-Policy', "default-src 'self'")
458460
res.setHeader('X-Content-Type-Options', 'nosniff')
459461
res.setHeader('Location', loc)
460462
res.end(doc)

test/send.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,14 @@ describe('send(file).pipe(res)', function () {
342342
.expect(301, />Redirecting to <a href="\/pets\/">\/pets\/<\/a></, done)
343343
})
344344

345+
it('should respond with default Content-Security-Policy', function (done) {
346+
request(createServer({root: fixtures}))
347+
.get('/pets')
348+
.expect('Location', '/pets/')
349+
.expect('Content-Security-Policy', "default-src 'self'")
350+
.expect(301, done)
351+
})
352+
345353
it('should not redirect to protocol-relative locations', function (done) {
346354
request(createServer({root: fixtures}))
347355
.get('//pets')
@@ -369,6 +377,13 @@ describe('send(file).pipe(res)', function () {
369377
.get('/foobar')
370378
.expect(404, />Not Found</, done)
371379
})
380+
381+
it('should respond with default Content-Security-Policy', function (done) {
382+
request(createServer({root: fixtures}))
383+
.get('/foobar')
384+
.expect('Content-Security-Policy', "default-src 'self'")
385+
.expect(404, done)
386+
})
372387
})
373388

374389
describe('with conditional-GET', function () {

0 commit comments

Comments
 (0)