2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
2022-05-16 10:25:49 -04:00
|
|
|
n/handle-callback-err,
|
2019-05-29 05:21:06 -04:00
|
|
|
max-len,
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
const { assert } = require('chai')
|
|
|
|
const async = require('async')
|
|
|
|
const User = require('./helpers/User')
|
|
|
|
const request = require('./helpers/request')
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
const assert_has_common_headers = function (response) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const { headers } = response
|
|
|
|
assert.equal(headers['x-download-options'], 'noopen')
|
|
|
|
assert.equal(headers['x-xss-protection'], '1; mode=block')
|
|
|
|
return assert.equal(headers['referrer-policy'], 'origin-when-cross-origin')
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
const assert_has_cache_headers = function (response) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const { headers } = response
|
|
|
|
assert.equal(headers['surrogate-control'], 'no-store')
|
|
|
|
assert.equal(
|
|
|
|
headers['cache-control'],
|
|
|
|
'no-store, no-cache, must-revalidate, proxy-revalidate'
|
|
|
|
)
|
2020-12-16 05:37:00 -05:00
|
|
|
assert.equal(headers.pragma, 'no-cache')
|
|
|
|
return assert.equal(headers.expires, '0')
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
const assert_has_no_cache_headers = function (response) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const { headers } = response
|
|
|
|
assert.isUndefined(headers['surrogate-control'])
|
|
|
|
assert.isUndefined(headers['cache-control'])
|
2020-12-16 05:37:00 -05:00
|
|
|
assert.isUndefined(headers.pragma)
|
|
|
|
return assert.isUndefined(headers.expires)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
const assert_has_asset_caching_headers = function (response) {
|
2020-06-26 05:49:52 -04:00
|
|
|
const { headers } = response
|
|
|
|
assert.equal(headers['cache-control'], 'public, max-age=31536000')
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('SecurityHeaders', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return (this.user = new User())
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not have x-powered-by header', function (done) {
|
2019-08-07 10:04:04 -04:00
|
|
|
return request.get('/', (err, res, body) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
assert.isUndefined(res.headers['x-powered-by'])
|
|
|
|
return done()
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should have all common headers', function (done) {
|
2019-08-07 10:04:04 -04:00
|
|
|
return request.get('/', (err, res, body) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
assert_has_common_headers(res)
|
|
|
|
return done()
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not have cache headers on public pages', function (done) {
|
2019-08-07 10:04:04 -04:00
|
|
|
return request.get('/', (err, res, body) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
assert_has_no_cache_headers(res)
|
|
|
|
return done()
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should have caching headers on static assets', function (done) {
|
2020-06-26 05:49:52 -04:00
|
|
|
request.get('/favicon.ico', (err, res) => {
|
|
|
|
assert_has_asset_caching_headers(res)
|
|
|
|
done(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should have cache headers when user is logged in', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return async.series(
|
|
|
|
[
|
|
|
|
cb => this.user.login(cb),
|
|
|
|
cb => this.user.request.get('/', cb),
|
2021-04-27 03:52:58 -04:00
|
|
|
cb => this.user.logout(cb),
|
2019-05-29 05:21:06 -04:00
|
|
|
],
|
|
|
|
(err, results) => {
|
|
|
|
const main_response = results[1][0]
|
|
|
|
assert_has_cache_headers(main_response)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should have cache headers on project page', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return async.series(
|
|
|
|
[
|
|
|
|
cb => this.user.login(cb),
|
|
|
|
cb => {
|
|
|
|
return this.user.createProject(
|
|
|
|
'public-project',
|
|
|
|
(error, project_id) => {
|
|
|
|
if (error != null) {
|
|
|
|
return done(error)
|
|
|
|
}
|
|
|
|
this.project_id = project_id
|
|
|
|
return this.user.makePublic(this.project_id, 'readAndWrite', cb)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
2021-04-27 03:52:58 -04:00
|
|
|
cb => this.user.logout(cb),
|
2019-05-29 05:21:06 -04:00
|
|
|
],
|
|
|
|
(err, results) => {
|
|
|
|
return request.get(`/project/${this.project_id}`, (err, res, body) => {
|
|
|
|
assert_has_cache_headers(res)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2020-06-26 05:49:52 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should have caching headers on static assets when user is logged in', function (done) {
|
2020-06-26 05:49:52 -04:00
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
cb => this.user.login(cb),
|
|
|
|
cb => this.user.request.get('/favicon.ico', cb),
|
2021-04-27 03:52:58 -04:00
|
|
|
cb => this.user.logout(cb),
|
2020-06-26 05:49:52 -04:00
|
|
|
],
|
|
|
|
(err, results) => {
|
|
|
|
const res = results[1][0]
|
|
|
|
assert_has_asset_caching_headers(res)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|