Skip to content

A platform for community discussion. Free, open, simple.

PERF: Avoid replacing CSP nonces in non-cacheable responses (#42008)

Alan Guo Xiang Tan661ea0c2bd23
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 0558d49191f..072475baeee 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -68,7 +68,7 @@ module ApplicationHelper
end
def csp_nonce_placeholder
- ContentSecurityPolicy.nonce_placeholder(response.headers)
+ ContentSecurityPolicy.nonce_placeholder(response.headers, request_env: request.env)
end
def track_view_session_id_placeholder
diff --git a/lib/content_security_policy.rb b/lib/content_security_policy.rb
index ebf27abff6a..13ea9a21fd7 100644
--- a/lib/content_security_policy.rb
+++ b/lib/content_security_policy.rb
@@ -8,10 +8,16 @@ class ContentSecurityPolicy
new.build(theme_id, base_url: base_url, path_info: path_info, report_only: report_only)
end
- def nonce_placeholder(response_headers)
+ def nonce_placeholder(response_headers, request_env: nil)
response_headers[
::Middleware::CspScriptNonceInjector::PLACEHOLDER_HEADER
- ] ||= "[[csp_nonce_placeholder_#{SecureRandom.hex}]]"
+ ] ||= if request_env.nil? || request_env[::Middleware::AnonymousCache::CACHEABLE_ENV]
+ "[[csp_nonce_placeholder_#{SecureRandom.hex}]]"
+ else
+ request_env[::Middleware::CspScriptNonceInjector::NONCE_ENV] ||= SecureRandom.alphanumeric(
+ 25,
+ )
+ end
end
end
diff --git a/lib/middleware/anonymous_cache.rb b/lib/middleware/anonymous_cache.rb
index 5a4f40c5907..31b62ea11f6 100644
--- a/lib/middleware/anonymous_cache.rb
+++ b/lib/middleware/anonymous_cache.rb
@@ -8,6 +8,8 @@ require "http_user_agent_encoder"
module Middleware
class AnonymousCache
+ CACHEABLE_ENV = "discourse.anonymous_cache.cacheable"
+
def self.cache_key_segments
@@cache_key_segments ||= {
m: "key_is_mobile?",
@@ -361,6 +363,7 @@ module Middleware
PAYLOAD_INVALID_REQUEST_METHODS = %w[GET HEAD]
def call(env)
+ env[CACHEABLE_ENV] = false
return @app.call(env) if defined?(@@disabled) && @@disabled
if PAYLOAD_INVALID_REQUEST_METHODS.include?(env[Rack::REQUEST_METHOD]) &&
@@ -400,8 +403,9 @@ module Middleware
end
end
+ cacheable = env[CACHEABLE_ENV] = helper.cacheable?
result =
- if helper.cacheable?
+ if cacheable
helper.cached(env) || helper.cache(@app.call(env), env)
else
@app.call(env)
diff --git a/lib/middleware/csp_script_nonce_injector.rb b/lib/middleware/csp_script_nonce_injector.rb
index eeeea4c6b28..abd520d9bb6 100644
--- a/lib/middleware/csp_script_nonce_injector.rb
+++ b/lib/middleware/csp_script_nonce_injector.rb
@@ -2,6 +2,7 @@
module Middleware
class CspScriptNonceInjector
+ NONCE_ENV = "discourse.csp_nonce"
PLACEHOLDER_HEADER = "Discourse-CSP-Nonce-Placeholder"
def initialize(app, settings = {})
@@ -12,9 +13,12 @@ module Middleware
status, headers, response = @app.call(env)
if nonce_placeholder = headers.delete(PLACEHOLDER_HEADER)
- nonce = SecureRandom.alphanumeric(25)
- parts = []
- response.each { |part| parts << part.to_s.gsub(nonce_placeholder, nonce) }
+ nonce = env[NONCE_ENV] || SecureRandom.alphanumeric(25)
+ parts = response
+ if !env[NONCE_ENV]
+ parts = []
+ response.each { |part| parts << part.to_s.gsub(nonce_placeholder, nonce) }
+ end
%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}' ")
diff --git a/plugins/discourse-lti/lib/discourse_lti/lti_omniauth_strategy.rb b/plugins/discourse-lti/lib/discourse_lti/lti_omniauth_strategy.rb
index dd1b74baaf6..3d54ed526f5 100644
--- a/plugins/discourse-lti/lib/discourse_lti/lti_omniauth_strategy.rb
+++ b/plugins/discourse-lti/lib/discourse_lti/lti_omniauth_strategy.rb
@@ -196,7 +196,7 @@ class DiscourseLti::LtiOmniauthStrategy
html = <<~HTML
<html>
<head>
- <script src="#{UrlHelper.absolute(script_path, GlobalSetting.cdn_url)}" nonce="#{ContentSecurityPolicy.try(:nonce_placeholder, response_headers)}"></script>
+ <script src="#{UrlHelper.absolute(script_path, GlobalSetting.cdn_url)}" nonce="#{ContentSecurityPolicy.try(:nonce_placeholder, response_headers, request_env: request.env)}"></script>
</head>
<body>
<form method="post">