HTTPPermissionsPolicyTests.js 3.5 KB

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