SecurityHeadersTests.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* eslint-disable
  2. camelcase,
  3. n/handle-callback-err,
  4. max-len,
  5. no-return-assign,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const { assert } = require('chai')
  16. const async = require('async')
  17. const User = require('./helpers/User')
  18. const request = require('./helpers/request')
  19. const assert_has_common_headers = function (response) {
  20. const { headers } = response
  21. assert.include(headers, {
  22. 'x-download-options': 'noopen',
  23. 'x-xss-protection': '0',
  24. 'cross-origin-resource-policy': 'same-origin',
  25. 'cross-origin-opener-policy': 'same-origin',
  26. 'x-content-type-options': 'nosniff',
  27. 'x-permitted-cross-domain-policies': 'none',
  28. 'referrer-policy': 'origin-when-cross-origin',
  29. })
  30. assert.isUndefined(headers['cross-origin-embedder-policy'])
  31. }
  32. const assert_has_cache_headers = function (response) {
  33. assert.include(response.headers, {
  34. 'surrogate-control': 'no-store',
  35. 'cache-control': 'no-store, no-cache, must-revalidate, proxy-revalidate',
  36. pragma: 'no-cache',
  37. expires: '0',
  38. })
  39. }
  40. const assert_has_no_cache_headers = function (response) {
  41. assert.doesNotHaveAnyKeys(response.headers, [
  42. 'surrogate-control',
  43. 'cache-control',
  44. 'pragma',
  45. 'expires',
  46. ])
  47. }
  48. const assert_has_asset_caching_headers = function (response) {
  49. assert.equal(response.headers['cache-control'], 'public, max-age=31536000')
  50. }
  51. describe('SecurityHeaders', function () {
  52. beforeEach(function () {
  53. return (this.user = new User())
  54. })
  55. it('should not have x-powered-by header', function (done) {
  56. return request.get('/', (err, res, body) => {
  57. assert.isUndefined(res.headers['x-powered-by'])
  58. return done()
  59. })
  60. })
  61. it('should have all common headers', function (done) {
  62. return request.get('/', (err, res, body) => {
  63. assert_has_common_headers(res)
  64. return done()
  65. })
  66. })
  67. it('should not have cache headers on public pages', function (done) {
  68. return request.get('/', (err, res, body) => {
  69. assert_has_no_cache_headers(res)
  70. return done()
  71. })
  72. })
  73. it('should have caching headers on static assets', function (done) {
  74. request.get('/favicon.ico', (err, res) => {
  75. assert_has_asset_caching_headers(res)
  76. done(err)
  77. })
  78. })
  79. it('should have cache headers when user is logged in', function (done) {
  80. return async.series(
  81. [
  82. cb => this.user.login(cb),
  83. cb => this.user.request.get('/', cb),
  84. cb => this.user.logout(cb),
  85. ],
  86. (err, results) => {
  87. const main_response = results[1][0]
  88. assert_has_cache_headers(main_response)
  89. return done()
  90. }
  91. )
  92. })
  93. it('should have cache headers on project page', function (done) {
  94. return async.series(
  95. [
  96. cb => this.user.login(cb),
  97. cb => {
  98. return this.user.createProject(
  99. 'public-project',
  100. (error, project_id) => {
  101. if (error != null) {
  102. return done(error)
  103. }
  104. this.project_id = project_id
  105. return this.user.makePublic(this.project_id, 'readAndWrite', cb)
  106. }
  107. )
  108. },
  109. cb => this.user.logout(cb),
  110. ],
  111. (err, results) => {
  112. return request.get(`/project/${this.project_id}`, (err, res, body) => {
  113. assert_has_cache_headers(res)
  114. return done()
  115. })
  116. }
  117. )
  118. })
  119. it('should have caching headers on static assets when user is logged in', function (done) {
  120. async.series(
  121. [
  122. cb => this.user.login(cb),
  123. cb => this.user.request.get('/favicon.ico', cb),
  124. cb => this.user.logout(cb),
  125. ],
  126. (err, results) => {
  127. const res = results[1][0]
  128. assert_has_asset_caching_headers(res)
  129. done()
  130. }
  131. )
  132. })
  133. })