File tree Expand file tree Collapse file tree 2 files changed +34
-2
lines changed
Expand file tree Collapse file tree 2 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -415,6 +415,35 @@ def before_load
415415end
416416```
417417
418+ ### Using in rack middleware
419+
420+ The ` SecureHeaders::header_hash ` generates a hash of all header values, which is useful for merging with rack middleware values.
421+
422+ ``` ruby
423+ class MySecureHeaders
424+ include SecureHeaders
425+ def initialize (app )
426+ @app = app
427+ end
428+
429+ def call (env )
430+ status, headers, response = @app .call(env)
431+ security_headers = if override?
432+ SecureHeaders ::header_hash(:csp => false ) # uses global config, but overrides CSP config
433+ else
434+ SecureHeaders ::header_hash # uses global config
435+ end
436+ [status, headers.merge(security_headers), [response.body]]
437+ end
438+ end
439+
440+ module Testapp
441+ class Application < Rails ::Application
442+ config.middleware.use MySecureHeaders
443+ end
444+ end
445+ ```
446+
418447## Similar libraries
419448
420449* Rack [ rack-secure_headers] ( https://github.com/harmoni/rack-secure_headers )
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ module SecureHeaders
1919 ALL_HEADER_CLASSES = [
2020 SecureHeaders ::ContentSecurityPolicy ,
2121 SecureHeaders ::StrictTransportSecurity ,
22+ SecureHeaders ::PublicKeyPins ,
2223 SecureHeaders ::XContentTypeOptions ,
2324 SecureHeaders ::XDownloadOptions ,
2425 SecureHeaders ::XFrameOptions ,
@@ -57,8 +58,10 @@ def header_hash(options = nil)
5758 ::SecureHeaders ::Configuration . send ( klass ::Constants ::CONFIG_KEY )
5859 end
5960
60- header = get_a_header ( klass ::Constants ::CONFIG_KEY , klass , config )
61- memo [ header . name ] = header . value
61+ unless klass == SecureHeaders ::PublicKeyPins && !config . is_a? ( Hash )
62+ header = get_a_header ( klass ::Constants ::CONFIG_KEY , klass , config )
63+ memo [ header . name ] = header . value
64+ end
6265 memo
6366 end
6467 end
You can’t perform that action at this time.
0 commit comments