CsrfTests.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  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. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const { assert, expect } = require('chai')
  14. const sinon = require('sinon')
  15. const modulePath = '../../../../app/src/infrastructure/Csrf.js'
  16. const SandboxedModule = require('sandboxed-module')
  17. describe('Csrf', function () {
  18. beforeEach(function () {
  19. this.csurf_csrf = sinon
  20. .stub()
  21. .callsArgWith(2, (this.err = { code: 'EBADCSRFTOKEN' }))
  22. this.Csrf = SandboxedModule.require(modulePath, {
  23. requires: {
  24. csurf: sinon.stub().returns(this.csurf_csrf),
  25. },
  26. })
  27. this.csrf = new this.Csrf()
  28. this.next = sinon.stub()
  29. this.path = '/foo/bar'
  30. this.req = {
  31. path: this.path,
  32. method: 'POST',
  33. }
  34. return (this.res = {})
  35. })
  36. describe('the middleware', function () {
  37. describe('when there are no excluded routes', function () {
  38. it('passes the csrf error on', function () {
  39. this.csrf.middleware(this.req, this.res, this.next)
  40. return expect(this.next.calledWith(this.err)).to.equal(true)
  41. })
  42. })
  43. describe('when the route is excluded', function () {
  44. it('does not pass the csrf error on', function () {
  45. this.csrf.disableDefaultCsrfProtection(this.path, 'POST')
  46. this.csrf.middleware(this.req, this.res, this.next)
  47. return expect(this.next.calledWith(this.err)).to.equal(false)
  48. })
  49. })
  50. describe('when there is a partial route match', function () {
  51. it('passes the csrf error on when the match is too short', function () {
  52. this.csrf.disableDefaultCsrfProtection('/foo', 'POST')
  53. this.csrf.middleware(this.req, this.res, this.next)
  54. return expect(this.next.calledWith(this.err)).to.equal(true)
  55. })
  56. it('passes the csrf error on when the match is too long', function () {
  57. this.csrf.disableDefaultCsrfProtection('/foo/bar/baz', 'POST')
  58. this.csrf.middleware(this.req, this.res, this.next)
  59. return expect(this.next.calledWith(this.err)).to.equal(true)
  60. })
  61. })
  62. describe('when there are multiple exclusions', function () {
  63. it('does not pass the csrf error on when the match is present', function () {
  64. this.csrf.disableDefaultCsrfProtection(this.path, 'POST')
  65. this.csrf.disableDefaultCsrfProtection('/test', 'POST')
  66. this.csrf.disableDefaultCsrfProtection('/a/b/c', 'POST')
  67. this.csrf.middleware(this.req, this.res, this.next)
  68. return expect(this.next.calledWith(this.err)).to.equal(false)
  69. })
  70. it('passes the csrf error on when the match is not present', function () {
  71. this.csrf.disableDefaultCsrfProtection('/url', 'POST')
  72. this.csrf.disableDefaultCsrfProtection('/test', 'POST')
  73. this.csrf.disableDefaultCsrfProtection('/a/b/c', 'POST')
  74. this.csrf.middleware(this.req, this.res, this.next)
  75. return expect(this.next.calledWith(this.err)).to.equal(true)
  76. })
  77. })
  78. describe('when the method does not match', function () {
  79. it('passes the csrf error on', function () {
  80. this.csrf.disableDefaultCsrfProtection(this.path, 'POST')
  81. this.req.method = 'GET'
  82. this.csrf.middleware(this.req, this.res, this.next)
  83. return expect(this.next.calledWith(this.err)).to.equal(true)
  84. })
  85. })
  86. describe('when the route is excluded, but the error is not a bad-csrf-token error', function () {
  87. it('passes the error on', function () {
  88. let err
  89. this.Csrf = SandboxedModule.require(modulePath, {
  90. globals: {
  91. console: console,
  92. },
  93. requires: {
  94. csurf: (this.csurf = sinon
  95. .stub()
  96. .returns(
  97. (this.csurf_csrf = sinon
  98. .stub()
  99. .callsArgWith(2, (err = { code: 'EOTHER' })))
  100. )),
  101. },
  102. })
  103. this.csrf = new this.Csrf()
  104. this.csrf.disableDefaultCsrfProtection(this.path, 'POST')
  105. this.csrf.middleware(this.req, this.res, this.next)
  106. expect(this.next.calledWith(err)).to.equal(true)
  107. return expect(this.next.calledWith(this.err)).to.equal(false)
  108. })
  109. })
  110. })
  111. describe('validateRequest', function () {
  112. describe('when the request is invalid', function () {
  113. it('calls the callback with error', function () {
  114. this.cb = sinon.stub()
  115. this.Csrf.validateRequest(this.req, this.cb)
  116. return expect(this.cb.calledWith(this.err)).to.equal(true)
  117. })
  118. })
  119. describe('when the request is valid', function () {
  120. it('calls the callback without an error', function () {
  121. this.Csrf = SandboxedModule.require(modulePath, {
  122. globals: {
  123. console: console,
  124. },
  125. requires: {
  126. csurf: (this.csurf = sinon
  127. .stub()
  128. .returns((this.csurf_csrf = sinon.stub().callsArg(2)))),
  129. },
  130. })
  131. this.cb = sinon.stub()
  132. this.Csrf.validateRequest(this.req, this.cb)
  133. return expect(this.cb.calledWith()).to.equal(true)
  134. })
  135. })
  136. })
  137. describe('validateToken', function () {
  138. describe('when the request is invalid', function () {
  139. it('calls the callback with `false`', function () {
  140. this.cb = sinon.stub()
  141. this.Csrf.validateToken('token', {}, this.cb)
  142. expect(this.cb.calledWith(this.err)).to.equal(true)
  143. })
  144. })
  145. describe('when the request is valid', function () {
  146. it('calls the callback with `true`', function () {
  147. this.Csrf = SandboxedModule.require(modulePath, {
  148. globals: {
  149. console: console,
  150. },
  151. requires: {
  152. csurf: (this.csurf = sinon
  153. .stub()
  154. .returns((this.csurf_csrf = sinon.stub().callsArg(2)))),
  155. },
  156. })
  157. this.cb = sinon.stub()
  158. this.Csrf.validateToken('goodtoken', {}, this.cb)
  159. return expect(this.cb.calledWith()).to.equal(true)
  160. })
  161. })
  162. describe('when there is no token', function () {
  163. it('calls the callback with an error', function () {
  164. this.Csrf = SandboxedModule.require(modulePath, {
  165. globals: {
  166. console: console,
  167. },
  168. requires: {
  169. csurf: (this.csurf = sinon
  170. .stub()
  171. .returns((this.csurf_csrf = sinon.stub().callsArg(2)))),
  172. },
  173. })
  174. this.cb = sinon.stub()
  175. this.Csrf.validateToken(null, {}, error => {
  176. expect(error).to.exist
  177. })
  178. })
  179. })
  180. })
  181. })