rack_nonce_middleware 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rack_nonce_middleware.rb +37 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8d43c77cc5ff633d2464001a7effbc53f405c9799be26d22a696ab811b316b03
4
+ data.tar.gz: 0d394b9f3fc25144c013f0e1624eeb9a29c70b8157b2dbe239b7b77cf4871516
5
+ SHA512:
6
+ metadata.gz: 1492e314723b4ec0ff521b4d9a80e0de9a035de7bcc6a31a270e5dcf178975a8ebbf97cb290e3386ef3f65e3349c7bbd437a068c8d0fcf4a9871555ae56c0f25
7
+ data.tar.gz: 7a6345a3bbf4572d7817db9efff479dd3a4676bbdb134539514464eae244a81313533689358e2978f0b9fbf82c12737a6309a3c9eb54f591054bfc0d9295af31
@@ -0,0 +1,37 @@
1
+ # Description: Rack middleware to add a nonce to the CSP header
2
+ # and expose it to the application.
3
+
4
+ # Usage:
5
+ # In your backend application, add the middleware to the stack:
6
+ #
7
+ # require 'rack-nonce-middleware'
8
+ # use(Rack::Protection, {
9
+ # use: %i[content_security_policy],
10
+ # script_src: "'self' #{RackNonceMiddleware::NONCE} https://....",
11
+ # style_src: "'self' #{RackNonceMiddleware::NONCE} https://....",
12
+ # use RackNonceMiddleware # Make sure thus is included after Rack::Protection
13
+ #
14
+ # In your views, add the nonce to the script and style tags:
15
+ # <script nonce="<%= env['csp.nonce'] %>">...</script>
16
+ # <style nonce="<%= env['csp.nonce'] %>">...</style>
17
+
18
+ class RackNonceMiddleware
19
+ NONCE = 'nonce-NONCE-VALUE'.freeze
20
+
21
+ def initialize(app)
22
+ @app = app
23
+ end
24
+
25
+ def call(env)
26
+ nonce = SecureRandom.alphanumeric(16)
27
+ env['csp.nonce'] = nonce
28
+ status, headers, response = @app.call(env)
29
+
30
+ puts "====> #{headers.keys}"
31
+ if headers['Content-Security-Policy']
32
+ headers['Content-Security-Policy'] = headers['Content-Security-Policy'].gsub(NONCE, "nonce-#{nonce}")
33
+ end
34
+
35
+ [status, headers, response]
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack_nonce_middleware
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tadas Tamosauskas, Pragmatic Genomics Ltd
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-07-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Rack middleware to add a nonce to the CSP header and expose it to the
14
+ view templates
15
+ email: contact@sequenceserver.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/rack_nonce_middleware.rb
21
+ homepage: https://rubygems.org/gems/rack_nonce_middleware
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.5.14
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: A Rack middleware for adding a CSP nonce to requests
44
+ test_files: []