Csrf.test.mjs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { beforeEach, describe, expect, it, vi } from 'vitest'
  2. import sinon from 'sinon'
  3. const modulePath = '../../../../app/src/infrastructure/Csrf.mjs'
  4. describe('Csrf', function () {
  5. beforeEach(async function (ctx) {
  6. ctx.csurf_csrf = sinon
  7. .stub()
  8. .callsArgWith(2, (ctx.err = { code: 'EBADCSRFTOKEN' }))
  9. vi.doMock('csurf', () => ({
  10. default: sinon.stub().returns(ctx.csurf_csrf),
  11. }))
  12. ctx.Csrf = (await import(modulePath)).default
  13. ctx.csrf = new ctx.Csrf()
  14. ctx.next = sinon.stub()
  15. ctx.path = '/foo/bar'
  16. ctx.req = {
  17. path: ctx.path,
  18. method: 'POST',
  19. }
  20. ctx.res = {}
  21. })
  22. describe('the middleware', function () {
  23. describe('when there are no excluded routes', function () {
  24. it('passes the csrf error on', function (ctx) {
  25. ctx.csrf.middleware(ctx.req, ctx.res, ctx.next)
  26. return expect(ctx.next.calledWith(ctx.err)).to.equal(true)
  27. })
  28. })
  29. describe('when the route is excluded', function () {
  30. it('does not pass the csrf error on', function (ctx) {
  31. ctx.csrf.disableDefaultCsrfProtection(ctx.path, 'POST')
  32. ctx.csrf.middleware(ctx.req, ctx.res, ctx.next)
  33. return expect(ctx.next.calledWith(ctx.err)).to.equal(false)
  34. })
  35. })
  36. describe('when there is a partial route match', function () {
  37. it('passes the csrf error on when the match is too short', function (ctx) {
  38. ctx.csrf.disableDefaultCsrfProtection('/foo', 'POST')
  39. ctx.csrf.middleware(ctx.req, ctx.res, ctx.next)
  40. return expect(ctx.next.calledWith(ctx.err)).to.equal(true)
  41. })
  42. it('passes the csrf error on when the match is too long', function (ctx) {
  43. ctx.csrf.disableDefaultCsrfProtection('/foo/bar/baz', 'POST')
  44. ctx.csrf.middleware(ctx.req, ctx.res, ctx.next)
  45. return expect(ctx.next.calledWith(ctx.err)).to.equal(true)
  46. })
  47. })
  48. describe('when there are multiple exclusions', function () {
  49. it('does not pass the csrf error on when the match is present', function (ctx) {
  50. ctx.csrf.disableDefaultCsrfProtection(ctx.path, 'POST')
  51. ctx.csrf.disableDefaultCsrfProtection('/test', 'POST')
  52. ctx.csrf.disableDefaultCsrfProtection('/a/b/c', 'POST')
  53. ctx.csrf.middleware(ctx.req, ctx.res, ctx.next)
  54. return expect(ctx.next.calledWith(ctx.err)).to.equal(false)
  55. })
  56. it('passes the csrf error on when the match is not present', function (ctx) {
  57. ctx.csrf.disableDefaultCsrfProtection('/url', 'POST')
  58. ctx.csrf.disableDefaultCsrfProtection('/test', 'POST')
  59. ctx.csrf.disableDefaultCsrfProtection('/a/b/c', 'POST')
  60. ctx.csrf.middleware(ctx.req, ctx.res, ctx.next)
  61. return expect(ctx.next.calledWith(ctx.err)).to.equal(true)
  62. })
  63. })
  64. describe('when the method does not match', function () {
  65. it('passes the csrf error on', function (ctx) {
  66. ctx.csrf.disableDefaultCsrfProtection(ctx.path, 'POST')
  67. ctx.req.method = 'GET'
  68. ctx.csrf.middleware(ctx.req, ctx.res, ctx.next)
  69. return expect(ctx.next.calledWith(ctx.err)).to.equal(true)
  70. })
  71. })
  72. describe('when the route is excluded, but the error is not a bad-csrf-token error', function () {
  73. it('passes the error on', async function (ctx) {
  74. const err = { code: 'EOTHER' }
  75. ctx.csurf_csrf.callsArgWith(2, err)
  76. const csrf = new ctx.Csrf()
  77. csrf.disableDefaultCsrfProtection(ctx.path, 'POST')
  78. csrf.middleware(ctx.req, ctx.res, ctx.next)
  79. expect(ctx.next.calledWith(err)).to.equal(true)
  80. expect(ctx.next.calledWith(ctx.err)).to.equal(false)
  81. })
  82. })
  83. })
  84. describe('validateRequest', function () {
  85. describe('when the request is invalid', function () {
  86. it('calls the callback with error', function (ctx) {
  87. ctx.cb = sinon.stub()
  88. ctx.Csrf.validateRequest(ctx.req, ctx.cb)
  89. expect(ctx.cb.calledWith(ctx.err)).to.equal(true)
  90. })
  91. })
  92. describe('when the request is valid', function () {
  93. it('calls the callback without an error', async function (ctx) {
  94. vi.doMock('csurf', () => ({
  95. default: (ctx.csurf = sinon
  96. .stub()
  97. .returns((ctx.csurf_csrf = sinon.stub().callsArg(2)))),
  98. }))
  99. ctx.Csrf = (await import(modulePath)).default
  100. ctx.cb = sinon.stub()
  101. ctx.Csrf.validateRequest(ctx.req, ctx.cb)
  102. expect(ctx.cb.calledWith()).to.equal(true)
  103. })
  104. })
  105. })
  106. describe('validateToken', function () {
  107. describe('when the request is invalid', function () {
  108. it('calls the callback with `false`', function (ctx) {
  109. ctx.cb = sinon.stub()
  110. ctx.Csrf.validateToken('token', {}, ctx.cb)
  111. expect(ctx.cb.calledWith(ctx.err)).to.equal(true)
  112. })
  113. })
  114. describe('when the request is valid', function () {
  115. it('calls the callback with `true`', async function (ctx) {
  116. vi.doMock('csurf', () => ({
  117. default: (ctx.csurf = sinon
  118. .stub()
  119. .returns((ctx.csurf_csrf = sinon.stub().callsArg(2)))),
  120. }))
  121. ctx.Csrf = (await import(modulePath)).default
  122. ctx.cb = sinon.stub()
  123. ctx.Csrf.validateToken('goodtoken', {}, ctx.cb)
  124. expect(ctx.cb.calledWith()).to.equal(true)
  125. })
  126. })
  127. describe('when there is no token', function () {
  128. it('calls the callback with an error', async function (ctx) {
  129. vi.doMock('csurf', () => ({
  130. default: (ctx.csurf = sinon
  131. .stub()
  132. .returns((ctx.csurf_csrf = sinon.stub().callsArg(2)))),
  133. }))
  134. ctx.Csrf = (await import(modulePath)).default
  135. ctx.cb = sinon.stub()
  136. ctx.Csrf.validateToken(null, {}, error => {
  137. expect(error).to.exist
  138. })
  139. })
  140. })
  141. })
  142. })