content_security_policy_spec.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ContentSecurityPolicy do
  4. subject { described_class.new }
  5. around do |example|
  6. original_asset_host = Rails.configuration.action_controller.asset_host
  7. original_web_domain = Rails.configuration.x.web_domain
  8. original_use_https = Rails.configuration.x.use_https
  9. example.run
  10. Rails.configuration.action_controller.asset_host = original_asset_host
  11. Rails.configuration.x.web_domain = original_web_domain
  12. Rails.configuration.x.use_https = original_use_https
  13. end
  14. describe '#base_host' do
  15. before { Rails.configuration.x.web_domain = 'host.example' }
  16. it 'returns the configured value for the web domain' do
  17. expect(subject.base_host).to eq 'host.example'
  18. end
  19. end
  20. describe '#assets_host' do
  21. context 'when asset_host is not configured' do
  22. before { Rails.configuration.action_controller.asset_host = nil }
  23. context 'with a configured web domain' do
  24. before { Rails.configuration.x.web_domain = 'host.example' }
  25. context 'when use_https is enabled' do
  26. before { Rails.configuration.x.use_https = true }
  27. it 'returns value from base host with https protocol' do
  28. expect(subject.assets_host).to eq 'https://host.example'
  29. end
  30. end
  31. context 'when use_https is disabled' do
  32. before { Rails.configuration.x.use_https = false }
  33. it 'returns value from base host with http protocol' do
  34. expect(subject.assets_host).to eq 'http://host.example'
  35. end
  36. end
  37. end
  38. end
  39. context 'when asset_host is configured' do
  40. before do
  41. Rails.configuration.action_controller.asset_host = 'https://assets.host.example'
  42. end
  43. it 'returns full value from configured host' do
  44. expect(subject.assets_host).to eq 'https://assets.host.example'
  45. end
  46. end
  47. end
  48. describe '#media_hosts' do
  49. context 'when there is no configured CDN' do
  50. it 'defaults to using the assets_host value' do
  51. expect(subject.media_hosts).to contain_exactly(subject.assets_host)
  52. end
  53. end
  54. context 'when an S3 alias host is configured' do
  55. around do |example|
  56. ClimateControl.modify S3_ALIAS_HOST: 'asset-host.s3-alias.example' do
  57. example.run
  58. end
  59. end
  60. it 'uses the s3 alias host value' do
  61. expect(subject.media_hosts).to contain_exactly(subject.assets_host, 'https://asset-host.s3-alias.example')
  62. end
  63. end
  64. context 'when an S3 alias host with a trailing path is configured' do
  65. around do |example|
  66. ClimateControl.modify S3_ALIAS_HOST: 'asset-host.s3-alias.example/pathname' do
  67. example.run
  68. end
  69. end
  70. it 'uses the s3 alias host value and preserves the path' do
  71. expect(subject.media_hosts).to contain_exactly(subject.assets_host, 'https://asset-host.s3-alias.example/pathname/')
  72. end
  73. end
  74. context 'when an S3 cloudfront host is configured' do
  75. around do |example|
  76. ClimateControl.modify S3_CLOUDFRONT_HOST: 'asset-host.s3-cloudfront.example' do
  77. example.run
  78. end
  79. end
  80. it 'uses the s3 cloudfront host value' do
  81. expect(subject.media_hosts).to contain_exactly(subject.assets_host, 'https://asset-host.s3-cloudfront.example')
  82. end
  83. end
  84. context 'when an azure alias host is configured' do
  85. around do |example|
  86. ClimateControl.modify AZURE_ALIAS_HOST: 'asset-host.azure-alias.example' do
  87. example.run
  88. end
  89. end
  90. it 'uses the azure alias host value' do
  91. expect(subject.media_hosts).to contain_exactly(subject.assets_host, 'https://asset-host.azure-alias.example')
  92. end
  93. end
  94. context 'when s3_enabled is configured' do
  95. around do |example|
  96. ClimateControl.modify S3_ENABLED: 'true', S3_HOSTNAME: 'asset-host.s3.example' do
  97. example.run
  98. end
  99. end
  100. it 'uses the s3 hostname host value' do
  101. expect(subject.media_hosts).to contain_exactly(subject.assets_host, 'https://asset-host.s3.example')
  102. end
  103. end
  104. context 'when PAPERCLIP_ROOT_URL is configured' do
  105. around do |example|
  106. ClimateControl.modify PAPERCLIP_ROOT_URL: 'https://paperclip-host.example' do
  107. example.run
  108. end
  109. end
  110. it 'uses the provided URL in the content security policy' do
  111. expect(subject.media_hosts).to contain_exactly(subject.assets_host, 'https://paperclip-host.example')
  112. end
  113. end
  114. end
  115. end