HTTPPermissionsPolicy.test.mjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { expect, vi } from 'vitest'
  2. import sinon from 'sinon'
  3. import HttpPermissionsPolicyMiddleware from '../../../../app/src/infrastructure/HttpPermissionsPolicy.mjs'
  4. import MockRequest from '../helpers/MockRequest.mjs'
  5. import MockResponse from '../helpers/MockResponse.mjs'
  6. const modulePath =
  7. '../../../../app/src/infrastructure/HttpPermissionsPolicy.mjs'
  8. describe('HttpPermissionsPolicy', function () {
  9. beforeEach(async function (ctx) {
  10. ctx.next = sinon.stub()
  11. ctx.HttpPermissionsPolicy = (await import(modulePath)).default
  12. ctx.path = '/foo/bar'
  13. ctx.req = new MockRequest(vi)
  14. ctx.res = new MockResponse(vi)
  15. })
  16. describe('when a single blocked policy element is provided', function () {
  17. it('returns a valid header string', function () {
  18. const policy = {
  19. blocked: ['accelerometer'],
  20. }
  21. const httpPermissionsMiddleware = new HttpPermissionsPolicyMiddleware(
  22. policy
  23. )
  24. expect(httpPermissionsMiddleware.policy).to.equal('accelerometer=()')
  25. })
  26. })
  27. describe('when a single allowed policy element is provided', function () {
  28. it('returns a valid header string', function () {
  29. const policy = {
  30. allowed: { camera: 'self' },
  31. }
  32. const httpPermissionsMiddleware = new HttpPermissionsPolicyMiddleware(
  33. policy
  34. )
  35. expect(httpPermissionsMiddleware.policy).to.equal('camera=(self)')
  36. })
  37. })
  38. describe('when a full policy is provided', function () {
  39. it('returns a valid header string', function () {
  40. const policy = {
  41. blocked: ['usb', 'hid'],
  42. allowed: { camera: 'self https://example.com', fullscreen: 'self' },
  43. }
  44. const httpPermissionsMiddleware = new HttpPermissionsPolicyMiddleware(
  45. policy
  46. )
  47. expect(httpPermissionsMiddleware.policy).to.equal(
  48. 'usb=(), hid=(), camera=(self https://example.com), fullscreen=(self)'
  49. )
  50. })
  51. })
  52. describe('when a conflicting policy is provided', function () {
  53. it('returns an error', function () {
  54. const policy = {
  55. blocked: ['usb'],
  56. allowed: { usb: 'self' },
  57. }
  58. expect(() => new HttpPermissionsPolicyMiddleware(policy)).to.throw(
  59. 'Invalid Permissions-Policy header configuration'
  60. )
  61. })
  62. })
  63. describe('when the allowlist contains an incomplete directive', function () {
  64. it('returns an error', function () {
  65. const policy = {
  66. blocked: ['usb'],
  67. allowed: { camera: '' },
  68. }
  69. expect(() => new HttpPermissionsPolicyMiddleware(policy)).to.throw(
  70. 'Invalid Permissions-Policy header configuration'
  71. )
  72. })
  73. })
  74. describe('when an empty denylist is provided', function () {
  75. it('returns a valid header string ', function () {
  76. const policy = {
  77. allowed: { camera: 'self' },
  78. blocked: [],
  79. }
  80. const httpPermissionsMiddleware = new HttpPermissionsPolicyMiddleware(
  81. policy
  82. )
  83. expect(httpPermissionsMiddleware.policy).to.equal('camera=(self)')
  84. })
  85. })
  86. describe('when an empty allowlist is provided', function () {
  87. it('returns a valid header string ', function () {
  88. const policy = {
  89. allowed: {},
  90. blocked: ['usb'],
  91. }
  92. const httpPermissionsMiddleware = new HttpPermissionsPolicyMiddleware(
  93. policy
  94. )
  95. expect(httpPermissionsMiddleware.policy).to.equal('usb=()')
  96. })
  97. })
  98. })