Skip to content

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

SECURITY: Prevent cache poisoning/XSS via color scheme cookies

Martin Brennane1b647229a4a
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 072475baeee..0f9e4ba86cb 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -690,11 +690,11 @@ module ApplicationHelper
def user_scheme_id
return @user_scheme_id if defined?(@user_scheme_id)
scheme_id = cookies[:color_scheme_id] || current_user&.user_option&.color_scheme_id
- @user_scheme_id = scheme_id if scheme_id &&
- (
- ColorScheme.exists?(id: scheme_id, user_selectable: true) ||
- theme&.color_scheme_id == scheme_id.to_i
- )
+
+ @user_scheme_id = ColorScheme.valid_id(scheme_id) if ColorScheme.exists?(
+ id: scheme_id,
+ user_selectable: true,
+ ) || theme&.color_scheme_id == scheme_id.to_i
end
def scheme_id
@@ -717,11 +717,11 @@ module ApplicationHelper
def user_dark_scheme_id
return @user_dark_scheme_id if defined?(@user_dark_scheme_id)
scheme_id = cookies[:dark_scheme_id] || current_user&.user_option&.dark_scheme_id
- @user_dark_scheme_id = scheme_id if scheme_id &&
- (
- ColorScheme.exists?(id: scheme_id, user_selectable: true) ||
- theme&.dark_color_scheme_id == scheme_id.to_i
- )
+
+ @user_dark_scheme_id = ColorScheme.valid_id(scheme_id) if ColorScheme.exists?(
+ id: scheme_id,
+ user_selectable: true,
+ ) || theme&.dark_color_scheme_id == scheme_id.to_i
end
def dark_scheme_id
@@ -1105,6 +1105,7 @@ module ApplicationHelper
end
def color_scheme_stylesheet_link_tag(href, media, css_class, scheme_id)
+ scheme_id = Integer(scheme_id, exception: false)
%[<link href="#{href}" media="#{media}" rel="stylesheet" class="#{css_class}"#{scheme_id && scheme_id != -1 ? %[ data-scheme-id="#{scheme_id}"] : ""}/>]
end
end
diff --git a/app/models/color_scheme.rb b/app/models/color_scheme.rb
index 3a606e84394..f05e77ff2da 100644
--- a/app/models/color_scheme.rb
+++ b/app/models/color_scheme.rb
@@ -331,6 +331,17 @@ class ColorScheme < ActiveRecord::Base
@hex_cache ||= DistributedCache.new("scheme_hex_for_name")
end
+ def self.valid_ids_cache
+ @valid_ids_cache ||= DistributedCache.new("color_scheme_valid_ids")
+ end
+
+ # Used on the hot anonymous cache path (Middleware::AnonymousCache), so the
+ # set of valid ids is cached in-process to avoid a DB query per request.
+ def self.valid_id(id)
+ id = Integer(id, exception: false)
+ id if id && valid_ids_cache.defer_get_set("ids") { pluck(:id).to_set }.include?(id)
+ end
+
default_scope { where(remote_copy: false) }
attr_accessor :is_base
@@ -556,6 +567,7 @@ class ColorScheme < ActiveRecord::Base
def dump_caches
self.class.hex_cache.clear
+ self.class.valid_ids_cache.clear
ApplicationSerializer.expire_cache_fragment!("user_color_schemes")
end
diff --git a/lib/middleware/anonymous_cache.rb b/lib/middleware/anonymous_cache.rb
index 31b62ea11f6..536d8744bd0 100644
--- a/lib/middleware/anonymous_cache.rb
+++ b/lib/middleware/anonymous_cache.rb
@@ -22,6 +22,8 @@ module Middleware
l: "key_locale",
lso: "key_show_original_content",
cm: "key_forced_color_mode",
+ cs: "key_color_scheme_id",
+ ds: "key_dark_scheme_id",
}
end
@@ -176,6 +178,18 @@ module Middleware
%w[light dark].include?(val) ? val : ""
end
+ def key_color_scheme_id
+ valid_color_scheme_cookie_id("color_scheme_id")
+ end
+
+ def key_dark_scheme_id
+ valid_color_scheme_cookie_id("dark_scheme_id")
+ end
+
+ def valid_color_scheme_cookie_id(cookie_name)
+ ColorScheme.valid_id(@request.cookies[cookie_name])
+ end
+
def key_compress_anon
GlobalSetting.compress_anon_cache
end
diff --git a/spec/lib/middleware/anonymous_cache_spec.rb b/spec/lib/middleware/anonymous_cache_spec.rb
index fb16a3fafc7..71cc3e7fef4 100644
--- a/spec/lib/middleware/anonymous_cache_spec.rb
+++ b/spec/lib/middleware/anonymous_cache_spec.rb
@@ -69,6 +69,27 @@ RSpec.describe Middleware::AnonymousCache do
end
end
+ describe "per color scheme cache" do
+ it "handles valid color scheme keys only", :aggregate_failures do
+ color_scheme = Fabricate(:color_scheme)
+
+ with_no_scheme_key = new_helper.cache_key
+ with_bad_scheme_key =
+ new_helper(
+ "HTTP_COOKIE" => %(color_scheme_id=#{color_scheme.id}"><link rel="modulepreload">),
+ ).cache_key
+ with_color_scheme_key =
+ new_helper("HTTP_COOKIE" => "color_scheme_id=#{color_scheme.id}").cache_key
+ with_dark_scheme_key =
+ new_helper("HTTP_COOKIE" => "dark_scheme_id=#{color_scheme.id}").cache_key
+
+ expect(with_bad_scheme_key).to eq(with_no_scheme_key)
+ expect(with_color_scheme_key).not_to eq(with_no_scheme_key)
+ expect(with_dark_scheme_key).not_to eq(with_no_scheme_key)
+ expect(with_color_scheme_key).not_to eq(with_dark_scheme_key)
+ end
+ end
+
context "with header or cookie based custom locale" do
it "handles different languages" do
# Normally does not check the language header
diff --git a/spec/requests/application_controller_spec.rb b/spec/requests/application_controller_spec.rb
index 9a6dfd0aa60..e1ee665556b 100644
--- a/spec/requests/application_controller_spec.rb
+++ b/spec/requests/application_controller_spec.rb
@@ -2002,12 +2002,29 @@ RSpec.describe ApplicationController do
describe "color definition stylesheets" do
let!(:dark_scheme) { ColorScheme.find_by(base_scheme_id: ColorScheme::NAMES_TO_ID_MAP["Dark"]) }
+ let!(:light_scheme) do
+ ColorScheme.find_by(base_scheme_id: ColorScheme::NAMES_TO_ID_MAP["Solarized Light"])
+ end
before do
Theme.find_default.update!(dark_color_scheme_id: dark_scheme.id)
SiteSetting.interface_color_selector = "sidebar_footer"
end
+ context "when scheme cookies contain HTML" do
+ it "does not add injected links to the page" do
+ injected_link =
+ '<link rel="modulepreload" data-plugin-name="poc" href="https://example.com/xss.js">'
+ cookies[:color_scheme_id] = %(#{light_scheme.id}">#{injected_link})
+ cookies[:dark_scheme_id] = %(#{dark_scheme.id}">#{injected_link})
+
+ get "/"
+
+ injected_links = css_select('link[rel="modulepreload"][data-plugin-name="poc"]')
+ expect(injected_links).to be_empty
+ end
+ end
+
context "with early hints" do
before { global_setting :early_hint_header_mode, "preload" }