SecurityHeadersTests.js 4.0 KB

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