Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SECURITY: Don't reuse CSP nonce between anonymous requests
- Loading branch information
1 parent
672f3e7
commit 0976c8f
Showing
15 changed files
with
105 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<meta id="data-google-tag-manager" | ||
data-data-layer="<%= google_tag_manager_json %>" | ||
data-nonce="<%= ApplicationHelper.google_tag_manager_nonce(request.env) %>" | ||
data-nonce="<%= google_tag_manager_nonce_placeholder %>" | ||
data-container-id="<%= SiteSetting.gtm_container_id %>" /> | ||
|
||
<%= preload_script 'google-tag-manager' %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
module Middleware | ||
class GtmScriptNonceInjector | ||
def initialize(app, settings = {}) | ||
@app = app | ||
end | ||
|
||
def call(env) | ||
status, headers, response = @app.call(env) | ||
|
||
if nonce_placeholder = headers.delete("Discourse-GTM-Nonce-Placeholder") | ||
nonce = SecureRandom.hex | ||
parts = [] | ||
response.each { |part| parts << part.to_s.sub(nonce_placeholder, nonce) } | ||
%w[Content-Security-Policy Content-Security-Policy-Report-Only].each do |name| | ||
next if headers[name].blank? | ||
headers[name] = headers[name].sub("script-src ", "script-src 'nonce-#{nonce}' ") | ||
end | ||
[status, headers, parts] | ||
else | ||
[status, headers, response] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters