@@ -188,5 +188,64 @@ module SecureHeaders
188188 expect ( env [ ContentSecurityPolicyConfig ::HEADER_NAME ] ) . not_to match ( /rails-nonce/ )
189189 end
190190 end
191+
192+ it "supports calling content_security_policy_nonce without parameters (Rails compatibility)" do
193+ begin
194+ allow ( SecureRandom ) . to receive ( :base64 ) . and_return ( "xyz789" )
195+
196+ # Create a test class that simulates what GoodJob does
197+ # They call content_security_policy_nonce without any parameters
198+ test_class = Class . new ( Message ) do
199+ def self . template
200+ <<-TEMPLATE
201+ <script nonce="<%= content_security_policy_nonce %>">
202+ console.log("test")
203+ </script>
204+ TEMPLATE
205+ end
206+ end
207+
208+ message = test_class . new ( request )
209+ result = message . result
210+
211+ # The nonce should be included in the rendered output
212+ expect ( result ) . to include ( 'nonce="xyz789"' )
213+
214+ # Call middleware to generate headers
215+ _ , env = middleware . call request . env
216+
217+ # The nonce should be added to script-src in the CSP header (default behavior)
218+ expect ( env [ ContentSecurityPolicyConfig ::HEADER_NAME ] ) . to match ( /script-src[^;]*'nonce-xyz789'/ )
219+ end
220+ end
221+
222+ it "supports calling content_security_policy_nonce with :style parameter" do
223+ begin
224+ allow ( SecureRandom ) . to receive ( :base64 ) . and_return ( "style123" )
225+
226+ # Create a test class that calls content_security_policy_nonce with :style
227+ test_class = Class . new ( Message ) do
228+ def self . template
229+ <<-TEMPLATE
230+ <style nonce="<%= content_security_policy_nonce(:style) %>">
231+ body { background: red; }
232+ </style>
233+ TEMPLATE
234+ end
235+ end
236+
237+ message = test_class . new ( request )
238+ result = message . result
239+
240+ # The nonce should be included in the rendered output
241+ expect ( result ) . to include ( 'nonce="style123"' )
242+
243+ # Call middleware to generate headers
244+ _ , env = middleware . call request . env
245+
246+ # The nonce should be added to style-src in the CSP header
247+ expect ( env [ ContentSecurityPolicyConfig ::HEADER_NAME ] ) . to match ( /style-src[^;]*'nonce-style123'/ )
248+ end
249+ end
191250 end
192251end
0 commit comments